1 /*! 2 * This file is part of Aloha Editor Project http://aloha-editor.org 3 * Copyright � 2010-2011 Gentics Software GmbH, aloha@gentics.com 4 * Contributors http://aloha-editor.org/contribution.php 5 * Licensed unter the terms of http://www.aloha-editor.org/license.html 6 *//* 7 * Aloha Editor is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU Affero General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version.* 11 * 12 * Aloha Editor is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU Affero General Public License for more details. 16 * 17 * You should have received a copy of the GNU Affero General Public License 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 define( 22 ['aloha/jquery'], 23 function(jQuery, undefined) { 24 "use strict"; 25 26 var 27 $ = jQuery; 28 29 return { 30 _eventHandlers: null, 31 32 /** 33 * Attach a handler to an event 34 * 35 * @param {String} eventType A string containing the event name to bind to 36 * @param {Function} handler A function to execute each time the event is triggered 37 38 * @param {Object} scope Optional. Set the scope in which handler is executed 39 */ 40 bind: function(eventType, handler, scope) { 41 this._eventHandlers = this._eventHandlers || {}; 42 if (!this._eventHandlers[eventType]) { 43 this._eventHandlers[eventType] = []; 44 } 45 this._eventHandlers[eventType].push({ 46 handler: handler, 47 scope: (scope ? scope : window) 48 }); 49 }, 50 51 /** 52 * Remove a previously-attached event handler 53 * 54 * @param {String} eventType A string containing the event name to unbind 55 * @param {Function} handler The function that is to be no longer executed. Optional. If not given, unregisters all functions for the given event. 56 */ 57 unbind: function(eventType, handler) { 58 this._eventHandlers = this._eventHandlers || {}; 59 if (!this._eventHandlers[eventType]) { 60 return; 61 } 62 if (!handler) { 63 // No handler function given, unbind all event handlers for the eventType 64 this._eventHandlers[eventType] = []; 65 } else { 66 this._eventHandlers[eventType] = $.grep(this._eventHandlers[eventType], function(element) { 67 if (element.handler === handler) { 68 return false; 69 } 70 return true; 71 }); 72 } 73 }, 74 75 /** 76 * Execute all handlers attached to the given event type. 77 * All arguments except the eventType are directly passed to the callback function. 78 * 79 * @param (String} eventType A string containing the event name for which the event handlers should be invoked. 80 */ 81 trigger: function(eventType) { 82 this._eventHandlers = this._eventHandlers || {}; 83 if (!this._eventHandlers[eventType]) { 84 return; 85 } 86 87 88 // preparedArguments contains all arguments except the first one. 89 var preparedArguments = []; 90 $.each(arguments, function(i, argument) { 91 if (i>0) { 92 preparedArguments.push(argument); 93 } 94 }); 95 96 $.each(this._eventHandlers[eventType], function(index, element) { 97 element.handler.apply(element.scope, preparedArguments); 98 }); 99 }, 100 101 /** 102 * Clears all event handlers. Call this method when cleaning up. 103 */ 104 unbindAll: function() { 105 this._eventHandlers = null; 106 } 107 }; 108 });