r9937 - apps/gobi/trunk/code/Redirect

[email protected] Sat, 19 Sep 2009 23:26:35 +0200 (CEST)
Newsgroups gmane.comp.java.helma.cvs
Message-ID <20090919212635.49C743D0E3@mia>
Author: hannes
Date: 2009-09-19 23:26:35 +0200 (Sat, 19 Sep 2009)
New Revision: 9937

Modified:
   apps/gobi/trunk/code/Redirect/Redirect.js
Log:
More fun: redirect all sub-URLs of the redirect page to the corresponding sub-URL of the target URL

Details at http://dev.helma.org/trac/helma/changeset/9937

Modified: apps/gobi/trunk/code/Redirect/Redirect.js
===================================================================
--- apps/gobi/trunk/code/Redirect/Redirect.js	2009-09-19 20:24:37 UTC (rev 9936)
+++ apps/gobi/trunk/code/Redirect/Redirect.js	2009-09-19 21:26:35 UTC (rev 9937)
@@ -12,7 +12,7 @@
     } else {
         if (this.body.indexOf('://') == -1) {
             // local redirect, set message
-            res.message = "(Redirected from " + this.getHtmlLink() + ")";
+            res.message = this.getRedirectMessage();
         }
         res.redirect(this.getUrl());
     }
@@ -40,4 +40,43 @@
 
 function href(action) {
     return this.rawHref(action) + "?redirect=no";
+}
+
+function getRedirectMessage() {
+    return "(Redirected from " + this.getHtmlLink() + ")";
+}
+
+// Dynamically generate transient redirect child pages so that
+// this/foo/bar is redirected to target/foo/bar
+function getChildElement(name) {
+    // create a transient
+    var child = new Redirect(name);
+    child.url = this.getChildUrl(name);
+    child.message = this.message || this.getRedirectMessage();
+    // overwrite getUrl() to just return the precalculated url
+    child.getUrl = function() {
+        return this.url;
+    }
+    // plain simple main action that always redirects
+    child.main_action = function() {
+        res.message = this.message;
+        res.redirect(this.getUrl());
+    }
+    return child;
+}
+
+// Create a child url of the target url, trying to be smart about
+// query strings and slashes etc.
+function getChildUrl(name) {
+    var url = this.getUrl(),
+        query = '';
+    var q = url.indexOf('?');
+    if (q > -1) {
+        url = url.substring(0, q);
+        query = url.substring(q);
+    }
+    if (!url.endsWith('/')) {
+        url = url + '/';
+    }
+    return url + encodeURIComponent(name) + '/' + query;
 }
\ No newline at end of file