web/data/cubicweb.ajax.js
changeset 7284 a474cb989546
parent 7199 589801acf08e
child 7309 22403d5c3da4
equal deleted inserted replaced
7283:1699393372b3 7284:a474cb989546
    84 });
    84 });
    85 
    85 
    86 
    86 
    87 var JSON_BASE_URL = baseuri() + 'json?';
    87 var JSON_BASE_URL = baseuri() + 'json?';
    88 
    88 
       
    89 /**
       
    90  * returns true if `url` is a mod_concat-like url
       
    91  * (e.g. http://..../data??resource1.js,resource2.js)
       
    92  */
       
    93 function _modconcatLikeUrl(url) {
       
    94     var base = baseuri();
       
    95     if (!base.endswith('/')) {
       
    96         base += '/';
       
    97     }
       
    98     var modconcat_rgx = new RegExp('(' + base + 'data/([a-z0-9]+/))?\\?\\?(.+)');
       
    99     return modconcat_rgx.exec(url);
       
   100 }
       
   101 
       
   102 /**
       
   103  * decomposes a mod_concat-like url into its corresponding list of
       
   104  * resources' urls
       
   105  *
       
   106  * >>> _listResources('http://foo.com/data/??a.js,b.js,c.js')
       
   107  * ['http://foo.com/data/a.js', 'http://foo.com/data/b.js', 'http://foo.com/data/c.js']
       
   108  */
       
   109 function _listResources(src) {
       
   110     var resources = [];
       
   111     var groups = _modconcatLikeUrl(src);
       
   112     if (groups == null) {
       
   113         resources.push(src);
       
   114     } else {
       
   115         var dataurl = groups[0];
       
   116         $.each(cw.utils.lastOf(groups).split(','),
       
   117                function() {
       
   118                    resources.push(dataurl + this);
       
   119                });
       
   120     }
       
   121     return resources;
       
   122 }
       
   123 
    89 //============= utility function handling remote calls responses. ==============//
   124 //============= utility function handling remote calls responses. ==============//
    90 function _loadAjaxHtmlHead($node, $head, tag, srcattr) {
   125 function _loadAjaxHtmlHead($node, $head, tag, srcattr) {
    91     var jqtagfilter = tag + '[' + srcattr + ']';
   126     var jqtagfilter = tag + '[' + srcattr + ']';
    92     if (cw['loaded_'+srcattr] === undefined) {
   127     if (cw['loaded_'+srcattr] === undefined) {
    93         cw['loaded_'+srcattr] = [];
   128         cw['loaded_'+srcattr] = [];
    94         var loaded = cw['loaded_'+srcattr];
   129         var loaded = cw['loaded_'+srcattr];
    95         jQuery('head ' + jqtagfilter).each(function(i) {
   130         jQuery('head ' + jqtagfilter).each(function(i) {
    96                    loaded.push(this.getAttribute(srcattr));
   131             // tab1.push.apply(tab1, tab2) <=> tab1 += tab2 (python-wise)
    97                });
   132             loaded.push.apply(loaded, _listResources(this.getAttribute(srcattr)));
       
   133         });
    98     } else {
   134     } else {
    99         var loaded = cw['loaded_'+srcattr];
   135         var loaded = cw['loaded_'+srcattr];
   100     }
   136     }
   101     $node.find(tag).each(function(i) {
   137     $node.find(tag).each(function(i) {
   102         var url = this.getAttribute(srcattr);
   138         var srcnode = this;
       
   139         var url = srcnode.getAttribute(srcattr);
   103         if (url) {
   140         if (url) {
   104             if (jQuery.inArray(url, loaded) == -1) {
   141             $.each(_listResources(url), function() {
   105                 // take care to <script> tags: jQuery append method script nodes
   142                 var resource = '' + this; // implicit object->string cast
   106                 // don't appears in the DOM (See comments on
   143                 if ($.inArray(resource, loaded) == -1) {
   107                 // http://api.jquery.com/append/), which cause undesired
   144                     // take care to <script> tags: jQuery append method script nodes
   108                 // duplicated load in our case. After trying to use bare DOM api
   145                     // don't appears in the DOM (See comments on
   109                 // to avoid this, we switched to handle a list of already loaded
   146                     // http://api.jquery.com/append/), which cause undesired
   110                 // stuff ourselves, since bare DOM api gives bug with the
   147                     // duplicated load in our case. After trying to use bare DOM api
   111                 // server-response event, since we loose control on when the
   148                     // to avoid this, we switched to handle a list of already loaded
   112                 // script is loaded (jQuery load it immediatly).
   149                     // stuff ourselves, since bare DOM api gives bug with the
   113                 loaded.push(url);
   150                     // server-response event, since we loose control on when the
   114                 jQuery(this).appendTo($head);
   151                     // script is loaded (jQuery load it immediatly).
   115             }
   152                     loaded.push(resource);
       
   153                     jQuery(srcnode).appendTo($head);
       
   154                 }
       
   155             });
   116         } else {
   156         } else {
   117             jQuery(this).appendTo($head);
   157             jQuery(srcnode).appendTo($head);
   118         }
   158         }
   119     });
   159     });
   120     $node.find(jqtagfilter).remove();
   160     $node.find(jqtagfilter).remove();
   121 }
   161 }
   122 
   162