]> source.dussan.org Git - gitblit.git/commitdiff
Support for adding bottom scripts
authorTom <tw201207@gmail.com>
Wed, 12 Nov 2014 19:19:29 +0000 (20:19 +0100)
committerTom <tw201207@gmail.com>
Wed, 12 Nov 2014 19:19:29 +0000 (20:19 +0100)
Needed if we want to have opacity changes in image diffs because
jQuery is bottom-loaded, so we must ensure that any scripts using
jQuery are run later.

I'm not a Wicket expert; maybe there's a cleverer or cleaner way to do
this. There is a JavascriptUtils class in Wicket, but that writes to
the response -- I don't quite see how that would give me control over
the precise placement of the scripts to ensure they come after that
bottom-loaded jQuery.

src/main/java/com/gitblit/wicket/pages/BasePage.html
src/main/java/com/gitblit/wicket/pages/BasePage.java

index 7aa78360eb29316b3196850e57c8e5595cd0937f..b998428c53f923e6b855b853dff3334e874aeabf 100644 (file)
@@ -51,5 +51,6 @@
                <!-- Include scripts at end for faster page loading -->\r
                <script type="text/javascript" src="bootstrap/js/jquery.js"></script>\r
                <script type="text/javascript" src="bootstrap/js/bootstrap.js"></script>                \r
+               <wicket:container wicket:id="bottomScripts"></wicket:container>\r
        </body>\r
 </html>
\ No newline at end of file
index b696700395076d4c71b0f44e2c9f8c1e83bdc619..534c196be421357fdb505fd8a0f6594ee272d8d4 100644 (file)
@@ -35,6 +35,7 @@ import javax.servlet.http.HttpServletRequest;
 \r
 import org.apache.commons.io.IOUtils;\r
 import org.apache.wicket.Application;\r
+import org.apache.wicket.Component;\r
 import org.apache.wicket.Page;\r
 import org.apache.wicket.PageParameters;\r
 import org.apache.wicket.RedirectToUrlException;\r
@@ -42,6 +43,8 @@ import org.apache.wicket.markup.html.CSSPackageResource;
 import org.apache.wicket.markup.html.basic.Label;\r
 import org.apache.wicket.markup.html.link.ExternalLink;\r
 import org.apache.wicket.markup.html.panel.FeedbackPanel;\r
+import org.apache.wicket.markup.html.resources.JavascriptResourceReference;\r
+import org.apache.wicket.markup.repeater.RepeatingView;\r
 import org.apache.wicket.protocol.http.RequestUtils;\r
 import org.apache.wicket.protocol.http.WebResponse;\r
 import org.apache.wicket.protocol.http.servlet.ServletWebRequest;\r
@@ -75,11 +78,13 @@ public abstract class BasePage extends SessionPage {
 \r
        public BasePage() {\r
                super();\r
+               add(new RepeatingView("bottomScripts").setRenderBodyOnly(true));\r
                customizeHeader();\r
        }\r
 \r
        public BasePage(PageParameters params) {\r
                super(params);\r
+               add(new RepeatingView("bottomScripts").setRenderBodyOnly(true));\r
                customizeHeader();\r
        }\r
 \r
@@ -506,4 +511,38 @@ public abstract class BasePage extends SessionPage {
                return sb.toString();\r
        }\r
 \r
+       /**\r
+        * Adds a HTML script element loading the javascript designated by the given path.\r
+        *\r
+        * @param scriptPath\r
+        *            page-relative path to the Javascript resource; normally starts with "script/"\r
+        */\r
+       protected void addBottomScript(String scriptPath) {\r
+               Component bottomScriptContainer = get("bottomScripts");\r
+               if (bottomScriptContainer instanceof RepeatingView) {\r
+                       // Always true.\r
+                       RepeatingView bottomScripts = (RepeatingView) bottomScriptContainer;\r
+                       Label script = new Label(bottomScripts.newChildId(), "<script type='text/javascript' src='"\r
+                                       + urlFor(new JavascriptResourceReference(this.getClass(), scriptPath)) + "'></script>\n");\r
+                       bottomScripts.add(script.setEscapeModelStrings(false).setRenderBodyOnly(true));\r
+               }\r
+       }\r
+\r
+       /**\r
+        * Adds a HTML script element containing the given code.\r
+        *\r
+        * @param code\r
+        *            inline script code\r
+        */\r
+       protected void addBottomScriptInline(String code) {\r
+               Component bottomScriptContainer = get("bottomScripts");\r
+               if (bottomScriptContainer instanceof RepeatingView) {\r
+                       // Always true.\r
+                       RepeatingView bottomScripts = (RepeatingView) bottomScriptContainer;\r
+                       Label script = new Label(bottomScripts.newChildId(),\r
+                                       "<script type='text/javascript'>/*<![CDATA[*/\n" + code + "\n//]]>\n</script>\n");\r
+                       bottomScripts.add(script.setEscapeModelStrings(false).setRenderBodyOnly(true));\r
+               }\r
+       }\r
+\r
 }\r