1 /* plugin.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 define( 28 ['aloha/core', 'jquery', 'util/class', 'aloha/pluginmanager', 'aloha/console'], 29 function(Aloha, jQuery, Class, PluginManager, console ) { 30 "use strict"; 31 32 /** 33 * Abstract Plugin Object 34 * @namespace Aloha 35 * @class Plugin 36 * @constructor 37 * @param {String} pluginPrefix unique plugin prefix 38 */ 39 var Plugin = Class.extend({ 40 41 name: null, 42 43 /** 44 * contains the plugin's default settings object 45 * @cfg {Object} default settings for the plugin 46 */ 47 defaults: {}, 48 49 /** 50 * contains the plugin's settings object 51 * @cfg {Object} settings the plugins settings stored in an object 52 */ 53 settings: {}, 54 55 /** 56 * Names of other plugins which must be loaded in order for this plugin to 57 * function. 58 * @cfg {Array} 59 */ 60 dependencies: [], 61 62 _constructor: function( name ) { 63 /** 64 * Settings of the plugin 65 */ 66 if (typeof name !== "string") { 67 console.error('Cannot initialise unnamed plugin, skipping'); 68 } else { 69 this.name = name; 70 } 71 }, 72 73 /** 74 * @return true if dependencies satisfied, false otherwise 75 */ 76 checkDependencies: function() { 77 var 78 dependenciesSatisfied = true, 79 that = this; 80 81 jQuery.each(this.dependencies, function() { 82 83 if (!Aloha.isPluginLoaded(this)) { 84 dependenciesSatisfied = false; 85 console.error('plugin.' + that.name, 'Required plugin "' + this + '" not found.'); 86 } 87 }); 88 89 return dependenciesSatisfied; 90 }, 91 92 /** 93 * Init method of the plugin. Called from Aloha Core to initialize this plugin 94 * @return void 95 * @hide 96 */ 97 init: function() {}, 98 99 /** 100 * Get the configuration settings for an editable obj. 101 * Handles both conf arrays or conf objects 102 * <ul> 103 * <li>Array configuration parameters are: 104 * <pre> 105 * "list": { 106 * config : [ 'b', 'h1' ], 107 * editables : { 108 * '#title' : [ ], 109 * 'div' : [ 'b', 'i' ], 110 * '.article' : [ 'h1' ] 111 * } 112 * } 113 * </pre> 114 * 115 * The hash keys of the editables are css selectors. For a 116 * 117 * <pre> 118 * <div class="article">content</div> 119 * </pre> 120 * 121 * the selectors 'div' and '.article' match and the returned configuration is 122 * 123 * <pre> 124 * [ 'b', 'i', 'h1'] 125 * </pre> 126 * 127 * The '#title' object would return an empty configuration. 128 * 129 * <pre> 130 * [ ] 131 * </pre> 132 * 133 * All other objects would get the 'config' configuration. If config is not set 134 * the plugin default configuration is returned. 135 * 136 * <pre> 137 * [ 'b', 'h1'] 138 * </pre></li> 139 * <li>Object configuration parameters are : 140 * <pre> 141 * "image": { 142 * config : { 'img': { 'max_width': '50px', 143 * 'max_height': '50px' }}, 144 * editables : { 145 * '#title': {}, 146 * 'div': {'img': {}}, 147 * '.article': {'img': { 'max_width': '150px', 148 * 'max_height': '150px' }} 149 * } 150 * } 151 * </pre> 152 * The '#title' object would return an empty configuration.<br/> 153 * The 'div' object would return the default configuration.<br/> 154 * the '.article' would return : 155 * <pre> 156 * {'img': { 'max_width': '150px', 157 * 'max_height': '150px' }} 158 * </pre> 159 * </li> 160 * 161 * @param {jQuery} obj jQuery object of an Editable Object 162 * @return {Array} config A Array with configuration entries 163 */ 164 getEditableConfig: function (obj) { 165 var configObj = null, 166 configSpecified = false, 167 that = this; 168 169 if ( this.settings.editables ) { 170 // check if the editable's selector matches and if so add its configuration to object configuration 171 jQuery.each( this.settings.editables, function (selector, selectorConfig) { 172 if ( obj.is(selector) ) { 173 configSpecified = true; 174 if (selectorConfig instanceof Array) { 175 configObj = []; 176 configObj = jQuery.merge(configObj, selectorConfig); 177 } else if (typeof selectorConfig === "object") { 178 configObj = {}; 179 configObj['aloha-editable-selector'] = selector; 180 for (var k in selectorConfig) { 181 if ( selectorConfig.hasOwnProperty(k) ) { 182 if (selectorConfig[k] instanceof Array) { 183 //configObj[k] = []; 184 //configObj[k] = jQuery.extend(true, configObj[k], that.config[k], selectorConfig[k]); 185 configObj[k] = selectorConfig[k]; 186 } else if (typeof selectorConfig[k] === "object") { 187 configObj[k] = {}; 188 configObj[k] = jQuery.extend(true, configObj[k], that.config[k], selectorConfig[k]); 189 } else { 190 configObj[k] = selectorConfig[k]; 191 } 192 193 } 194 } 195 } else { 196 configObj = selectorConfig; 197 } 198 } 199 }); 200 } 201 202 // fall back to default configuration 203 if ( !configSpecified ) { 204 if ( typeof this.settings.config === 'undefined' || !this.settings.config ) { 205 configObj = this.config; 206 } else { 207 configObj = this.settings.config; 208 } 209 } 210 211 return configObj; 212 }, 213 214 /** 215 * Make the given jQuery object (representing an editable) clean for saving 216 * @param obj jQuery object to make clean 217 * @return void 218 */ 219 makeClean: function ( obj ) {}, 220 221 /** 222 * Make a system-wide unique id out of a plugin-wide unique id by prefixing it with the plugin prefix 223 * @param id plugin-wide unique id 224 * @return system-wide unique id 225 * @hide 226 * @deprecated 227 */ 228 getUID: function(id) { 229 console.deprecated ('plugin', 'getUID() is deprecated. Use plugin.name instead.'); 230 return this.name; 231 }, 232 233 /** 234 * Return string representation of the plugin, which is the prefix 235 * @return name 236 * @hide 237 * @deprecated 238 */ 239 toString: function() { 240 return this.name; 241 }, 242 243 /** 244 * Log a plugin message to the logger 245 * @param level log level 246 * @param message log message 247 * @return void 248 * @hide 249 * @deprecated 250 */ 251 log: function (level, message) { 252 console.deprecated ('plugin', 'log() is deprecated. Use Aloha.console instead.'); 253 console.log(level, this, message); 254 } 255 }); 256 257 /** 258 * Static method used as factory to create plugins. 259 * 260 * @param {String} pluginName name of the plugin 261 * @param {Object} definition definition of the plugin, should have at least an "init" and "destroy" method. 262 */ 263 Plugin.create = function(pluginName, definition) { 264 265 var pluginInstance = new ( Plugin.extend( definition ) )( pluginName ); 266 pluginInstance.settings = jQuery.extendObjects( true, pluginInstance.defaults, Aloha.settings[pluginName] ); 267 PluginManager.register( pluginInstance ); 268 269 return pluginInstance; 270 }; 271 272 return Plugin; 273 }); 274