if (templateContent != null) {
Element element = source.getFirstElement();
final EndTag tag = element != null ? element.getEndTag() : null;
ScriptEngine scriptEngine = scriptEngineUtils.scriptEngine(ajaxTemplateExtension);
ScriptContext scriptContext = new AssetsScriptContext();
final Bindings bindings = scriptEngine.createBindings();
bindings.put("renderContext", renderContext);
bindings.put("resource", resource);
scriptContext.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
// The following binding is necessary for Javascript, which doesn't offer a console by default.
bindings.put("out", new PrintWriter(scriptContext.getWriter()));
scriptEngine.eval(templateContent, scriptContext);
StringWriter writer = (StringWriter) scriptContext.getWriter();
final String staticsAsset = writer.toString();
if (StringUtils.isNotBlank(staticsAsset)) {
if (tag != null) {
outputDocument.replace(tag.getBegin(), tag.getBegin() + 1, "\n" + staticsAsset + "\n<");
out = outputDocument.toString();
} else {
out = previousOut + "\n" + staticsAsset;
}
}
}
} else if (resource.getContextConfiguration().equals("page")) {
if (renderContext.isEditMode()) {
// Add static div for edit mode
List<Element> bodyElementList = source.getAllElements(HTMLElementName.BODY);
Set<String> javascript = assets.get("javascript");
if (javascript == null) {
assets.put("javascript", (javascript = new HashSet<String>()));
}
javascript.add(renderContext.getRequest().getContextPath() + "/modules/assets/javascript/jquery.min.js");
javascript.add(renderContext.getRequest().getContextPath() + "/modules/assets/javascript/jquery.Jcrop.js");
if (bodyElementList.size() > 0) {
Element bodyElement = bodyElementList.get(bodyElementList.size() - 1);
EndTag bodyEndTag = bodyElement.getEndTag();
outputDocument.replace(bodyEndTag.getBegin(), bodyEndTag.getBegin() + 1, "</div><");
bodyElement = bodyElementList.get(0);
StartTag bodyStartTag = bodyElement.getStartTag();
outputDocument.replace(bodyStartTag.getEnd(), bodyStartTag.getEnd(), "\n" +
"<div class=\"jahia-template-gxt editmode-gxt\" jahiatype=\"editmode\" id=\"editmode\"" +
" config=\"" +
renderContext.getEditModeConfigName() +
"\"" + " path=\"" +
resource.getNode().getPath() +
"\" locale=\"" +
resource.getLocale() + "\"" +
" template=\"" +
resource.getResolvedTemplate() +
"\"" + " nodetypes=\"" +
ConstraintsHelper.getConstraints(
renderContext.getMainResource().getNode()) +
"\"" + ">");
}
}
List<Element> headElementList = source.getAllElements(HTMLElementName.HEAD);
for (Element element : headElementList) {
String templateContent = getResolvedTemplate();
if (templateContent != null) {
final EndTag headEndTag = element.getEndTag();
ScriptEngine scriptEngine = scriptEngineUtils.scriptEngine(templateExtension);
ScriptContext scriptContext = new AssetsScriptContext();
final Bindings bindings = scriptEngine.createBindings();
bindings.put("renderContext", renderContext);
bindings.put("resource", resource);
bindings.put("contextPath", renderContext.getRequest().getContextPath());
scriptContext.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
// The following binding is necessary for Javascript, which doesn't offer a console by default.
bindings.put("out", new PrintWriter(scriptContext.getWriter()));
scriptEngine.eval(templateContent, scriptContext);
StringWriter writer = (StringWriter) scriptContext.getWriter();
final String staticsAsset = writer.toString();
if (StringUtils.isNotBlank(staticsAsset)) {