artabro/wire/modules/System/SystemNotifications/Notifications.min.js

1 line
15 KiB
JavaScript
Raw Permalink Normal View History

2024-08-27 11:35:37 +02:00
var Notifications={options:{ajaxURL:"./",version:1,reverse:0,updateLast:0,updateDelay:5e3,updateDelayFast:1550,iconMessage:"smile-o",iconWarning:"meh-o",iconError:"frown-o",iconRemove:"times-circle",classCommon:"NoticeItem",classMessage:"NoticeMessage",classWarning:"NoticeWarning",classError:"NoticeError",classDebug:"NoticeDebug",classContainer:"container",ghostDelay:2e3,ghostDelayError:4e3,ghostFadeSpeed:"fast",ghostOpacity:.9,ghostLimit:20,processKey:"",i18n:{sec:"sec",secs:"secs",min:"min",mins:"mins",hour:"hour",hours:"hours",day:"day",days:"days",expires:"expires",now:"now",fromNow:"from now",ago:"ago"}},updateTimeout:null,renderTimeout:null,timerTimeout:null,updating:false,runtime:[],numRender:0,numEmptyRequests:0,ghostsActive:0,currentDelay:0,turbo:false,timeNow:0,useSession:false,$menu:null,$bug:null,$list:null,relativeTime:function(secs){var str="";if(secs==0)return str;var i18n=Notifications.options.i18n;var past=secs>0;secs=Math.abs(secs);if(secs>1&&secs<60){str=secs+" "+(secs==1?i18n.sec:i18n.secs)}else if(secs>=60&&secs<3600){str=Math.floor(secs/60);str+=" "+(str==1?i18n.min:i18n.mins)}else if(secs>=3600&&secs<86400){str=Math.floor(secs/3600);str+=" "+(str==1?i18n.hour:i18n.hours)}else if(secs>=86400){str=Math.floor(secs/86400);str+=" "+(str==1?i18n.day:i18n.days)}str+=" ";if(past){if(secs<3)str=i18n.now}else{if(secs<3)str=i18n.now}return str},setTurboMode:function(turbo){if(turbo){if(Notifications.currentDelay!=Notifications.options.updateDelayFast){Notifications.currentDelay=Notifications.options.updateDelayFast;Notifications.update()}}else{Notifications.currentDelay=Notifications.options.updateDelay}},_updateItemTime:function($li,timeNow){var $created=$li.find("small.created");var createdStr="";if($created.length>0){var created=parseInt($created.attr("data-created"));var secs=timeNow-created;createdStr=Notifications.relativeTime(secs,true)}var expires=$li.attr("data-expires");if(expires){expires=parseInt(expires);if(expires>0&&expires<=timeNow){$li.slideUp("fast",function(){Notifications._remove($li)})}else{if(createdStr.length>0)createdStr+=" / ";var expiresStr=Notifications.options.i18n.expires+" "+Notifications.relativeTime(timeNow-expires);if(Math.abs(timeNow-expires)<10)expiresStr="<strong>"+expiresStr+"</strong>";createdStr+=expiresStr}}$created.html(createdStr)},_updateTime:function(){if(Notifications.timeNow==0)return;Notifications.$list.children("li").each(function(){Notifications._updateItemTime($(this),Notifications.timeNow)})},update:function(){if(Notifications.updating){clearTimeout(Notifications.updateTimeout);Notifications.updateTimeout=setTimeout("Notifications.update()",Notifications.currentDelay);return false}Notifications.updating=true;var rm="";var $rm=Notifications.$list.find("li.removed");$rm.each(function(){rm+=$(this).attr("id")+",";$(this).remove()});var url="./?Notifications=update&time="+Notifications.options.updateLast;if(rm.length)url+="&rm="+rm;if(Notifications.useSession&&Notifications.options.processKey.length){url+="&processKey="+Notifications.options.processKey+"."+sessionStorage.pwWindowName}$.getJSON(url,function(data){Notifications._update(data);clearTimeout(Notifications.updateTimeout);Notifications.updateTimeout=setTimeout("Notifications.update()",Notifications.currentDelay);Notifications.updating=false})},_update:function(data){var timeNow=parseInt(data.time);var annoy=false;var qty=data.notifications.length;var alerts=[];if(qty>0){Notifications.numEmptyRequests=0}else{Notifications.numEmptyRequests++;if(Notifications.numEmptyRequests>2)Notifications.setTurboMode(false)}if(timeNow>0)Notifications.timeNow=timeNow;Notifications.options.updateLast=Notifications.timeNow;for(var n=0;n<qty;n++){var notification=data.notifications[n];if(notification.flagNames.indexOf("alert")>-1){alerts[alerts.length]=notification}else{Notifications._add(notification,!data.runtime);if(notification.flagNames.indexOf("annoy")>-1)annoy=true}if(notification.flagNames.indexOf("no-ghost")<0)Notifications._ghost(notification,n)}if(annoy&&!Notifications.$menu.hasClass("open"))Notifications.$bug.t