1 /* registry.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 /*global define:true */
 28 /**
 29  * Registry base class.
 30  * TODO: document that it also contains Observable.
 31  *
 32  */
 33 define(
 34 ['jquery', 'aloha/observable', 'util/class'],
 35 function (jQuery, Observable, Class) {
 36 	"use strict";
 37 
 38 	return Class.extend(Observable, {
 39 
 40 		/**
 41 		 * Object containing the registered entries by key.
 42 		 */
 43 		_entries: null,
 44 
 45 		/**
 46 		 * Array containing the registered ids in order
 47 		 * of registry
 48 		 */
 49 		_ids: null,
 50 
 51 		_constructor: function () {
 52 			this._entries = {};
 53 			this._ids = [];
 54 		},
 55 
 56 		/**
 57 		 * Register an entry with an id
 58 		 * 
 59 		 * @event register
 60 		 * @param id id of the registered entry
 61  62 		 * @param entry registered entry
 63 		 */
 64 		register: function (id, entry) {
 65 			// TODO check whether an entry with the id is already registered
 66 			this._entries[id] = entry;
 67 			this._ids.push(id);
 68 			this.trigger('register', entry, id);
 69 		},
 70 
 71 		/**
 72 		 * Unregister the entry with given id
 73 		 * 
 74 		 * @event unregister
 75 		 * @param id id of the registered entry
 76 		 */
 77 		unregister: function (id) {
 78 			// TODO check whether an entry was registered
 79 			var i, oldEntry = this._entries[id];
 80 			delete this._entries[id];
 81 			for (i in this._ids) {
 82 				if (this._ids.hasOwnProperty(i) && this._ids[i] === id) {
 83 					this._ids.splice(i, 1);
 84 					break;
 85 				}
 86 			}
 87 			this.trigger('unregister', oldEntry, id);
 88 		},
 89 
 90 		/**
 91 		 * Get the entry registered with the given id
 92 		 * 
 93 		 * @param id id of the registered entry
 94 		 * @return registered entry
 95 		 */
 96 		get: function (id) {
 97 			return this._entries[id];
 98 		},
 99 
100 		/**
101 		 * Check whether an entry was registered with given id
102 		 * 
103 		 * @param id id to check
104 		 * @return true if an entry was registered, false if not
105 		 */
106 		has: function (id) {
107 			return (this._entries[id] ? true : false);
108 		},
109 
110 		/**
111 		 * Get an object mapping the ids (properties) to the registered entries
112 		 * Note, that iterating over the properties of the returned object
113 		 * will return the entries in an unspecified order
114 		 * 
115 		 * @return object containing the registered entries
116 		 */
117 		getEntries: function () {
118 			// clone the entries so the user does not accidentally modify our _entries object.
119 			return jQuery.extend({}, this._entries);
120 		},
121 
122 		/**
123 		 * Get the ids of the registered objects as array.
124 		 * The array will contain the ids in order of registry
125 		 * 
126 		 * @return array if registered ids
127 		 */
128 		getIds: function () {
129 			return jQuery.extend([], this._ids);
130 		}
131 	});
132 });
133