forked from KrisSiegel/msngr.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsngr.min.js
1 lines (1 loc) · 18.9 KB
/
msngr.min.js
1
var msngr=msngr||function(){"use strict";var a={warnings:!0};a.config={};var b=function(b,c,d){return a.objects.message(b,c,d)};b.version="3.2.2";var c=function(a){return Object.prototype.toString.call(a)},d=function(a){return a.bind({})},e=function(a,c){if(void 0===a||null===a)return c;if(void 0===c||null===c)return a;var d,e=Object.prototype.toString.call(a),f=Object.prototype.toString.call(c);if("[object Object]"===e&&"[object Object]"===f){d={};for(var g in a)a.hasOwnProperty(g)&&(d[g]=a[g]);for(var g in c)if(c.hasOwnProperty(g))if("[object Object]"===Object.prototype.toString.call(c[g]))void 0===d[g]&&(d[g]={}),d[g]=b.merge(a[g],c[g]);else if("[object Array]"===Object.prototype.toString.call(a[g])&&"[object Array]"===Object.prototype.toString.call(c[g])){d[g]=a[g]||[];for(var h=0;h<c[g].length;++h)-1===d[g].indexOf(c[g][h])&&d[g].push(c[g][h])}else d[g]=c[g];return d}if("[object String]"===e&&"[object String]"===f)return d=a+c;if("[object Array]"===e&&"[object Array]"===f){d=a;for(var h=0;h<c.length;++h)-1===d.indexOf(c[h])&&d.push(c[h]);return d}if("[object Function]"===e&&"[object Function]"===f)return function(a,c,d){return function(){return b.merge(a.apply(this,d),c.apply(this,d))}}(a,c,arguments);var i=["[object Function]","[object Object]"];if(-1!==i.indexOf(e)&&-1!==i.indexOf(f)){var j="[object Function]"===e?a:c,k="[object Object]"===e?a:c;if(void 0!==j&&void 0!==k)for(var g in k)k.hasOwnProperty(g)&&(j[g]=k[g]);return d=j}return d};return b.extend=function(c,d){return d=d||b,"[object Function]"===Object.prototype.toString.call(c)&&(c=c.apply(this,[b,a])),d=b.merge(c,d)},b.merge=function(){var a;if(arguments.length>0)for(var b=0;b<arguments.length;++b)a=e(a,arguments[b]);return a},b.copy=function(a){if(void 0===a||null===a)return a;var e=c(a);if(-1===["[object Object]","[object Function]"].indexOf(e))return a;var f;"[object Object]"===c(a)?f={}:"[object Function]"===c(a)&&(f=d(a));for(var g in a)if(a.hasOwnProperty(g)){var h=c(a[g]);-1!==["[object Object]","[object Function]"].indexOf(h)?f[g]=b.copy(a[g]):f[g]=a[g]}return f},b.config=function(c,d){return void 0!==d&&(a.config[c]=b.merge(a.config[c]||{},b.copy(d))),a.config[c]},Object.defineProperty(b,"debug",{set:function(c){c===!0?b.internal=a:c===!1&&delete b.internal},get:function(){return void 0!==b.internal}}),Object.defineProperty(b,"warnings",{set:function(b){a.warnings=b},get:function(){return a.warnings}}),b}();msngr.extend(function(a,b){"use strict";return{argumentsToArray:function(b){return a.isArray(b)?b:a.isArguments(b)?Array.prototype.slice.call(b,0):[b]}}}),msngr.extend(function(a,b){"use strict";return{isHtmlElement:function(a){var b=this.getType(a);return 0===b.indexOf("[object HTML")||0===b.indexOf("[object global]")},isNodeList:function(a){return"[object NodeList]"===this.getType(a)},findElement:function(b,c){var d=a.findElements(b,c);return void 0!==d&&d.length>0?d[0]:d},findElements:function(b,c){var d;if(a.isHtmlElement(b)&&(d=b),void 0===d&&a.isString(b)){var e=c||document,f=a.querySelectorAllWithEq(b,e);null!==f&&(d=f)}return d},getDomPath:function(b){var c=a.findElement(b);if(void 0!==c){if(!a.isEmptyString(c.id))return"#"+c.id;var d,e,f=function(b){var c=b.parentNode;if(a.exist(c)){if(e=b.tagName,c.childNodes.length>1)for(var g=0;g<c.childNodes.length;++g)if(c.childNodes[g]===b){e=e+":eq("+g+")";break}d=a.isEmptyString(d)?e:e+" > "+d,a.exist(c.parentNode)&&f(c)}};return f(b),a.isEmptyString(d)&&(c.id=a.id(),d="#"+c.id),d}},querySelectorAllWithEq:function(a,b){if(void 0===a)return null;for(var c=b||document,d=[],e=function(b){if(-1!==b.indexOf(":eq(")){var c=b.indexOf(":eq("),e=b.substring(0,c),f=b.substring(c+4,b.indexOf(")",c));a=b.substring(b.indexOf(")",c)+1,b.length).trim(),">"===e.charAt(0)&&(e=e.substring(1,e.length)),">"===a.charAt(0)&&(a=a.substring(1,a.length)),d.push({selector:e,index:parseInt(f,10)})}};-1!==a.indexOf(":eq");)e(a);for(var f;d.length>0;){var g=d.shift();f=(f||c).querySelectorAll(g.selector)[g.index]}return a.trim().length>0?(f||c).querySelectorAll(a):[f]},querySelectorWithEq:function(b,c){return a.querySelectorAllWithEq(b,c)[0]}}}),msngr.extend(function(a,b){"use strict";return b.InvalidParametersException=function(b,c){var d={name:"InvalidParametersException",severity:"unrecoverable",message:"Invalid parameters supplied to the {method} method".replace("{method}",b)};return a.isEmptyString(c)||(d.message=d.message+" "+c),d},b.ReservedKeywordsException=function(a){return{name:"ReservedKeywordsException",severity:"unrecoverable",message:"Reserved keyword {keyword} supplied as action.".replace("{keyword}",a)}},b.MangledException=function(a,b){return{name:"MangledException",severity:"unrecoverable",message:"The {variable} was unexpectedly mangled in {method}.".replace("{variable}",a).replace("{method}",b)}},{}}),msngr.extend(function(a,b){"use strict";if("undefined"!=typeof window&&"undefined"!=typeof window.postMessage){a.config("immediate",{channel:"__msngr_immediate"});var c=[];window.addEventListener("message",function(a){a.source===window&&a.data===b.config.immediate.channel&&(a.stopPropagation(),c.length>0&&c.shift()())},!0)}var d,e,f=function(){return performance.now()},g=function(){return process.hrtime()[1]/1e6},h=function(){return Date.now()},i=void 0,j="",k=void 0;return{id:function(){var b=a.now(),c="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(a){var c=(b+16*Math.random())%16|0;return b=Math.floor(b/16),("x"==a?c:3&c|8).toString(16)});return c},now:function(b){void 0===i&&("undefined"!=typeof performance?(i=f,j="performance"):"undefined"!=typeof process?(i=g,j="node"):(i=h,j="legacy"));var c=i();return b===!0&&k===c?a.now(b):(k=c,c)},removeFromArray:function(a,b){var c=a.indexOf(b),d=a.length-1;if(c!==d){var e=a[d];a[d]=a[c],a[c]=e}a.pop()},deDupeArray:function(a){for(var b={},c=[],d=a.length,e=0;d>e;++e)void 0===b[a[e]]&&(b[a[e]]=!0,c.push(a[e]));return c},isBrowser:function(){return void 0===d&&(d="undefined"!=typeof XMLHttpRequest),d},immediate:function(a){void 0===e&&(e="undefined"!=typeof setImmediate?setImmediate:"undefined"!=typeof window&&"undefined"!=typeof window.postMessage?function(a){c.push(a),window.postMessage(b.config.immediate.channel,"*")}:function(a){setTimeout(a,0)}),e(a)}}}),msngr.extend(function(a,b){"use strict";return b.reiterativeValidation=function(b,c){var d=!1;if(a.exist(b)&&a.exist(c)){a.isArray(c)||(c=[c]);for(var e=0;e<c.length&&(d=b.apply(this,[c[e]]),d!==!1);++e);}return d},{getType:function(b){return a.exist(b)?Object.prototype.toString.call(b):""+b},isArguments:function(b){return"[object Arguments]"===a.getType(b)},areArguments:function(){return b.reiterativeValidation(a.isArguments,a.argumentsToArray(arguments))},isNullOrUndefined:function(a){return void 0===a||null===a},exist:function(b){return!a.isNullOrUndefined(b)},exists:function(){return b.reiterativeValidation(a.exist,a.argumentsToArray(arguments))},isString:function(b){return"[object String]"===a.getType(b)},areStrings:function(){return b.reiterativeValidation(a.isString,a.argumentsToArray(arguments))},isDate:function(b){return"[object Date]"===a.getType(b)},areDates:function(){return b.reiterativeValidation(a.isDate,a.argumentsToArray(arguments))},isArray:function(b){return"[object Array]"===a.getType(b)},areArrays:function(){return b.reiterativeValidation(a.isArray,a.argumentsToArray(arguments))},isNumber:function(b){return"[object Number]"===a.getType(b)},areNumbers:function(){return b.reiterativeValidation(a.isNumber,a.argumentsToArray(arguments))},isObject:function(b){return"[object Object]"===a.getType(b)},areObjects:function(){return b.reiterativeValidation(a.isObject,a.argumentsToArray(arguments))},isFunction:function(b){return"[object Function]"===a.getType(b)},areFunctions:function(){return b.reiterativeValidation(a.isFunction,a.argumentsToArray(arguments))},isEmptyString:function(b){var c=a.isString(b);return void 0===b||null===b||c&&0===b.toString().trim().length?!0:!1},areEmptyStrings:function(){return b.reiterativeValidation(a.isEmptyString,a.argumentsToArray(arguments))}}}),msngr.extend(function(a,b){"use strict";return b.objects=b.objects||{},b.objects.executer=function(c){if(!a.exist(c)||!a.isArray(c))throw b.InvalidParametersException("executor");for(var d=0;d<c.length;++d)a.isFunction(c[d])&&(c[d]={method:c[d]});var e=function(b,c,d,e){a.immediate(function(){var f=!1,g=function(){return f=!0,function(a){e.apply(d,[a])}};a.isArray(c)||(c=a.exist(c)?[c]:[]),c.push(g);var h=b.apply(d||this,c);f!==!0&&e.apply(d,[h])})};return{parallel:function(b){var d=[],f=0;if(0===c.length&&a.exist(b))return b.apply(j,[[]]);for(var g=0;g<c.length;++g){var h=c[g].method,i=c[g].params,j=c[g].context;!function(g,h,i){e(g,h,i,function(e){a.exist(e)&&d.push(e),++f,f===c.length&&a.exist(b)&&b.apply(j,[d])})}(h,i,j)}}}},{executer:b.objects.executer}}),msngr.extend(function(a,b){"use strict";return b.objects=b.objects||{},b.objects.memory=function(){var b={},c={},d=0,e={index:function(e){if(a.exist(e)&&a.exist(e.topic)){var f=a.id();return b[f]=a.copy(e),a.exist(c[e.topic])||(c[e.topic]={uuids:[],category:{}}),a.exist(c[e.topic].category[e.category])||(c[e.topic].category[e.category]={uuids:[],subcategory:{}}),a.exist(c[e.topic].category[e.category].subcategory[e.subcategory])||(c[e.topic].category[e.category].subcategory[e.subcategory]={uuids:[]}),a.exist(e.category)||a.exist(e.subcategory)||c[e.topic].uuids.push(f),a.exist(e.category)&&!a.exist(e.subcategory)&&c[e.topic].category[e.category].uuids.push(f),a.exist(e.category)&&a.exist(e.subcategory)&&c[e.topic].category[e.category].subcategory[e.subcategory].uuids.push(f),d++,f}},"delete":function(e){if(a.exist(e)&&a.exist(b[e])){var f=b[e];return a.removeFromArray(c[f.topic].uuids,e),a.removeFromArray(c[f.topic].category[f.category].uuids,e),a.removeFromArray(c[f.topic].category[f.category].subcategory[f.subcategory].uuids,e),delete b[e],d--,!0}return!1},query:function(b){var d=[];if(a.exist(b)&&a.exist(b.topic)&&a.exist(c[b.topic])){var e=c[b.topic],f=((e||{}).category||{})[b.category],g=((f||{}).subcategory||{})[b.subcategory];d=d.concat(e.uuids||[]),d=d.concat((f||{}).uuids||[]),d=d.concat((g||{}).uuids||[])}return a.deDupeArray(d)},clear:function(){return b={},c={},d=0,!0}};return Object.defineProperty(e,"count",{get:function(){return d}}),e},{}}),msngr.extend(function(a,b){"use strict";b.objects=b.objects||{},b.option=function(a,c){b.option[a]=c};var c=b.objects.memory(),d=b.objects.memory(),e={},f=0,g={},h=0,i={},j=0;return Object.defineProperty(b,"handlerCount",{get:function(){return f}}),Object.defineProperty(b,"boundCount",{get:function(){return j}}),Object.defineProperty(b,"payloadCount",{get:function(){return h}}),b.reset=function(){e={},i={},f=0,j=0,c.clear(),d.clear(),g={},h=0},b.processOpts=function(c,d,e,f){var g=[];for(var h in c)c.hasOwnProperty(h)&&a.exist(b.option[h])&&g.push({method:b.option[h],params:[d,e,c]});if(0===g.length)return f.apply(this,[e]);var i=b.objects.executer(g);i.parallel(function(b){var c=e;if(a.exist(b)&&b.length>0)for(var d=0;d<b.length;++d)a.exist(b[d])&&(c=a.merge(b[d],c));f.apply(this,[c])})},b.domListener=function(b){var c=this,d=a.getDomPath(c);return a.exist(i[d])&&a.exist(i[d][b.type])?i[d][b.type].emit():void 0},b.objects.message=function(k,l,m){var n=void 0;if(!a.exist(k))throw b.InvalidParametersException("msngr");if(!a.isObject(k)&&!a.isString(k))throw b.InvalidParametersException("msngr");if(a.isEmptyString(k))throw b.InvalidParametersException("msngr");a.isObject(k)?n=a.copy(k):(n={},n.topic=k,a.isEmptyString(l)||(n.category=l),a.isEmptyString(m)||(n.subcategory=m)),n.topic=n.topic?n.topic.toLowerCase():n.topic,n.category=n.category?n.category.toLowerCase():n.category,n.subcategory=n.subcategory?n.subcategory.toLowerCase():n.subcategory;var o={},p={emits:0,persists:0,options:0,ons:0,onces:0,binds:0},q=function(a,d,f){var d=d||c.query(n)||[];b.processOpts(o,n,a,function(a){for(var c=[],g=[],h=0;h<d.length;++h){var i=e[d[h]];c.push({method:i.handler,params:[a,n]}),i.once===!0&&g.push(i.handler)}for(var j=b.objects.executer(c),h=0;h<g.length;++h)s.drop(g[h]);j.parallel(f)})},r=function(){var b,c=d.query(n);if(0!==c.length){if(1===c.length)return g[c[0]];for(var e=0;e<c.length;++e)b=a.extend(innerPay,b);return b}},s={option:function(c,d){if(!a.exist(c)||!a.isString(c))throw b.InvalidParametersException("option");return o[c]=d,p.options=p.options+1,s},emit:function(b,c){return a.isFunction(b)&&(c=b,b=void 0),q(b,void 0,c),p.emits=p.emits+1,s},persist:function(b){void 0===b&&(b=null);var c=d.query(n);if(0===c.length){var e=d.index(n);g[e]=b,c=[e]}else for(var f=0;f<c.length;++f)g[c[f]]=a.merge(b,g[c[f]]);var i=r();return++h,p.persists=p.persists+1,s.emit(i)},cease:function(){for(var a=d.query(n),b=0;b<a.length;++b)delete g[a[b]],--h;return s},on:function(a){var b=c.index(n);e[b]={handler:a,context:n.context||this,once:!1},f++;var d=r();return void 0!==d&&q(d,[b],void 0),p.ons=p.ons+1,s},once:function(a){var b=c.index(n);e[b]={handler:a,context:n.context||this,once:!0},f++;var d=r();return void 0!==d&&q(d,[b],void 0),p.onces=p.onces+1,s},bind:function(c,d){var e=a.findElement(c),f=a.getDomPath(e);return a.exist(i[f])||(i[f]={}),i[f][d]=s,e.addEventListener(d,b.domListener),++j,p.binds=p.binds+1,s},drop:function(a){var b=c.query(n);if(b.length>0)for(var d=0;d<b.length;++d){var g=b[d];e[g].handler===a&&(delete e[g],f--,c["delete"](g))}return s},unbind:function(c,d){var e=a.findElement(c),f=a.getDomPath(e);return a.exist(i[f])&&a.exist(i[f][d])&&(e.removeEventListener(d,b.domListener),delete i[f][d],--j),s},dropAll:function(){var a=c.query(n);if(a.length>0)for(var b=0;b<a.length;++b){var d=a[b];delete e[d],f--,c["delete"](d)}return s}};return Object.defineProperty(s,"message",{get:function(){return n}}),Object.defineProperty(s,"topic",{get:function(){return n.topic}}),Object.defineProperty(s,"category",{get:function(){return n.category}}),Object.defineProperty(s,"subcategory",{get:function(){return n.subcategory}}),Object.defineProperty(s,"subscribers",{get:function(){return c.query(n).length}}),a.debug===!0&&Object.defineProperty(s,"counts",{get:function(){return p}}),s},{}}),msngr.extend(function(a,b){"use strict";a.config("net",{defaults:{protocol:"http",port:{http:"80",https:"443"},autoJson:!0}});var c=function(b,c,d){try{var e=new XMLHttpRequest;e.onreadystatechange=function(){if(4===e.readyState)if(200===e.status||201===e.status){var a;if(c.autoJson===!0&&"application/json"===this.getResponseHeader("content-type"))try{a=JSON.parse(e.response)}catch(b){a=void 0}d.apply(void 0,[null,a||e.response])}else{var f={status:e.status,response:e.response};d.apply(void 0,[f,null])}};var f=b.protocol+"://"+b.host;b.canOmitPort===!0?f+=c.path:f=f+":"+b.port+c.path;var g;if(a.exist(c.payload)){if(a.isObject(c.payload))try{g=JSON.stringify(c.payload)}catch(h){}void 0===g&&(g=c.payload)}if(e.open(c.method,f),a.exist(c.headers))for(var i in c.headers)c.headers.hasOwnProperty(i)&&e.setRequestHeader(i,c.headers[i]);e.send(g)}catch(h){d.apply(void 0,[h,null])}},d=function(b,c,d){var e=require("http"),f=e.request({method:c.method,host:b.host,port:b.port,path:c.path,headers:c.headers},function(a){a.setEncoding("utf8");var b="";a.on("data",function(a){b+=a}),a.on("end",function(){var e;if(c.autoJson===!0&&"application/json"===a.headers["content-type"])try{e=JSON.parse(b)}catch(g){e=void 0}e=e||b;var h;f.statusCode>=400&&(h={status:f.statusCode,response:e||b},e=null),d.apply(void 0,[h,e])})});if(a.exist(c.payload)){var g;if(a.isObject(c.payload))try{g=JSON.stringify(c.payload)}catch(h){}void 0===g&&(g=c.payload),f.write(g)}f.end()},e=function(e,f,g){if(f.path=f.path||"/",f.autoJson=f.autoJson||b.config.net.defaults.autoJson,a.exist(f.query)&&(a.isString(f.query)&&(f.queryString=f.query),a.isObject(f.query))){f.queryString="?";for(var h in f.query)f.query.hasOwnProperty(h)&&("?"!==f.queryString&&(f.queryString=f.queryString+"&"),f.queryString=f.queryString+encodeURIComponent(h)+"="+encodeURIComponent(f.query[h]))}f.path=f.path+(f.queryString||""),a.isBrowser()?c(e,f,g):d(e,f,g)},f=function(c,d,e){var f,g={protocol:void 0,host:void 0,port:void 0,canOmitPort:!1},h=!1,i=!1;if(a.isEmptyString(c)&&(i=!0,f="Protocol or host not provided"),!i&&!a.isEmptyString(c)&&a.isEmptyString(d)&&a.isEmptyString(e)){var j=c.split("://");2==j.length?(g.protocol=j[0],g.host=j[1]):(g.host=c,g.protocol=b.config.net.defaults.protocol);var k=g.host.lastIndexOf(":");-1!==k?(g.port=g.host.substring(k+1,g.host.length),g.host=g.host.substring(0,k)):g.port=b.config.net.defaults.port[g.protocol],h=!0}if(!i&&!h&&!a.isEmptyString(c)&&!a.isEmptyString(d)&&a.isEmptyString(e)){g.protocol=c,g.host=d;var k=g.host.lastIndexOf(":");-1!==k?(g.port=g.host.substring(k+1,g.host.length),g.host=g.host.substring(0,k)):g.port=b.config.net.defaults.port[g.protocol],h=!0}if(i||h||a.isEmptyString(c)||a.isEmptyString(d)||a.isEmptyString(e)||(g.protocol=c,g.host=d,g.port=e,h=!0),!i&&h&&b.config.net.defaults.port[g.protocol]===g.port&&(g.canOmitPort=!0),i||h||(i=!0,f="Unable to handle input into method. Please open a GitHub issue with your input :)"),i===!0)throw b.InvalidParametersException("net",f);var l=function(a){var b=a.indexOf("/");return a.substring(0,-1===b?a.length:b)};return g.host=l(g.host),g.port=l(g.port),g};return{net:function(b,c,d){var g=f(b,c,d),h={get:function(b,c){var d=a.merge(b,{});d.method="get",e(g,d,c)},post:function(b,c){var d=a.merge(b,{});d.method="post",e(g,d,c)},put:function(b,c){var d=a.merge(b,{});d.method="put",e(g,d,c)},"delete":function(b,c){var d=a.merge(b,{});d.method="delete",e(g,d,c)},options:function(b,c){var d=a.merge(b,{});d.method="options",e(g,d,c)}};return Object.defineProperty(h,"protocol",{get:function(){return g.protocol}}),Object.defineProperty(h,"host",{get:function(){return g.host}}),Object.defineProperty(h,"port",{get:function(){return g.port}}),h}}}),msngr.extend(function(a,b){"use strict";return a.config("cross-window",{channel:"__msngr_cross-window"}),"undefined"==typeof localStorage||"undefined"==typeof window?{}:(window.addEventListener("storage",function(c){if(c.key===b.config["cross-window"].channel){var d;try{d=JSON.parse(c.newValue)}catch(e){throw"msngr was unable to parse the data in its storage channel"}void 0!==d&&a.isObject(d)&&b.objects.message(d.message).emit(d.payload)}}),b.option("cross-window",function(a,c,d,e){d=d||{},d=d["cross-window"]||{};var f={message:a,payload:c};try{localStorage.setItem(b.config["cross-window"].channel,JSON.stringify(f))}catch(g){throw"msngr was unable to store data in its storage channel"}}),{})}),msngr.extend(function(a,b){"use strict";return b.option("dom",function(b,c,d,e){d=d||{},d=d.dom||{};var f=d.doc||d.document||document,g=void 0;if(a.isObject(d)&&a.exist(d.selectors)&&a.isString(d.selectors)?g=[d.selectors]:a.isString(d)?g=[d]:a.isArray(d)&&(g=d),a.exist(f)&&a.exist(g)&&0!==g.length){for(var h=[],i=g.length,j=0;i>j;++j){var k=a.findElements(g[j],f);k.length>0&&(h=h.concat(Array.prototype.slice.call(k)))}if(0!==h.length){for(var l=void 0,m=h.length,n=0,j=0;m>j;++j){var o=void 0,p=h[j],q=p.getAttribute("name"),r=p.id,s=p.tagName.toLowerCase(),t=p.value;a.exist(q)&&!a.isEmptyString(q)?o=q:a.exist(r)&&!a.isEmptyString(r)?o=r:(o=s+n,n++),void 0===l&&(l={}),l[o]=t}return l}}}),{}}),"undefined"!=typeof module&&"undefined"!=typeof module.exports&&(module.exports=msngr);