Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.12 KB | None | 0 0
  1. function(window){//Closure scope for protection of our chat
  2.  
  3.  
  4. var chatArea, chatInput, chatState, chat, logsLastChild;
  5.  
  6.  
  7. var logsStarted = false;
  8.  
  9.  
  10. var tabIndex =7;
  11.  
  12.  
  13.  
  14. //Bind method to DOM support IE8 +
  15.  
  16.  
  17. function bindEvent(element, eventName, callback) {
  18.  
  19.  
  20. if (element.addEventListener) {
  21.  
  22.  
  23. element.addEventListener(eventName, callback, false);
  24.  
  25.  
  26. } else {
  27.  
  28.  
  29. element.attachEvent("on" + eventName, callback);
  30.  
  31.  
  32. }
  33.  
  34.  
  35. }
  36.  
  37.  
  38. //Unbinds method from DOM
  39.  
  40.  
  41. function unBindEvent(element, eventName, callback) {
  42.  
  43.  
  44. if (element.addEventListener) {
  45.  
  46.  
  47. element.removeEventListener(eventName, callback, false);
  48.  
  49.  
  50. } else {
  51.  
  52.  
  53. element.detachEvent("on" + eventName, callback);
  54.  
  55.  
  56. }
  57.  
  58.  
  59. }
  60.  
  61.  
  62.  
  63. //Get a cleaned input value from the DOM by id
  64.  
  65.  
  66. function getTrimmedValue(inputId, clearValue){
  67.  
  68.  
  69. var res = "";
  70.  
  71.  
  72. var element = document.getElementById(inputId);
  73.  
  74.  
  75. if(element && element.value){
  76.  
  77.  
  78. res = element.value;
  79.  
  80.  
  81. res = res.replace(/^[\s*\n*\r*\t\*]*$[\s*\n*\r*\t\*]*/g, "");
  82.  
  83.  
  84. if(clearValue){
  85.  
  86.  
  87. element.value = '';
  88.  
  89.  
  90. }
  91.  
  92.  
  93. }
  94.  
  95.  
  96. return res;
  97.  
  98.  
  99. }
  100.  
  101.  
  102.  
  103. //shows an element by it's id
  104.  
  105.  
  106. function showElementById(inputId){
  107.  
  108.  
  109. var elem = document.getElementById(inputId);
  110.  
  111.  
  112. if(elem){
  113.  
  114.  
  115. elem.style.display = 'block';
  116.  
  117.  
  118. }
  119.  
  120.  
  121. }
  122.  
  123.  
  124.  
  125. //hides an element by it's id
  126.  
  127.  
  128. function hideElementById(inputId){
  129.  
  130.  
  131. var elem = document.getElementById(inputId);
  132.  
  133.  
  134. if(elem){
  135.  
  136.  
  137. elem.style.display = 'none';
  138.  
  139.  
  140. }
  141.  
  142.  
  143. }
  144.  
  145.  
  146.  
  147. //Add lines to the chat from events
  148.  
  149.  
  150. function addLines(data) {
  151.  
  152.  
  153. var linesAdded = false;
  154.  
  155.  
  156. for (var i = 0; i < data.lines.length; i++) {
  157.  
  158.  
  159. var line = data.lines[i];
  160.  
  161.  
  162. if(line.source !== 'visitor' || chatState != chat.chatStates.CHATTING) {
  163.  
  164.  
  165. var chatLine = createLine(line);
  166.  
  167.  
  168. addLineToDom(chatLine);
  169.  
  170.  
  171. linesAdded = true;
  172.  
  173.  
  174. }
  175.  
  176.  
  177. }
  178.  
  179.  
  180. if(linesAdded){
  181.  
  182.  
  183. scrollToBottom();
  184.  
  185.  
  186. }
  187.  
  188.  
  189. }
  190.  
  191.  
  192. //Create a chat line
  193.  
  194.  
  195. function createLine(line) {
  196.  
  197.  
  198. var div = document.createElement("DIV");
  199.  
  200.  
  201. div.innerHTML = line.by + ": ";
  202.  
  203.  
  204. if(line.source === 'visitor' ){
  205.  
  206.  
  207. div.appendChild(document.createTextNode(line.text));
  208.  
  209.  
  210. }else{
  211.  
  212.  
  213. div.innerHTML += line.text;
  214.  
  215.  
  216. }
  217.  
  218.  
  219. div.setAttribute("title", line.by + " " + getText(line.text));
  220.  
  221.  
  222. div.setAttribute("tabIndex", tabIndex);
  223.  
  224.  
  225. tabIndex = tabIndex + 1;
  226.  
  227.  
  228. return div;
  229.  
  230.  
  231. }
  232.  
  233.  
  234.  
  235. function getText(text){
  236.  
  237.  
  238. var div = document.createElement("DIV");
  239.  
  240.  
  241. div.appendChild(document.createTextNode(text));
  242.  
  243.  
  244. return div.innerText || div.textContent;
  245.  
  246.  
  247. }
  248.  
  249.  
  250.  
  251. //Add a line to the chat view DOM
  252.  
  253.  
  254. function addLineToDom(line){
  255.  
  256.  
  257. if (!chatArea) {
  258.  
  259.  
  260. chatArea = document.getElementById("chatLines");
  261.  
  262.  
  263. }
  264.  
  265.  
  266. chatArea.appendChild(line);
  267.  
  268.  
  269. }
  270.  
  271.  
  272.  
  273. //Scroll to the bottom of the chat view
  274.  
  275.  
  276. function scrollToBottom(){
  277.  
  278.  
  279. if (!chatArea) {
  280.  
  281.  
  282. chatArea = document.getElementById("chatLines");
  283.  
  284.  
  285. }
  286.  
  287.  
  288. chatArea.scrollTop = chatArea.scrollHeight;
  289.  
  290.  
  291. }
  292.  
  293.  
  294.  
  295. //Sends a chat line
  296.  
  297.  
  298. function sendLine() {
  299.  
  300.  
  301. var text = getTrimmedValue("chatInput", true);
  302.  
  303.  
  304. if (text && chat) {
  305.  
  306.  
  307. var line = createLine({
  308.  
  309.  
  310. by : chat.getVisitorName(),
  311.  
  312.  
  313. text: text,
  314.  
  315.  
  316. source : 'visitor'
  317.  
  318.  
  319. });
  320.  
  321.  
  322.  
  323. chat.addLine({
  324.  
  325.  
  326. text: text,
  327.  
  328.  
  329. error: function(){
  330.  
  331.  
  332. line.className = "error";
  333.  
  334.  
  335. }
  336.  
  337.  
  338. });
  339.  
  340.  
  341. addLineToDom(line);
  342.  
  343.  
  344. scrollToBottom();
  345.  
  346.  
  347. }
  348.  
  349.  
  350. }
  351.  
  352.  
  353.  
  354. //Listener for enter events in the text area
  355.  
  356.  
  357. function keyChanges(e) {
  358.  
  359.  
  360. e = e || window.event;
  361.  
  362.  
  363. var key = e.keyCode || e.which;
  364.  
  365.  
  366. if (key == 13) {
  367.  
  368.  
  369. if (e.type == "keyup") {
  370.  
  371.  
  372. sendLine();
  373.  
  374.  
  375. setVisitorTyping(false);
  376.  
  377.  
  378. }
  379.  
  380.  
  381. return false;
  382.  
  383.  
  384. }else{
  385.  
  386.  
  387. setVisitorTyping(true);
  388.  
  389.  
  390. }
  391.  
  392.  
  393. }
  394.  
  395.  
  396.  
  397. //Set the visitor typing state
  398.  
  399.  
  400. function setVisitorTyping(typing){
  401.  
  402.  
  403. if(chat){
  404.  
  405.  
  406. chat.setVisitorTyping({ typing: typing });
  407.  
  408.  
  409. }
  410.  
  411.  
  412. }
  413.  
  414.  
  415.  
  416. //Set the visitor name
  417.  
  418.  
  419. function setVisitorName(){
  420.  
  421.  
  422. var name = getTrimmedValue("visitorName", true);
  423.  
  424.  
  425. if(chat && name){
  426.  
  427.  
  428. chat.setVisitorName({ visitorName : name});
  429.  
  430.  
  431. }
  432.  
  433.  
  434. }
  435.  
  436.  
  437.  
  438. //Ends the chat
  439.  
  440.  
  441. function endChat() {
  442.  
  443.  
  444. if (chat) {
  445.  
  446.  
  447. chat.endChat({success: function(){
  448.  
  449.  
  450. showElementById("requestChat");
  451.  
  452.  
  453. }});
  454.  
  455.  
  456. }
  457.  
  458.  
  459. }
  460.  
  461.  
  462.  
  463. //Sets a custom variable in the chat session
  464.  
  465.  
  466. function setCustomVariable() {
  467.  
  468.  
  469. var val = getTrimmedValue("varValue", true);
  470.  
  471.  
  472. var name = getTrimmedValue("varName", true);
  473.  
  474.  
  475. if (val && name && chat) {
  476.  
  477.  
  478. var vars = {};
  479.  
  480.  
  481. vars[name] = val;
  482.  
  483.  
  484. chat.setCustomVariable({
  485.  
  486.  
  487. customVariable: vars,
  488.  
  489.  
  490. error : function(){
  491.  
  492.  
  493.  
  494. }
  495.  
  496.  
  497. });
  498.  
  499.  
  500. }
  501.  
  502.  
  503. }
  504.  
  505.  
  506.  
  507. //Sends an email of the transcript when the chat has ended
  508.  
  509.  
  510. function sendEmail(){
  511.  
  512.  
  513. var email = getTrimmedValue("emailAddress", true);
  514.  
  515.  
  516. if(chat && email){
  517.  
  518.  
  519. chat.requestTranscript({email: email});
  520.  
  521.  
  522. }
  523.  
  524.  
  525. }
  526.  
  527.  
  528.  
  529. //Sets the local chat state
  530.  
  531.  
  532. function updateChatState(data){
  533.  
  534.  
  535. chatState = data.state;
  536.  
  537.  
  538. }
  539.  
  540.  
  541.  
  542. function agentTyping(data){
  543.  
  544.  
  545. if(data.agentTyping){
  546.  
  547.  
  548. showElementById("agentIsTyping");
  549.  
  550.  
  551. }else{
  552.  
  553.  
  554. hideElementById("agentIsTyping");
  555.  
  556.  
  557. }
  558.  
  559.  
  560. }
  561.  
  562.  
  563. //starts a chat
  564.  
  565.  
  566. function startChat() {
  567.  
  568.  
  569. if (chat) {
  570.  
  571.  
  572. var chatRequest = {
  573.  
  574.  
  575. skill : getTrimmedValue("chatSkill", true)
  576.  
  577.  
  578. };
  579.  
  580.  
  581. chat.requestChat(chatRequest);
  582.  
  583.  
  584. }
  585.  
  586.  
  587. }
  588.  
  589.  
  590. //Shows the chat request button
  591.  
  592.  
  593. function showChatRequest(){
  594.  
  595.  
  596. showElementById("startAChat");
  597.  
  598.  
  599. }
  600.  
  601.  
  602. //hides the chat request button
  603.  
  604.  
  605. function hideChatRequest(){
  606.  
  607.  
  608. hideElementById("startAChat");
  609.  
  610.  
  611. }
  612.  
  613.  
  614. //Writes a log line for events
  615.  
  616.  
  617. //So we can debug some non visual stuff
  618.  
  619.  
  620. function writeLog(logName, data){
  621.  
  622.  
  623. var log = document.createElement("DIV");
  624.  
  625.  
  626. try{
  627.  
  628.  
  629. data = typeof data === 'string' ? data : JSON.stringify(data);
  630.  
  631.  
  632. }catch (exc){
  633.  
  634.  
  635. return;
  636.  
  637.  
  638. }
  639.  
  640.  
  641. var date = new Date();
  642.  
  643.  
  644. date = "" + (date.getHours() > 10 ? date.getHours() : "0" + date.getHours()) +
  645.  
  646.  
  647. ":" + (date.getMinutes() > 10 ? date.getMinutes() : "0" + date.getMinutes()) +
  648.  
  649.  
  650. ":" + (date.getSeconds() > 10 ? date.getSeconds() : "0" + date.getSeconds());
  651.  
  652.  
  653. log.innerHTML = date + " " + logName + " : " + data;
  654.  
  655.  
  656. if(!logsStarted){
  657.  
  658.  
  659. document.getElementById("logs").appendChild(log);
  660.  
  661.  
  662. logsStarted = true;
  663.  
  664.  
  665. }else{
  666.  
  667.  
  668. document.getElementById("logs").insertBefore(log, logsLastChild);
  669.  
  670.  
  671. }
  672.  
  673.  
  674. logsLastChild = log;
  675.  
  676.  
  677.  
  678. }
  679.  
  680.  
  681.  
  682. //Creates an instance of the chat API with method binding
  683.  
  684.  
  685. function startChatApi(){
  686.  
  687.  
  688. var lpNumber = getTrimmedValue("siteId");
  689.  
  690.  
  691. var appKey = getTrimmedValue("appKey");
  692.  
  693.  
  694. var domain = getTrimmedValue("domainId");
  695.  
  696.  
  697. if(lpNumber && appKey){
  698.  
  699.  
  700. chat = new lpTag.taglets.ChatOverRestAPI({
  701.  
  702.  
  703. lpNumber: lpNumber,
  704.  
  705.  
  706. appKey: appKey,
  707.  
  708.  
  709. domain: domain,
  710.  
  711.  
  712. onInit: [showChatRequest,function(data){ writeLog("onInit", data);} ],
  713.  
  714.  
  715. onInfo: function(data){ writeLog("onInfo", data);},
  716.  
  717.  
  718. onLine: addLines,
  719.  
  720.  
  721. onState: [ updateChatState, function(data){ writeLog("onState", data);} ],
  722.  
  723.  
  724. onStart: [ hideChatRequest , updateChatState, bindInputForChat, function(data){ writeLog("onStart", data);} ],
  725.  
  726.  
  727. onStop: [showChatRequest, updateChatState, unBindInputForChat],
  728.  
  729.  
  730. onAgentTyping: [agentTyping , function(data) { writeLog("onAgentTyping", data); }],
  731.  
  732.  
  733. onRequestChat: function(data) { writeLog("onRequestChat", data); }
  734.  
  735.  
  736. });
  737.  
  738.  
  739. showElementById("chatContainer");
  740.  
  741.  
  742. hideElementById("startCredentials");
  743.  
  744.  
  745. }
  746.  
  747.  
  748. };
  749.  
  750.  
  751. //Binds the chat text area to allow sending of chat lines when chat is active
  752.  
  753.  
  754. function bindInputForChat(){
  755.  
  756.  
  757. bindEvent(document.getElementById("sendButton"), "click", sendLine);
  758.  
  759.  
  760. bindEvent(document.getElementById("chatInput"), "keyup", keyChanges);
  761.  
  762.  
  763. bindEvent(document.getElementById("chatInput"), "keydown", keyChanges);
  764.  
  765.  
  766. }
  767.  
  768.  
  769. //Unbinds chat text area input to prevent sending chat lines
  770.  
  771.  
  772. function unBindInputForChat(){
  773.  
  774.  
  775. unBindEvent(document.getElementById("sendButton"), "click", sendLine);
  776.  
  777.  
  778. unBindEvent(document.getElementById("chatInput"), "keyup", keyChanges);
  779.  
  780.  
  781. unBindEvent(document.getElementById("chatInput"), "keydown", keyChanges);
  782.  
  783.  
  784. }
  785.  
  786.  
  787. //Binds all listeners once the page has loaded
  788.  
  789.  
  790. bindEvent(window, "load", function () {
  791.  
  792.  
  793. bindEvent(document.getElementById("closeChat"), "click", endChat);//End chat button
  794.  
  795.  
  796. bindEvent(document.getElementById("setCustomVariable"), "click", setCustomVariable);//Set a custom variable
  797.  
  798.  
  799. bindEvent(document.getElementById("requestChat"), "click", startChat);//Start a chat
  800.  
  801.  
  802. bindEvent(document.getElementById("startAPI"), "click", startChatApi);//Start the chat API
  803.  
  804.  
  805. bindEvent(document.getElementById("setvisitorName"), "click", setVisitorName);//Set the visitor name
  806.  
  807.  
  808. bindEvent(document.getElementById("sendTranscript"), "click", sendEmail);//Set a transcript request when the chat ends
  809.  
  810.  
  811. });
  812.  
  813.  
  814. })(window);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement