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