2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @fileOverview Defines the {@link CKEDITOR.editor} class, which represents an
\r
13 // The counter for automatic instance names.
\r
14 var nameCounter = 0;
\r
16 var getNewName = function()
\r
18 var name = 'editor' + ( ++nameCounter );
\r
19 return ( CKEDITOR.instances && CKEDITOR.instances[ name ] ) ? getNewName() : name;
\r
22 // ##### START: Config Privates
\r
24 // These function loads custom configuration files and cache the
\r
25 // CKEDITOR.editorConfig functions defined on them, so there is no need to
\r
26 // download them more than once for several instances.
\r
27 var loadConfigLoaded = {};
\r
28 var loadConfig = function( editor )
\r
30 var customConfig = editor.config.customConfig;
\r
32 // Check if there is a custom config to load.
\r
33 if ( !customConfig )
\r
36 customConfig = CKEDITOR.getUrl( customConfig );
\r
38 var loadedConfig = loadConfigLoaded[ customConfig ] || ( loadConfigLoaded[ customConfig ] = {} );
\r
40 // If the custom config has already been downloaded, reuse it.
\r
41 if ( loadedConfig.fn )
\r
43 // Call the cached CKEDITOR.editorConfig defined in the custom
\r
44 // config file for the editor instance depending on it.
\r
45 loadedConfig.fn.call( editor, editor.config );
\r
47 // If there is no other customConfig in the chain, fire the
\r
48 // "configLoaded" event.
\r
49 if ( CKEDITOR.getUrl( editor.config.customConfig ) == customConfig || !loadConfig( editor ) )
\r
50 editor.fireOnce( 'customConfigLoaded' );
\r
54 // Load the custom configuration file.
\r
55 CKEDITOR.scriptLoader.load( customConfig, function()
\r
57 // If the CKEDITOR.editorConfig function has been properly
\r
58 // defined in the custom configuration file, cache it.
\r
59 if ( CKEDITOR.editorConfig )
\r
60 loadedConfig.fn = CKEDITOR.editorConfig;
\r
62 loadedConfig.fn = function(){};
\r
64 // Call the load config again. This time the custom
\r
65 // config is already cached and so it will get loaded.
\r
66 loadConfig( editor );
\r
73 var initConfig = function( editor, instanceConfig )
\r
75 // Setup the lister for the "customConfigLoaded" event.
\r
76 editor.on( 'customConfigLoaded', function()
\r
78 if ( instanceConfig )
\r
80 // Register the events that may have been set at the instance
\r
81 // configuration object.
\r
82 if ( instanceConfig.on )
\r
84 for ( var eventName in instanceConfig.on )
\r
86 editor.on( eventName, instanceConfig.on[ eventName ] );
\r
90 // Overwrite the settings from the in-page config.
\r
91 CKEDITOR.tools.extend( editor.config, instanceConfig, true );
\r
93 delete editor.config.on;
\r
96 onConfigLoaded( editor );
\r
99 // The instance config may override the customConfig setting to avoid
\r
100 // loading the default ~/config.js file.
\r
101 if ( instanceConfig && instanceConfig.customConfig != undefined )
\r
102 editor.config.customConfig = instanceConfig.customConfig;
\r
104 // Load configs from the custom configuration files.
\r
105 if ( !loadConfig( editor ) )
\r
106 editor.fireOnce( 'customConfigLoaded' );
\r
109 // ##### END: Config Privates
\r
111 var onConfigLoaded = function( editor )
\r
113 // Set config related properties.
\r
115 var skin = editor.config.skin.split( ',' ),
\r
116 skinName = skin[ 0 ],
\r
117 skinPath = CKEDITOR.getUrl( skin[ 1 ] || (
\r
118 '_source/' + // @Packager.RemoveLine
\r
119 'skins/' + skinName + '/' ) );
\r
121 editor.skinName = skinName;
\r
122 editor.skinPath = skinPath;
\r
123 editor.skinClass = 'cke_skin_' + skinName;
\r
125 // Fire the "configLoaded" event.
\r
126 editor.fireOnce( 'configLoaded' );
\r
128 // Load language file.
\r
129 loadSkin( editor );
\r
132 var loadLang = function( editor )
\r
134 CKEDITOR.lang.load( editor.config.language, editor.config.defaultLanguage, function( languageCode, lang )
\r
136 editor.langCode = languageCode;
\r
138 // As we'll be adding plugin specific entries that could come
\r
139 // from different language code files, we need a copy of lang,
\r
140 // not a direct reference to it.
\r
141 editor.lang = CKEDITOR.tools.prototypedCopy( lang );
\r
143 // We're not able to support RTL in Firefox 2 at this time.
\r
144 if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 && editor.lang.dir == 'rtl' )
\r
145 editor.lang.dir = 'ltr';
\r
147 loadPlugins( editor );
\r
151 var loadPlugins = function( editor )
\r
153 var config = editor.config,
\r
154 plugins = config.plugins,
\r
155 extraPlugins = config.extraPlugins,
\r
156 removePlugins = config.removePlugins;
\r
158 if ( extraPlugins )
\r
160 // Remove them first to avoid duplications.
\r
161 var removeRegex = new RegExp( '(?:^|,)(?:' + extraPlugins.replace( /\s*,\s*/g, '|' ) + ')(?=,|$)' , 'g' );
\r
162 plugins = plugins.replace( removeRegex, '' );
\r
164 plugins += ',' + extraPlugins;
\r
167 if ( removePlugins )
\r
169 removeRegex = new RegExp( '(?:^|,)(?:' + removePlugins.replace( /\s*,\s*/g, '|' ) + ')(?=,|$)' , 'g' );
\r
170 plugins = plugins.replace( removeRegex, '' );
\r
173 // Load all plugins defined in the "plugins" setting.
\r
174 CKEDITOR.plugins.load( plugins.split( ',' ), function( plugins )
\r
176 // The list of plugins.
\r
177 var pluginsArray = [];
\r
179 // The language code to get loaded for each plugin. Null
\r
180 // entries will be appended for plugins with no language files.
\r
181 var languageCodes = [];
\r
183 // The list of URLs to language files.
\r
184 var languageFiles = [];
\r
186 // Cache the loaded plugin names.
\r
187 editor.plugins = plugins;
\r
189 // Loop through all plugins, to build the list of language
\r
190 // files to get loaded.
\r
191 for ( var pluginName in plugins )
\r
193 var plugin = plugins[ pluginName ],
\r
194 pluginLangs = plugin.lang,
\r
195 pluginPath = CKEDITOR.plugins.getPath( pluginName ),
\r
198 // Set the plugin path in the plugin.
\r
199 plugin.path = pluginPath;
\r
201 // If the plugin has "lang".
\r
204 // Resolve the plugin language. If the current language
\r
205 // is not available, get the first one (default one).
\r
206 lang = ( CKEDITOR.tools.indexOf( pluginLangs, editor.langCode ) >= 0 ? editor.langCode : pluginLangs[ 0 ] );
\r
208 if ( !plugin.lang[ lang ] )
\r
210 // Put the language file URL into the list of files to
\r
212 languageFiles.push( CKEDITOR.getUrl( pluginPath + 'lang/' + lang + '.js' ) );
\r
216 CKEDITOR.tools.extend( editor.lang, plugin.lang[ lang ] );
\r
221 // Save the language code, so we know later which
\r
222 // language has been resolved to this plugin.
\r
223 languageCodes.push( lang );
\r
225 pluginsArray.push( plugin );
\r
228 // Load all plugin specific language files in a row.
\r
229 CKEDITOR.scriptLoader.load( languageFiles, function()
\r
231 // Initialize all plugins that have the "beforeInit" and "init" methods defined.
\r
232 var methods = [ 'beforeInit', 'init', 'afterInit' ];
\r
233 for ( var m = 0 ; m < methods.length ; m++ )
\r
235 for ( var i = 0 ; i < pluginsArray.length ; i++ )
\r
237 var plugin = pluginsArray[ i ];
\r
239 // Uses the first loop to update the language entries also.
\r
240 if ( m === 0 && languageCodes[ i ] && plugin.lang )
\r
241 CKEDITOR.tools.extend( editor.lang, plugin.lang[ languageCodes[ i ] ] );
\r
243 // Call the plugin method (beforeInit and init).
\r
244 if ( plugin[ methods[ m ] ] )
\r
245 plugin[ methods[ m ] ]( editor );
\r
249 // Load the editor skin.
\r
250 editor.fire( 'pluginsLoaded' );
\r
251 loadTheme( editor );
\r
256 var loadSkin = function( editor )
\r
258 CKEDITOR.skins.load( editor, 'editor', function()
\r
260 loadLang( editor );
\r
264 var loadTheme = function( editor )
\r
266 var theme = editor.config.theme;
\r
267 CKEDITOR.themes.load( theme, function()
\r
269 var editorTheme = editor.theme = CKEDITOR.themes.get( theme );
\r
270 editorTheme.path = CKEDITOR.themes.getPath( theme );
\r
271 editorTheme.build( editor );
\r
273 if ( editor.config.autoUpdateElement )
\r
274 attachToForm( editor );
\r
278 var attachToForm = function( editor )
\r
280 var element = editor.element;
\r
282 // If are replacing a textarea, we must
\r
283 if ( editor.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE && element.is( 'textarea' ) )
\r
285 var form = element.$.form && new CKEDITOR.dom.element( element.$.form );
\r
288 function onSubmit()
\r
290 editor.updateElement();
\r
292 form.on( 'submit',onSubmit );
\r
294 // Setup the submit function because it doesn't fire the
\r
296 if ( !form.$.submit.nodeName )
\r
298 form.$.submit = CKEDITOR.tools.override( form.$.submit, function( originalSubmit )
\r
302 editor.updateElement();
\r
304 // For IE, the DOM submit function is not a
\r
305 // function, so we need thid check.
\r
306 if ( originalSubmit.apply )
\r
307 originalSubmit.apply( this, arguments );
\r
314 // Remove 'submit' events registered on form element before destroying.(#3988)
\r
315 editor.on( 'destroy', function()
\r
317 form.removeListener( 'submit', onSubmit );
\r
323 function updateCommandsMode()
\r
326 commands = this._.commands,
\r
329 for ( var name in commands )
\r
331 command = commands[ name ];
\r
332 command[ command.modes[ mode ] ? 'enable' : 'disable' ]();
\r
337 * Initializes the editor instance. This function is called by the editor
\r
338 * contructor (editor_basic.js).
\r
341 CKEDITOR.editor.prototype._init = function()
\r
343 // Get the properties that have been saved in the editor_base
\r
345 var element = CKEDITOR.dom.element.get( this._.element ),
\r
346 instanceConfig = this._.instanceConfig;
\r
347 delete this._.element;
\r
348 delete this._.instanceConfig;
\r
350 this._.commands = {};
\r
351 this._.styles = [];
\r
354 * The DOM element that has been replaced by this editor instance. This
\r
355 * element holds the editor data on load and post.
\r
356 * @name CKEDITOR.editor.prototype.element
\r
357 * @type CKEDITOR.dom.element
\r
359 * var editor = CKEDITOR.instances.editor1;
\r
360 * alert( <b>editor.element</b>.getName() ); "textarea"
\r
362 this.element = element;
\r
365 * The editor instance name. It hay be the replaced element id, name or
\r
366 * a default name using a progressive counter (editor1, editor2, ...).
\r
367 * @name CKEDITOR.editor.prototype.name
\r
370 * var editor = CKEDITOR.instances.editor1;
\r
371 * alert( <b>editor.name</b> ); "editor1"
\r
373 this.name = ( element && ( this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
\r
374 && ( element.getId() || element.getNameAtt() ) )
\r
377 if ( this.name in CKEDITOR.instances )
\r
378 throw '[CKEDITOR.editor] The instance "' + this.name + '" already exists.';
\r
381 * The configurations for this editor instance. It inherits all
\r
382 * settings defined in (@link CKEDITOR.config}, combined with settings
\r
383 * loaded from custom configuration files and those defined inline in
\r
384 * the page when creating the editor.
\r
385 * @name CKEDITOR.editor.prototype.config
\r
388 * var editor = CKEDITOR.instances.editor1;
\r
389 * alert( <b>editor.config.theme</b> ); "default" e.g.
\r
391 this.config = CKEDITOR.tools.prototypedCopy( CKEDITOR.config );
\r
394 * Namespace containing UI features related to this editor instance.
\r
395 * @name CKEDITOR.editor.prototype.ui
\r
396 * @type CKEDITOR.ui
\r
399 this.ui = new CKEDITOR.ui( this );
\r
402 * Controls the focus state of this editor instance. This property
\r
403 * is rarely used for normal API operations. It is mainly
\r
404 * destinated to developer adding UI elements to the editor interface.
\r
405 * @name CKEDITOR.editor.prototype.focusManager
\r
406 * @type CKEDITOR.focusManager
\r
409 this.focusManager = new CKEDITOR.focusManager( this );
\r
411 CKEDITOR.fire( 'instanceCreated', null, this );
\r
413 this.on( 'mode', updateCommandsMode, null, null, 1 );
\r
415 initConfig( this, instanceConfig );
\r
419 CKEDITOR.tools.extend( CKEDITOR.editor.prototype,
\r
420 /** @lends CKEDITOR.editor.prototype */
\r
423 * Adds a command definition to the editor instance. Commands added with
\r
424 * this function can be later executed with {@link #execCommand}.
\r
425 * @param {String} commandName The indentifier name of the command.
\r
426 * @param {CKEDITOR.commandDefinition} commandDefinition The command definition.
\r
428 * editorInstance.addCommand( 'sample',
\r
430 * exec : function( editor )
\r
432 * alert( 'Executing a command for the editor name "' + editor.name + '"!' );
\r
436 addCommand : function( commandName, commandDefinition )
\r
438 return this._.commands[ commandName ] = new CKEDITOR.command( this, commandDefinition );
\r
442 * Add a trunk of css text to the editor which will be applied to the wysiwyg editing document.
\r
443 * Note: This function should be called before editor is loaded to take effect.
\r
444 * @param css {String} CSS text.
\r
446 * editorInstance.addCss( 'body { background-color: grey; }' );
\r
448 addCss : function( css )
\r
450 this._.styles.push( css );
\r
454 * Destroys the editor instance, releasing all resources used by it.
\r
455 * If the editor replaced an element, the element will be recovered.
\r
456 * @param {Boolean} [noUpdate] If the instance is replacing a DOM
\r
457 * element, this parameter indicates whether or not to update the
\r
458 * element with the instance contents.
\r
460 * alert( CKEDITOR.instances.editor1 ); e.g "object"
\r
461 * <b>CKEDITOR.instances.editor1.destroy()</b>;
\r
462 * alert( CKEDITOR.instances.editor1 ); "undefined"
\r
464 destroy : function( noUpdate )
\r
467 this.updateElement();
\r
469 this.theme.destroy( this );
\r
470 this.fire( 'destroy' );
\r
471 CKEDITOR.remove( this );
\r
472 CKEDITOR.fire( 'instanceDestroyed', null, this );
\r
476 * Executes a command.
\r
477 * @param {String} commandName The indentifier name of the command.
\r
478 * @param {Object} [data] Data to be passed to the command
\r
479 * @returns {Boolean} "true" if the command has been successfuly
\r
480 * executed, otherwise "false".
\r
482 * editorInstance.execCommand( 'Bold' );
\r
484 execCommand : function( commandName, data )
\r
486 var command = this.getCommand( commandName );
\r
495 if ( command && command.state != CKEDITOR.TRISTATE_DISABLED )
\r
497 if ( this.fire( 'beforeCommandExec', eventData ) !== true )
\r
499 eventData.returnValue = command.exec( eventData.commandData );
\r
501 // Fire the 'afterCommandExec' immediately if command is synchronous.
\r
502 if ( !command.async && this.fire( 'afterCommandExec', eventData ) !== true )
\r
503 return eventData.returnValue;
\r
507 // throw 'Unknown command name "' + commandName + '"';
\r
512 * Gets one of the registered commands. Note that, after registering a
\r
513 * command definition with addCommand, it is transformed internally
\r
514 * into an instance of {@link CKEDITOR.command}, which will be then
\r
515 * returned by this function.
\r
516 * @param {String} commandName The name of the command to be returned.
\r
517 * This is the same used to register the command with addCommand.
\r
518 * @returns {CKEDITOR.command} The command object identified by the
\r
521 getCommand : function( commandName )
\r
523 return this._.commands[ commandName ];
\r
527 * Gets the editor data. The data will be in raw format. It is the same
\r
528 * data that is posted by the editor.
\r
530 * @returns (String) The editor data.
\r
532 * if ( CKEDITOR.instances.editor1.<b>getData()</b> == '' )
\r
533 * alert( 'There is no data available' );
\r
535 getData : function()
\r
537 this.fire( 'beforeGetData' );
\r
539 var eventData = this._.data;
\r
541 if ( typeof eventData != 'string' )
\r
543 var element = this.element;
\r
544 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
\r
545 eventData = element.is( 'textarea' ) ? element.getValue() : element.getHtml();
\r
550 eventData = { dataValue : eventData };
\r
552 // Fire "getData" so data manipulation may happen.
\r
553 this.fire( 'getData', eventData );
\r
555 return eventData.dataValue;
\r
558 getSnapshot : function()
\r
560 var data = this.fire( 'getSnapshot' );
\r
562 if ( typeof data != 'string' )
\r
564 var element = this.element;
\r
565 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
\r
566 data = element.is( 'textarea' ) ? element.getValue() : element.getHtml();
\r
572 loadSnapshot : function( snapshot )
\r
574 this.fire( 'loadSnapshot', snapshot );
\r
578 * Sets the editor data. The data must be provided in raw format (HTML).
\r
579 * <b>Note:</b> This's an asynchronous method, the {@param callback}
\r
580 * function should be relied on if you want to interact with the editor
\r
581 * after data is fully loaded.
\r
583 * @param {String} data HTML code to replace the curent content in the editor.
\r
584 * @param {Function} callback Function to be called after the setData is completed.
\r
586 * CKEDITOR.instances.editor1.<b>setData( '<p>This is the editor data.</p>' )</b>;
\r
587 * CKEDITOR.instances.editor1.setData( '<p>Some other editor data.</p>', function()
\r
589 * CKEDITOR.instances.editor1.checkDirty(); // true
\r
592 setData : function( data , callback )
\r
596 this.on( 'dataReady', function( evt )
\r
598 evt.removeListener();
\r
599 callback.call( evt.editor );
\r
602 // Fire "setData" so data manipulation may happen.
\r
603 var eventData = { dataValue : data };
\r
604 this.fire( 'setData', eventData );
\r
606 this._.data = eventData.dataValue;
\r
608 this.fire( 'afterSetData', eventData );
\r
612 * Inserts HTML into the currently selected position in the editor.
\r
613 * @param {String} data HTML code to be inserted into the editor.
\r
615 * CKEDITOR.instances.editor1.<b>insertHtml( '<p>This is a new paragraph.</p>' )</b>;
\r
617 insertHtml : function( data )
\r
619 this.fire( 'insertHtml', data );
\r
623 * Inserts an element into the currently selected position in the
\r
625 * @param {CKEDITOR.dom.element} element The element to be inserted
\r
628 * var element = CKEDITOR.dom.element.createFromHtml( '<img src="hello.png" border="0" title="Hello" />' );
\r
629 * CKEDITOR.instances.editor1.<b>insertElement( element )</b>;
\r
631 insertElement : function( element )
\r
633 this.fire( 'insertElement', element );
\r
636 checkDirty : function()
\r
638 return ( this.mayBeDirty && this._.previousValue !== this.getSnapshot() );
\r
641 resetDirty : function()
\r
643 if ( this.mayBeDirty )
\r
644 this._.previousValue = this.getSnapshot();
\r
648 * Updates the <textarea> element that has been replaced by the editor with
\r
649 * the current data available in the editor.
\r
651 * CKEDITOR.instances.editor1.updateElement();
\r
652 * alert( document.getElementById( 'editor1' ).value ); // The current editor data.
\r
654 updateElement : function()
\r
656 var element = this.element;
\r
657 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
\r
659 var data = this.getData();
\r
661 if( this.config.htmlEncodeOutput )
\r
662 data = CKEDITOR.tools.htmlEncode( data );
\r
664 if ( element.is( 'textarea' ) )
\r
665 element.setValue( data );
\r
667 element.setHtml( data );
\r
672 CKEDITOR.on( 'loaded', function()
\r
674 // Run the full initialization for pending editors.
\r
675 var pending = CKEDITOR.editor._pending;
\r
678 delete CKEDITOR.editor._pending;
\r
680 for ( var i = 0 ; i < pending.length ; i++ )
\r
681 pending[ i ]._init();
\r
686 * Whether escape HTML when editor update original input element.
\r
687 * @name CKEDITOR.config.htmlEncodeOutput
\r
691 * config.htmlEncodeOutput = true;
\r