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 ( 30 jQuery, 31 undefined 32 ) { 33 "use strict"; 34 35 var $ = jQuery; 36 37 return { 38 _eventHandlers: null, 39 40 /** 41 * Attach a handler to an event 42 * 43 * @param {String} eventType A string containing the event name to bind to 44 * @param {Function} handler A function to execute each time the event is triggered 45 * @param {Object} scope Optional. Set the scope in which handler is executed 46 */ 47 bind: function (eventType, handler, scope) { 48 this._eventHandlers = this._eventHandlers || {}; 49 if (!this._eventHandlers[eventType]) { 50 this._eventHandlers[eventType] = []; 51 } 52 this._eventHandlers[eventType].push({ 53 handler: handler, 54 scope: (scope || window) 55 }); 56 }, 57 58 /** 59 * Remove a previously-attached event handler 60 * 61 * @param {String} eventType A string containing the event name to unbind 62 * @param {Function} handler The function that is to be no longer executed. Optional. If not given, unregisters all functions for the given event. 63 */ 64 unbind: function (eventType, handler) { 65 this._eventHandlers = this._eventHandlers || {}; 66 if (!this._eventHandlers[eventType]) { 67 return; 68 } 69 if (!handler) { 70 // No handler function given, unbind all event handlers for the eventType 71 this._eventHandlers[eventType] = []; 72 } else { 73 this._eventHandlers[eventType] = $.grep(this._eventHandlers[eventType], function (element) { 74 75 if (element.handler === handler) { 76 return false; 77 } 78 return true; 79 }); 80 } 81 }, 82 83 /** 84 * Execute all handlers attached to the given event type. 85 * All arguments except the eventType are directly passed to the callback function. 86 * 87 * @param (String} eventType A string containing the event name for which the event handlers should be invoked. 88 */ 89 trigger: function (eventType) { 90 this._eventHandlers = this._eventHandlers || {}; 91 if (!this._eventHandlers[eventType]) { 92 return; 93 } 94 95 // preparedArguments contains all arguments except the first one. 96 var preparedArguments = []; 97 $.each(arguments, function (i, argument) { 98 if (i > 0) { 99 preparedArguments.push(argument); 100 } 101 }); 102 103 $.each(this._eventHandlers[eventType], function (index, element) { 104 element.handler.apply(element.scope, preparedArguments); 105 }); 106 }, 107 108 /** 109 * Clears all event handlers. Call this method when cleaning up. 110 */ 111 unbindAll: function () { 112 this._eventHandlers = null; 113 } 114 }; 115 }); 116