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