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  * recipients can access the Corresponding Source.
 26  */
 27 // Do not add dependencies that require depend on aloha/core
 28 define(['jquery', 'util/class'], function (jQuery, Class) {
 29 	"use strict";
 30 
 31 	var Aloha = window.Aloha;
 32 
 33 	/**
 34 	 * The Plugin Manager controls the lifecycle of all Aloha Plugins.
 35 	 *
 36 	 * @namespace Aloha
 37 	 * @class PluginManager
 38 	 * @singleton
 39 	 */
 40 	return new (Class.extend({
 41 		plugins: {},
 42 
 43 		/**
 44 		 * Initialize all registered plugins
 45 		 * @return void
 46 		 * @hide
 47 		 */
 48 		init: function (next, userPlugins) {
 49 			var me = this,
 50 				globalSettings = (Aloha && Aloha.settings) ? Aloha.settings.plugins || {} : {},
 51 				i,
 52 				plugin,
 53 				pluginName;
 54 
 55 			// Global to local settings
 56 			for (pluginName in globalSettings) {
 57 
 58 				if (globalSettings.hasOwnProperty(pluginName)) {
 59 
 60 					plugin = this.plugins[pluginName] || false;
 61 
 62 					if (plugin) {
 63 						plugin.settings = globalSettings[pluginName] || {};
 64 					}
 65 				}
 66 			}
 67 
 68 			// Default: All loaded plugins are enabled
 69 			if (!userPlugins.length) {
 70 
 71 				for (pluginName in this.plugins) {
 72 
 73 					if (this.plugins.hasOwnProperty(pluginName)) {
 74 						userPlugins.push(pluginName);
 75 					}
 76 				}
 77 			}
 78 
 79 			// Enable Plugins specified by User
 80 			for (i = 0; i < userPlugins.length; ++i) {
 81 
 82 				pluginName = userPlugins[i];
 83 				plugin = this.plugins[pluginName] || false;
 84 
 85 				if (plugin) {
 86 
 87 					plugin.settings = plugin.settings || {};
 88 
 89 					if (typeof plugin.settings.enabled === 'undefined') {
 90 						plugin.settings.enabled = true;
 91 					}
 92 
 93 					if (plugin.settings.enabled) {
 94 						if (plugin.checkDependencies()) {
 95 							plugin.init();
 96 						}
 97 					}
 98 				}
 99 			}
100 
101 			next();
102 		},
103 
104 		/**
105 106 		 * Register a plugin
107 		 * @param {Plugin} plugin plugin to register
108 		 */
109 		register: function (plugin) {
110 
111 			if (!plugin.name) {
112 				throw new Error('Plugin does not have an name.');
113 			}
114 
115 			if (this.plugins[plugin.name]) {
116 				throw new Error('Already registered the plugin "' + plugin.name + '"!');
117 			}
118 
119 			this.plugins[plugin.name] = plugin;
120 		},
121 
122 		/**
123 		 * Pass the given jQuery object, which represents an editable to all plugins, so that they can make the content clean (prepare for saving)
124 		 * @param obj jQuery object representing an editable
125 		 * @return void
126 		 * @hide
127 		 */
128 		makeClean: function (obj) {
129 			var i, plugin;
130 			// iterate through all registered plugins
131 			for (plugin in this.plugins) {
132 				if (this.plugins.hasOwnProperty(plugin)) {
133 					if (Aloha.Log.isDebugEnabled()) {
134 						Aloha.Log.debug(this, 'Passing contents of HTML Element with id { ' + obj.attr('id') + ' } for cleaning to plugin { ' + plugin + ' }');
135 					}
136 					this.plugins[plugin].makeClean(obj);
137 				}
138 			}
139 		},
140 
141 		/**
142 		 * Expose a nice name for the Plugin Manager
143 		 * @hide
144 		 */
145 		toString: function () {
146 			return 'pluginmanager';
147 		}
148 
149 	}))();
150 });
151