Fix bug with multiple scripts loading by widget

This commit is contained in:
Dmitriy Simushev 2013-05-21 09:22:48 +00:00
parent 11d726867c
commit b3c77aa972
2 changed files with 2 additions and 2 deletions

View File

@ -8,7 +8,7 @@
var Mibew={}; var Mibew={};
(function(a){a.Objects={};a.Widget=function(b){this.requestedScripts={};this.handlers=[];this.handlersDependences={};this.requestURL=b.requestURL;this.requestTimeout=b.requestTimeout;this.visitorCookieName=b.visitorCookieName;this.inviteStyle=b.inviteStyle;this.locale=b.locale;this.dataToSend={};var c=document.createElement("link");c.setAttribute("rel","stylesheet");c.setAttribute("type","text/css");c.setAttribute("href",b.inviteStyle);document.getElementsByTagName("head")[0].appendChild(c)};a.Widget.prototype.makeRequest= (function(a){a.Objects={};a.Widget=function(b){this.requestedScripts={};this.handlers=[];this.handlersDependences={};this.requestURL=b.requestURL;this.requestTimeout=b.requestTimeout;this.visitorCookieName=b.visitorCookieName;this.inviteStyle=b.inviteStyle;this.locale=b.locale;this.dataToSend={};var c=document.createElement("link");c.setAttribute("rel","stylesheet");c.setAttribute("type","text/css");c.setAttribute("href",b.inviteStyle);document.getElementsByTagName("head")[0].appendChild(c)};a.Widget.prototype.makeRequest=
function(){var b=a.Utils.readCookie(this.visitorCookieName);this.dataToSend.entry=escape(document.referrer);this.dataToSend.locale=this.locale;this.dataToSend.rnd=Math.random();!1!==b?this.dataToSend.user_id=b:this.dataToSend.user_id&&delete this.dataToSend.user_id;this.doLoadScript(this.requestURL+"?"+this.getQuery(),"responseScript");this.dataToSend={}};a.Widget.prototype.getQuery=function(){var b=[],c;for(c in this.dataToSend)this.dataToSend.hasOwnProperty(c)&&b.push(c+"="+this.dataToSend[c]); function(){var b=a.Utils.readCookie(this.visitorCookieName);this.dataToSend.entry=escape(document.referrer);this.dataToSend.locale=this.locale;this.dataToSend.rnd=Math.random();!1!==b?this.dataToSend.user_id=b:this.dataToSend.user_id&&delete this.dataToSend.user_id;this.doLoadScript(this.requestURL+"?"+this.getQuery(),"responseScript");this.dataToSend={}};a.Widget.prototype.getQuery=function(){var b=[],c;for(c in this.dataToSend)this.dataToSend.hasOwnProperty(c)&&b.push(c+"="+this.dataToSend[c]);
return b.join("&")};a.Widget.prototype.sendToServer=function(b){for(var c in b)if(b.hasOwnProperty(c)){var a=b[c];"string"!==typeof a&&"number"!==typeof a||("string"===typeof a&&(a=encodeURIComponent(a)),this.dataToSend[c]=a)}};a.Widget.prototype.onResponse=function(b){var c=b.load,d=b.handlers,e=b.data;b=b.dependences;var j=this,f;for(f in c)c.hasOwnProperty(f)&&!(c[f]in this.requestedScripts)&&(this.requestedScripts[f]={},this.requestedScripts[f].url=c[f],this.requestedScripts[f].status="loading", return b.join("&")};a.Widget.prototype.sendToServer=function(b){for(var c in b)if(b.hasOwnProperty(c)){var a=b[c];"string"!==typeof a&&"number"!==typeof a||("string"===typeof a&&(a=encodeURIComponent(a)),this.dataToSend[c]=a)}};a.Widget.prototype.onResponse=function(b){var c=b.load,d=b.handlers,e=b.data;b=b.dependences;var j=this,f;for(f in c)c.hasOwnProperty(f)&&!(f in this.requestedScripts)&&(this.requestedScripts[f]={},this.requestedScripts[f].url=c[f],this.requestedScripts[f].status="loading",
this.loadScript(f));for(var h in b)b.hasOwnProperty(h)&&(h in this.handlersDependences||(this.handlersDependences[h]=b[h]));for(c=0;c<d.length;c++){var g=d[c];if(this.canRunHandler(g))a.APIFunctions[g](e);else g in this.handlers||(this.handlers[g]=function(){a.APIFunctions[g](e)})}this.cleanUpAfterRequest();window.setTimeout(function(){j.makeRequest()},this.requestTimeout)};a.Widget.prototype.cleanUpAfterRequest=function(){document.getElementsByTagName("head")[0].removeChild(document.getElementById("responseScript"))}; this.loadScript(f));for(var h in b)b.hasOwnProperty(h)&&(h in this.handlersDependences||(this.handlersDependences[h]=b[h]));for(c=0;c<d.length;c++){var g=d[c];if(this.canRunHandler(g))a.APIFunctions[g](e);else g in this.handlers||(this.handlers[g]=function(){a.APIFunctions[g](e)})}this.cleanUpAfterRequest();window.setTimeout(function(){j.makeRequest()},this.requestTimeout)};a.Widget.prototype.cleanUpAfterRequest=function(){document.getElementsByTagName("head")[0].removeChild(document.getElementById("responseScript"))};
a.Widget.prototype.loadScript=function(b){var c=this,a=this.doLoadScript(this.requestedScripts[b].url,b);a.onload=function(){c.scriptReady(b)};a.onreadystatechange=function(){("complete"==this.readyState||"loaded"==this.readyState)&&c.scriptReady(b)}};a.Widget.prototype.doLoadScript=function(b,c){var a=document.createElement("script");a.setAttribute("type","text/javascript");a.setAttribute("src",b);a.setAttribute("id",c);document.getElementsByTagName("head")[0].appendChild(a);return a};a.Widget.prototype.scriptReady= a.Widget.prototype.loadScript=function(b){var c=this,a=this.doLoadScript(this.requestedScripts[b].url,b);a.onload=function(){c.scriptReady(b)};a.onreadystatechange=function(){("complete"==this.readyState||"loaded"==this.readyState)&&c.scriptReady(b)}};a.Widget.prototype.doLoadScript=function(b,c){var a=document.createElement("script");a.setAttribute("type","text/javascript");a.setAttribute("src",b);a.setAttribute("id",c);document.getElementsByTagName("head")[0].appendChild(a);return a};a.Widget.prototype.scriptReady=
function(b){this.requestedScripts[b].status="ready";for(var a in this.handlers)this.handlers.hasOwnProperty(a)&&this.canRunHandler(a)&&(this.handlers[a](),delete this.handlers[a])};a.Widget.prototype.canRunHandler=function(b){b=this.handlersDependences[b];for(var a=0;a<b.length;a++)if("ready"!=this.requestedScripts[b[a]].status)return!1;return!0};a.Widget.init=function(b){a.Objects.widget=new a.Widget(b);a.Objects.widget.makeRequest()};a.Utils={};a.Utils.createCookie=function(b,a){var d=/([^\.]+\.[^\.]+)$/.exec(document.location.hostname)[1]; function(b){this.requestedScripts[b].status="ready";for(var a in this.handlers)this.handlers.hasOwnProperty(a)&&this.canRunHandler(a)&&(this.handlers[a](),delete this.handlers[a])};a.Widget.prototype.canRunHandler=function(b){b=this.handlersDependences[b];for(var a=0;a<b.length;a++)if("ready"!=this.requestedScripts[b[a]].status)return!1;return!0};a.Widget.init=function(b){a.Objects.widget=new a.Widget(b);a.Objects.widget.makeRequest()};a.Utils={};a.Utils.createCookie=function(b,a){var d=/([^\.]+\.[^\.]+)$/.exec(document.location.hostname)[1];

View File

@ -185,7 +185,7 @@ var Mibew = {};
continue; continue;
} }
// Check if script already loaded // Check if script already loaded
if (! (load[id] in this.requestedScripts)) { if (! (id in this.requestedScripts)) {
this.requestedScripts[id] = {}; this.requestedScripts[id] = {};
this.requestedScripts[id].url = load[id]; this.requestedScripts[id].url = load[id];
this.requestedScripts[id].status = 'loading'; this.requestedScripts[id].status = 'loading';