From 29baaa700c1bbba95e7d3338f7a351ce60b8553c Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Mon, 20 May 2013 09:29:14 +0000 Subject: [PATCH] Upadate widget. It can pass some data to server now --- src/messenger/webim/js/compiled/widget.js | 17 ++--- src/messenger/webim/js/source/widget.js | 76 +++++++++++++++++++++-- 2 files changed, 79 insertions(+), 14 deletions(-) diff --git a/src/messenger/webim/js/compiled/widget.js b/src/messenger/webim/js/compiled/widget.js index 39f2e306..cc80df2e 100644 --- a/src/messenger/webim/js/compiled/widget.js +++ b/src/messenger/webim/js/compiled/widget.js @@ -6,11 +6,12 @@ http://www.apache.org/licenses/LICENSE-2.0 */ 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= -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)&& -(h in this.handlersDependences||(this.handlersDependences[h]=a[h]));for(c=0;c'; -d&&(a+='

'+d+"

");e&&(a+=''+d+'');a=a+('

'+b+"

")+'
';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.getElementById("mibewAgentButton").onclick(),b.Invitation.hide())}})(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(){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", +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&&(b+='

'+d+"

");e&&(b+=''+d+'');b=b+('

'+a+"

")+'
';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); diff --git a/src/messenger/webim/js/source/widget.js b/src/messenger/webim/js/source/widget.js index fec1e212..f8cdd4bd 100644 --- a/src/messenger/webim/js/source/widget.js +++ b/src/messenger/webim/js/source/widget.js @@ -75,6 +75,13 @@ var Mibew = {}; */ this.locale = options.locale; + /** + * Data that must be sent to the server with next request + * @type Object + * @private + */ + this.dataToSend = {}; + // Load additional styles var styleSheet = document.createElement('link'); styleSheet.setAttribute('rel', 'stylesheet'); @@ -91,14 +98,73 @@ var Mibew = {}; // Try to get user id from local cookie 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.requestURL - + '?entry=' + escape(document.referrer) - + '&locale=' + this.locale - + '&rnd=' + Math.random() - + ((userId !== false) ? '&user_id=' + userId : ''), + + '?' + this.getQuery(), '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; } - - /** * Helper function which create new widget object, store it into * Mibew.Objects.widget and run automatic requests.