2 Copyright (c) 2003-2009, 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 var loadedConfig = loadConfigLoaded[ customConfig ] || ( loadConfigLoaded[ customConfig ] = {} );
\r
38 // If the custom config has already been downloaded, reuse it.
\r
39 if ( loadedConfig.fn )
\r
41 // Call the cached CKEDITOR.editorConfig defined in the custom
\r
42 // config file for the editor instance depending on it.
\r
43 loadedConfig.fn.call( editor, editor.config );
\r
45 // If there is no other customConfig in the chain, fire the
\r
46 // "configLoaded" event.
\r
47 if ( editor.config.customConfig == customConfig || !loadConfig( editor ) )
\r
48 editor.fireOnce( 'customConfigLoaded' );
\r
52 // Load the custom configuration file.
\r
53 CKEDITOR.scriptLoader.load( customConfig, function()
\r
55 // If the CKEDITOR.editorConfig function has been properly
\r
56 // defined in the custom configuration file, cache it.
\r
57 if ( CKEDITOR.editorConfig )
\r
58 loadedConfig.fn = CKEDITOR.editorConfig;
\r
60 loadedConfig.fn = function(){};
\r
62 // Call the load config again. This time the custom
\r
63 // config is already cached and so it will get loaded.
\r
64 loadConfig( editor );
\r
71 var initConfig = function( editor, instanceConfig )
\r
73 // Setup the lister for the "customConfigLoaded" event.
\r
74 editor.on( 'customConfigLoaded', function()
\r
76 if ( instanceConfig )
\r
78 // Register the events that may have been set at the instance
\r
79 // configuration object.
\r
80 if ( instanceConfig.on )
\r
82 for ( var eventName in instanceConfig.on )
\r
84 editor.on( eventName, instanceConfig.on[ eventName ] );
\r
88 // Overwrite the settings from the in-page config.
\r
89 CKEDITOR.tools.extend( editor.config, instanceConfig, true );
\r
91 delete editor.config.on;
\r
94 onConfigLoaded( editor );
\r
97 // The instance config may override the customConfig setting to avoid
\r
98 // loading the default ~/config.js file.
\r
99 if ( instanceConfig && instanceConfig.customConfig != undefined )
\r
100 editor.config.customConfig = instanceConfig.customConfig;
\r
102 // Load configs from the custom configuration files.
\r
103 if ( !loadConfig( editor ) )
\r
104 editor.fireOnce( 'customConfigLoaded' );
\r
107 // ##### END: Config Privates
\r
109 var onConfigLoaded = function( editor )
\r
111 // Set config related properties.
\r
113 var skin = editor.config.skin.split( ',' ),
\r
114 skinName = skin[ 0 ],
\r
115 skinPath = CKEDITOR.getUrl( skin[ 1 ] || (
\r
116 '_source/' + // @Packager.RemoveLine
\r
117 'skins/' + skinName + '/' ) );
\r
119 editor.skinName = skinName;
\r
120 editor.skinPath = skinPath;
\r
121 editor.skinClass = 'cke_skin_' + skinName;
\r
123 // Fire the "configLoaded" event.
\r
124 editor.fireOnce( 'configLoaded' );
\r
126 // Load language file.
\r
127 loadLang( editor );
\r
130 var loadLang = function( editor )
\r
132 CKEDITOR.lang.load( editor.config.language, editor.config.defaultLanguage, function( languageCode, lang )
\r
134 editor.langCode = languageCode;
\r
136 // As we'll be adding plugin specific entries that could come
\r
137 // from different language code files, we need a copy of lang,
\r
138 // not a direct reference to it.
\r
139 editor.lang = CKEDITOR.tools.prototypedCopy( lang );
\r
141 // We're not able to support RTL in Firefox 2 at this time.
\r
142 if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 && editor.lang.dir == 'rtl' )
\r
143 editor.lang.dir = 'ltr';
\r
145 loadPlugins( editor );
\r
149 var loadPlugins = function( editor )
\r
151 var config = editor.config,
\r
152 plugins = config.plugins,
\r
153 extraPlugins = config.extraPlugins,
\r
154 removePlugins = config.removePlugins;
\r
156 if ( extraPlugins )
\r
158 // Remove them first to avoid duplications.
\r
159 var removeRegex = new RegExp( '(?:^|,)(?:' + extraPlugins.replace( /\s*,\s*/g, '|' ) + ')(?=,|$)' , 'g' );
\r
160 plugins = plugins.replace( removeRegex, '' );
\r
162 plugins += ',' + extraPlugins;
\r
165 if ( removePlugins )
\r
167 removeRegex = new RegExp( '(?:^|,)(?:' + removePlugins.replace( /\s*,\s*/g, '|' ) + ')(?=,|$)' , 'g' );
\r
168 plugins = plugins.replace( removeRegex, '' );
\r
171 // Load all plugins defined in the "plugins" setting.
\r
172 CKEDITOR.plugins.load( plugins.split( ',' ), function( plugins )
\r
174 // The list of plugins.
\r
175 var pluginsArray = [];
\r
177 // The language code to get loaded for each plugin. Null
\r
178 // entries will be appended for plugins with no language files.
\r
179 var languageCodes = [];
\r
181 // The list of URLs to language files.
\r
182 var languageFiles = [];
\r
184 // Cache the loaded plugin names.
\r
185 editor.plugins = plugins;
\r
187 // Loop through all plugins, to build the list of language
\r
188 // files to get loaded.
\r
189 for ( var pluginName in plugins )
\r
191 var plugin = plugins[ pluginName ],
\r
192 pluginLangs = plugin.lang,
\r
193 pluginPath = CKEDITOR.plugins.getPath( pluginName ),
\r
196 // Set the plugin path in the plugin.
\r
197 plugin.path = pluginPath;
\r
199 // If the plugin has "lang".
\r
202 // Resolve the plugin language. If the current language
\r
203 // is not available, get the first one (default one).
\r
204 lang = ( CKEDITOR.tools.indexOf( pluginLangs, editor.langCode ) >= 0 ? editor.langCode : pluginLangs[ 0 ] );
\r
206 if ( !plugin.lang[ lang ] )
\r
208 // Put the language file URL into the list of files to
\r
210 languageFiles.push( CKEDITOR.getUrl( pluginPath + 'lang/' + lang + '.js' ) );
\r
214 CKEDITOR.tools.extend( editor.lang, plugin.lang[ lang ] );
\r
219 // Save the language code, so we know later which
\r
220 // language has been resolved to this plugin.
\r
221 languageCodes.push( lang );
\r
223 pluginsArray.push( plugin );
\r
226 // Load all plugin specific language files in a row.
\r
227 CKEDITOR.scriptLoader.load( languageFiles, function()
\r
229 // Initialize all plugins that have the "beforeInit" and "init" methods defined.
\r
230 var methods = [ 'beforeInit', 'init', 'afterInit' ];
\r
231 for ( var m = 0 ; m < methods.length ; m++ )
\r
233 for ( var i = 0 ; i < pluginsArray.length ; i++ )
\r
235 var plugin = pluginsArray[ i ];
\r
237 // Uses the first loop to update the language entries also.
\r
238 if ( m === 0 && languageCodes[ i ] && plugin.lang )
\r
239 CKEDITOR.tools.extend( editor.lang, plugin.lang[ languageCodes[ i ] ] );
\r
241 // Call the plugin method (beforeInit and init).
\r
242 if ( plugin[ methods[ m ] ] )
\r
243 plugin[ methods[ m ] ]( editor );
\r
247 // Load the editor skin.
\r
248 editor.fire( 'pluginsLoaded' );
\r
249 loadSkin( editor );
\r
254 var loadSkin = function( editor )
\r
256 CKEDITOR.skins.load( editor, 'editor', function()
\r
258 loadTheme( editor );
\r
262 var loadTheme = function( editor )
\r
264 var theme = editor.config.theme;
\r
265 CKEDITOR.themes.load( theme, function()
\r
267 var editorTheme = editor.theme = CKEDITOR.themes.get( theme );
\r
268 editorTheme.path = CKEDITOR.themes.getPath( theme );
\r
269 editorTheme.build( editor );
\r
271 if ( editor.config.autoUpdateElement )
\r
272 attachToForm( editor );
\r
276 var attachToForm = function( editor )
\r
278 var element = editor.element;
\r
280 // If are replacing a textarea, we must
\r
281 if ( editor.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE && element.is( 'textarea' ) )
\r
283 var form = element.$.form && new CKEDITOR.dom.element( element.$.form );
\r
286 function onSubmit()
\r
288 editor.updateElement();
\r
290 form.on( 'submit',onSubmit );
\r
292 // Setup the submit function because it doesn't fire the
\r
294 if ( !form.$.submit.nodeName )
\r
296 form.$.submit = CKEDITOR.tools.override( form.$.submit, function( originalSubmit )
\r
300 editor.updateElement();
\r
302 // For IE, the DOM submit function is not a
\r
303 // function, so we need thid check.
\r
304 if ( originalSubmit.apply )
\r
305 originalSubmit.apply( this, arguments );
\r
312 // Remove 'submit' events registered on form element before destroying.(#3988)
\r
313 editor.on( 'destroy', function()
\r
315 form.removeListener( 'submit', onSubmit );
\r
321 function updateCommandsMode()
\r
324 commands = this._.commands,
\r
327 for ( var name in commands )
\r
329 command = commands[ name ];
\r
330 command[ command.modes[ mode ] ? 'enable' : 'disable' ]();
\r
335 * Initializes the editor instance. This function is called by the editor
\r
336 * contructor (editor_basic.js).
\r
339 CKEDITOR.editor.prototype._init = function()
\r
341 // Get the properties that have been saved in the editor_base
\r
343 var element = CKEDITOR.dom.element.get( this._.element ),
\r
344 instanceConfig = this._.instanceConfig;
\r
345 delete this._.element;
\r
346 delete this._.instanceConfig;
\r
348 this._.commands = {};
\r
349 this._.styles = [];
\r
352 * The DOM element that has been replaced by this editor instance. This
\r
353 * element holds the editor data on load and post.
\r
354 * @name CKEDITOR.editor.prototype.element
\r
355 * @type CKEDITOR.dom.element
\r
357 * var editor = CKEDITOR.instances.editor1;
\r
358 * alert( <b>editor.element</b>.getName() ); "textarea"
\r
360 this.element = element;
\r
363 * The editor instance name. It hay be the replaced element id, name or
\r
364 * a default name using a progressive counter (editor1, editor2, ...).
\r
365 * @name CKEDITOR.editor.prototype.name
\r
368 * var editor = CKEDITOR.instances.editor1;
\r
369 * alert( <b>editor.name</b> ); "editor1"
\r
371 this.name = ( element && ( this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
\r
372 && ( element.getId() || element.getNameAtt() ) )
\r
375 if ( this.name in CKEDITOR.instances )
\r
376 throw '[CKEDITOR.editor] The instance "' + this.name + '" already exists.';
\r
379 * The configurations for this editor instance. It inherits all
\r
380 * settings defined in (@link CKEDITOR.config}, combined with settings
\r
381 * loaded from custom configuration files and those defined inline in
\r
382 * the page when creating the editor.
\r
383 * @name CKEDITOR.editor.prototype.config
\r
386 * var editor = CKEDITOR.instances.editor1;
\r
387 * alert( <b>editor.config.theme</b> ); "default" e.g.
\r
389 this.config = CKEDITOR.tools.prototypedCopy( CKEDITOR.config );
\r
392 * Namespace containing UI features related to this editor instance.
\r
393 * @name CKEDITOR.editor.prototype.ui
\r
394 * @type CKEDITOR.ui
\r
397 this.ui = new CKEDITOR.ui( this );
\r
400 * Controls the focus state of this editor instance. This property
\r
401 * is rarely used for normal API operations. It is mainly
\r
402 * destinated to developer adding UI elements to the editor interface.
\r
403 * @name CKEDITOR.editor.prototype.focusManager
\r
404 * @type CKEDITOR.focusManager
\r
407 this.focusManager = new CKEDITOR.focusManager( this );
\r
409 CKEDITOR.fire( 'instanceCreated', null, this );
\r
411 this.on( 'mode', updateCommandsMode, null, null, 1 );
\r
413 initConfig( this, instanceConfig );
\r
417 CKEDITOR.tools.extend( CKEDITOR.editor.prototype,
\r
418 /** @lends CKEDITOR.editor.prototype */
\r
421 * Adds a command definition to the editor instance. Commands added with
\r
422 * this function can be later executed with {@link #execCommand}.
\r
423 * @param {String} commandName The indentifier name of the command.
\r
424 * @param {CKEDITOR.commandDefinition} commandDefinition The command definition.
\r
426 * editorInstance.addCommand( 'sample',
\r
428 * exec : function( editor )
\r
430 * alert( 'Executing a command for the editor name "' + editor.name + '"!' );
\r
434 addCommand : function( commandName, commandDefinition )
\r
436 return this._.commands[ commandName ] = new CKEDITOR.command( this, commandDefinition );
\r
440 * Add a trunk of css text to the editor which will be applied to the wysiwyg editing document.
\r
441 * Note: This function should be called before editor is loaded to take effect.
\r
442 * @param css {String} CSS text.
\r
444 * editorInstance.addCss( 'body { background-color: grey; }' );
\r
446 addCss : function( css )
\r
448 this._.styles.push( css );
\r
452 * Destroys the editor instance, releasing all resources used by it.
\r
453 * If the editor replaced an element, the element will be recovered.
\r
454 * @param {Boolean} [noUpdate] If the instance is replacing a DOM
\r
455 * element, this parameter indicates whether or not to update the
\r
456 * element with the instance contents.
\r
458 * alert( CKEDITOR.instances.editor1 ); e.g "object"
\r
459 * <b>CKEDITOR.instances.editor1.destroy()</b>;
\r
460 * alert( CKEDITOR.instances.editor1 ); "undefined"
\r
462 destroy : function( noUpdate )
\r
465 this.updateElement();
\r
467 this.theme.destroy( this );
\r
468 this.fire( 'destroy' );
\r
469 CKEDITOR.remove( this );
\r
470 CKEDITOR.fire( 'instanceDestroyed', null, this );
\r
474 * Executes a command.
\r
475 * @param {String} commandName The indentifier name of the command.
\r
476 * @param {Object} [data] Data to be passed to the command
\r
477 * @returns {Boolean} "true" if the command has been successfuly
\r
478 * executed, otherwise "false".
\r
480 * editorInstance.execCommand( 'Bold' );
\r
482 execCommand : function( commandName, data )
\r
484 var command = this.getCommand( commandName );
\r
493 if ( command && command.state != CKEDITOR.TRISTATE_DISABLED )
\r
495 if ( this.fire( 'beforeCommandExec', eventData ) !== true )
\r
497 eventData.returnValue = command.exec( eventData.commandData );
\r
499 // Fire the 'afterCommandExec' immediately if command is synchronous.
\r
500 if ( !command.async && this.fire( 'afterCommandExec', eventData ) !== true )
\r
501 return eventData.returnValue;
\r
505 // throw 'Unknown command name "' + commandName + '"';
\r
510 * Gets one of the registered commands. Note that, after registering a
\r
511 * command definition with addCommand, it is transformed internally
\r
512 * into an instance of {@link CKEDITOR.command}, which will be then
\r
513 * returned by this function.
\r
514 * @param {String} commandName The name of the command to be returned.
\r
515 * This is the same used to register the command with addCommand.
\r
516 * @returns {CKEDITOR.command} The command object identified by the
\r
519 getCommand : function( commandName )
\r
521 return this._.commands[ commandName ];
\r
525 * Gets the editor data. The data will be in raw format. It is the same
\r
526 * data that is posted by the editor.
\r
528 * @returns (String) The editor data.
\r
530 * if ( CKEDITOR.instances.editor1.<b>getData()</b> == '' )
\r
531 * alert( 'There is no data available' );
\r
533 getData : function()
\r
535 this.fire( 'beforeGetData' );
\r
537 var eventData = this._.data;
\r
539 if ( typeof eventData != 'string' )
\r
541 var element = this.element;
\r
542 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
\r
543 eventData = element.is( 'textarea' ) ? element.getValue() : element.getHtml();
\r
548 eventData = { dataValue : eventData };
\r
550 // Fire "getData" so data manipulation may happen.
\r
551 this.fire( 'getData', eventData );
\r
553 return eventData.dataValue;
\r
556 getSnapshot : function()
\r
558 var data = this.fire( 'getSnapshot' );
\r
560 if ( typeof data != 'string' )
\r
562 var element = this.element;
\r
563 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
\r
564 data = element.is( 'textarea' ) ? element.getValue() : element.getHtml();
\r
570 loadSnapshot : function( snapshot )
\r
572 this.fire( 'loadSnapshot', snapshot );
\r
576 * Sets the editor data. The data must be provided in raw format.
\r
577 * @param {String} data HTML code to replace the curent content in the editor.
\r
578 * @param {Function} callback Function to be called after the setData is completed.
\r
580 * CKEDITOR.instances.editor1.<b>setData( '<p>This is the editor data.</p>' )</b>;
\r
581 * CKEDITOR.instances.editor1.setData( '<p>Some other editor data.</p>', function()
\r
583 * CKEDITOR.instances.editor1.checkDirty(); // true
\r
586 setData : function( data , callback )
\r
590 this.on( 'dataReady', function( evt )
\r
592 evt.removeListener();
\r
593 callback.call( evt.editor );
\r
596 // Fire "setData" so data manipulation may happen.
\r
597 var eventData = { dataValue : data };
\r
598 this.fire( 'setData', eventData );
\r
600 this._.data = eventData.dataValue;
\r
602 this.fire( 'afterSetData', eventData );
\r
606 * Inserts HTML into the currently selected position in the editor.
\r
607 * @param {String} data HTML code to be inserted into the editor.
\r
609 * CKEDITOR.instances.editor1.<b>insertHtml( '<p>This is a new paragraph.</p>' )</b>;
\r
611 insertHtml : function( data )
\r
613 this.fire( 'insertHtml', data );
\r
617 * Inserts an element into the currently selected position in the
\r
619 * @param {CKEDITOR.dom.element} element The element to be inserted
\r
622 * var element = CKEDITOR.dom.element.createFromHtml( '<img src="hello.png" border="0" title="Hello" />' );
\r
623 * CKEDITOR.instances.editor1.<b>insertElement( element )</b>;
\r
625 insertElement : function( element )
\r
627 this.fire( 'insertElement', element );
\r
630 checkDirty : function()
\r
632 return ( this.mayBeDirty && this._.previousValue !== this.getSnapshot() );
\r
635 resetDirty : function()
\r
637 if ( this.mayBeDirty )
\r
638 this._.previousValue = this.getSnapshot();
\r
642 * Updates the <textarea> element that has been replaced by the editor with
\r
643 * the current data available in the editor.
\r
645 * CKEDITOR.instances.editor1.updateElement();
\r
646 * alert( document.getElementById( 'editor1' ).value ); // The current editor data.
\r
648 updateElement : function()
\r
650 var element = this.element;
\r
651 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
\r
653 if ( element.is( 'textarea' ) )
\r
654 element.setValue( this.getData() );
\r
656 element.setHtml( this.getData() );
\r
661 CKEDITOR.on( 'loaded', function()
\r
663 // Run the full initialization for pending editors.
\r
664 var pending = CKEDITOR.editor._pending;
\r
667 delete CKEDITOR.editor._pending;
\r
669 for ( var i = 0 ; i < pending.length ; i++ )
\r
670 pending[ i ]._init();
\r