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; } var pollKeysDelay = {}; function setPollTimer(time = 1200000, _key = ''){ if(pollKeysDelay[_key] == true){ return; } vvDebugg('--#poll: setTimeout::'+time+'::'+_key); clearTimeout(pollTimer); pollTimer = null; pollTimer = setTimeout("pollData()",time); pollKeysDelay[_key] = true; setTimeout(function(){ pollKeysDelay[_key] = false; },1000); } function pollData(force = false){ if(!force){ if(!settings['poll_enabled']){ return; } if(pollRunning > 0){ pollRunningQueue++; return; }else{ pollRunningQueue--; } }else{ fp = true; } 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,'polldata'); 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++; if($("._notifications").text() == ""){ $("._notifications").html("
Loading...
"); } }, complete: function(data){ pollDebugg('--#poll: complete'); $(".notifications-loading").remove(); //Cooldown setTimeout(function(){ pollRunning--; },2000); }, error: function (request, status, error) { pollDebugg(error); pollDebugg(request.responseText); pollDebugg('***#poll: error'); setPollTimer(10000,'polldata'); }, success: function(data){ console.log('Poll data:',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 ){ if(typeof settings.notifications == 'undefined'){ settings.notifications = {}; } } if(data.notifications != undefined ){ pollDebugg('notifications found'); if(typeof settings.notifications == 'undefined'){ settings.notifications = {}; } //Sort the notifications on added date var notifications = []; for(i in data.notifications){ notifications.push(data.notifications[i]); } notifications = notifications.sort(function(a, b) { return b.added - a.added; }); for(i in notifications){ notis = notifications[i]; settings.notifications[notis.id] = notis; var _cookie = getCookie("notis["+settings.uid+']['+notis.id+']'); if (_cookie == null) { 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); if(Object.keys(data.notifications).length == 0 ){ $("._notifications").html("You're all caught up - no new notifications.
"); } if(data.settings.notifications_count == 0){ $("#read-all-btn").hide(); }else{ $("#read-all-btn").show(); } } 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 = {}; } //Sort the tasks on completed first - data.tasks is an object var tasks = []; for(i in data.tasks){ tasks.push(data.tasks[i]); } tasks = tasks.sort(function(a, b) { return a.completed - b.completed; }); $("._tasks").html(''); for(i in tasks){ task = 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); $(document).trigger('vvTasksActions',settings.tasks); } if(data.change != undefined ){ pollDebugg('change found'); pollDebugg(data.change); for(i in data.change){ change = data.change[i]; if(change == 'add' && data.location == window.location.pathname){ $("#pending-update").show(); /* 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); $.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' && data.location == window.location.pathname){ $("#pending-update").show(); $(".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){ pollDebugg('--#poll: success'); setPollTimer(60*1000,'polldata'); }else{ pollDebugg('--#poll: '+data.msg); setPollTimer(120*1000,'polldata'); } } }) ); if(fp){ fp = false; } }