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 editor.tabIndex = editor.config.tabIndex || editor.element.getAttribute( 'tabindex' ) || 0;
\r
127 // Fire the "configLoaded" event.
\r
128 editor.fireOnce( 'configLoaded' );
\r
130 // Load language file.
\r
131 loadSkin( editor );
\r
134 var loadLang = function( editor )
\r
136 CKEDITOR.lang.load( editor.config.language, editor.config.defaultLanguage, function( languageCode, lang )
\r
138 editor.langCode = languageCode;
\r
140 // As we'll be adding plugin specific entries that could come
\r
141 // from different language code files, we need a copy of lang,
\r
142 // not a direct reference to it.
\r
143 editor.lang = CKEDITOR.tools.prototypedCopy( lang );
\r
145 // We're not able to support RTL in Firefox 2 at this time.
\r
146 if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 && editor.lang.dir == 'rtl' )
\r
147 editor.lang.dir = 'ltr';
\r
149 loadPlugins( editor );
\r
153 var loadPlugins = function( editor )
\r
155 var config = editor.config,
\r
156 plugins = config.plugins,
\r
157 extraPlugins = config.extraPlugins,
\r
158 removePlugins = config.removePlugins;
\r
160 if ( extraPlugins )
\r
162 // Remove them first to avoid duplications.
\r
163 var removeRegex = new RegExp( '(?:^|,)(?:' + extraPlugins.replace( /\s*,\s*/g, '|' ) + ')(?=,|$)' , 'g' );
\r
164 plugins = plugins.replace( removeRegex, '' );
\r
166 plugins += ',' + extraPlugins;
\r
169 if ( removePlugins )
\r
171 removeRegex = new RegExp( '(?:^|,)(?:' + removePlugins.replace( /\s*,\s*/g, '|' ) + ')(?=,|$)' , 'g' );
\r
172 plugins = plugins.replace( removeRegex, '' );
\r
175 // Load all plugins defined in the "plugins" setting.
\r
176 CKEDITOR.plugins.load( plugins.split( ',' ), function( plugins )
\r
178 // The list of plugins.
\r
179 var pluginsArray = [];
\r
181 // The language code to get loaded for each plugin. Null
\r
182 // entries will be appended for plugins with no language files.
\r
183 var languageCodes = [];
\r
185 // The list of URLs to language files.
\r
186 var languageFiles = [];
\r
188 // Cache the loaded plugin names.
\r
189 editor.plugins = plugins;
\r
191 // Loop through all plugins, to build the list of language
\r
192 // files to get loaded.
\r
193 for ( var pluginName in plugins )
\r
195 var plugin = plugins[ pluginName ],
\r
196 pluginLangs = plugin.lang,
\r
197 pluginPath = CKEDITOR.plugins.getPath( pluginName ),
\r
200 // Set the plugin path in the plugin.
\r
201 plugin.path = pluginPath;
\r
203 // If the plugin has "lang".
\r
206 // Resolve the plugin language. If the current language
\r
207 // is not available, get the first one (default one).
\r
208 lang = ( CKEDITOR.tools.indexOf( pluginLangs, editor.langCode ) >= 0 ? editor.langCode : pluginLangs[ 0 ] );
\r
210 if ( !plugin.lang[ lang ] )
\r
212 // Put the language file URL into the list of files to
\r
214 languageFiles.push( CKEDITOR.getUrl( pluginPath + 'lang/' + lang + '.js' ) );
\r
218 CKEDITOR.tools.extend( editor.lang, plugin.lang[ lang ] );
\r
223 // Save the language code, so we know later which
\r
224 // language has been resolved to this plugin.
\r
225 languageCodes.push( lang );
\r
227 pluginsArray.push( plugin );
\r
230 // Load all plugin specific language files in a row.
\r
231 CKEDITOR.scriptLoader.load( languageFiles, function()
\r
233 // Initialize all plugins that have the "beforeInit" and "init" methods defined.
\r
234 var methods = [ 'beforeInit', 'init', 'afterInit' ];
\r
235 for ( var m = 0 ; m < methods.length ; m++ )
\r
237 for ( var i = 0 ; i < pluginsArray.length ; i++ )
\r
239 var plugin = pluginsArray[ i ];
\r
241 // Uses the first loop to update the language entries also.
\r
242 if ( m === 0 && languageCodes[ i ] && plugin.lang )
\r
243 CKEDITOR.tools.extend( editor.lang, plugin.lang[ languageCodes[ i ] ] );
\r
245 // Call the plugin method (beforeInit and init).
\r
246 if ( plugin[ methods[ m ] ] )
\r
247 plugin[ methods[ m ] ]( editor );
\r
251 // Load the editor skin.
\r
252 editor.fire( 'pluginsLoaded' );
\r
253 loadTheme( editor );
\r
258 var loadSkin = function( editor )
\r
260 CKEDITOR.skins.load( editor, 'editor', function()
\r
262 loadLang( editor );
\r
266 var loadTheme = function( editor )
\r
268 var theme = editor.config.theme;
\r
269 CKEDITOR.themes.load( theme, function()
\r
271 var editorTheme = editor.theme = CKEDITOR.themes.get( theme );
\r
272 editorTheme.path = CKEDITOR.themes.getPath( theme );
\r
273 editorTheme.build( editor );
\r
275 if ( editor.config.autoUpdateElement )
\r
276 attachToForm( editor );
\r
280 var attachToForm = function( editor )
\r
282 var element = editor.element;
\r
284 // If are replacing a textarea, we must
\r
285 if ( editor.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE && element.is( 'textarea' ) )
\r
287 var form = element.$.form && new CKEDITOR.dom.element( element.$.form );
\r
290 function onSubmit()
\r
292 editor.updateElement();
\r
294 form.on( 'submit',onSubmit );
\r
296 // Setup the submit function because it doesn't fire the
\r
298 if ( !form.$.submit.nodeName )
\r
300 form.$.submit = CKEDITOR.tools.override( form.$.submit, function( originalSubmit )
\r
304 editor.updateElement();
\r
306 // For IE, the DOM submit function is not a
\r
307 // function, so we need thid check.
\r
308 if ( originalSubmit.apply )
\r
309 originalSubmit.apply( this, arguments );
\r
316 // Remove 'submit' events registered on form element before destroying.(#3988)
\r
317 editor.on( 'destroy', function()
\r
319 form.removeListener( 'submit', onSubmit );
\r
325 function updateCommandsMode()
\r
328 commands = this._.commands,
\r
331 for ( var name in commands )
\r
333 command = commands[ name ];
\r
334 command[ command.startDisabled ? 'disable' : command.modes[ mode ] ? 'enable' : 'disable' ]();
\r
339 * Initializes the editor instance. This function is called by the editor
\r
340 * contructor (editor_basic.js).
\r
343 CKEDITOR.editor.prototype._init = function()
\r
345 // Get the properties that have been saved in the editor_base
\r
347 var element = CKEDITOR.dom.element.get( this._.element ),
\r
348 instanceConfig = this._.instanceConfig;
\r
349 delete this._.element;
\r
350 delete this._.instanceConfig;
\r
352 this._.commands = {};
\r
353 this._.styles = [];
\r
356 * The DOM element that has been replaced by this editor instance. This
\r
357 * element holds the editor data on load and post.
\r
358 * @name CKEDITOR.editor.prototype.element
\r
359 * @type CKEDITOR.dom.element
\r
361 * var editor = CKEDITOR.instances.editor1;
\r
362 * alert( <b>editor.element</b>.getName() ); "textarea"
\r
364 this.element = element;
\r
367 * The editor instance name. It hay be the replaced element id, name or
\r
368 * a default name using a progressive counter (editor1, editor2, ...).
\r
369 * @name CKEDITOR.editor.prototype.name
\r
372 * var editor = CKEDITOR.instances.editor1;
\r
373 * alert( <b>editor.name</b> ); "editor1"
\r
375 this.name = ( element && ( this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
\r
376 && ( element.getId() || element.getNameAtt() ) )
\r
379 if ( this.name in CKEDITOR.instances )
\r
380 throw '[CKEDITOR.editor] The instance "' + this.name + '" already exists.';
\r
383 * The configurations for this editor instance. It inherits all
\r
384 * settings defined in (@link CKEDITOR.config}, combined with settings
\r
385 * loaded from custom configuration files and those defined inline in
\r
386 * the page when creating the editor.
\r
387 * @name CKEDITOR.editor.prototype.config
\r
390 * var editor = CKEDITOR.instances.editor1;
\r
391 * alert( <b>editor.config.theme</b> ); "default" e.g.
\r
393 this.config = CKEDITOR.tools.prototypedCopy( CKEDITOR.config );
\r
396 * Namespace containing UI features related to this editor instance.
\r
397 * @name CKEDITOR.editor.prototype.ui
\r
398 * @type CKEDITOR.ui
\r
401 this.ui = new CKEDITOR.ui( this );
\r
404 * Controls the focus state of this editor instance. This property
\r
405 * is rarely used for normal API operations. It is mainly
\r
406 * destinated to developer adding UI elements to the editor interface.
\r
407 * @name CKEDITOR.editor.prototype.focusManager
\r
408 * @type CKEDITOR.focusManager
\r
411 this.focusManager = new CKEDITOR.focusManager( this );
\r
413 CKEDITOR.fire( 'instanceCreated', null, this );
\r
415 this.on( 'mode', updateCommandsMode, null, null, 1 );
\r
417 initConfig( this, instanceConfig );
\r
421 CKEDITOR.tools.extend( CKEDITOR.editor.prototype,
\r
422 /** @lends CKEDITOR.editor.prototype */
\r
425 * Adds a command definition to the editor instance. Commands added with
\r
426 * this function can be later executed with {@link #execCommand}.
\r
427 * @param {String} commandName The indentifier name of the command.
\r
428 * @param {CKEDITOR.commandDefinition} commandDefinition The command definition.
\r
430 * editorInstance.addCommand( 'sample',
\r
432 * exec : function( editor )
\r
434 * alert( 'Executing a command for the editor name "' + editor.name + '"!' );
\r
438 addCommand : function( commandName, commandDefinition )
\r
440 return this._.commands[ commandName ] = new CKEDITOR.command( this, commandDefinition );
\r
444 * Add a trunk of css text to the editor which will be applied to the wysiwyg editing document.
\r
445 * Note: This function should be called before editor is loaded to take effect.
\r
446 * @param css {String} CSS text.
\r
448 * editorInstance.addCss( 'body { background-color: grey; }' );
\r
450 addCss : function( css )
\r
452 this._.styles.push( css );
\r
456 * Destroys the editor instance, releasing all resources used by it.
\r
457 * If the editor replaced an element, the element will be recovered.
\r
458 * @param {Boolean} [noUpdate] If the instance is replacing a DOM
\r
459 * element, this parameter indicates whether or not to update the
\r
460 * element with the instance contents.
\r
462 * alert( CKEDITOR.instances.editor1 ); e.g "object"
\r
463 * <b>CKEDITOR.instances.editor1.destroy()</b>;
\r
464 * alert( CKEDITOR.instances.editor1 ); "undefined"
\r
466 destroy : function( noUpdate )
\r
469 this.updateElement();
\r
473 // -> currentMode.unload( holderElement );
\r
474 this._.modes[ this.mode ].unload( this.getThemeSpace( 'contents' ) );
\r
477 this.theme.destroy( this );
\r
485 if ( this.toolbox )
\r
487 toolbars = this.toolbox.toolbars;
\r
488 for ( ; index < toolbars.length ; index++ )
\r
490 items = toolbars[ index ].items;
\r
491 for ( j = 0 ; j < items.length ; j++ )
\r
493 instance = items[ j ];
\r
494 if ( instance.clickFn ) CKEDITOR.tools.removeFunction( instance.clickFn );
\r
495 if ( instance.keyDownFn ) CKEDITOR.tools.removeFunction( instance.keyDownFn );
\r
497 if ( instance.index ) CKEDITOR.ui.button._.instances[ instance.index ] = null;
\r
502 if ( this.contextMenu )
\r
503 CKEDITOR.tools.removeFunction( this.contextMenu._.functionId );
\r
505 if ( this._.filebrowserFn )
\r
506 CKEDITOR.tools.removeFunction( this._.filebrowserFn );
\r
508 this.fire( 'destroy' );
\r
509 CKEDITOR.remove( this );
\r
510 CKEDITOR.fire( 'instanceDestroyed', null, this );
\r
514 * Executes a command.
\r
515 * @param {String} commandName The indentifier name of the command.
\r
516 * @param {Object} [data] Data to be passed to the command
\r
517 * @returns {Boolean} "true" if the command has been successfuly
\r
518 * executed, otherwise "false".
\r
520 * editorInstance.execCommand( 'Bold' );
\r
522 execCommand : function( commandName, data )
\r
524 var command = this.getCommand( commandName );
\r
533 if ( command && command.state != CKEDITOR.TRISTATE_DISABLED )
\r
535 if ( this.fire( 'beforeCommandExec', eventData ) !== true )
\r
537 eventData.returnValue = command.exec( eventData.commandData );
\r
539 // Fire the 'afterCommandExec' immediately if command is synchronous.
\r
540 if ( !command.async && this.fire( 'afterCommandExec', eventData ) !== true )
\r
541 return eventData.returnValue;
\r
545 // throw 'Unknown command name "' + commandName + '"';
\r
550 * Gets one of the registered commands. Note that, after registering a
\r
551 * command definition with addCommand, it is transformed internally
\r
552 * into an instance of {@link CKEDITOR.command}, which will be then
\r
553 * returned by this function.
\r
554 * @param {String} commandName The name of the command to be returned.
\r
555 * This is the same used to register the command with addCommand.
\r
556 * @returns {CKEDITOR.command} The command object identified by the
\r
559 getCommand : function( commandName )
\r
561 return this._.commands[ commandName ];
\r
565 * Gets the editor data. The data will be in raw format. It is the same
\r
566 * data that is posted by the editor.
\r
568 * @returns (String) The editor data.
\r
570 * if ( CKEDITOR.instances.editor1.<b>getData()</b> == '' )
\r
571 * alert( 'There is no data available' );
\r
573 getData : function()
\r
575 this.fire( 'beforeGetData' );
\r
577 var eventData = this._.data;
\r
579 if ( typeof eventData != 'string' )
\r
581 var element = this.element;
\r
582 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
\r
583 eventData = element.is( 'textarea' ) ? element.getValue() : element.getHtml();
\r
588 eventData = { dataValue : eventData };
\r
590 // Fire "getData" so data manipulation may happen.
\r
591 this.fire( 'getData', eventData );
\r
593 return eventData.dataValue;
\r
596 getSnapshot : function()
\r
598 var data = this.fire( 'getSnapshot' );
\r
600 if ( typeof data != 'string' )
\r
602 var element = this.element;
\r
603 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
\r
604 data = element.is( 'textarea' ) ? element.getValue() : element.getHtml();
\r
610 loadSnapshot : function( snapshot )
\r
612 this.fire( 'loadSnapshot', snapshot );
\r
616 * Sets the editor data. The data must be provided in raw format (HTML).<br />
\r
618 * Note that this menthod is asynchronous. The "callback" parameter must
\r
619 * be used if interaction with the editor is needed after setting the data.
\r
620 * @param {String} data HTML code to replace the curent content in the
\r
622 * @param {Function} callback Function to be called after the setData
\r
625 * CKEDITOR.instances.editor1.<b>setData</b>( '<p>This is the editor data.</p>' );
\r
627 * CKEDITOR.instances.editor1.<b>setData</b>( '<p>Some other editor data.</p>', function()
\r
629 * this.checkDirty(); // true
\r
632 setData : function( data , callback )
\r
636 this.on( 'dataReady', function( evt )
\r
638 evt.removeListener();
\r
639 callback.call( evt.editor );
\r
643 // Fire "setData" so data manipulation may happen.
\r
644 var eventData = { dataValue : data };
\r
645 this.fire( 'setData', eventData );
\r
647 this._.data = eventData.dataValue;
\r
649 this.fire( 'afterSetData', eventData );
\r
653 * Inserts HTML into the currently selected position in the editor.
\r
654 * @param {String} data HTML code to be inserted into the editor.
\r
656 * CKEDITOR.instances.editor1.<b>insertHtml( '<p>This is a new paragraph.</p>' )</b>;
\r
658 insertHtml : function( data )
\r
660 this.fire( 'insertHtml', data );
\r
664 * Inserts an element into the currently selected position in the
\r
666 * @param {CKEDITOR.dom.element} element The element to be inserted
\r
669 * var element = CKEDITOR.dom.element.createFromHtml( '<img src="hello.png" border="0" title="Hello" />' );
\r
670 * CKEDITOR.instances.editor1.<b>insertElement( element )</b>;
\r
672 insertElement : function( element )
\r
674 this.fire( 'insertElement', element );
\r
677 checkDirty : function()
\r
679 return ( this.mayBeDirty && this._.previousValue !== this.getSnapshot() );
\r
682 resetDirty : function()
\r
684 if ( this.mayBeDirty )
\r
685 this._.previousValue = this.getSnapshot();
\r
689 * Updates the <textarea> element that has been replaced by the editor with
\r
690 * the current data available in the editor.
\r
692 * CKEDITOR.instances.editor1.updateElement();
\r
693 * alert( document.getElementById( 'editor1' ).value ); // The current editor data.
\r
695 updateElement : function()
\r
697 var element = this.element;
\r
698 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
\r
700 var data = this.getData();
\r
702 if ( this.config.htmlEncodeOutput )
\r
703 data = CKEDITOR.tools.htmlEncode( data );
\r
705 if ( element.is( 'textarea' ) )
\r
706 element.setValue( data );
\r
708 element.setHtml( data );
\r
713 CKEDITOR.on( 'loaded', function()
\r
715 // Run the full initialization for pending editors.
\r
716 var pending = CKEDITOR.editor._pending;
\r
719 delete CKEDITOR.editor._pending;
\r
721 for ( var i = 0 ; i < pending.length ; i++ )
\r
722 pending[ i ]._init();
\r
727 * Whether escape HTML when editor update original input element.
\r
728 * @name CKEDITOR.config.htmlEncodeOutput
\r
733 * config.htmlEncodeOutput = true;
\r
737 * Fired when a CKEDITOR instance is created, but still before initializing it.
\r
738 * To interact with a fully initialized instance, use the
\r
739 * {@link CKEDITOR#instanceReady} event instead.
\r
740 * @name CKEDITOR#instanceCreated
\r
742 * @param {CKEDITOR.editor} editor The editor instance that has been created.
\r
746 * Fired when a CKEDITOR instance is destroyed.
\r
747 * @name CKEDITOR#instanceDestroyed
\r
749 * @param {CKEDITOR.editor} editor The editor instance that has been destroyed.
\r
753 * Fired when all plugins are loaded and initialized into the editor instance.
\r
754 * @name CKEDITOR#pluginsLoaded
\r