No more js global variable platal_baseurl, use $.plURL() instead.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Sun, 23 Jan 2011 14:02:20 +0000 (15:02 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Sun, 23 Jan 2011 14:08:48 +0000 (15:08 +0100)
Also add usefull string functions (startsWith, endsWith, htmlEntities).

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
htdocs/javascript/core.js
templates/plpage.header.tpl

index bc9463e..4df8866 100644 (file)
 
     /* Add new functions to jQuery namesapce */
     $.extend({
+        plURL: (function() {
+            var base;
+            return function(url) {
+                if (url.startsWith('http', true)) {
+                    return url;
+                }
+                if (typeof base == 'undefined') {
+                    base = $('head base');
+                    if (base.length > 0) {
+                        base = base.attr('href');
+                        if (!base.endsWith('/')) {
+                            base += '/';
+                        }
+                    } else {
+                        base = '';
+                    }
+                }
+                return base + url;
+            }
+        }()),
+
         /* The goal of the following functions is to provide an AJAX API that
          * take a different callback in case of HTTP success code (2XX) and in
          * other cases.
@@ -74,7 +95,7 @@
                 }
             }
             return $.ajax({
-                url: source,
+                url: $.plURL(source),
                 type: method,
                 success: ajaxHandler,
                 data : data,
@@ -238,6 +259,53 @@ RegExp.escape = function(text) {
 }
 
 // }}}
+// {{{ String extension
+
+String.prototype.startsWith = function(str, caseInsensitive) {
+    var cmp = this;
+
+    if (str.length > this.length) {
+        return false;
+    }
+    if (caseInsensitive) {
+        str = str.toLowerCase();
+        cmp = cmp.toLowerCase();
+    }
+    return cmp.substr(0, str.length) == str;
+}
+
+String.prototype.endsWith = function(str, caseInsensitive) {
+    var cmp = this;
+
+    if (str.length > this.length) {
+        return false;
+    }
+    if (caseInsensitive) {
+        str = str.toLowerCase();
+        cmp = cmp.toLowerCase();
+    }
+    return cmp.substr(cmp.length - str.length, str.length) == str;
+}
+
+String.prototype.htmlEntities = function() {
+    return this.replace(/&/g,'&amp;')
+               .replace(new RegExp('<','g'),'&lt;')
+               .replace(/>/g,'&gt;');
+}
+
+String.prototype.contains = function(str, caseInsensitive) {
+    var cmp = this;
+    if (str.length > this.length) {
+        return false;
+    }
+    if (caseInsensitive) {
+        str = str.toLowerCase();
+        cmp = cmp.toLowerCase();
+    }
+    return cmp.indexOf(str) >= 0;
+}
+
+// }}}
 // {{{ PmWiki decoding
 
 Nix = {
index f1c50ae..0f45653 100644 (file)
@@ -37,9 +37,6 @@
 <link rel="{$link.rel}" href="{$link.href}" />
 {/foreach}
 {/if}
-<script type="text/javascript">
-  var platal_baseurl = "{$globals->baseurl}/";
-</script>
 {if t($pl_js)}
 {foreach from=$pl_js item=js}
 <script type="text/javascript" src="{$js}"></script>