1 line
1.6 KiB
JavaScript
1 line
1.6 KiB
JavaScript
!function(n){function e(n,e){var a="UPEvent."+n;t.exec(a,e)}var t={},a={},o=(new Date).getTime();t.exec=function(n,e){e=e||{};var t=++o;e.callbackId=t,a[t]=e.callback||function(n,e){},delete e.callback;var i='up://["'+n+'",'+JSON.stringify(e)+"]";prompt("TITLE",i)},n.nativeCallback=function(n,e,t){a[n]&&(a[n](e,t,JSON.parse(t)),delete a[n])},n.gCreateEvent=function(n,e){var t=document.createEvent("Events");if(t.initEvent(n,!1,!1),e)for(var a in e)e.hasOwnProperty(a)&&(t[a]=e[a]);return t};var i=function(n){this.type=n,this.handleMap={},this.count=0,this.onHandleCountChange=null};n.gEventHandleMap={create:function(n){return gEventHandleMap[n]=new i(n)},count:0},i.prototype.addEvent=function(n){var e=n.upEventFlag;e||(e=gEventHandleMap.count++,n.upEventFlag=e),this.handleMap[e]||(this.handleMap[e]=n,1==++this.count&&this.onHandleCountChange&&this.onHandleCountChange())},i.prototype.removeEvent=function(n){var e=n.upEventFlag;this.handleMap[e]&&(delete this.handleMap[e],0==--this.count&&this.onHandleCountChange&&this.onHandleCountChange())},i.prototype.fire=function(n){if(this.count){var e=[],t=0,a=Object.keys(this.handleMap);for(t=0;t<a.length;t++)e.push(this.handleMap[a[t]]);for(t=0;t<e.length;t++)e[t](n)}},t.addEventListener=function(n,t){var a=gEventHandleMap[n];a||(a=gEventHandleMap.create(n),a.onHandleCountChange=function(){e(n,{count:this.count})}),a.addEvent(t)},t.removeEventListener=function(n,e){var t=gEventHandleMap[n];t&&t.removeEvent(e)},n.nativeFireEvent=function(e,t){var a=gEventHandleMap[e];if(a){var o=n.gCreateEvent(e,JSON.parse(t));a.fire(o)}},n.upNativeComm=t}(window); |