Clean up JavaScript code of the Widget

This commit is contained in:
Dmitriy Simushev 2013-05-24 10:40:40 +00:00
parent 1480519bb2
commit 545ff7db87
3 changed files with 116 additions and 96 deletions

View File

@ -6,12 +6,13 @@
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
*/ */
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(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;this.dataToSend={};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 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 a=b.Utils.readCookie(this.visitorCookieName);this.dataToSend.entry=escape(document.referrer);this.dataToSend.locale=this.locale;this.dataToSend.rnd=Math.random();!1!==a?this.dataToSend.user_id=a:this.dataToSend.user_id&&delete this.dataToSend.user_id;this.doLoadScript(this.requestURL+"?"+this.getQuery(),"responseScript");this.dataToSend={}};b.Widget.prototype.getQuery=function(){var a=[],c;for(c in this.dataToSend)this.dataToSend.hasOwnProperty(c)&&a.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)&&!(f in this.requestedScripts)&&(this.requestedScripts[f]={},this.requestedScripts[f].url=c[f],this.requestedScripts[f].status="loading", return a.join("&")};b.Widget.prototype.sendToServer=function(a){for(var c in a)if(a.hasOwnProperty(c)){var b=a[c];"string"!==typeof b&&"number"!==typeof b||("string"===typeof b&&(b=encodeURIComponent(b)),this.dataToSend[c]=b)}};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)&&!(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 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"))};
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= 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,b){var d=document.createElement("script");d.setAttribute("type","text/javascript");d.setAttribute("src",a);d.setAttribute("id",b);document.getElementsByTagName("head")[0].appendChild(d);return d};b.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(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=""+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.operator,d=b.invitation.avatar,e=b.invitation.url;b='<div id="mibewinvitationpopup" style="display: none;"><div id="mibewinvitationclose"><a href="javascript:void(0);" onclick="Mibew.Invitation.reject();">&times;</a></div>'; 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.Invitation={};b.Invitation.create=function(a){var b=a.operatorName,d=a.avatarUrl,e=a.threadUrl;a='<div id="mibewinvitationpopup" style="display: none;"><div id="mibewinvitationclose"><a href="javascript:void(0);" onclick="Mibew.Invitation.reject();">&times;</a></div>';
a&&(b+='<h1 onclick="Mibew.Invitation.accept();">'+a+"</h1>");d&&(b+='<img id="mibewinvitationavatar" src="'+d+'" title="'+a+'" alt="'+a+'" onclick="Mibew.Invitation.accept();" />');e&&(b+='<iframe id="mibewinvitationframe" src="'+e+'" onload="Mibew.Invitation.show();"></iframe>');b+='<div style="clear: both;"></div></div>';if(a=document.getElementById("mibewinvitation"))a.innerHTML=b};a.APIFunctions.inviteOnClose=function(){a.Invitation.hide()};a.Invitation={};a.Invitation.hide=function(){var a= b&&(a+='<h1 onclick="Mibew.Invitation.accept();">'+b+"</h1>");d&&(a+='<img id="mibewinvitationavatar" src="'+d+'" title="'+b+'" alt="'+b+'" onclick="Mibew.Invitation.accept();" />');e&&(a+='<iframe id="mibewinvitationframe" src="'+e+'" onload="Mibew.Invitation.show();"></iframe>');a+='<div style="clear: both;"></div></div>';if(b=document.getElementById("mibewinvitation"))b.innerHTML=a};b.Invitation.show=function(){var a=document.getElementById("mibewinvitationpopup");a&&(a.style.display="block")};
document.getElementById("mibewinvitationpopup");a&&a.parentNode.removeChild(a)};a.Invitation.show=function(){var a=document.getElementById("mibewinvitationpopup");a&&(a.style.display="block")};a.Invitation.accept=function(){document.getElementById("mibewAgentButton")&&(document.getElementById("mibewAgentButton").onclick(),a.Invitation.hide())};a.Invitation.reject=function(){a.Objects.widget.sendToServer({invitation_rejected:1});a.Invitation.hide()}})(Mibew); b.Invitation.hide=function(){var a=document.getElementById("mibewinvitationpopup");a&&a.parentNode.removeChild(a)};b.Invitation.accept=function(){document.getElementById("mibewAgentButton")&&(document.getElementById("mibewAgentButton").onclick(),b.Invitation.hide())};b.Invitation.reject=function(){b.Objects.widget.sendToServer({invitation_rejected:1});b.Invitation.hide()};b.APIFunctions={};b.APIFunctions.updateUserId=function(a){b.Utils.createCookie(b.Objects.widget.visitorCookieName,a.user.id)};
b.APIFunctions.invitationCreate=function(a){b.Invitation.create(a.invitation)};b.APIFunctions.invitationClose=function(){b.Invitation.hide()}})(Mibew);

View File

@ -320,6 +320,7 @@ var Mibew = {};
Mibew.Objects.widget.makeRequest(); Mibew.Objects.widget.makeRequest();
} }
/** /**
* @namespace Holds utility functions * @namespace Holds utility functions
*/ */
@ -359,6 +360,99 @@ var Mibew = {};
return value; return value;
} }
/**
* @namespace Holds invitation stuff
*/
Mibew.Invitation = {};
/**
* Create invitation popup
* @param {Object} options Popup options. It can contain following items:
* - 'operatorName': String, name of the operator who invite visitor to
* chat;
* - 'avatarUrl' String, URL of operator's avatar;
* - 'threadUrl': String, URL of the invitation thread which must be
* dispaly in invitation iframe.
*/
Mibew.Invitation.create = function (options) {
var operatorName = options.operatorName;
var avatarUrl = options.avatarUrl;
var threadUrl = options.threadUrl;
var popuptext = '<div id="mibewinvitationpopup" style="display: none;">';
popuptext += '<div id="mibewinvitationclose">'
+ '<a href="javascript:void(0);" onclick="Mibew.Invitation.reject();">'
+ '&times;</a></div>';
// Add operator name
if (operatorName) {
popuptext += '<h1 onclick="Mibew.Invitation.accept();">'
+ operatorName
+ '</h1>';
}
// Add operator avatar
if (avatarUrl) {
popuptext += '<img id="mibewinvitationavatar" src="' + avatarUrl
+ '" title="' + operatorName
+ '" alt="' + operatorName
+ '" onclick="Mibew.Invitation.accept();" />';
}
// Broadcast message from the thread related with invitation into iframe
if (threadUrl) {
popuptext += '<iframe id="mibewinvitationframe" src="' + threadUrl
+ '" onload="Mibew.Invitation.show();"></iframe>';
}
popuptext += '<div style="clear: both;"></div></div>';
var invitationdiv = document.getElementById("mibewinvitation");
if (invitationdiv) {
invitationdiv.innerHTML = popuptext;
}
}
/**
* Display invitation popup
*/
Mibew.Invitation.show = function() {
var invitationPopup = document.getElementById('mibewinvitationpopup');
if (invitationPopup) {
invitationPopup.style.display = 'block';
}
}
/**
* Hide invitation popup and remove it from DOM
*/
Mibew.Invitation.hide = function() {
var invitationPopup = document.getElementById('mibewinvitationpopup');
if (invitationPopup) {
invitationPopup.parentNode.removeChild(invitationPopup);
}
}
/**
* Accept invitation and open chat window
*/
Mibew.Invitation.accept = function() {
if (document.getElementById('mibewAgentButton')) {
document.getElementById('mibewAgentButton').onclick();
Mibew.Invitation.hide();
}
}
/**
* Visitor rejects invitation
*/
Mibew.Invitation.reject = function() {
Mibew.Objects.widget.sendToServer({'invitation_rejected': 1});
Mibew.Invitation.hide();
}
/** /**
* @namespace Holds functions that can be called by the Core * @namespace Holds functions that can be called by the Core
*/ */
@ -377,91 +471,16 @@ var Mibew = {};
/** /**
* Show invitation popup * Show invitation popup
* @param {Object} response Data passed from server * @param {Object} data Response data passed from server
*/ */
Mibew.APIFunctions.inviteOnResponse = function(response) { Mibew.APIFunctions.invitationCreate = function(data) {
var operator = response.invitation.operator; Mibew.Invitation.create(data.invitation);
var avatar = response.invitation.avatar;
var url = response.invitation.url;
var popuptext = '<div id="mibewinvitationpopup" style="display: none;">';
popuptext += '<div id="mibewinvitationclose">'
+ '<a href="javascript:void(0);" onclick="Mibew.Invitation.reject();">'
+ '&times;</a></div>';
// Add operator name
if (operator) {
popuptext += '<h1 onclick="Mibew.Invitation.accept();">'
+ operator
+ '</h1>';
}
// Add operator avatar
if (avatar) {
popuptext += '<img id="mibewinvitationavatar" src="' + avatar
+ '" title="' + operator
+ '" alt="' + operator
+ '" onclick="Mibew.Invitation.accept();" />';
}
// Translate message from the thread related with invitation into iframe
if (url) {
popuptext += '<iframe id="mibewinvitationframe" src="' + url
+ '" onload="Mibew.Invitation.show();"></iframe>';
}
popuptext += '<div style="clear: both;"></div></div>';
var invitationdiv = document.getElementById("mibewinvitation");
if (invitationdiv) {
invitationdiv.innerHTML = popuptext;
}
} }
/** /**
* Close invitation popup * Close invitation popup
*/ */
Mibew.APIFunctions.inviteOnClose = function() { Mibew.APIFunctions.invitationClose = function() {
Mibew.Invitation.hide();
}
/**
* @namespace Holds invitation stuff
*/
Mibew.Invitation = {};
/**
* Hide invitation popup and remove it from DOM
*/
Mibew.Invitation.hide = function() {
var invitationPopup = document.getElementById('mibewinvitationpopup');
if (invitationPopup) {
invitationPopup.parentNode.removeChild(invitationPopup);
}
}
/**
* Display invitation popup
*/
Mibew.Invitation.show = function() {
var invitationPopup = document.getElementById('mibewinvitationpopup');
if (invitationPopup) {
invitationPopup.style.display = 'block';
}
}
/**
* Accept invitation and open chat window
*/
Mibew.Invitation.accept = function() {
if (document.getElementById('mibewAgentButton')) {
document.getElementById('mibewAgentButton').onclick();
Mibew.Invitation.hide();
}
}
Mibew.Invitation.reject = function() {
Mibew.Objects.widget.sendToServer({'invitation_rejected': 1});
Mibew.Invitation.hide(); Mibew.Invitation.hide();
} }

View File

@ -66,8 +66,8 @@ if (Settings::get('enabletracking') == '1') {
if (! $invitation_state['invited'] if (! $invitation_state['invited']
&& ! empty($_SESSION['invitation_threadid']) && ! empty($_SESSION['invitation_threadid'])
) { ) {
$response['handlers'][] = 'inviteOnClose'; $response['handlers'][] = 'invitationClose';
$response['dependences']['inviteOnClose'] = array(); $response['dependences']['invitationClose'] = array();
unset($_SESSION['invitation_threadid']); unset($_SESSION['invitation_threadid']);
} }
@ -84,17 +84,17 @@ if (Settings::get('enabletracking') == '1') {
// Get operator info // Get operator info
$operator = operator_by_id($thread->agentId); $operator = operator_by_id($thread->agentId);
$locale = isset($_GET['locale']) ? $_GET['locale'] : ''; $locale = isset($_GET['locale']) ? $_GET['locale'] : '';
$operatorName = ($locale == $home_locale) $operator_name = ($locale == $home_locale)
? $operator['vclocalename'] ? $operator['vclocalename']
: $operator['vccommonname']; : $operator['vccommonname'];
// Show invitation dialog at widget side // Show invitation dialog at widget side
$response['handlers'][] = 'inviteOnResponse'; $response['handlers'][] = 'invitationCreate';
$response['dependences']['inviteOnResponse'] = array(); $response['dependences']['invitationCreate'] = array();
$response['data']['invitation'] = array( $response['data']['invitation'] = array(
'operator' => htmlspecialchars($operatorName), 'operatorName' => htmlspecialchars($operator_name),
'avatar' => htmlspecialchars($operator['vcavatar']), 'avatarUrl' => htmlspecialchars($operator['vcavatar']),
'url' => get_app_location(true, is_secure_request()) 'threadUrl' => get_app_location(true, is_secure_request())
. '/client.php?act=invitation' . '/client.php?act=invitation'
); );