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