Skip to content

Instantly share code, notes, and snippets.

@ericf
Created December 7, 2010 21:40
Show Gist options
  • Select an option

  • Save ericf/732466 to your computer and use it in GitHub Desktop.

Select an option

Save ericf/732466 to your computer and use it in GitHub Desktop.
protected void renderYUI3Instance(HtmlContentWriter container, String component, String constructor) throws IOException {
writeInlineScript(container, new YUI3Instance(component, constructor));
}
// …
private class YUI3Instance implements TextContent {
private String component;
private String constructor;
private TextContent content;
private String[] useModules;
public YUI3Instance(String component, String constructor) {
this.component = component;
this.constructor = constructor;
}
public YUI3Instance(TextContent content, String...useModules) {
this.content = content;
this.useModules = useModules;
}
public void writeTo(Writer script) throws IOException {
String configs = null;
Collection<YUI3ModuleGroup> groups = app().yui3Config.getGroups();
if ( ! groups.isEmpty()) {
StringBuilder b = new StringBuilder();
for (YUI3ModuleGroup group : groups)
b.append("window.").append(group.getName()).append("_config, ");
configs = b.toString();
}
script.write("YUI(");
if (configs != null)
script.write(configs);
app().yui3Config.writeTo(script, false, isSecureRequest(), isDebug(), getLogInclude());
script.write(")");
if (component != null && constructor != null)
writeComponentInstance(script);
else
writeInstance(script);
}
private void writeInstance(Writer script) throws IOException {
if (useModules != null && useModules.length > 0) {
script.write(".use(");
for (String m : useModules)
script.append("'").append(m).append("', ");
} else {
script.write(".use('*', ");
}
script.write("function(Y){");
content.writeTo(script);
script.write("});");
}
private void writeComponentInstance(Writer script) throws IOException {
script.write(".use('"+ component +"', function(Y){");
script.write("(new " + constructor + "(");
JSONWriter json = JSON.jsonWriter(script);
writeWindowConfigJSON(json.object());
json.close();
script.write("));");
script.write("});");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment