UNPKG

soma.js

Version:

soma.js is a javascript framework created to build scalable and maintainable applications.

252 lines (224 loc) 7.89 kB
/* Copyright (c) | 2013 | soma-events | Romuald Quantin | www.soundstep.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ (function (soma) { 'use strict'; soma.events = {}; soma.events.version = '0.5.6'; if (!Function.prototype.bind) { Function.prototype.bind = function bind(that) { var target = this; if (typeof target !== 'function') { throw new Error('Error, you must bind a function.'); } var args = Array.prototype.slice.call(arguments, 1); // for normal call var bound = function () { if (this instanceof bound) { var F = function(){}; F.prototype = target.prototype; var self = new F(); var result = target.apply( self, args.concat(Array.prototype.slice.call(arguments)) ); if (Object(result) === result) { return result; } return self; } else { return target.apply( that, args.concat(Array.prototype.slice.call(arguments)) ); } }; return bound; }; } soma.Event = function (type, params, bubbles, cancelable) { var e = soma.Event.createGenericEvent(type, bubbles, cancelable); if (params !== null && params !== undefined) { e.params = params; } e.isCloned = false; e.clone = this.clone.bind(e); e.isIE9orIE10 = this.isIE9orIE10; e.isDefaultPrevented = this.isDefaultPrevented; if (this.isIE9orIE10() || !e.preventDefault || (e.getDefaultPrevented === undefined && e.defaultPrevented === undefined )) { e.preventDefault = this.preventDefault.bind(e); } if (this.isIE9orIE10()) { e.IE9or10PreventDefault = false; } return e; }; soma.Event.prototype.clone = function () { var e = soma.Event.createGenericEvent(this.type, this.bubbles, this.cancelable); e.params = this.params; e.isCloned = true; e.clone = this.clone; e.isDefaultPrevented = this.isDefaultPrevented; e.isIE9orIE10 = this.isIE9orIE10; if (this.isIE9orIE10()) { e.IE9or10PreventDefault = this.IE9or10PreventDefault; } return e; }; soma.Event.prototype.preventDefault = function () { if (!this.cancelable) { return false; } if (this.isIE9orIE10()) { this.IE9or10PreventDefault = true; } else { this.defaultPrevented = true; } return this; }; soma.Event.prototype.isDefaultPrevented = function () { if (!this.cancelable) { return false; } if (this.isIE9orIE10()) { return this.IE9or10PreventDefault; } if (this.defaultPrevented !== undefined) { return this.defaultPrevented; } else if (this.getDefaultPrevented !== undefined) { return this.getDefaultPrevented(); } return false; }; soma.Event.createGenericEvent = function (type, bubbles, cancelable) { var event; bubbles = bubbles !== undefined ? bubbles : true; if (typeof document === 'object' && document.createEvent) { event = document.createEvent('Event'); event.initEvent(type, !!bubbles, !!cancelable); } else if (typeof document === 'object' && document.createEventObject) { event = document.createEventObject(); event.type = type; event.bubbles = !!bubbles; event.cancelable = !!cancelable; } else { event = new EventObject(type, !!bubbles, !!cancelable); } return event; }; soma.Event.prototype.isIE9orIE10 = function() { if (typeof document !== 'object') { return false; } return (document.body.style.scrollbar3dLightColor !== undefined && document.body.style.opacity !== undefined) || document.body.style.msTouchAction !== undefined; }; soma.Event.prototype.toString = function() { return '[soma.Event]'; }; var EventObject = function(type, bubbles, cancelable) { this.type = type; this.bubbles = !!bubbles; this.cancelable = !!cancelable; this.defaultPrevented = false; this.currentTarget = null; this.target = null; }; soma.EventDispatcher = function () { this.listeners = []; }; soma.EventDispatcher.prototype.addEventListener = function(type, listener, priority) { if (!this.listeners || !type || !listener) { return; } if (isNaN(priority)) { priority = 0; } for (var i=0; i<this.listeners.length; i++) { var eventObj = this.listeners[i]; if (eventObj.type === type && eventObj.listener === listener) { return; } } this.listeners.push({type: type, listener: listener, priority: priority, scope:this}); }; soma.EventDispatcher.prototype.removeEventListener = function(type, listener) { if (!this.listeners || !type || !listener) { return; } var i = this.listeners.length; while(i-- > 0) { var eventObj = this.listeners[i]; if (eventObj.type === type && eventObj.listener === listener) { this.listeners.splice(i, 1); } } }; soma.EventDispatcher.prototype.hasEventListener = function(type) { if (!this.listeners || !type) { return false; } var i = 0; var l = this.listeners.length; for (; i < l; ++i) { var eventObj = this.listeners[i]; if (eventObj.type === type) { return true; } } return false; }; soma.EventDispatcher.prototype.dispatchEvent = function(event) { if (!this.listeners || !event) { throw new Error('Error in EventDispatcher (dispatchEvent), one of the parameters is null or undefined.'); } var events = []; var i; for (i = 0; i < this.listeners.length; i++) { var eventObj = this.listeners[i]; if (eventObj.type === event.type) { events.push(eventObj); } } events.sort(function(a, b) { return b.priority - a.priority; }); for (i = 0; i < events.length; i++) { events[i].listener.apply((event.srcElement) ? event.srcElement : event.currentTarget, [event]); } return !event.isDefaultPrevented(); }; soma.EventDispatcher.prototype.dispatch = function(type, params, bubbles, cancelable) { if (!this.listeners || !type || type === '') { throw new Error('Error in EventDispatcher (dispatch), one of the parameters is null or undefined.'); } var event = new soma.Event(type, params, bubbles, cancelable); this.dispatchEvent(event); return event; }; soma.EventDispatcher.prototype.dispose = function() { this.listeners = null; }; soma.EventDispatcher.prototype.toString = function() { return '[soma.EventDispatcher]'; }; // register for AMD module if (typeof define === 'function' && typeof define.amd !== 'undefined') { define("soma-events", soma); } // export for node.js if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { module.exports = soma; } if (typeof exports !== 'undefined') { exports = soma; } })(this['soma'] = this['soma'] || {});