mirror of
				https://github.com/Mibew/i18n.git
				synced 2025-11-01 01:36:57 +03:00 
			
		
		
		
	Refactor JavaScript invitation and tracking code
This commit is contained in:
		
							parent
							
								
									52f00cbe59
								
							
						
					
					
						commit
						c64c1a441d
					
				| @ -1,10 +0,0 @@ | ||||
| /* | ||||
|  Copyright 2005-2013 the original author or authors. | ||||
| 
 | ||||
|  Licensed under the Apache License, Version 2.0 (the "License"). | ||||
|  You may obtain a copy of the License at | ||||
|      http://www.apache.org/licenses/LICENSE-2.0
 | ||||
| */ | ||||
| var style=document.createElement("style");document.getElementsByTagName("head")[0].appendChild(style);window.createPopup||(style.appendChild(document.createTextNode("")),style.setAttribute("type","text/css"));var sheet=document.styleSheets[document.styleSheets.length-1];if(window.createPopup)sheet.cssText=mibewInviteStyle;else{var node=document.createTextNode(mibewInviteStyle);style.appendChild(node)} | ||||
| function mibewInviteOnResponse(a){var c=a.invitation.message,b=a.invitation.operator,d=a.invitation.avatar;a='<div id="mibewinvitationpopup"><div id="mibewinvitationclose"><a href="javascript:void(0);" onclick="mibewHideInvitation();">×</a></div>';b&&(a+='<h1 onclick="mibewOpenAgent();">'+b+"</h1>");d&&(a+='<img id="mibewinvitationavatar" src="'+d+'" title="'+b+'" alt="'+b+'" onclick="mibewOpenAgent();" />');a=a+('<p onclick="mibewOpenAgent();">'+c+"</p>")+'<div style="clear: both;"></div></div>'; | ||||
| if(c=document.getElementById("mibewinvitation"))c.innerHTML=a}function mibewHideInvitation(){document.getElementById("mibewinvitationpopup")&&(document.getElementById("mibewinvitationpopup").style.display="none")}function mibewOpenAgent(){document.getElementById("mibewAgentButton")&&(document.getElementById("mibewAgentButton").onclick(),mibewHideInvitation())}; | ||||
| @ -1,13 +0,0 @@ | ||||
| /* | ||||
|  Copyright 2005-2013 the original author or authors. | ||||
| 
 | ||||
|  Licensed under the Apache License, Version 2.0 (the "License"). | ||||
|  You may obtain a copy of the License at | ||||
|      http://www.apache.org/licenses/LICENSE-2.0
 | ||||
| */ | ||||
| var mibewRequestedScripts=[],mibewHandlers=[],mibewHandlersDependences=[];function mibewMakeRequest(){var a=mibewReadCookie(mibewVisitorCookieName);mibewDoLoadScript(mibewRequestUrl+"&rnd="+Math.random()+(!1!==a?"&user_id="+a:""),"responseScript")} | ||||
| function mibewOnResponse(a){var b=a.load,c=a.handlers,d=a.data;a=a.dependences;for(id in b)b[id]in mibewRequestedScripts||(mibewRequestedScripts[id]=[],mibewRequestedScripts[id].url=b[id],mibewRequestedScripts[id].status="loading",mibewLoadScript(id));for(handler in a)handler in mibewHandlersDependences||(mibewHandlersDependences[handler]=a[handler]);for(b=0;b<c.length;b++){var e=c[b];if(mibewCanRunHandler(c[b]))window[e](d);else c[b]in mibewHandlers||(mibewHandlers[e]=function(){window[e](d)})}mibewCleanUpAfterRequest(); | ||||
| window.setTimeout(mibewMakeRequest,mibewRequestTimeout)}function mibewCleanUpAfterRequest(){document.getElementsByTagName("head")[0].removeChild(document.getElementById("responseScript"))}function mibewDoLoadScript(a,b){var c=document.createElement("script");c.setAttribute("type","text/javascript");c.setAttribute("src",a);c.setAttribute("id",b);document.getElementsByTagName("head")[0].appendChild(c);return c} | ||||
| function mibewLoadScript(a){var b=mibewDoLoadScript(mibewRequestedScripts[a].url,a);b.onload=function(){mibewScriptReady(a)};b.onreadystatechange=function(){("complete"==this.readyState||"loaded"==this.readyState)&&mibewScriptReady(a)}}function mibewScriptReady(a){mibewRequestedScripts[a].status="ready";for(handlerName in mibewHandlers)mibewCanRunHandler(handlerName)&&(mibewHandlers[handlerName](),delete mibewHandlers[handlerName])} | ||||
| function mibewCanRunHandler(a){a=mibewHandlersDependences[a];for(var b=0;b<a.length;b++)if("ready"!=mibewRequestedScripts[a[b]].status)return!1;return!0}function mibewCreateCookie(a,b){var c=/([^\.]+\.[^\.]+)$/.exec(document.location.hostname)[1];document.cookie=""+a+"="+b+"; path=/; "+(c?"domain="+c+";":"")}function mibewReadCookie(a){var b=document.cookie.split("; ");a+="=";for(var c=!1,d=0;d<b.length;d++)if(-1!=b[d].indexOf(a)){c=b[d].substr(a.length);break}return c} | ||||
| function mibewUpdateUserId(a){mibewCreateCookie(mibewVisitorCookieName,a.user.id)}; | ||||
							
								
								
									
										16
									
								
								src/messenger/webim/js/compiled/widget.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								src/messenger/webim/js/compiled/widget.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,16 @@ | ||||
| /* | ||||
|  Copyright 2005-2013 the original author or authors. | ||||
| 
 | ||||
|  Licensed under the Apache License, Version 2.0 (the "License"). | ||||
|  You may obtain a copy of the License at | ||||
|      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.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= | ||||
| 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= | ||||
| "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]; | ||||
| 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>'; | ||||
| 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.getElementById("mibewAgentButton").onclick(),b.Invitation.hide())}})(Mibew); | ||||
| @ -1,58 +0,0 @@ | ||||
| /** | ||||
|  * @preserve Copyright 2005-2013 the original author or authors. | ||||
|  * | ||||
|  * Licensed under the Apache License, Version 2.0 (the "License"). | ||||
|  * You may obtain a copy of the License at | ||||
|  *     http://www.apache.org/licenses/LICENSE-2.0
 | ||||
|  */ | ||||
| 
 | ||||
| var style = document.createElement('style'); | ||||
| document.getElementsByTagName('head')[0].appendChild(style); | ||||
|   | ||||
| if (!window.createPopup) { | ||||
|     style.appendChild(document.createTextNode('')); | ||||
|     style.setAttribute("type", "text/css"); | ||||
| } | ||||
|   | ||||
| var sheet = document.styleSheets[document.styleSheets.length - 1]; | ||||
| if (!window.createPopup) { | ||||
|     var node = document.createTextNode(mibewInviteStyle); | ||||
|     style.appendChild(node); | ||||
| } else { | ||||
|     sheet.cssText = mibewInviteStyle; | ||||
| } | ||||
| 
 | ||||
| function mibewInviteOnResponse(response) | ||||
| { | ||||
| 	var message = response.invitation.message; | ||||
| 	var operator = response.invitation.operator; | ||||
| 	var avatar = response.invitation.avatar; | ||||
| 
 | ||||
| 	var popuptext = '<div id="mibewinvitationpopup">'; | ||||
| 	popuptext += '<div id="mibewinvitationclose"><a href="javascript:void(0);" onclick="mibewHideInvitation();">×</a></div>'; | ||||
| 	if (operator) { | ||||
| 		popuptext += '<h1 onclick="mibewOpenAgent();">' + operator + '</h1>'; | ||||
| 	} | ||||
| 	if (avatar) { | ||||
| 		popuptext += '<img id="mibewinvitationavatar" src="' + avatar + '" title="' + operator + '" alt="' + operator + '" onclick="mibewOpenAgent();" />'; | ||||
| 	} | ||||
| 	popuptext += '<p onclick="mibewOpenAgent();">' + message + '</p>'; | ||||
| 	popuptext += '<div style="clear: both;"></div></div>'; | ||||
| 	var invitationdiv = document.getElementById("mibewinvitation"); | ||||
| 	if (invitationdiv) { | ||||
| 		invitationdiv.innerHTML = popuptext; | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| function mibewHideInvitation() { | ||||
|     if (document.getElementById('mibewinvitationpopup')) { | ||||
| 	document.getElementById('mibewinvitationpopup').style.display='none'; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| function mibewOpenAgent() { | ||||
|     if (document.getElementById('mibewAgentButton')) { | ||||
| 	document.getElementById('mibewAgentButton').onclick(); | ||||
| 	mibewHideInvitation(); | ||||
|     } | ||||
| } | ||||
| @ -1,155 +0,0 @@ | ||||
| /** | ||||
|  * @preserve Copyright 2005-2013 the original author or authors. | ||||
|  * | ||||
|  * Licensed under the Apache License, Version 2.0 (the "License"). | ||||
|  * You may obtain a copy of the License at | ||||
|  *     http://www.apache.org/licenses/LICENSE-2.0
 | ||||
|  */ | ||||
| 
 | ||||
| var mibewRequestedScripts = new Array(); | ||||
| var mibewHandlers = new Array(); | ||||
| var mibewHandlersDependences = new Array(); | ||||
| 
 | ||||
| function mibewMakeRequest() | ||||
| { | ||||
| 	// Try to get user id from local cookie
 | ||||
| 	var userId = mibewReadCookie(mibewVisitorCookieName); | ||||
| 
 | ||||
| 	mibewDoLoadScript( | ||||
| 		mibewRequestUrl + '&rnd=' + Math.random() | ||||
| 			+ ((userId !== false) ? '&user_id=' + userId : ''), | ||||
| 		'responseScript' | ||||
| 	); | ||||
| } | ||||
| 
 | ||||
| function mibewOnResponse(response) | ||||
| { | ||||
| 	var load = response.load; | ||||
| 	var handlers = response.handlers; | ||||
| 	var data = response.data; | ||||
| 	var dependences = response.dependences; | ||||
| 
 | ||||
| 	for(id in load){ | ||||
| 		if(! (load[id] in mibewRequestedScripts)){ | ||||
| 			mibewRequestedScripts[id] = new Array(); | ||||
| 			mibewRequestedScripts[id]['url'] = load[id]; | ||||
| 			mibewRequestedScripts[id]['status'] = 'loading'; | ||||
| 			mibewLoadScript(id); | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	for(handler in dependences){ | ||||
| 		if(! (handler in mibewHandlersDependences)){ | ||||
| 			mibewHandlersDependences[handler] = dependences[handler]; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	for(var i = 0; i < handlers.length; i++){ | ||||
| 		var handlerName = handlers[i]; | ||||
| 		if(mibewCanRunHandler(handlers[i])){ | ||||
| 			window[handlerName](data); | ||||
| 		}else{ | ||||
| 			if(! (handlers[i] in mibewHandlers)){ | ||||
| 				mibewHandlers[handlerName] = function(){ | ||||
| 					window[handlerName](data); | ||||
| 				}; | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	mibewCleanUpAfterRequest(); | ||||
| 
 | ||||
| 	window.setTimeout(mibewMakeRequest,mibewRequestTimeout); | ||||
| } | ||||
| 
 | ||||
| function mibewCleanUpAfterRequest() | ||||
| { | ||||
| 	document.getElementsByTagName('head')[0].removeChild(document.getElementById('responseScript')); | ||||
| } | ||||
| 
 | ||||
| function mibewDoLoadScript(url, id) | ||||
| { | ||||
| 	var script = document.createElement('script'); | ||||
| 	script.setAttribute('type', 'text/javascript'); | ||||
| 	script.setAttribute('src', url); | ||||
| 	script.setAttribute('id', id); | ||||
| 	document.getElementsByTagName('head')[0].appendChild(script); | ||||
| 	return script; | ||||
| } | ||||
| 
 | ||||
| function mibewLoadScript(id) | ||||
| { | ||||
| 	var script = mibewDoLoadScript(mibewRequestedScripts[id]['url'], id); | ||||
| 	script.onload = function(){ | ||||
| 		mibewScriptReady(id); | ||||
| 	} | ||||
| 	script.onreadystatechange = function(){ | ||||
| 		if (this.readyState == 'complete' || this.readyState == 'loaded') { | ||||
| 			mibewScriptReady(id); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| function mibewScriptReady(id) | ||||
| { | ||||
| 	mibewRequestedScripts[id]['status'] = 'ready'; | ||||
| 	for(handlerName in mibewHandlers){ | ||||
| 		if(mibewCanRunHandler(handlerName)){ | ||||
| 			mibewHandlers[handlerName](); | ||||
| 			delete mibewHandlers[handlerName]; | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| function mibewCanRunHandler(handlerName) | ||||
| { | ||||
| 	var dependences = mibewHandlersDependences[handlerName]; | ||||
| 	for(var i = 0; i < dependences.length; i++){ | ||||
| 		if(mibewRequestedScripts[dependences[i]]['status'] != 'ready'){ | ||||
| 			return false; | ||||
| 		} | ||||
| 	} | ||||
| 	return true; | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * Create session cookie for top level domain with path equals to '/'. | ||||
|  * | ||||
|  * @param {String} name Cookie name | ||||
|  * @param {String} value Cookie value | ||||
|  */ | ||||
| function mibewCreateCookie(name, value) { | ||||
|     var domainParts = /([^\.]+\.[^\.]+)$/.exec(document.location.hostname); | ||||
|     var domain = domainParts[1]; | ||||
|     document.cookie = "" + name + "=" + value + "; " | ||||
|         + "path=/; " | ||||
|         + (domain ? ("domain=" + domain + ";") : ''); | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * Try to read cookie. | ||||
|  * | ||||
|  * @param {String} name Cookie name | ||||
|  * @returns {String|Boolean} Cookie value or boolean false if cookie with | ||||
|  * specified name does not exist | ||||
|  */ | ||||
| function mibewReadCookie(name) { | ||||
|     var cookies = document.cookie.split('; '); | ||||
|     var nameForSearch = name + '='; | ||||
|     var value = false; | ||||
|     for (var i = 0; i < cookies.length; i++) { | ||||
|         if (cookies[i].indexOf(nameForSearch) != -1) { | ||||
|             value = cookies[i].substr(nameForSearch.length); | ||||
|             break; | ||||
|         } | ||||
|     } | ||||
|     return value; | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * Update user id. API function | ||||
|  * @param {Object} response Data object from server | ||||
|  */ | ||||
| function mibewUpdateUserId(response) { | ||||
|     mibewCreateCookie(mibewVisitorCookieName, response.user.id); | ||||
| } | ||||
							
								
								
									
										374
									
								
								src/messenger/webim/js/source/widget.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										374
									
								
								src/messenger/webim/js/source/widget.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,374 @@ | ||||
| /** | ||||
|  * @preserve Copyright 2005-2013 the original author or authors. | ||||
|  * | ||||
|  * Licensed under the Apache License, Version 2.0 (the "License"). | ||||
|  * You may obtain a copy of the License at | ||||
|  *     http://www.apache.org/licenses/LICENSE-2.0
 | ||||
|  */ | ||||
| 
 | ||||
| /** | ||||
|  * @namespace Holds all Mibew functionality | ||||
|  */ | ||||
| var Mibew = {}; | ||||
| 
 | ||||
| (function(Mibew){ | ||||
| 
 | ||||
|     /** | ||||
|      * @namespace Holds objects instances | ||||
|      */ | ||||
|     Mibew.Objects = {}; | ||||
| 
 | ||||
|     /** | ||||
|      * Create new widget | ||||
|      * @constructor | ||||
|      * @todo Add options validation | ||||
|      */ | ||||
|     Mibew.Widget = function(options) { | ||||
|         /** | ||||
|          * Holds all scripts that must be loaded | ||||
|          * @type Object | ||||
|          * @private | ||||
|          */ | ||||
|         this.requestedScripts = {}; | ||||
| 
 | ||||
|         /** | ||||
|          * List of handlers that must be called | ||||
|          * @type Array | ||||
|          * @private | ||||
|          */ | ||||
|         this.handlers = []; | ||||
| 
 | ||||
|         /** | ||||
|          * List of dependences between handlers and scripts | ||||
|          * @type Object | ||||
|          * @private | ||||
|          */ | ||||
|         this.handlersDependences = {}; | ||||
| 
 | ||||
|         /** | ||||
|          * URL for requests | ||||
|          * @type String | ||||
|          */ | ||||
|         this.requestURL = options.requestURL; | ||||
| 
 | ||||
|         /** | ||||
|          * Timeout between requests to server | ||||
|          * @type Number | ||||
|          */ | ||||
|         this.requestTimeout = options.requestTimeout; | ||||
| 
 | ||||
|         /** | ||||
|          * Name of tracking cookie | ||||
|          * @type String | ||||
|          */ | ||||
|         this.visitorCookieName = options.visitorCookieName; | ||||
| 
 | ||||
|         /** | ||||
|          * URL of file with additional CSS rules for invitation | ||||
|          * @type String | ||||
|          */ | ||||
|         this.inviteStyle = options.inviteStyle; | ||||
| 
 | ||||
|         /** | ||||
|          * Locale of the Widget | ||||
|          * @type String | ||||
|          */ | ||||
|         this.locale = options.locale; | ||||
| 
 | ||||
|         // Load additional styles
 | ||||
|         var styleSheet = document.createElement('link'); | ||||
|         styleSheet.setAttribute('rel', 'stylesheet'); | ||||
|         styleSheet.setAttribute('type', 'text/css'); | ||||
|         styleSheet.setAttribute('href', options.inviteStyle); | ||||
|         document.getElementsByTagName('head')[0].appendChild(styleSheet); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Make request to the server. | ||||
|      * @private | ||||
|      */ | ||||
|     Mibew.Widget.prototype.makeRequest = function() { | ||||
|         // Try to get user id from local cookie
 | ||||
|         var userId = Mibew.Utils.readCookie(this.visitorCookieName); | ||||
| 
 | ||||
|         this.doLoadScript( | ||||
|             this.requestURL | ||||
|                 + '?entry=' + escape(document.referrer) | ||||
|                 + '&locale=' + this.locale | ||||
|                 + '&rnd=' + Math.random() | ||||
|                 + ((userId !== false) ? '&user_id=' + userId : ''), | ||||
|             'responseScript' | ||||
|         ); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Parse server response. Called as JSONP callback function | ||||
|      * @param {Object} response Data got from server | ||||
|      */ | ||||
|     Mibew.Widget.prototype.onResponse = function(response) { | ||||
|         // Create some shortcuts
 | ||||
|         var load = response.load; | ||||
|         var handlers = response.handlers; | ||||
|         var data = response.data; | ||||
|         var dependences = response.dependences; | ||||
|         var context = this; | ||||
| 
 | ||||
|         // Update list of scripts that must be loaded
 | ||||
|         for(var id in load){ | ||||
|             if (! load.hasOwnProperty(id)) { | ||||
|                 continue; | ||||
|             } | ||||
|             // Check if script already loaded
 | ||||
|             if (! (load[id] in this.requestedScripts)) { | ||||
|                 this.requestedScripts[id] = {}; | ||||
|                 this.requestedScripts[id].url = load[id]; | ||||
|                 this.requestedScripts[id].status = 'loading'; | ||||
|                 this.loadScript(id); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         // Update list of dependences
 | ||||
|         for(var handler in dependences){ | ||||
|             if (! dependences.hasOwnProperty(handler)) { | ||||
|                 continue; | ||||
|             } | ||||
|             // Check if dependences for this handler already stored
 | ||||
|             if (! (handler in this.handlersDependences)) { | ||||
|                 this.handlersDependences[handler] = dependences[handler]; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         // Process all recieved handlers. Run handler if all dependences loaded
 | ||||
|         // and add it to handlers list otherwise.
 | ||||
|         for (var i = 0; i < handlers.length; i++) { | ||||
|             // Create shortcuts
 | ||||
|             var handlerName = handlers[i]; | ||||
| 
 | ||||
|             // TODO: Allow to run objects methods
 | ||||
|             if (this.canRunHandler(handlerName)) { | ||||
|                 Mibew.APIFunctions[handlerName](data); | ||||
|             } else { | ||||
|                 if (! (handlerName in this.handlers)) { | ||||
|                     this.handlers[handlerName] = function(){ | ||||
|                         Mibew.APIFunctions[handlerName](data); | ||||
|                     }; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         this.cleanUpAfterRequest(); | ||||
| 
 | ||||
|         // Make new request after timeout
 | ||||
|         window.setTimeout( | ||||
|             function() { | ||||
|                 context.makeRequest(); | ||||
|             }, | ||||
|             this.requestTimeout); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Remove dynamically loaded request script from DOM | ||||
|      * @private | ||||
|      */ | ||||
|     Mibew.Widget.prototype.cleanUpAfterRequest = function() { | ||||
|         document.getElementsByTagName('head')[0] | ||||
|             .removeChild(document.getElementById('responseScript')); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Load script and add handler for script onLoad event | ||||
|      * @param {String} id Identifier of the script to load | ||||
|      * @private | ||||
|      */ | ||||
|     Mibew.Widget.prototype.loadScript = function(id) { | ||||
|         // Store context
 | ||||
|         var context = this; | ||||
|         // Load script by adding script tag to DOM
 | ||||
|         var script = this.doLoadScript(this.requestedScripts[id].url, id); | ||||
| 
 | ||||
|         // Check if script loaded
 | ||||
|         script.onload = function(){ | ||||
|             context.scriptReady(id); | ||||
|         } | ||||
|         // Do it in crossbrowser way
 | ||||
|         script.onreadystatechange = function(){ | ||||
|             if (this.readyState == 'complete' || this.readyState == 'loaded') { | ||||
|                 context.scriptReady(id); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Dynamically add script tag to DOM | ||||
|      * @param {String} url URL of the script to load | ||||
|      * @param {String} id Identifier of the script to load | ||||
|      * @private | ||||
|      */ | ||||
|     Mibew.Widget.prototype.doLoadScript = function(url, id) { | ||||
|         var script = document.createElement('script'); | ||||
|         script.setAttribute('type', 'text/javascript'); | ||||
|         script.setAttribute('src', url); | ||||
|         script.setAttribute('id', id); | ||||
|         document.getElementsByTagName('head')[0].appendChild(script); | ||||
|         return script; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Event listener for script onLoad event. Run handlers which have no | ||||
|      * unload dependences. | ||||
|      * @param {String} id Identifier of the loaded script | ||||
|      */ | ||||
|     Mibew.Widget.prototype.scriptReady = function(id) { | ||||
|         this.requestedScripts[id].status = 'ready'; | ||||
|         for (var handlerName in this.handlers) { | ||||
|             if (! this.handlers.hasOwnProperty(handlerName)) { | ||||
|                 continue; | ||||
|             } | ||||
|             if(this.canRunHandler(handlerName)){ | ||||
|                 this.handlers[handlerName](); | ||||
|                 delete this.handlers[handlerName]; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Check if handler can be run | ||||
|      */ | ||||
|     Mibew.Widget.prototype.canRunHandler = function(handlerName) { | ||||
|         var dependences = this.handlersDependences[handlerName]; | ||||
|         // Check for dependencess
 | ||||
|         for(var i = 0; i < dependences.length; i++){ | ||||
|             if(this.requestedScripts[dependences[i]].status != 'ready'){ | ||||
|                 return false; | ||||
|             } | ||||
|         } | ||||
|         return true; | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|     /** | ||||
|      * Helper function which create new widget object, store it into | ||||
|      * Mibew.Objects.widget and run automatic requests. | ||||
|      */ | ||||
|     Mibew.Widget.init = function(options) { | ||||
|         Mibew.Objects.widget = new Mibew.Widget(options); | ||||
|         Mibew.Objects.widget.makeRequest(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @namespace Holds utility functions | ||||
|      */ | ||||
|     Mibew.Utils = {}; | ||||
| 
 | ||||
|     /** | ||||
|      * Create session cookie for top level domain with path equals to '/'. | ||||
|      * | ||||
|      * @param {String} name Cookie name | ||||
|      * @param {String} value Cookie value | ||||
|      */ | ||||
|     Mibew.Utils.createCookie = function(name, value) { | ||||
|         var domainParts = /([^\.]+\.[^\.]+)$/.exec(document.location.hostname); | ||||
|         var domain = domainParts[1]; | ||||
|         document.cookie = "" + name + "=" + value + "; " | ||||
|             + "path=/; " | ||||
|             + (domain ? ("domain=" + domain + ";") : ''); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Try to read cookie. | ||||
|      * | ||||
|      * @param {String} name Cookie name | ||||
|      * @returns {String|Boolean} Cookie value or boolean false if cookie with | ||||
|      * specified name does not exist | ||||
|      */ | ||||
|     Mibew.Utils.readCookie = function(name) { | ||||
|         var cookies = document.cookie.split('; '); | ||||
|         var nameForSearch = name + '='; | ||||
|         var value = false; | ||||
|         for (var i = 0; i < cookies.length; i++) { | ||||
|             if (cookies[i].indexOf(nameForSearch) != -1) { | ||||
|                 value = cookies[i].substr(nameForSearch.length); | ||||
|                 break; | ||||
|             } | ||||
|         } | ||||
|         return value; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @namespace Holds functions that can be called by the Core | ||||
|      */ | ||||
|     Mibew.APIFunctions = {}; | ||||
| 
 | ||||
|     /** | ||||
|      * Update user id. API function | ||||
|      * @param {Object} response Data object from server | ||||
|      */ | ||||
|     Mibew.APIFunctions.updateUserId = function(response) { | ||||
|         Mibew.Utils.createCookie( | ||||
|             Mibew.Objects.widget.visitorCookieName, | ||||
|             response.user.id | ||||
|         ); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Show invitation popup | ||||
|      * @param {Object} response Data passed from server | ||||
|      */ | ||||
|     Mibew.APIFunctions.inviteOnResponse = function(response) { | ||||
|         var message = response.invitation.message; | ||||
|         var operator = response.invitation.operator; | ||||
|         var avatar = response.invitation.avatar; | ||||
| 
 | ||||
|         var popuptext = '<div id="mibewinvitationpopup">'; | ||||
|         popuptext += '<div id="mibewinvitationclose">' | ||||
|             + '<a href="javascript:void(0);" onclick="Mibew.Invitation.hide();">' | ||||
|             + '×</a></div>'; | ||||
|         if (operator) { | ||||
|             popuptext += '<h1 onclick="Mibew.Invitation.accept();">' | ||||
|                 + operator | ||||
|                 + '</h1>'; | ||||
|         } | ||||
|         if (avatar) { | ||||
|             popuptext += '<img id="mibewinvitationavatar" src="' + avatar | ||||
|                 + '" title="' + operator | ||||
|                 + '" alt="' + operator | ||||
|                 + '" onclick="Mibew.Invitation.accept();" />'; | ||||
|         } | ||||
|         popuptext += '<p onclick="Mibew.Invitation.accept();">' | ||||
|             + message | ||||
|             + '</p>'; | ||||
|         popuptext += '<div style="clear: both;"></div></div>'; | ||||
| 
 | ||||
|         var invitationdiv = document.getElementById("mibewinvitation"); | ||||
|         if (invitationdiv) { | ||||
|                 invitationdiv.innerHTML = popuptext; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @namespace Holds invitation stuff | ||||
|      */ | ||||
|     Mibew.Invitation = {}; | ||||
| 
 | ||||
|     /** | ||||
|      * Hide invitation popup | ||||
|      */ | ||||
|     Mibew.Invitation.hide = function() { | ||||
|         var invitationPopup = document.getElementById('mibewinvitationpopup'); | ||||
|         if (invitationPopup) { | ||||
|             invitationPopup.style.display = 'none'; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Accept invitation and open chat window | ||||
|      */ | ||||
|     Mibew.Invitation.accept = function() { | ||||
|         if (document.getElementById('mibewAgentButton')) { | ||||
|             document.getElementById('mibewAgentButton').onclick(); | ||||
|             Mibew.Invitation.hide(); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| })(Mibew); | ||||
| @ -262,7 +262,7 @@ function build_widget_response($response) { | ||||
| 		'dependences' => array(), | ||||
| 		'data' => array() | ||||
| 	); | ||||
| 	return "mibewOnResponse(" . json_encode($result) . ");"; | ||||
| 	return "Mibew.Objects.widget.onResponse(" . json_encode($result) . ");"; | ||||
| } | ||||
| 
 | ||||
| ?>
 | ||||
| @ -18,7 +18,8 @@ | ||||
| function generate_button($title, $locale, $style, $invitationstyle, $group, $inner, $showhost, $forcesecure, $modsecurity) | ||||
| { | ||||
| 	global $visitorcookie; | ||||
| 	$link = get_app_location($showhost, $forcesecure) . "/client.php"; | ||||
| 	$app_location = get_app_location($showhost, $forcesecure); | ||||
| 	$link = $app_location . "/client.php"; | ||||
| 	if ($locale) | ||||
| 		$link = append_query($link, "locale=$locale"); | ||||
| 	if ($style) | ||||
| @ -31,20 +32,37 @@ function generate_button($title, $locale, $style, $invitationstyle, $group, $inn | ||||
| 	$temp = get_popup($link, "$jslink", | ||||
| 					  $inner, $title, "webim", "toolbar=0,scrollbars=0,location=0,status=1,menubar=0,width=640,height=480,resizable=1"); | ||||
| 	if (Settings::get('enabletracking')) { | ||||
| 	    $temp = preg_replace('/^(<a )/', '\1id="mibewAgentButton" ', $temp); | ||||
| 	    $temp .= '<div id="mibewinvitation"></div><script type="text/javascript">var mibewInviteStyle = \'@import url('; | ||||
| 	    $temp .= get_app_location($showhost, $forcesecure); | ||||
| 	    $temp .= '/styles/invitations/'; | ||||
| 	    $temp .= ($invitationstyle?$invitationstyle:(Settings::get('invitationstyle'))); | ||||
| 	    $temp .= '/invite.css);\'; var mibewRequestTimeout = '; | ||||
| 	    $temp .= Settings::get('updatefrequency_tracking'); | ||||
| 	    $temp .= '*1000; var mibewRequestUrl = \''; | ||||
| 	    $temp .= get_app_location($showhost, $forcesecure); | ||||
| 	    $temp .= '/request.php?entry=\' + escape(document.referrer) + \'&lang=ru\'; '; | ||||
| 	    $temp .= ' var mibewVisitorCookieName = \''.$visitorcookie.'\''; | ||||
| 	    $temp .= '</script><script type="text/javascript" src="'; | ||||
| 	    $temp .= get_app_location($showhost, $forcesecure); | ||||
| 	    $temp .= '/js/compiled/request.js"></script><script type="text/javascript">mibewMakeRequest();</script>'; | ||||
| 		$widget_data = array(); | ||||
| 
 | ||||
| 		// URL of file with additional CSS rules for invitation popup
 | ||||
| 		$widget_data['inviteStyle'] = $app_location . '/styles/invitations/' . | ||||
| 			($invitationstyle | ||||
| 				? $invitationstyle | ||||
| 				: (Settings::get('invitationstyle')) | ||||
| 			) .	'/invite.css'; | ||||
| 
 | ||||
| 		// Time between requests to the server in milliseconds
 | ||||
| 		$widget_data['requestTimeout'] = Settings::get('updatefrequency_tracking') | ||||
| 			* 1000; | ||||
| 
 | ||||
| 		// URL for requests
 | ||||
| 		$widget_data['requestURL'] = $app_location . '/widget.php'; | ||||
| 
 | ||||
| 		// Locale for invitation
 | ||||
| 		$widget_data['locale'] = $locale; | ||||
| 
 | ||||
| 		// Name of the cookie to track user. Use if third-party cookie blocked
 | ||||
| 		$widget_data['visitorCookieName'] = $visitorcookie; | ||||
| 
 | ||||
| 		// Build additional button code
 | ||||
| 	    $temp = preg_replace('/^(<a )/', '\1id="mibewAgentButton" ', $temp) . | ||||
| 			'<div id="mibewinvitation"></div>' . | ||||
| 			'<script type="text/javascript" src="' . | ||||
| 				$app_location .	'/js/compiled/widget.js' . | ||||
| 			'"></script>' . | ||||
| 			'<script type="text/javascript">' . | ||||
| 				'Mibew.Widget.init('.json_encode($widget_data).')' . | ||||
| 			'</script>'; | ||||
| 	} | ||||
| 	return "<!-- mibew button -->" . $temp . "<!-- / mibew button -->"; | ||||
| } | ||||
|  | ||||
| @ -61,18 +61,17 @@ if (Settings::get('enabletracking') == '1') { | ||||
| 
 | ||||
| 	if ($user_id !== false) { | ||||
| 		// Update local cookie value at target site
 | ||||
| 		$response['handlers'][] = 'mibewUpdateUserId'; | ||||
| 		$response['dependences']['mibewUpdateUserId'] = array(); | ||||
| 		$response['handlers'][] = 'updateUserId'; | ||||
| 		$response['dependences']['updateUserId'] = array(); | ||||
| 		$response['data']['user']['id'] = $user_id; | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| if ($invited !== FALSE) { | ||||
|     $response['load']['mibewInvitationScript'] = get_app_location(true, is_secure_request()) . '/js/compiled/invite.js'; | ||||
|     $response['handlers'][] = 'mibewInviteOnResponse'; | ||||
|     $response['dependences']['mibewInviteOnResponse'] = array('mibewInvitationScript'); | ||||
|     $locale = isset($_GET['lang']) ? $_GET['lang'] : ''; | ||||
|     $response['handlers'][] = 'inviteOnResponse'; | ||||
|     $response['dependences']['inviteOnResponse'] = array(); | ||||
|     $locale = isset($_GET['locale']) ? $_GET['locale'] : ''; | ||||
|     $operatorName = ($locale == $home_locale) ? $operator['vclocalename'] : $operator['vccommonname']; | ||||
|     $response['data']['invitation']['operator'] = htmlspecialchars($operatorName); | ||||
|     $response['data']['invitation']['message'] = getlocal("invitation.message"); | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user