var audio = new Audio('/library/sound/notification.mp3?v=2'), fp = true,pollTimer=null; var pllDebugg = false; var pollRunning = 0; var pollRunningQueue = 0; function pollDebugg(msg){ if(pllDebugg){ vvDebugg(msg); } } function initPollData(){ if(settings['PIPE_URL'] == undefined || settings['PIPE_URL'] == ""){ pollDebugg('pipe_url not set'); return; } if(settings['utoken'] == undefined || settings['utoken'] == ""){ pollDebugg('utoken not set'); return; } if(pollTimer ==null){ //pollData(); } } function isJson(str) { try { JSON.parse(str); } catch (e) { return false; } return true; } function setPollTimer(time = 1200000){ vvDebugg('--#poll: setTimeout::'+time); clearTimeout(pollTimer); pollTimer = null; pollTimer = setTimeout("pollData()",time); } function pollData(){ if(!settings['poll_enabled']){ return; } if(pollRunning > 0){ pollRunningQueue++; return; }else{ pollRunningQueue--; } vvDebugg('++--> POLL DATA :: QUEUE >> '+pollRunningQueue); pollDebugg('++ poll count: '+pollRunning); var _url = settings['PULL_URL']+'?init=pollData&fp='+fp+'&utoken='+settings['utoken']+'&r='+Math.random()+'&lang='+settings['LANG']; var tempSettings = Object.assign({}, settings); tempSettings['pData'] = ""; var _data = {"pData":settings['pData'],settings:tempSettings,site_id:settings['site_id']}; pollDebugg('--#poll: run'); pollDebugg('--#poll:'); if(settings['utoken'] == "" || settings['pData'] == false){ pollRunning = 0; pollDebugg('*** pData == false'); setPollTimer(100000); return; } pollDebugg(_data); pollDebugg(_url); if(!userOnScreen){ console.log('User not on screen'); return; } _data.server = settings['SERVER']; storeRequest( $.ajax({ url:_url, data:_data, type:'POST', dataType:"JSON", beforeSend: function(){ pollDebugg('--#poll: beforeSend'); pollRunning++; }, complete: function(data){ pollDebugg('--#poll: complete'); //Cooldown setTimeout(function(){ pollRunning--; },2000); }, error: function (request, status, error) { pollDebugg(error); pollDebugg(request.responseText); pollDebugg('***#poll: error'); setPollTimer(); }, success: function(data){ pollDebugg(data); if(data.success == false){ if(data.msg == 'token invalid'){ //top.location = "/?init=sign-out&r="+window.location.pathname; window.location.reload(); } if(data.action == 'signout'){ //top.location = "/?init=sign-out&r="+window.location.pathname; window.location.reload(); } if(data.action == 'check_session'){ //top.location = "/?init=sign-out&r="+window.location.pathname; window.location.reload(); } if(data.action == 'login'){ //top.location = "/?init=sign-out&r="+window.location.pathname; window.location.reload(); } return; } if(data.notifications != undefined ){ pollDebugg('notifications found'); if(typeof settings.notifications == 'undefined'){ settings.notifications = {}; } for(i in data.notifications){ notis = data.notifications[i]; settings.notifications[notis.id] = notis; var _cookie = getCookie("notis["+settings.uid+']['+notis.id+']'); if (_cookie == null) { console.log(notis); if(notis.read){ _cookie = true; } } $("._notifications .notification_"+notis.id).remove(); $("._notifications-alerts .notification_alert_"+notis.id).remove(); $("._notifications").append(htmlspecialchars_decode(notis.html)); if (_cookie == null) { $(".notification_"+notis.id).addClass('attention'); $("._notifications-alerts").append(htmlspecialchars_decode(notis.alert_html)); } else{ data.settings.notifications_count--; } } $(document).trigger('vvNotifications',settings.notifications); } for(i in data.settings){ if(isJson(data.settings[i])){ data.settings[i] = JSON.parse(data.settings[i]); } settings[i] = data.settings[i]; } if(data.add != undefined ){ pollDebugg('add item'); } if(data.tasks != undefined ){ pollDebugg('tasks found'); if(typeof settings.tasks == 'undefined'){ settings.tasks = {}; } console.log(data.tasks); for(i in data.tasks){ task = data.tasks[i]; if(task.id != undefined && task.id != ""){ settings.tasks[task.id] = task.completed; $("._tasks .task_"+task.id).remove(); $("._tasks").append(htmlspecialchars_decode(task.html)); }else{ } } $(document).trigger('vvTasks',settings.tasks); } if(data.change != undefined ){ pollDebugg('change found'); pollDebugg(data.change); for(i in data.change){ change = data.change[i]; pollDebugg(i+' >> '+change); if(change == 'add'){ if(data.location == window.location.pathname){ console.log('---->>> Add post'); //$(document).trigger('vvReloadAjax'); //audio.play(); console.log(data._pdata[i]); if( $('#item-'+data._pdata[i].id).get(0) == undefined){ //settings['pData']['data'].push({id:data._pdata[i].id}); url = window.location.href; if(url.includes('?')){ url = url+'&test-tr=true&vvAjaxRequest=true' } else{ url = url+'?test-tr=true&vvAjaxRequest=true' } console.log('---->>> url: '+url); $("#pending-update").show(); /*$.ajax({ url:url, data:{data:data._pdata[i]}, type:'POST', dataType:"HTML", beforeSend: function(){ }, complete: function(data){ }, error: function (request, status, error) { }, success: function(data){ $(".table-responsive tbody").prepend(data); } }); */ } } }else if(change == 'remove'){ $(".table-entries").hide(); console.log('---->>> remove post -> #item-'+i); $("#item-"+i).remove(); for(x in settings['pData']['data']){ if(settings['pData']['data'][x]['id'] == i){ settings['pData']['data'].splice(x, 1); pollDebugg('Removed #item-'+i); //audio.play(); } } $("table tbody tr").each(function(){ _delete = true; for(x in settings['pData']['data']){ id = ('item-'+settings['pData']['data'][x]['id']); if(id == $(this).attr('id')){ _delete = false; console.log(id+' found'); } } if(_delete){ console.log($(this).attr('id')+' not found'); $(this).remove(); } }); }else{ for(x in settings['pData']['data']){ if(settings['pData']['data'][x]['id'] == i){ for(j in change){ if(settings['pData']['data'][x][j] != change[j]){ settings['pData']['data'][x][j] = change[j]; var val = "change"; if(settings['list_values'] != null && settings['list_values'][j] != undefined){ if(settings['list_styles'][j] != undefined){ val = ''+settings['list_values'][j][change[j]]+''; }else{ val = settings['list_values'][j][change[j]]; } }else{ val = change[j]; } if(val != null){ val = htmlspecialchars_decode(val); $("#item-"+i).find('.'+j).html(val); pollDebugg({x:x,j:j,val:val}); $(document).trigger('watchDataChanged',{x:x,j:j,val:val,id:i}) audio.play(); } } } } } } } } runTrueLogs(); if(data.success){ setPollTimer(60*1000); }else{ pollDebugg('--#poll: '+data.msg); setPollTimer(120*1000); } } }) ); if(fp){ fp = false; } }