-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediator.min.js
12 lines (12 loc) · 2.65 KB
/
mediator.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/**
* mediator
* A JavaScript mediator pattern experiment.
*
* @author Fabien Doiron <fabien.doiron@gmail.com>
* @copyright Fabien Doiron 2012
* @license MIT <http://opensource.org/licenses/mit-license.php>
* @link http://www.github.com/fabien-d/mediator.js
* @module mediator
* @version 0.0.1
*/
(function(e,t){"use strict";var n=e.document,r;r=function(){var e,r,i,s,o;return e=function(e){var t=[8,9,"a","b"],n=function(){return((1+Math.random())*65536|0).toString(16).substring(1)},r="";if(typeof e!="undefined"&&typeof e!="boolean")throw new Error("safe must be a boolean");return r=(n()+n()+"-"+n()+"-4"+n().substr(0,3)+"-"+t[Math.floor(Math.random()*t.length)]+n().substr(0,3)+"-"+n()+n()+n()).toLowerCase(),e?"gen-"+r.replace(/-/g,""):r},r=function(e){var r,i,s;if(typeof e!="object"||e instanceof Array)throw new Error("parameter must be an object");for(r in e)if(e.hasOwnProperty(r)){i=r.trim().split(/^(.*?)\s/),i[0]===""&&(i=i.splice(1));if(i.length!==2)throw new Error("object key must contain an event and element");s=n.querySelector(i[1].trim())||t,typeof s!="undefined"&&(typeof s.addEventListener=="function"?s.addEventListener(i[0],e[r],!1):s.attachEvent&&s.attachEvent("on"+i[0],e[r]))}if(typeof i=="undefined")throw new Error("listen object must contain at least 1 item");return this},i=function(e,t,n){var r,i,s,o;if(typeof e!="string")throw new Error("type must be a string");if(t&&typeof t!="string")throw new Error("id must be a string");r=t&&t!==""?this.subscribers[e][t]:this.subscribers[e];if(typeof r=="undefined")return!1;if(t)r.callback.call(r.context,n);else for(s in r)o=r[s],r.hasOwnProperty(s)&&o.callback.call(o.context,n);return this},s=function(t,n,r,i){var s=r||e(!0);if(typeof t!="string")throw new Error("type must be a string");if(typeof n!="function")throw new Error("fn must be a function");if(typeof s!="string")throw new Error("id must be a string");return typeof this.subscribers[t]=="undefined"&&(this.subscribers[t]={}),typeof this.subscribers[t][s]=="undefined"&&(this.subscribers[t][s]={}),this.subscribers[t][s]={callback:n,context:i||this},this},o=function(e){var t,n,r;if(typeof e=="undefined")throw new Error("type must be a string or function");switch(typeof e){case"string":delete this.subscribers[e];break;case"function":for(r in this.subscribers)if(this.subscribers.hasOwnProperty(r)){t=this.subscribers[r];for(n in t)t.hasOwnProperty(n)&&t[n].callback===e&&delete t[n]}break;default:throw new Error("type must be a string or function")}return this},{genID:e,listen:r,publish:i,subscribers:{},subscribe:s,unsubscribe:o}},typeof define=="function"?define([],function(){return new r}):typeof e.mediator=="undefined"&&(e.mediator=new r)})(this);