Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function(window){//Closure scope for protection of our chat
- var chatArea, chatInput, chatState, chat, logsLastChild;
- var logsStarted = false;
- var tabIndex =7;
- //Bind method to DOM support IE8 +
- function bindEvent(element, eventName, callback) {
- if (element.addEventListener) {
- element.addEventListener(eventName, callback, false);
- } else {
- element.attachEvent("on" + eventName, callback);
- }
- }
- //Unbinds method from DOM
- function unBindEvent(element, eventName, callback) {
- if (element.addEventListener) {
- element.removeEventListener(eventName, callback, false);
- } else {
- element.detachEvent("on" + eventName, callback);
- }
- }
- //Get a cleaned input value from the DOM by id
- function getTrimmedValue(inputId, clearValue){
- var res = "";
- var element = document.getElementById(inputId);
- if(element && element.value){
- res = element.value;
- res = res.replace(/^[\s*\n*\r*\t\*]*$[\s*\n*\r*\t\*]*/g, "");
- if(clearValue){
- element.value = '';
- }
- }
- return res;
- }
- //shows an element by it's id
- function showElementById(inputId){
- var elem = document.getElementById(inputId);
- if(elem){
- elem.style.display = 'block';
- }
- }
- //hides an element by it's id
- function hideElementById(inputId){
- var elem = document.getElementById(inputId);
- if(elem){
- elem.style.display = 'none';
- }
- }
- //Add lines to the chat from events
- function addLines(data) {
- var linesAdded = false;
- for (var i = 0; i < data.lines.length; i++) {
- var line = data.lines[i];
- if(line.source !== 'visitor' || chatState != chat.chatStates.CHATTING) {
- var chatLine = createLine(line);
- addLineToDom(chatLine);
- linesAdded = true;
- }
- }
- if(linesAdded){
- scrollToBottom();
- }
- }
- //Create a chat line
- function createLine(line) {
- var div = document.createElement("DIV");
- div.innerHTML = line.by + ": ";
- if(line.source === 'visitor' ){
- div.appendChild(document.createTextNode(line.text));
- }else{
- div.innerHTML += line.text;
- }
- div.setAttribute("title", line.by + " " + getText(line.text));
- div.setAttribute("tabIndex", tabIndex);
- tabIndex = tabIndex + 1;
- return div;
- }
- function getText(text){
- var div = document.createElement("DIV");
- div.appendChild(document.createTextNode(text));
- return div.innerText || div.textContent;
- }
- //Add a line to the chat view DOM
- function addLineToDom(line){
- if (!chatArea) {
- chatArea = document.getElementById("chatLines");
- }
- chatArea.appendChild(line);
- }
- //Scroll to the bottom of the chat view
- function scrollToBottom(){
- if (!chatArea) {
- chatArea = document.getElementById("chatLines");
- }
- chatArea.scrollTop = chatArea.scrollHeight;
- }
- //Sends a chat line
- function sendLine() {
- var text = getTrimmedValue("chatInput", true);
- if (text && chat) {
- var line = createLine({
- by : chat.getVisitorName(),
- text: text,
- source : 'visitor'
- });
- chat.addLine({
- text: text,
- error: function(){
- line.className = "error";
- }
- });
- addLineToDom(line);
- scrollToBottom();
- }
- }
- //Listener for enter events in the text area
- function keyChanges(e) {
- e = e || window.event;
- var key = e.keyCode || e.which;
- if (key == 13) {
- if (e.type == "keyup") {
- sendLine();
- setVisitorTyping(false);
- }
- return false;
- }else{
- setVisitorTyping(true);
- }
- }
- //Set the visitor typing state
- function setVisitorTyping(typing){
- if(chat){
- chat.setVisitorTyping({ typing: typing });
- }
- }
- //Set the visitor name
- function setVisitorName(){
- var name = getTrimmedValue("visitorName", true);
- if(chat && name){
- chat.setVisitorName({ visitorName : name});
- }
- }
- //Ends the chat
- function endChat() {
- if (chat) {
- chat.endChat({success: function(){
- showElementById("requestChat");
- }});
- }
- }
- //Sets a custom variable in the chat session
- function setCustomVariable() {
- var val = getTrimmedValue("varValue", true);
- var name = getTrimmedValue("varName", true);
- if (val && name && chat) {
- var vars = {};
- vars[name] = val;
- chat.setCustomVariable({
- customVariable: vars,
- error : function(){
- }
- });
- }
- }
- //Sends an email of the transcript when the chat has ended
- function sendEmail(){
- var email = getTrimmedValue("emailAddress", true);
- if(chat && email){
- chat.requestTranscript({email: email});
- }
- }
- //Sets the local chat state
- function updateChatState(data){
- chatState = data.state;
- }
- function agentTyping(data){
- if(data.agentTyping){
- showElementById("agentIsTyping");
- }else{
- hideElementById("agentIsTyping");
- }
- }
- //starts a chat
- function startChat() {
- if (chat) {
- var chatRequest = {
- skill : getTrimmedValue("chatSkill", true)
- };
- chat.requestChat(chatRequest);
- }
- }
- //Shows the chat request button
- function showChatRequest(){
- showElementById("startAChat");
- }
- //hides the chat request button
- function hideChatRequest(){
- hideElementById("startAChat");
- }
- //Writes a log line for events
- //So we can debug some non visual stuff
- function writeLog(logName, data){
- var log = document.createElement("DIV");
- try{
- data = typeof data === 'string' ? data : JSON.stringify(data);
- }catch (exc){
- return;
- }
- var date = new Date();
- date = "" + (date.getHours() > 10 ? date.getHours() : "0" + date.getHours()) +
- ":" + (date.getMinutes() > 10 ? date.getMinutes() : "0" + date.getMinutes()) +
- ":" + (date.getSeconds() > 10 ? date.getSeconds() : "0" + date.getSeconds());
- log.innerHTML = date + " " + logName + " : " + data;
- if(!logsStarted){
- document.getElementById("logs").appendChild(log);
- logsStarted = true;
- }else{
- document.getElementById("logs").insertBefore(log, logsLastChild);
- }
- logsLastChild = log;
- }
- //Creates an instance of the chat API with method binding
- function startChatApi(){
- var lpNumber = getTrimmedValue("siteId");
- var appKey = getTrimmedValue("appKey");
- var domain = getTrimmedValue("domainId");
- if(lpNumber && appKey){
- chat = new lpTag.taglets.ChatOverRestAPI({
- lpNumber: lpNumber,
- appKey: appKey,
- domain: domain,
- onInit: [showChatRequest,function(data){ writeLog("onInit", data);} ],
- onInfo: function(data){ writeLog("onInfo", data);},
- onLine: addLines,
- onState: [ updateChatState, function(data){ writeLog("onState", data);} ],
- onStart: [ hideChatRequest , updateChatState, bindInputForChat, function(data){ writeLog("onStart", data);} ],
- onStop: [showChatRequest, updateChatState, unBindInputForChat],
- onAgentTyping: [agentTyping , function(data) { writeLog("onAgentTyping", data); }],
- onRequestChat: function(data) { writeLog("onRequestChat", data); }
- });
- showElementById("chatContainer");
- hideElementById("startCredentials");
- }
- };
- //Binds the chat text area to allow sending of chat lines when chat is active
- function bindInputForChat(){
- bindEvent(document.getElementById("sendButton"), "click", sendLine);
- bindEvent(document.getElementById("chatInput"), "keyup", keyChanges);
- bindEvent(document.getElementById("chatInput"), "keydown", keyChanges);
- }
- //Unbinds chat text area input to prevent sending chat lines
- function unBindInputForChat(){
- unBindEvent(document.getElementById("sendButton"), "click", sendLine);
- unBindEvent(document.getElementById("chatInput"), "keyup", keyChanges);
- unBindEvent(document.getElementById("chatInput"), "keydown", keyChanges);
- }
- //Binds all listeners once the page has loaded
- bindEvent(window, "load", function () {
- bindEvent(document.getElementById("closeChat"), "click", endChat);//End chat button
- bindEvent(document.getElementById("setCustomVariable"), "click", setCustomVariable);//Set a custom variable
- bindEvent(document.getElementById("requestChat"), "click", startChat);//Start a chat
- bindEvent(document.getElementById("startAPI"), "click", startChatApi);//Start the chat API
- bindEvent(document.getElementById("setvisitorName"), "click", setVisitorName);//Set the visitor name
- bindEvent(document.getElementById("sendTranscript"), "click", sendEmail);//Set a transcript request when the chat ends
- });
- })(window);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement