# HG changeset patch # User Sylvain Thénault # Date 1306316551 -7200 # Node ID 9aadb5a04b53a80ad838d49af11ceff26edd380b # Parent cab99ccdb774d446134800305c32ea3ef75b8c29# Parent 06444c1233e081be72cd9ec39b6fbaf2c79ae9b6 merge default heads diff -r cab99ccdb774 -r 9aadb5a04b53 web/data/cubicweb.ajax.js --- a/web/data/cubicweb.ajax.js Wed May 25 11:41:16 2011 +0200 +++ b/web/data/cubicweb.ajax.js Wed May 25 11:42:31 2011 +0200 @@ -22,6 +22,9 @@ * * dummy ultra minimalist implementation of deferred for jQuery */ + +cw.ajax = new Namespace('cw.ajax'); + function Deferred() { this.__init__(this); } @@ -86,40 +89,64 @@ var JSON_BASE_URL = baseuri() + 'json?'; -/** - * returns true if `url` is a mod_concat-like url - * (e.g. http://..../data??resource1.js,resource2.js) - */ -function _modconcatLikeUrl(url) { - var base = baseuri(); - if (!base.endswith('/')) { - base += '/'; - } - var modconcat_rgx = new RegExp('(' + base + 'data/([a-z0-9]+/)?)\\?\\?(.+)'); - return modconcat_rgx.exec(url); -} + +jQuery.extend(cw.ajax, { + /* variant of jquery evalScript with cache: true in ajax call */ + _evalscript: function ( i, elem ) { + if ( elem.src ) { + jQuery.ajax({ + url: elem.src, + async: false, + cache: true, + dataType: "script" + }); + } else { + jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" ); + } + if ( elem.parentNode ) { + elem.parentNode.removeChild( elem ); + } + }, + + evalscripts: function ( scripts ) { + if ( scripts.length ) { + jQuery.each(scripts, cw.ajax._evalscript); + } + }, -/** - * decomposes a mod_concat-like url into its corresponding list of - * resources' urls - * - * >>> _listResources('http://foo.com/data/??a.js,b.js,c.js') - * ['http://foo.com/data/a.js', 'http://foo.com/data/b.js', 'http://foo.com/data/c.js'] - */ -function _listResources(src) { - var resources = []; - var groups = _modconcatLikeUrl(src); - if (groups == null) { - resources.push(src); - } else { - var dataurl = groups[1]; - $.each(cw.utils.lastOf(groups).split(','), - function() { - resources.push(dataurl + this); - }); + /** + * returns true if `url` is a mod_concat-like url + * (e.g. http://..../data??resource1.js,resource2.js) + */ + _modconcatLikeUrl: function(url) { + var base = baseuri(); + if (!base.endswith('/')) { base += '/'; } + var modconcat_rgx = new RegExp('(' + base + 'data/([a-z0-9]+/)?)\\?\\?(.+)'); + return modconcat_rgx.exec(url); + }, + + /** + * decomposes a mod_concat-like url into its corresponding list of + * resources' urls + * >>> _listResources('http://foo.com/data/??a.js,b.js,c.js') + * ['http://foo.com/data/a.js', 'http://foo.com/data/b.js', 'http://foo.com/data/c.js'] + */ + _listResources: function(src) { + var resources = []; + var groups = cw.ajax._modconcatLikeUrl(src); + if (groups == null) { + resources.push(src); + } else { + var dataurl = groups[1]; + $.each(cw.utils.lastOf(groups).split(','), + function() { + resources.push(dataurl + this); + } + ); + } + return resources; } - return resources; -} +}); //============= utility function handling remote calls responses. ==============// function _loadAjaxHtmlHead($node, $head, tag, srcattr) { @@ -129,31 +156,47 @@ var loaded = cw['loaded_'+srcattr]; jQuery('head ' + jqtagfilter).each(function(i) { // tab1.push.apply(tab1, tab2) <=> tab1 += tab2 (python-wise) - loaded.push.apply(loaded, _listResources(this.getAttribute(srcattr))); + loaded.push.apply(loaded, cw.ajax._listResources(this.getAttribute(srcattr))); }); } else { var loaded = cw['loaded_'+srcattr]; } $node.find(tag).each(function(i) { - var srcnode = this; - var url = srcnode.getAttribute(srcattr); + var $srcnode = jQuery(this); + var url = $srcnode.attr(srcattr); if (url) { - $.each(_listResources(url), function() { - var resource = '' + this; // implicit object->string cast - if ($.inArray(resource, loaded) == -1) { - // take care to