/**
* TODO:gs make more stable by only adding some js stuff to the end of the page. First check if document.height is ready
* when puttings js to the end or menachism like ext.onReady is needed
*/
private String injectJavaScript(String page, boolean addCheckForInlineEvents) {
StringOutput sb = new StringOutput();
//do not use parser and just check for css and script stuff myself and append just before body and head
SimpleHtmlParser parser = new SimpleHtmlParser(page);
if (!parser.isValidHtml()) {
return page;
}
String docType = parser.getHtmlDocType();
if (docType != null) sb.append(docType).append("\n");
if (parser.getXhtmlNamespaces() == null) sb.append("<html><head>");
else {
sb.append(parser.getXhtmlNamespaces());
sb.append("<head><meta http-equiv=\"Content-Script-Type\" content=\"text/javascript\">");//neded to allow body onload attribute
}
//<meta http-equiv="content-type" content="text/html; charset=utf-8" />
sb.append("<meta http-equiv=\"content-type\" content=\"text/html; charset=");
if (parser.getCharsetName() != null) sb.append(parser.getCharsetName());
else sb.append(DEFAULT_ENCODING);
sb.append("\"");
if (docType != null && docType.indexOf("XHTML") > 0) sb.append("/"); // close tag only when xhtml to validate
sb.append(">");
if (!parser.hasOwnCss()) {
// add olat content css as used in html editor
sb.append("<link href=\"").append(themeBaseUri).append("all/content.css\" rel=\"stylesheet\" type=\"text/css\" ");
if (docType != null && docType.indexOf("XHTML") > 0) sb.append("/"); // close tag only when xhtml to validate
sb.append(">\n");
if (customCssURL != null) {
// add the custom CSS, e.g. the course css that overrides the standard content css
sb.append("<link href=\"").append(customCssURL).append("\" rel=\"stylesheet\" type=\"text/css\" ");
if (docType != null && docType.indexOf("XHTML") > 0) sb.append("/"); // close tag only when xhtml to validate
sb.append(">\n");
}
}
//TODO:gs:a do not include if it is a scorm packge!! may results in problems
if (this.enableTextmarking) {
if (log.isDebug()) log.debug("Textmarking is enabled, including tooltips js files into iframe source...");
sb.append("\n<script type=\"text/javascript\" src=\"");
StaticMediaDispatcher.renderStaticURI(sb, "js/prototype/prototype.js");
sb.append("\"></script>");
sb.append("\n<script type=\"text/javascript\" src=\"");
StaticMediaDispatcher.renderStaticURI(sb, "js/ext/adapter/prototype/ext-prototype-adapter.js");
sb.append("\"></script>");
sb.append("\n<link rel=\"stylesheet\" type=\"text/css\" href=\"");
StaticMediaDispatcher.renderStaticURI(sb, "js/ext/resources/css/ext-all.css");
sb.append("\"");
if (docType != null && docType.indexOf("XHTML") > 0) sb.append("/"); // close tag only when xhtml to validate
// Loading ExtJS minimalisic, only what's needed for the quick tips
sb.append(">\n<script type=\"text/javascript\" src=\"");
StaticMediaDispatcher.renderStaticURI(sb, "js/ext/pkgs/ext-foundation.js");
sb.append("\"></script>");
sb.append("\n<script type=\"text/javascript\" src=\"");
StaticMediaDispatcher.renderStaticURI(sb, "js/ext/pkgs/cmp-foundation.js");
sb.append("\"></script>");
sb.append("\n<script type=\"text/javascript\" src=\"");
StaticMediaDispatcher.renderStaticURI(sb, "js/ext/pkgs/ext-dd.js");
sb.append("\"></script>");
sb.append("\n<script type=\"text/javascript\" src=\"");
StaticMediaDispatcher.renderStaticURI(sb, "js/ext/pkgs/pkg-tips.js");
sb.append("\"></script>");
// Load glossary code now
sb.append("\n<script type=\"text/javascript\" id=\"textmarkerLib\" src=\"");
sb.append( getWindowControl().getWindowBackOffice().getWindowManager().getMapPathFor(GlossaryMarkupItemController.class) ) ;
sb.append("/js/glossaryhighlighter.js");
sb.append("\"></script>");
sb.append("\n<link rel=\"stylesheet\" type=\"text/css\" id=\"textmarkercss\" href=\"");
sb.append( getWindowControl().getWindowBackOffice().getWindowManager().getMapPathFor(GlossaryMarkupItemController.class) ) ;
sb.append("/css/textmarker.css");
sb.append("\">\n");
}
// Load some iframe.js helper code
sb.append("\n<script type=\"text/javascript\">\n/* <![CDATA[ */\n");
// Set the iframe id, used by the resize function. Important to set before iframe.js is loaded
sb.append("b_iframeid=\"").append(this.frameId).append("\";");
sb.append("b_isInlineUri=").append(Boolean.valueOf(addCheckForInlineEvents).toString()).append(";");
sb.append("\n/* ]]> */\n</script>");
sb.append("<script type=\"text/javascript\" src=\"").append(staticFilesPath).append("/js/iframe.js\"></script>\n");
// Resize frame to fit height of html page.
// Do this only when there is some content available. This can be false when
// the content is written all dynamically via javascript. In this cases, the
// resizeing is meaningless anyway.
if (parser.getHtmlContent().length() > 0) {
sb.append("\n<script type=\"text/javascript\">\n/* <![CDATA[ */\n");
// register the resize code to be executed on document load and click events
if (adjusteightAutomatically) {
sb.append("b_addOnloadEvent(b_sizeIframe);");
sb.append("b_addOnclickEvent(b_sizeIframe);");
}
// register the tooltips enabling on document load event
sb.append("b_addOnloadEvent(b_enableTooltips);");
sb.append("b_addOnloadEvent(b_hideExtMessageBox);");
if (addCheckForInlineEvents) {
// Refresh dirty menu tree by triggering client side request to component which fires events
// which is not possible by mappers. The method will first check if the page is loaded in our
// iframe and ignore all other requests (files in framesets, sub-iframes, AJAX calls etc)
if ((System.currentTimeMillis() - this.suppressEndlessReload) > 2000) sb.append("b_addOnloadEvent(b_sendNewUriEventToParent);");
this.suppressEndlessReload = System.currentTimeMillis();
}
sb.append("b_addOnloadEvent(b_changeLinkTargets);");
if (this.enableTextmarking){
sb.append("b_addOnloadEvent(b_glossaryHighlight);");
}
sb.append("\n/* ]]> */\n</script>");
}
String origHTMLHead = parser.getHtmlHead();
// jsMath brute force approach to render latex formulas: add library if
// a jsmath class is found in the code and the library is not already in
// the header of the page
if (BaseChiefController.isJsMathEnabled()) {
if ((page.indexOf("class=\"math\"") != -1 || page.indexOf("class='math'") != -1) && (origHTMLHead == null || origHTMLHead.indexOf("jsMath/easy/load.js") == -1)) {
sb.append("\n<script type=\"text/javascript\" src=\"");
StaticMediaDispatcher.renderStaticURI(sb, "js/jsMath/easy/load.js");
sb.append("\"></script>");
// don't show jsmath info box, aready visible in parent window
sb.append("<style type='text/css'>#jsMath_button {display:none}</style>");
}
}
// add some custom header things like js code or css
if (customHeaderContent != null) {
sb.append(customHeaderContent);
}
// Add HTML header stuff from original page: css, javascript, title etc.
if (origHTMLHead != null) sb.append(origHTMLHead);
sb.append("</head>");
// use the original body tag, may include all kind of attributes (class, style, onload, on...)
sb.append(parser.getBodyTag());
// finally add content and finish page
sb.append(parser.getHtmlContent());
sb.append("</body></html>");
return sb.toString();
}