forked from mistic100/uEvent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
microevent.min.js
7 lines (7 loc) · 2.72 KB
/
microevent.min.js
1
2
3
4
5
6
7
/*!
* MicroEvent - to make any js object an event emitter
* Copyright 2011 Jerome Etienne (http://jetienne.com)
* Copyright 2015 Damien "Mistic" Sorel (http://www.strangeplanet.fr)
* Licensed under MIT (http://opensource.org/licenses/MIT)
*/
(function(root,factory){if(typeof module!=="undefined"&&module.exports){module.exports=factory()}else if(typeof define==="function"&&define.amd){define([],factory)}else{root.MicroEvent=factory()}})(this,function(){"use strict";var MicroEvent=function(){};MicroEvent.prototype={on:function(events,fct){this._events=this._events||{};if(typeof events==="object"){for(var event in events){if(events.hasOwnProperty(event)){this._events[event]=this._events[event]||[];this._events[event].push(events[event])}}}else{events.split(" ").forEach(function(event){this._events[event]=this._events[event]||[];this._events[event].push(fct)},this)}return this},off:function(events,fct){this._events=this._events||{};if(typeof events==="object"){for(var event in events){if(events.hasOwnProperty(event)&&event in this._events){var index=this._events[event].indexOf(events[event]);if(index!==-1)this._events[event].splice(index,1)}}}else if(!!events){events.split(" ").forEach(function(event){if(event in this._events){if(fct){var index=this._events[event].indexOf(fct);if(index!==-1)this._events[event].splice(index,1)}else{this._events[event]=[]}}},this)}else{this._events={}}return this},once:function(events,fct){this._once=this._once||{};if(typeof events==="object"){for(var event in events){if(events.hasOwnProperty(event)){this._once[event]=this._once[event]||[];this._once[event].push(events[event])}}}else{events.split(" ").forEach(function(event){this._once[event]=this._once[event]||[];this._once[event].push(fct)},this)}return this},trigger:function(event){this._events=this._events||{};this._once=this._once||{};var args=Array.prototype.slice.call(arguments,1),callbacks;if(event in this._events){callbacks=this._events[event].slice();while(callbacks.length){callbacks.shift().apply(this,args)}}if(event in this._once){callbacks=this._once[event].slice();while(callbacks.length){callbacks.shift().apply(this,args)}delete this._once[event]}return this},change:function(event,value){this._events=this._events||{};if(event in this._events){var args=Array.prototype.slice.call(arguments,1);for(var i=0,l=this._events[event].length;i<l;i++){args[0]=value;value=this._events[event][i].apply(this,args)}}return value}};MicroEvent.mixin=function(obj,names){names=names||{};var props=["on","off","once","trigger","change"];for(var i=0,l=props.length;i<l;i++){var method=names[props[i]]||props[i];if(typeof obj==="function"){obj.prototype[method]=MicroEvent.prototype[props[i]]}else{obj[method]=MicroEvent.prototype[props[i]]}}};return MicroEvent});