/**
* (c) 2016 IP Security Consulting, all rights reserved.
*/
(function() {
var CC = 'https://cc.moow.info';
var JQ; // lokalne jQuery
var ajaxChatActivePush = null;
var lastId = 0;
var maxChatMessageLength = 2000;
var needToReceiveAllMessages = true;
function loadScript(url, callback) {
var script = document.createElement("script")
script.type = "text/javascript";
if (script.readyState) { // IE
script.onreadystatechange = function () {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
callback();
}
};
} else { // Others
script.onload = function () {
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}
loadScript("//code.jquery.com/jquery-1.12.2.min.js", function () {
JQ = jQuery.noConflict(true);
JQ.ajax({
type: 'POST',
url: CC + '/frontend/chat/chat-init.php',
xhrFields: { withCredentials: true },
async: true,
cache: false,
data: { referer: window.location.origin + window.location.pathname + window.location.search },
dataType: 'json',
}).done(function (response) {
if (response.warning) {
console.log(response.warning);
return false;
}
JQ('body').append('\
\
\
\
\
\
' + response.chat_title + '
\
\
\
\
\
\
');
if (response.chat_img_enabled === 'true') {
JQ('#ipsec-web-chat-image-container').append('
')
}
if (response.chat_position === 'left') {
JQ('#ipsec-web-chat').css('left', response.chat_left + 'px');
JQ('#ipsec-web-chat-image').css('left', response.chat_img_left + 'px');
} else if (response.chat_position === 'right') {
JQ('#ipsec-web-chat').css('right', response.chat_right + 'px');
JQ('#ipsec-web-chat-image').css('right', response.chat_img_right + 'px');
}
if (response.session_user_name != null) {
JQ('#ipsec-web-chat-name').val(response.session_user_name);
}
if (response.session_user_email != null) {
JQ('#ipsec-web-chat-email').val(response.session_user_email);
}
JQ('#ipsec-web-chat-title-container, #ipsec-web-chat-image').click(function () {
JQ('#ipsec-web-chat-inner-container').slideToggle();
JQ('#ipsec-web-chat-title-container').removeClass('pulse')
if (response.chat_img_enabled) {
JQ('#ipsec-web-chat-image').fadeToggle();
}
JQ('#ipsec-web-chat-content').scrollTop(JQ('#ipsec-web-chat-content')[0].scrollHeight);
});
JQ('#ipsec-web-chat-msg').on('keyup', function(e) {
stripChatMessage();
if (e.which == 13 && ! e.shiftKey) {
JQ('#ipsec-web-chat-button').click();
}
});
JQ('#ipsec-web-chat-button').click(function () {
sendChatMessage();
});
if (response.is_active_now)
JQ('#ipsec-web-chat-overall-wrapper').show();
else
JQ('#ipsec-web-chat-overall-wrapper').hide();
response.deactivate_after_seconds.forEach(function(val) {
setTimeout(function() {
const repeater = function() {
JQ('#ipsec-web-chat-overall-wrapper').hide();
};
setInterval(repeater, 1000*3600*24);
repeater();
}, val * 1000);
});
response.activate_after_seconds.forEach(function(val) {
setTimeout(function() {
const repeater = function() {
JQ('#ipsec-web-chat-overall-wrapper').show();
};
setInterval(repeater, 1000*3600*24);
repeater();
}, val * 1000);
});
if (response.conversation_is_ongoing && needToReceiveAllMessages)
receiveChatMessage();
});
});
function sendChatMessage() {
if (JQ('#ipsec-web-chat-button').prop('disabled'))
return;
JQ('#ipsec-web-chat-button').prop('disabled', false);
stripChatMessage();
var name = JQ('#ipsec-web-chat-name').val();
var email = JQ('#ipsec-web-chat-email').val();
var msg = JQ('#ipsec-web-chat-msg').val();
if (name === undefined) name = '';
if (email === undefined) email = '';
if (name == '' && email == '') {
alert('Prepáčte, ale musíte zadať meno alebo e-mail');
return false;
}
msg = msg.replace(/^\s+|\s+$/g, '');
if (msg == '')
return;
JQ.ajax({
type: 'POST',
url: CC + '/frontend/chat/chat-send.php',
xhrFields: { withCredentials: true },
async: true,
data: {
referer: window.location.origin + window.location.pathname + window.location.search,
name: name,
email: email,
msg: msg,
},
dataType: 'json',
}).done(function (response) {
if (needToReceiveAllMessages)
receiveChatMessage();
JQ('#ipsec-web-chat-button').prop('disabled', false);
if (response.warning) {
JQ('.ipsec-web-chat-warning').html(response.warning);
JQ('#ipsec-web-chat-warning-container')[0].style.setProperty('display', 'flex', 'important');
return false;
}
JQ('#ipsec-web-chat-warning-container')[0].style.setProperty('display', 'none', 'important');
// JQ('#ipsec-web-chat-conversation').append(response.conversation);
JQ('#ipsec-web-chat-msg').val('');
JQ('#ipsec-web-chat-customer-details')[0].style.setProperty('display', 'none', 'important');
JQ('#ipsec-web-chat-content')[0].style.setProperty('display', 'block', 'important');
JQ('#ipsec-web-chat-content').scrollTop(JQ('#ipsec-web-chat-content')[0].scrollHeight);
return true;
}).fail(function() {
JQ('#ipsec-web-chat-button').effect('shake').prop('disabled', false);
});
}
function stripChatMessage() {
msg = JQ('#ipsec-web-chat-msg').val();
if (msg.length > maxChatMessageLength)
JQ('#ipsec-web-chat-msg').val(msg.substr(0, maxChatMessageLength));
}
function sendChatAttachment(attachment) {
}
function receiveChatMessage() {
const allMessages = needToReceiveAllMessages;
needToReceiveAllMessages = false;
setTimeout(
function() {
doReceiveChatMessage(allMessages);
},
1000
);
}
function doReceiveChatMessage(allMessages) {
ajaxChatActivePush = JQ.ajax({
type: 'POST',
url: CC + '/frontend/chat/chat-receive.php',
xhrFields: { withCredentials: true },
async: true,
data: {
referer: window.location.origin + window.location.pathname + window.location.search,
id: lastId,
all: allMessages ? 'yes' : 'no',
},
dataType: 'json',
}).done(function (response) {
if (response.warning) {
lastId = response.id;
JQ('.ipsec-web-chat-warning').html(response.warning);
JQ('#ipsec-web-chat-warning-container')[0].style.setProperty('display', 'flex', 'important');
if (ajaxChatActivePush != null)
ajaxChatActivePush = null;
receiveChatMessage();
return false;
}
if (response.hasOwnProperty('conversation') && response.conversation != '') {
JQ('#ipsec-web-chat-warning-container')[0].style.setProperty('display', 'none', 'important');
JQ('#ipsec-web-chat-customer-details')[0].style.setProperty('display', 'none', 'important');
JQ('#ipsec-web-chat-conversation').append(response.conversation);
JQ('#ipsec-web-chat-content')[0].style.setProperty('display', 'block', 'important');
JQ('#ipsec-web-chat-content').scrollTop(JQ('#ipsec-web-chat-content')[0].scrollHeight);
JQ('#ipsec-web-chat-title-container').addClass('pulse')
lastId = response.id;
}
if (ajaxChatActivePush != null)
ajaxChatActivePush = null;
receiveChatMessage();
return true;
}).fail(function () {
receiveChatMessage();
return true;
});
}
})();