Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var imID = window.location.pathname.substr(window.location.pathname.indexOf('/',1)+4);
- function newMSG(msg,name,classes,time){
- var chatView = $('.chat-view');
- if(!time) time = (new Date()).toLocaleTimeString();
- if(!classes) classes = '';
- else classes = ' '+classes;
- msg = msg.replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
- return '&#'+i.charCodeAt(0)+';';
- });
- $('.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>');
- chatView.scrollTop(chatView[0].scrollHeight);
- }
- function sendMSG(message,user){
- var messagePOST = JSON.stringify({"message":message});
- console.log(message);
- $.ajax({
- type: 'POST',
- url: '/api/v1.0/dialog/add',
- data: messagePOST,
- contentType: "application/json; charset=utf-8",
- headers: { '_id': imID },
- dataType: 'json',
- success: function(data){
- newMSG(message,user);
- $('.dialog__input').val('');
- },
- error: function(data){
- setTimeout(sendMSG,3000);
- }
- });
- }
- $('.chat-form').submit(function(e){
- e.preventDefault();
- var input = $('.dialog__input');
- var message = input.val();
- sendMSG(message,'operator');
- });
- $('.mute').click(function(e){
- e.preventDefault();
- });
- $('.clear').click(function(e){
- e.preventDefault();
- $('.dialog__msg').remove();
- });
- (function getMSG(){
- $.ajax({
- type: 'POST',
- url: '/api/v1.0/dialog/get',
- headers: { '_id': imID },
- dataType: "json",
- success: function(data){
- if(!this.closed(data)) return;
- for(var i = 0; i < data.length; i++){
- newMSG(data[i]['message'],data[i]['user'],'',data[i]['time']);
- }
- getMSG();
- },
- error: function(data){
- setTimeout(getMSG,3000);
- },
- closed: function(data){
- if(data.status == 'closed'){
- return;
- } else return true;
- }
- });
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement