1 /* observable.js is part of Aloha Editor project http://aloha-editor.org 2 * 3 * Aloha Editor is a WYSIWYG HTML5 inline editing library and editor. 4 * Copyright (c) 2010-2012 Gentics Software GmbH, Vienna, Austria. 5 * Contributors http://aloha-editor.org/contribution.php 6 * 7 * Aloha Editor is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * as published by the Free Software Foundation; either version 2 10 * of the License, or 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 General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 * 21 * As an additional permission to the GNU GPL version 2, you may distribute 22 * non-source (e.g., minimized or compacted) forms of the Aloha-Editor 23 * source code without the copy of the GNU GPL normally required, 24 * provided you include this license notice and a URL through which 25 * recipients can access the Corresponding Source. 26 */ 27 define( 28 ['jquery'], 29 function(jQuery, undefined) { 30 "use strict"; 31 32 var 33 $ = jQuery; 34 35 return { 36 _eventHandlers: null, 37 38 /** 39 * Attach a handler to an event 40 * 41 * @param {String} eventType A string containing the event name to bind to 42 * @param {Function} handler A function to execute each time the event is triggered 43 * @param {Object} scope Optional. Set the scope in which handler is executed 44 */ 45 bind: function(eventType, handler, scope) { 46 this._eventHandlers = this._eventHandlers || {}; 47 if (!this._eventHandlers[eventType]) { 48 this._eventHandlers[eventType] = []; 49 } 50 this._eventHandlers[eventType].push({ 51 handler: handler, 52 scope: (scope ? scope : window) 53 }); 54 }, 55 56 /** 57 * Remove a previously-attached event handler 58 * 59 * @param {String} eventType A string containing the event name to unbind 60 * @param {Function} handler The function that is to be no longer executed. Optional. If not given, unregisters all functions for the given event. 61 */ 62 unbind: function(eventType, handler) { 63 this._eventHandlers = this._eventHandlers || {}; 64 if (!this._eventHandlers[eventType]) { 65 return; 66 } 67 if (!handler) { 68 // No handler function given, unbind all event handlers for the eventType 69 this._eventHandlers[eventType] = []; 70 } else { 71 this._eventHandlers[eventType] = $.grep(this._eventHandlers[eventType], function(element) { 72 if (element.handler === handler) { 73 return false; 74 } 75 return true; 76 }); 77 } 78 }, 79 80 /** 81 * Execute all handlers attached to the given event type. 82 * All arguments except the eventType are directly passed to the callback function. 83 * 84 * @param (String} eventType A string containing the event name for which the event handlers should be invoked. 85 */ 86 trigger: function(eventType) { 87 this._eventHandlers = this._eventHandlers || {}; 88 if (!this._eventHandlers[eventType]) { 89 return; 90 } 91 92 // preparedArguments contains all arguments except the first one. 93 var preparedArguments = []; 94 $.each(arguments, function(i, argument) { 95 if (i>0) { 96 preparedArguments.push(argument); 97 } 98 }); 99 100 $.each(this._eventHandlers[eventType], function(index, element) { 101 element.handler.apply(element.scope, preparedArguments); 102 }); 103 }, 104 105 /** 106 * Clears all event handlers. Call this method when cleaning up. 107 */ 108 unbindAll: function() { 109 this._eventHandlers = null; 110 } 111 }; 112 });