Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var imID = window.location.pathname.substr(window.location.pathname.indexOf('/',1)+4);
  2.  
  3. function newMSG(msg,name,classes,time){
  4.     var chatView = $('.chat-view');
  5.     if(!time) time = (new Date()).toLocaleTimeString();
  6.     if(!classes) classes = '';
  7.     else classes = ' '+classes;
  8.     msg = msg.replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
  9.        return '&#'+i.charCodeAt(0)+';';
  10.     });
  11.     $('.dialog__input').before('<div class="dialog__msg'+classes+'"><div class="dialog__name">'+name+'</div><div class="dialog__time">'+time+'</div><div class="dialog__text">'+msg+'</div></div>');
  12.     chatView.scrollTop(chatView[0].scrollHeight);
  13. }
  14.  
  15. function sendMSG(message,user){
  16.     var messagePOST = JSON.stringify({"message":message});
  17.     console.log(message);
  18.     $.ajax({
  19.         type: 'POST',
  20.         url: '/api/v1.0/dialog/add',
  21.         data: messagePOST,
  22.         contentType: "application/json; charset=utf-8",
  23.         headers: { '_id': imID },
  24.         dataType: 'json',
  25.         success: function(data){
  26.             newMSG(message,user);
  27.             $('.dialog__input').val('');
  28.         },
  29.         error: function(data){
  30.             setTimeout(sendMSG,3000);
  31.         }
  32.     });
  33. }
  34.  
  35. $('.chat-form').submit(function(e){
  36.   e.preventDefault();
  37.   var input = $('.dialog__input');
  38.   var message = input.val();
  39.   sendMSG(message,'operator');
  40. });
  41.  
  42.  
  43.  
  44. $('.mute').click(function(e){
  45.     e.preventDefault();
  46. });
  47. $('.clear').click(function(e){
  48.     e.preventDefault();
  49.     $('.dialog__msg').remove();
  50. });
  51.  
  52.  
  53. (function getMSG(){
  54.     $.ajax({
  55.         type: 'POST',
  56.         url: '/api/v1.0/dialog/get',
  57.         headers: { '_id': imID },
  58.         dataType: "json",
  59.         success: function(data){
  60.             if(!this.closed(data)) return;
  61.             for(var i = 0; i < data.length; i++){
  62.                 newMSG(data[i]['message'],data[i]['user'],'',data[i]['time']);
  63.             }
  64.             getMSG();
  65.         },
  66.         error: function(data){
  67.             setTimeout(getMSG,3000);
  68.         },
  69.         closed: function(data){
  70.             if(data.status == 'closed'){
  71.                 return;
  72.             } else return true;
  73.         }
  74.     });
  75. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement