1 /*!
  2 * This file is part of Aloha Editor Project http://aloha-editor.org
  3 * Copyright © 2010-2011 Gentics Software GmbH, aloha@gentics.com
  4 * Contributors http://aloha-editor.org/contribution.php 
  5 * Licensed unter the terms of http://www.aloha-editor.org/license.html
  6 *//*
  7 * Aloha Editor is free software: you can redistribute it and/or modify
  8 * it under the terms of the GNU Affero General Public License as published by
  9 * the Free Software Foundation, either version 3 of the License, or
 10 * (at your option) 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 Affero General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Affero General Public License
 18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 19 */
 20 
 21 // Do not add dependencies that require depend on aloha/core
 22 define(
 23 [ 'aloha/jquery', 'util/class' ],
 24 function( jQuery, Class ) {
 25 	"use strict";
 26 	
 27 	/**
 28 	 * The Plugin Manager controls the lifecycle of all Aloha Plugins.
 29 	 *
 30 	 * @namespace Aloha
 31 	 * @class PluginManager
 32 	 * @singleton
 33 	 */
 34 	return new (Class.extend({
 35 		plugins: {},
 36 
 37 		/**
 38 		 * Initialize all registered plugins
 39 		 * @return void
 40 		 * @hide
 41 		 */
 42 		init: function(next, userPlugins) {
 43 
 44 			var
 45 				me = this,
 46 				globalSettings = ( Aloha && Aloha.settings ) ? Aloha.settings.plugins||{}: {},
 47 				i,
 48 				plugin,
 49 				pluginName;
 50 
 51 			// Global to local settings
 52 			for ( pluginName in globalSettings ) {
 53 				
 54 				if ( globalSettings.hasOwnProperty( pluginName ) ) {
 55 					
 56 					plugin = this.plugins[pluginName] || false;
 57 					
 58 					if ( plugin ) {
 59 						plugin.settings = globalSettings[ pluginName ] || {};
 60 					}
 61 				}
 62 			}
 63 
 64 			// Default: All loaded plugins are enabled
 65 			if ( !userPlugins.length ) {
 66 				
 67 				for ( pluginName in this.plugins ) {
 68 					
 69 					if ( this.plugins.hasOwnProperty( pluginName ) ) {
 70 						userPlugins.push( pluginName );
 71 					}
 72 				}
 73 			}
 74 			
 75 			// Enable Plugins specified by User
 76 			for ( i=0; i < userPlugins.length; ++i ) {
 77 				
 78 				pluginName = userPlugins[ i ];
 79 				plugin = this.plugins[ pluginName ]||false;
 80 				
 81 				if ( plugin ) {
 82 					
 83 					plugin.settings = plugin.settings || {};
 84 					
 85 					if ( typeof plugin.settings.enabled === 'undefined' ) {
 86 						plugin.settings.enabled = true;
 87 					}
 88 					
 89 					if ( plugin.settings.enabled ) {
 90 						if ( plugin.checkDependencies() ) {
 91 							plugin.init();
 92 						}
 93 					}
 94 				}
 95 			}
 96 			
 97 			next();
 98 		},
 99 
100 		/**
101 		 * Register a plugin
102 		 * @param {Plugin} plugin plugin to register
103 		 */
104 		register: function( plugin ) {
105 			
106 			if ( !plugin.name ) {
107 				throw new Error( 'Plugin does not have an name.' );
108 			}
109 			
110 			if ( this.plugins[ plugin.name ]) {
111 				throw new Error( 'Already registered the plugin "' + plugin.name  + '"!' );
112 			}
113 			
114 			this.plugins[ plugin.name ] = plugin;
115 		},
116 
117 		/**
118 		 * Pass the given jQuery object, which represents an editable to all plugins, so that they can make the content clean (prepare for saving)
119 		 * @param obj jQuery object representing an editable
120 		 * @return void
121 		 * @hide
122 		 */
123 		makeClean: function(obj) {
124 			var i, plugin;
125 			// iterate through all registered plugins
126 			for ( plugin in this.plugins ) {
127 				if ( this.plugins.hasOwnProperty( plugin ) ) {
128 					if (Aloha.Log.isDebugEnabled()) {
129 						Aloha.Log.debug(this, 'Passing contents of HTML Element with id { ' + obj.attr('id') +
130 										' } for cleaning to plugin { ' + plugin + ' }');
131 					}
132 					this.plugins[plugin].makeClean(obj);
133 				}
134 			}
135 		},
136 
137 		/**
138 		 * Expose a nice name for the Plugin Manager
139 		 * @hide
140 		 */
141 		toString: function() {
142 			return 'pluginmanager';
143 		}
144 		
145 	}))();
146 });