mirror of
https://github.com/Mibew/design.git
synced 2025-01-22 18:10:33 +03:00
Upadate widget. It can pass some data to server now
This commit is contained in:
parent
9424e48c74
commit
29baaa700c
@ -6,11 +6,12 @@
|
|||||||
http://www.apache.org/licenses/LICENSE-2.0
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*/
|
*/
|
||||||
var Mibew={};
|
var Mibew={};
|
||||||
(function(b){b.Objects={};b.Widget=function(a){this.requestedScripts={};this.handlers=[];this.handlersDependences={};this.requestURL=a.requestURL;this.requestTimeout=a.requestTimeout;this.visitorCookieName=a.visitorCookieName;this.inviteStyle=a.inviteStyle;this.locale=a.locale;var c=document.createElement("link");c.setAttribute("rel","stylesheet");c.setAttribute("type","text/css");c.setAttribute("href",a.inviteStyle);document.getElementsByTagName("head")[0].appendChild(c)};b.Widget.prototype.makeRequest=function(){var a=
|
(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=
|
||||||
b.Utils.readCookie(this.visitorCookieName);this.doLoadScript(this.requestURL+"?entry="+escape(document.referrer)+"&locale="+this.locale+"&rnd="+Math.random()+(!1!==a?"&user_id="+a:""),"responseScript")};b.Widget.prototype.onResponse=function(a){var c=a.load,d=a.handlers,e=a.data;a=a.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",this.loadScript(f));for(var h in a)a.hasOwnProperty(h)&&
|
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]);
|
||||||
(h in this.handlersDependences||(this.handlersDependences[h]=a[h]));for(c=0;c<d.length;c++){var g=d[c];if(this.canRunHandler(g))b.APIFunctions[g](e);else g in this.handlers||(this.handlers[g]=function(){b.APIFunctions[g](e)})}this.cleanUpAfterRequest();window.setTimeout(function(){j.makeRequest()},this.requestTimeout)};b.Widget.prototype.cleanUpAfterRequest=function(){document.getElementsByTagName("head")[0].removeChild(document.getElementById("responseScript"))};b.Widget.prototype.loadScript=function(a){var 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",
|
||||||
this,b=this.doLoadScript(this.requestedScripts[a].url,a);b.onload=function(){c.scriptReady(a)};b.onreadystatechange=function(){("complete"==this.readyState||"loaded"==this.readyState)&&c.scriptReady(a)}};b.Widget.prototype.doLoadScript=function(a,c){var b=document.createElement("script");b.setAttribute("type","text/javascript");b.setAttribute("src",a);b.setAttribute("id",c);document.getElementsByTagName("head")[0].appendChild(b);return b};b.Widget.prototype.scriptReady=function(a){this.requestedScripts[a].status=
|
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"))};
|
||||||
"ready";for(var b in this.handlers)this.handlers.hasOwnProperty(b)&&this.canRunHandler(b)&&(this.handlers[b](),delete this.handlers[b])};b.Widget.prototype.canRunHandler=function(a){a=this.handlersDependences[a];for(var b=0;b<a.length;b++)if("ready"!=this.requestedScripts[a[b]].status)return!1;return!0};b.Widget.init=function(a){b.Objects.widget=new b.Widget(a);b.Objects.widget.makeRequest()};b.Utils={};b.Utils.createCookie=function(a,b){var d=/([^\.]+\.[^\.]+)$/.exec(document.location.hostname)[1];
|
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=
|
||||||
document.cookie=""+a+"="+b+"; path=/; "+(d?"domain="+d+";":"")};b.Utils.readCookie=function(a){var b=document.cookie.split("; ");a+="=";for(var d=!1,e=0;e<b.length;e++)if(-1!=b[e].indexOf(a)){d=b[e].substr(a.length);break}return d};b.APIFunctions={};b.APIFunctions.updateUserId=function(a){b.Utils.createCookie(b.Objects.widget.visitorCookieName,a.user.id)};b.APIFunctions.inviteOnResponse=function(a){var b=a.invitation.message,d=a.invitation.operator,e=a.invitation.avatar;a='<div id="mibewinvitationpopup"><div id="mibewinvitationclose"><a href="javascript:void(0);" onclick="Mibew.Invitation.hide();">×</a></div>';
|
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];
|
||||||
d&&(a+='<h1 onclick="Mibew.Invitation.accept();">'+d+"</h1>");e&&(a+='<img id="mibewinvitationavatar" src="'+e+'" title="'+d+'" alt="'+d+'" onclick="Mibew.Invitation.accept();" />');a=a+('<p onclick="Mibew.Invitation.accept();">'+b+"</p>")+'<div style="clear: both;"></div></div>';if(b=document.getElementById("mibewinvitation"))b.innerHTML=a};b.Invitation={};b.Invitation.hide=function(){var a=document.getElementById("mibewinvitationpopup");a&&(a.style.display="none")};b.Invitation.accept=function(){document.getElementById("mibewAgentButton")&&
|
document.cookie=""+b+"="+a+"; path=/; "+(d?"domain="+d+";":"")};a.Utils.readCookie=function(b){var a=document.cookie.split("; ");b+="=";for(var d=!1,e=0;e<a.length;e++)if(-1!=a[e].indexOf(b)){d=a[e].substr(b.length);break}return d};a.APIFunctions={};a.APIFunctions.updateUserId=function(b){a.Utils.createCookie(a.Objects.widget.visitorCookieName,b.user.id)};a.APIFunctions.inviteOnResponse=function(b){var a=b.invitation.message,d=b.invitation.operator,e=b.invitation.avatar;b='<div id="mibewinvitationpopup"><div id="mibewinvitationclose"><a href="javascript:void(0);" onclick="Mibew.Invitation.hide();">×</a></div>';
|
||||||
(document.getElementById("mibewAgentButton").onclick(),b.Invitation.hide())}})(Mibew);
|
d&&(b+='<h1 onclick="Mibew.Invitation.accept();">'+d+"</h1>");e&&(b+='<img id="mibewinvitationavatar" src="'+e+'" title="'+d+'" alt="'+d+'" onclick="Mibew.Invitation.accept();" />');b=b+('<p onclick="Mibew.Invitation.accept();">'+a+"</p>")+'<div style="clear: both;"></div></div>';if(a=document.getElementById("mibewinvitation"))a.innerHTML=b};a.Invitation={};a.Invitation.hide=function(){var a=document.getElementById("mibewinvitationpopup");a&&(a.style.display="none")};a.Invitation.accept=function(){document.getElementById("mibewAgentButton")&&
|
||||||
|
(document.getElementById("mibewAgentButton").onclick(),a.Invitation.hide())}})(Mibew);
|
||||||
|
@ -75,6 +75,13 @@ var Mibew = {};
|
|||||||
*/
|
*/
|
||||||
this.locale = options.locale;
|
this.locale = options.locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data that must be sent to the server with next request
|
||||||
|
* @type Object
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
this.dataToSend = {};
|
||||||
|
|
||||||
// Load additional styles
|
// Load additional styles
|
||||||
var styleSheet = document.createElement('link');
|
var styleSheet = document.createElement('link');
|
||||||
styleSheet.setAttribute('rel', 'stylesheet');
|
styleSheet.setAttribute('rel', 'stylesheet');
|
||||||
@ -91,14 +98,73 @@ var Mibew = {};
|
|||||||
// Try to get user id from local cookie
|
// Try to get user id from local cookie
|
||||||
var userId = Mibew.Utils.readCookie(this.visitorCookieName);
|
var userId = Mibew.Utils.readCookie(this.visitorCookieName);
|
||||||
|
|
||||||
|
// Prepare GET params list
|
||||||
|
this.dataToSend.entry = escape(document.referrer),
|
||||||
|
this.dataToSend.locale = this.locale;
|
||||||
|
this.dataToSend.rnd = Math.random();
|
||||||
|
if (userId !== false) {
|
||||||
|
this.dataToSend.user_id = userId;
|
||||||
|
} else {
|
||||||
|
// Enshure that there is not user_id field
|
||||||
|
if (this.dataToSend.user_id) {
|
||||||
|
delete this.dataToSend.user_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.doLoadScript(
|
this.doLoadScript(
|
||||||
this.requestURL
|
this.requestURL
|
||||||
+ '?entry=' + escape(document.referrer)
|
+ '?' + this.getQuery(),
|
||||||
+ '&locale=' + this.locale
|
|
||||||
+ '&rnd=' + Math.random()
|
|
||||||
+ ((userId !== false) ? '&user_id=' + userId : ''),
|
|
||||||
'responseScript'
|
'responseScript'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Clean up request data
|
||||||
|
this.dataToSend = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build GET request params list
|
||||||
|
*
|
||||||
|
* @returns String GET params list
|
||||||
|
*/
|
||||||
|
Mibew.Widget.prototype.getQuery = function() {
|
||||||
|
var query = [];
|
||||||
|
for(var index in this.dataToSend) {
|
||||||
|
if (! this.dataToSend.hasOwnProperty(index)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
query.push(index + '=' + this.dataToSend[index]);
|
||||||
|
}
|
||||||
|
return query.join('&');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send arbitrary data to the Server as GET params
|
||||||
|
*
|
||||||
|
* @param {Object} data List of data that should be sent to the server.
|
||||||
|
* Properties of this object will automatically transform to GET params.
|
||||||
|
* Values must be strings or numbers. Strings will be automatically escaped
|
||||||
|
* before add to query.
|
||||||
|
* Do not use properties with following names: 'entry', 'locale', 'rnd',
|
||||||
|
* 'user_id'. They are reserved by the system and will be overridden by it.
|
||||||
|
*/
|
||||||
|
Mibew.Widget.prototype.sendToServer = function(data) {
|
||||||
|
for(var index in data) {
|
||||||
|
if (! data.hasOwnProperty(index)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var value = data[index];
|
||||||
|
// Only strings and numbers can be passed to the server
|
||||||
|
if ((typeof value !== 'string') && (typeof value !== 'number')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// Escape string to add in URL later
|
||||||
|
if (typeof value === 'string') {
|
||||||
|
value = encodeURIComponent(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.dataToSend[index] = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -245,8 +311,6 @@ var Mibew = {};
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function which create new widget object, store it into
|
* Helper function which create new widget object, store it into
|
||||||
* Mibew.Objects.widget and run automatic requests.
|
* Mibew.Objects.widget and run automatic requests.
|
||||||
|
Loading…
Reference in New Issue
Block a user