File: /home/imensosw/www/imenso.co/dev/wipit/js/chat.js
var socket = io.connect(socketPosrt);
var me = {};
me.avatar = "https://lh6.googleusercontent.com/-lr2nyjhhjXw/AAAAAAAAAAI/AAAAAAAARmE/MdtfUmC0M4s/photo.jpg?sz=48";
//var url = "https://"+window.location.hostname+"/wip/";
var url = site_url;
var you = {};
you.avatar = "https://a11.t26.net/taringa/avatares/9/1/2/F/7/8/Demon_King1/48x48_5C5.jpg";
var chatHeartbeatTime=500000000000;
var chatBbeat ;
var current_documentChatId=0;
socket.on('notifyUser_wip', function(data){
if(data.who!='translator' && current_documentChatId==data.buyer_documents_id)
{
$('.writing_text').text(data.msg); $('.writing').show();
setTimeout(function(){ $('.writing').hide(); }, 2000);
}
});
socket.on( 'message_wip', function( data ) {
if(data.who!='translator' && current_documentChatId==data.buyer_documents_id)
{
uspic="C";
if(data.who=='admin')
{
uspic="Wip";
}
control = '<li style="width:100%;">' +
'<div class="msj-rta macro">' +
'<div class="chat_pic pic_admin">'+uspic+'</div>'+
'<div class="text text-l">' +
'<p>'+data.msg+'</p>' +
'<p><small>'+data.date+'</small></p>' +
'</div>' +
'</li>';
updateChatNotification(data.chat_id,data.buyer_documents_id);
$(".chat_countion").append(control);
chatScroll();
}
else if(data.who!='translator' && $('#spanChatCount_'+data.buyer_documents_id).length)
{
old_n=$('#spanChatCount_'+data.buyer_documents_id).html();
old_n=Number(old_n)+1;
if(old_n<2)
{
$('#spanChatCount_'+data.buyer_documents_id).closest('a').addClass('chaton');
}
$('#spanChatCount_'+data.buyer_documents_id).html(old_n);
}
});
function openChat(that)
{
//$(".msj-rta").css('display','flex');
$(".mytext").attr("placeholder", "type a message");
$(".msj-rta :input").attr("disabled", false);
if( $(that).attr('is_Canceled') == "Y" || $(that).attr('is_completed') == "Y")
{
$(".mytext").attr("placeholder", "You can not chat");
$(".msj-rta :input").attr("disabled", true);
//$(".msj-rta").css('display','none');
}
$(".chat_tital").html($(that).attr('title'));
$('.chat_open').removeClass('active_chat');
$("#btnChat_"+$(that).attr('buyer_documents_id')).removeClass('chaton');
$("#spanChatCount_"+$(that).attr('buyer_documents_id')).text('') ;
$(that).addClass('active_chat');
$("#buyer_documents_id").val($(that).attr('buyer_documents_id'));
$("#spanChatCount_"+$(that).attr('buyer_documents_id')).html('');
startchat($(that).attr('buyer_documents_id'));
}
function formatAMPM(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
//-- No use time. It is a javaScript effect.
function insertChat(who, text,to,time = 0){
var who = who ;
var control = "";
var date = formatAMPM(new Date());
var myFormData = new FormData();
myFormData.append('document', $('#chatFile').prop('files')[0]);
myFormData.append('message', text );
myFormData.append('to', to );
myFormData.append('buyer_documents_id', $("#buyer_documents_id").val());
myFormData.append('type',"file");
document_file= $('#chatFile').prop('files')[0];
msg=text;
to=to;
buyer_documents_id=$("#buyer_documents_id").val();
rand="";
$("#chatFile").val('');
$("#chatFileSpan").html('');
$("#chatFileRemove").trigger('click');
if(document_file!=undefined)
{
rand=Math.floor((Math.random() * 999) + 1);
control = '<li style="width:100%" id="lichat'+rand+'">' +
'<div class="msj macro">' +
'<div class="text text-l">' +
'<div class="uploading_cnt"><img src="'+url+'images/preloader.svg" alt="preloader" class="svg" />'+
'<strong id="progress'+rand+'">0%</strong>'+
'</div>'+
'<p>'+msg +'</p>' +
'<p><small>'+date+'</small></p>' +
'</div>' +
'</div>' +
'</li>';
}
else
{
control = '<li style="width:100%">' +
'<div class="msj macro">' +
'<div class="text text-l">' +
'<p>'+ msg +'</p>' +
'<p><small>'+date+'</small></p>' +
'</div>' +
'</div>' +
'</li>';
}
$(".chat_countion").append(control);
chatScroll();
myFormData.append('rand',rand);
$.ajax({
url : url+"chat/sendChat",
data:myFormData ,
type: "POST",
async: true,
cache: false,
contentType: false,
processData: false,
xhr : function ()
{
var jqXHR = null;
if ( window.ActiveXObject )
{
jqXHR = new window.ActiveXObject( "Microsoft.XMLHTTP" );
}
else
{
jqXHR = new window.XMLHttpRequest();
}
//Upload progress
jqXHR.upload.addEventListener( "progress", function ( evt )
{
if ( evt.lengthComputable )
{
var percentComplete = Math.round( (evt.loaded * 100) / evt.total );
//Do something with upload progress
// console.log( 'Uploaded percent', percentComplete );
if($('#progress'+rand).length && rand!=""){
if(percentComplete>98)
{
percentComplete=99;
}
var percentVal = percentComplete + '%';
$('#progress'+rand).html(percentVal);
}
}
}, false );
return jqXHR;
},
success: function(data)
{
if(data.rand!="" && $('#lichat'+data.rand).length)
{
$('#lichat'+data.rand).remove();
control = '<li style="width:100%">' +
'<div class="msj macro">' +
'<div class="text text-l">' +
'<p>'+ data.msg +'</p>' +
'<p><small>'+date+'</small></p>' +
'</div>' +
'</div>' +
'</li>';
$(".chat_countion").append(control);
chatScroll();
}
socket.emit( 'message_wip', {chat_id:data.id,document_file:document_file,msg:data.msg,buyer_documents_id:buyer_documents_id,date:date,to: to,who:'translator'} );
},
dataType:"json",
});
}
function resetChat(){
$("ul").empty();
}
$(".mytext").on("keyup", function(e){
if (e.which == 13 ){
var text = $(this).val();
//alert(text);
if (text !== "" || ($("#chatFile").val() !== "" && text == "") ){
to=$('.active_chat').attr('cln-id');
$(this).val('');
insertChat("me", text,to);
}
}
else
{
socket.emit('notifyUser_wip', {msg:'Translator is typing',buyer_documents_id:current_documentChatId,who:'translator'} );
}
});
$("#chatFile").on("change", function(e){
$("#divChatFileName").css("display", "block");
$("#chatFileSpan").html( e.target.files[0].name );
});
$("#chatFileRemove").on("click", function(e){
$("#divChatFileName").css("display", "none");
$("#chatFile").val('');
$("#chatFileSpan").html('');
});
function chatHeartbeat(buyer_documents_id)
{
url1=url+"chat/chatHeartbeat";
to=$('.active_chat').attr('cln-id');
$.ajax({
type: 'post',
url: url1,
data:"to="+to+"&buyer_documents_id="+buyer_documents_id,
dataType: "json",
success: function(data) {
if(data.length)
{
control="";
for(i=0;i<data.length;i++)
{
if(data[i]['send_from']=="a" )
{
control += '<li style="width:100%">' +
'<div class="msj-rta macro">' +
'<div class="chat_pic pic_admin">Wip</div>'+
'<div class="text text-l">' +
'<p>'+ data[i]["message"] +'</p>' +
'<p><small>'+data[i]["sentDMY"]+'</small></p>' +
'</div>' +
'</div>' +
'</li>';
}
else if(data[i]['send_from']=="c" )
{
control += '<li style="width:100%">' +
'<div class="msj-rta macro">' +
'<div class="chat_pic pic_admin">C</div>'+
'<div class="text text-l">' +
'<p>'+ data[i]["message"] +'</p>' +
'<p><small>'+data[i]["sentDMY"]+'</small></p>' +
'</div>' +
'</div>' +
'</li>';
}
else
{
control += '<li style="width:100%;">' +
'<div class="msj macro">' +
'<div class="text text-l">' +
'<p>'+data[i]["message"]+'</p>' +
'<p><small>'+data[i]["sentDMY"]+'</small></p>' +
'</div>' +
//'<div class="chat_avatar" style="padding:0px 0px 0px 10px !important"><img class="img-circle" style="width:100%;" src="'+you.avatar+'" /></div>' +
'</li>';
}
}
$(".chat_countion").append(control);
}
}
});
/* chatBeat = setTimeout(function() {
chatHeartbeat(buyer_documents_id);
}, chatHeartbeatTime); */
}
function startchat(buyer_documents_id)
{
current_documentChatId=buyer_documents_id;
$('.frame').hide();
url1=url+"chat/startchat";
to=$('.active_chat').attr('cln-id');
$.ajax({
type: 'post',
url: url1,
data:"to="+to+"&buyer_documents_id="+buyer_documents_id,
dataType: "json",
success: function(data) {
$(".chat_countion").html('');
$('.frame').show();
if(data.length)
{
control="";
for(i=0;i<data.length;i++)
{
if(data[i]['send_from']=="a" )
{
control += '<li style="width:100%">' +
'<div class="msj-rta macro">' +
'<div class="chat_pic pic_admin">Wip</div>'+
'<div class="text text-l">' +
'<p>'+ data[i]["message"] +'</p>' +
'<p><small>'+data[i]["sentDMY"]+'</small></p>' +
'</div>' +
'</div>' +
'</li>';
}
else if(data[i]['send_from']=="c" )
{
control += '<li style="width:100%">' +
'<div class="msj-rta macro">' +
'<div class="chat_pic pic_admin">CL</div>'+
'<div class="text text-l">' +
'<p>'+ data[i]["message"] +'</p>' +
'<p><small>'+data[i]["sentDMY"]+'</small></p>' +
'</div>' +
'</div>' +
'</li>';
}
else
{
control += '<li style="width:100%;">' +
'<div class="msj macro">' +
'<div class="text text-l">' +
'<p>'+data[i]["message"]+'</p>' +
'<p><small>'+data[i]["sentDMY"]+'</small></p>' +
'</div>' +
//'<div class="chat_avatar" style="padding:0px 0px 0px 10px !important"><img class="img-circle" style="width:100%;" src="'+you.avatar+'" /></div>' +
'</li>';
}
}
$(".chat_countion").html(control);
// $('#chat_countion').animate({scrollTop: $("#chat_countion li").last().offset().top},'slow');
chatScroll();
//win_adjust();
}
}
});
/* chatBeat = setTimeout(function() {
chatHeartbeat(buyer_documents_id);
}, chatHeartbeatTime);*/
}
function updateChatNotification(chat_id,document_id)
{
$.ajax({
type: 'post',
url: url+"chat/chatNotificationUpdate",
data:"chat_id="+chat_id+"&document_id="+document_id,
dataType: "json",
success: function(data) {
}
});
}
$('.chat_open123').on('click',function(){
});
$('.chat_close').on('click',function(){
current_documentChatId=0;
// clearTimeout(chatBeat);
$("#buyer_documents_id").val('');
$('.frame').hide();
});
function chatScroll()
{
$('#chat_countion').animate({scrollTop: $("#chat_countion")[0].scrollHeight},'slow');
}
//-- Clear Chat
//resetChat();
//-- Print Messages
/*insertChat("me", "Hello Tom...", 0);
insertChat("you", "Hi, Pablo", 1500);
insertChat("me", "What would you like to talk about today?", 3500);
insertChat("you", "Tell me a joke",7000);
insertChat("me", "Spaceman: Computer! Computer! Do we bring battery?!", 9500);
insertChat("you", "LOL", 12000);
insertChat("you", "LOL1", 12400);
insertChat("you", "LOL", 12700);*/
//-- NOTE: No use time on insertChat.;