Refactoring API de IndexIterator.
[Plinn.git] / skins / ajax_scripts / fragment_importer.js
index 9830ef5..52d7eb0 100644 (file)
@@ -10,13 +10,10 @@ var FragmentImporter;
 
 var isTextMime = /^text\/.+/i;
 
-FragmentImporter = function(url, onAfterPopulate, baseElement) {
+FragmentImporter = function(url, onAfterPopulate) {
        var thisImporter = this;
        this.url = url;
        this.onAfterPopulate = (!onAfterPopulate) ? function(){return;} : onAfterPopulate;
-       this.baseElement = baseElement;
-       if (baseElement && window.console)
-               console.warn('Deprecation warning : usage of baseElement will be removed. Use an xml based response.');
 };
 
 FragmentImporter.prototype._load = function(url) {
@@ -40,12 +37,12 @@ FragmentImporter.prototype._load = function(url) {
                                break;
                        case 4 :
                                hideProgressImage();
-                               if (req.status == '200')
-                                       thisImporter.populateBaseElement(req);
-                               else
-                                       alert('Error: ' + req.status);
-                               
-               };
+                               if (req.status === 200) {
+                                       thisImporter.populateBaseElement(req); }
+                               else {
+                                       alert('Error: ' + req.status); }
+                               break;
+               }
        };
 
        req.open("GET", url, true);
@@ -53,17 +50,17 @@ FragmentImporter.prototype._load = function(url) {
 };
 
 FragmentImporter.prototype.load = function(fallBackUrl) {
-       if (fallBackUrl)
-               this._fallBackUrl = fallBackUrl;
-       else
-               this._fallBackUrl = this.url;
+       if (fallBackUrl) {
+               this._fallBackUrl = fallBackUrl; }
+       else {
+               this._fallBackUrl = this.url; }
        this._load(this.url);
 };
 
 FragmentImporter.prototype.useMacro = function(template, macro, fragmentId, queryString) {
        var url = this.url + "/use_macro?template=" + template + "&macro=" + macro + "&fragmentId=" + fragmentId;
-       if (queryString)
-               url += '&' + queryString;
+       if (queryString) {
+               url += '&' + queryString; }
        this._load(url);
 };
 
@@ -76,38 +73,36 @@ FragmentImporter.prototype.populateBaseElement = function(req) {
                window.location.href = this._fallBackUrl;
                return;
        }
-       if (contentType.indexOf('text/xml') != -1) {
+       if (contentType.indexOf('text/xml') !== -1) {
                var fragments = req.responseXML.documentElement.childNodes;
-               var fragment, dest, scripts;
-               for (var i=0 ; i < fragments.length ; i++) {
-                       fragment = fragments[i];
-                       if (fragment.nodeName == 'fragment') {
-                               dest = document.getElementById(fragment.getAttribute('id'));
-                               if(!dest)
-                                       continue;
-                               dest.innerHTML = fragment.firstChild.nodeValue;
-                               
-                               scripts = dest.getElementsByTagName('script');
-                               for (var j=0 ; j < scripts.length ; j++)
-                                       globalScriptRegistry.loadScript(scripts[j]);
+               var element, dest, scripts, i, j;
+               for (i=0 ; i < fragments.length ; i++) {
+                       element = fragments[i];
+                       switch (element.nodeName) {
+                               case 'fragment' :
+                                       dest = document.getElementById(element.getAttribute('id'));
+                                       if(dest) {
+                                               dest.innerHTML = element.firstChild.nodeValue;
+                                               scripts = dest.getElementsByTagName('script');
+                                               for (j=0 ; j < scripts.length ; j++) {
+                                                       globalScriptRegistry.loadScript(scripts[j]); }
+                                       }
+                                       break;
+                               case 'base' :
+                                       var headBase = document.getElementsByTagName('base');
+                                       if (headBase.length > 0) {
+                                               headBase[0].setAttribute('href', element.getAttribute('href'));
+                                       }
+                                       else {
+                                               headBase = document.createElement('base');
+                                               headBase.setAttribute('href', element.getAttribute('href'));
+                                               document.head.appendChild(headBase);
+                                       }
+                                       break;
                        }
                }
        }
-       else {
-               this.baseElement.innerHTML = req.responseText;
-       
-               var scripts = this.baseElement.getElementsByTagName('script');
-               for (var i=0 ; i < scripts.length ; i++)
-                       globalScriptRegistry.loadScript(scripts[i]);
-       }
-
-       var onAfterPopulate = this.onAfterPopulate;
-       if (typeof(onAfterPopulate) == "string") {
-               if (console)
-                       console.warn('Deprecation warning : onAfterPopulate may not be a string (' + onAfterPopulate + ')');
-               onAfterPopulate = eval(onAfterPopulate);
-       }
-       onAfterPopulate();
+       this.onAfterPopulate();
 };
 
-})();
\ No newline at end of file
+}());
\ No newline at end of file