cryptalk/client/public/js/cryptalk.min.js

2 lines
100 KiB
JavaScript
Raw Permalink Normal View History

(function(){"use strict";let index=0,subs={},emits={},noop=function(){};function on(fromId,event,handler,once){let i,item,subscription=[fromId,handler,handler.length>1];(subs[event]||(subs[event]=[])).push(subscription);if(emits[event]){i=0;subscription=[subscription];while(item=emits[event][i++]){emit(0,0,item[0],item[1],subscription);if(once){break}}}}function off(fromId,event,handler){let sub,i=0,toSubs=subs[event];if(toSubs){while(sub=toSubs[i++]){if(sub[0]===fromId&&(!handler||handler===sub[1])){toSubs.splice(--i,1)}}}}function emit(persistent,event,data,callback,explicitSubs){let sub,toSubs=explicitSubs||subs[event]||[],total=toSubs.length,left,loop,answers=[],done;if(subs["*"]){toSubs=toSubs.concat(subs["*"])}loop=left=toSubs.length;if(loop){done=!callback?noop:function(data){if(data){answers.push(data)}if(!--left){callback(answers,total);callback=0}};while(sub=toSubs[--loop]){sub[1](data,sub[2]?done:left--,event)}}if(!left&&callback){callback(answers,total)}if(persistent){(emits[event]||(emits[event]=[])).push([data,callback])}}function Castrato(){this.nodeId=index++;return this}Castrato.prototype.emit=function(persistent,event,data,handler){if(persistent!==true&&persistent!==false){handler=data;data=event;event=persistent;persistent=false}emit(persistent,event,data,handler);return this};Castrato.prototype.on=function(event,handler){on(this.nodeId,event,handler);return this};Castrato.prototype.once=function(event,handler){on(this.nodeId,event,function wrapper(data,done){off(this.nodeId,event,wrapper);handler(data,handler.length>1?done:done())},true);return this};Castrato.prototype.off=function(event,handler){off(this.nodeId,event,handler);return this};Castrato.prototype.destroy=function(){this.nodeId=0;index=0;subs={};emits={};return this};let castrato=new Castrato;function win(mediator){var exports={},focusCallback=function(){mediator.emit("window:focused")},blurCallback=function(){mediator.emit("window:blurred")};exports.setTitle=function(t){document.title=t},exports.getTitle=function(){return document.title};if(window.addEventListener){window.addEventListener("focus",focusCallback,true);window.addEventListener("blur",blurCallback,true)}else{window.observe("focusin",focusCallback);window.observe("focusout",blurCallback)}mediator.on("window:title",exports.setTitle);return exports}function notifications(mediator,settings,win){var enabled=true,native_supported=false,new_title,original_title,blink_timer,interval,last,now=function(){return performance.now()||Date.now()},on=function(){enabled=true},off=function(){enabled=false},resetState=function(){clearTimeout(blink_timer);if(original_title!==undefined)win.setTitle(original_title);original_title=undefined;new_title=undefined},doBlink=function(){if(enabled){if(win.getTitle()===original_title)win.setTitle(new_title);else win.setTitle(original_title);blink_timer=setTimeout(doBlink,interval)}else{resetState()}},enableNative=function(){if(native_supported&&Notification.permission!=="denied"){Notification.requestPermission()}},blinkTitleUntilFocus=function(t,i){interval=i===undefined?1e3:i;if(enabled&&original_title===undefined){new_title=t;original_title=win.getTitle();doBlink()}},notify=function(title,body,icon,fallback){if(enabled&&now()-last>settings.notifications.maxOnePerMs){if(fallback===undefined)fallback=false;if(native_supported&&Notification.permission==="granted"){var n=new Notification(title,{body:body,icon:icon});n.onshow=function(){setTimeout(function(){n.close()},3e3)};last=now()}else if(fallback){blinkTitleUntilFocus("Attention",1e3)}}};native_supported=window.Notification!==undefined;mediator.on("notification:send",function(data){notify(data.title,data.body,data.icon,true)});mediator.on("notification:on",function(){on()});mediator.on("notification:off",function(){off()});enableNative();off();last=now();resetState()}var templates={help:"<pre> \n"+"Cryptalk, encrypted instant chat. \n"+"