From c6e84b6d8243fd19b8d767c244be789ab45d7401 Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Sun, 23 Jan 2011 15:02:20 +0100 Subject: [PATCH] No more js global variable platal_baseurl, use $.plURL() instead. Also add usefull string functions (startsWith, endsWith, htmlEntities). Signed-off-by: Florent Bruneau --- htdocs/javascript/core.js | 70 ++++++++++++++++++++++++++++++++++++++++++++- templates/plpage.header.tpl | 3 -- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/htdocs/javascript/core.js b/htdocs/javascript/core.js index bc9463e..4df8866 100644 --- a/htdocs/javascript/core.js +++ b/htdocs/javascript/core.js @@ -41,6 +41,27 @@ /* 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,'&') + .replace(new RegExp('<','g'),'<') + .replace(/>/g,'>'); +} + +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 = { diff --git a/templates/plpage.header.tpl b/templates/plpage.header.tpl index f1c50ae..0f45653 100644 --- a/templates/plpage.header.tpl +++ b/templates/plpage.header.tpl @@ -37,9 +37,6 @@ {/foreach} {/if} - {if t($pl_js)} {foreach from=$pl_js item=js} -- 2.1.4