1 /* pluginmanager.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 26 * recipients can access the Corresponding Source. 27 */ 28 // Do not add dependencies that require depend on aloha/core 29 define([ 30 'jquery', 31 'util/class' 32 ], function ( 33 $, 34 Class 35 ) { 36 'use strict'; 37 38 var Aloha = window.Aloha; 39 40 /** 41 * For each plugin setting, assigns it into its corresponding plugin. 42 * 43 * @param {Array.<Plugin>} plugins 44 * @param {object<string, object>} settings 45 */ 46 function mapSettingsIntoPlugins(plugins, settings) { 47 var plugin; 48 for (plugin in settings) { 49 if (settings.hasOwnProperty(plugin) && plugins[plugin]) { 50 plugins[plugin].settings = settings[plugin] || {}; 51 } 52 } 53 } 54 55 /** 56 * Retrieves a set of plugins or the given `names' list, from among those 57 * specified in `plugins'. 58 * 59 * @param {object<string, object>} plugins 60 * @param {Array.<string>} names List of plugins names. 61 * @return {Array.<Plugins>} List of available plugins. 62 */ 63 function getPlugins(plugins, names) { 64 var available = []; 65 var plugin; 66 var i; 67 for (i = 0; i < names.length; i++) { 68 plugin = plugins[names[i]]; 69 if (plugin) { 70 available.push(plugin); 71 } 72 } 73 return available; 74 } 75 76 /** 77 * Initializes the plugins in the given list. 78 * 79 * @param {Array.<Plugins>} plugins Plugins to initialize. 80 * @param {function} callback Function to invoke once all plugins have been 81 * successfully initialized. 82 */ 83 function initializePlugins(plugins, callback) { 84 var numToEnable = plugins.length; 85 var onInit = function () { 86 if (0 === --numToEnable && callback) { 87 callback(); 88 } 89 }; 90 var i; 91 var ret; 92 var plugin; 93 for (i = 0; i < plugins.length; i++) { 94 plugin = plugins[i]; 95 plugin.settings = plugin.settings || {}; 96 if (typeof plugin.settings.enabled === 'undefined') { 97 plugin.settings.enabled = true; 98 } 99 if (plugin.settings.enabled && plugin.checkDependencies()) { 100 ret = plugin.init(); 101 if (ret && typeof ret.done === 'function') { 102 ret.done(onInit); 103 } else { 104 onInit(); 105 } 106 } 107 } 108 } 109 110 /** 111 * The Plugin Manager controls the lifecycle of all Aloha Plugins. 112 * 113 * @namespace Aloha 114 * @class PluginManager 115 * @singleton 116 */ 117 return new (Class.extend({ 118 119 plugins: {}, 120 121 /** 122 * Initialize all registered plugins. 123 * 124 * @param {function} next Callback to invoke after plugins have 125 * succefully initialized. 126 * @param {Array.<string>} enabled A list of plugin names which are to 127 * be enable. 128 */ 129 init: function (next, enabled) { 130 var manager = this; 131 var plugins = manager.plugins; 132 133 mapSettingsIntoPlugins(plugins, 134 Aloha && Aloha.settings && Aloha.settings.plugins); 135 136 // Because all plugins are enabled by default if specific plugins 137 // are not specified. 138 var plugin; 139 if (!plugins || 0 === enabled.length) { 140 enabled = []; 141 for (plugin in plugins) { 142 if (plugins.hasOwnProperty(plugin)) { 143 enabled.push(plugin); 144 } 145 } 146 } 147 148 initializePlugins(getPlugins(plugins, enabled), next); 149 }, 150 151 /** 152 * Register a plugin 153 * @param {Plugin} plugin plugin to register 154 */ 155 register: function (plugin) { 156 157 if (!plugin.name) { 158 throw new Error('Plugin does not have an name.'); 159 } 160 161 if (this.plugins[plugin.name]) { 162 throw new Error('Already registered the plugin "' + plugin.name + '"!'); 163 } 164 165 this.plugins[plugin.name] = plugin; 166 }, 167 168 /** 169 * Pass the given jQuery object, which represents an editable to all plugins, so that they can make the content clean (prepare for saving) 170 * @param obj jQuery object representing an editable 171 * @return void 172 * @hide 173 */ 174 makeClean: function (obj) { 175 var i, plugin; 176 // iterate through all registered plugins 177 for (plugin in this.plugins) { 178 if (this.plugins.hasOwnProperty(plugin)) { 179 if (Aloha.Log.isDebugEnabled()) { 180 Aloha.Log.debug(this, 'Passing contents of HTML Element with id { ' + obj.attr('id') + ' } for cleaning to plugin { ' + plugin + ' }'); 181 } 182 this.plugins[plugin].makeClean(obj); 183 } 184 } 185 }, 186 187 /** 188 * Expose a nice name for the Plugin Manager 189 * @hide 190 */ 191 toString: function () { 192 return 'pluginmanager'; 193 } 194 195 }))(); 196 }); 197