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 var config = editor.config;
\r
150 config.contentsLangDirection == 'ui' && ( config.contentsLangDirection = editor.lang.dir );
\r
152 loadPlugins( editor );
\r
156 var loadPlugins = function( editor )
\r
158 var config = editor.config,
\r
159 plugins = config.plugins,
\r
160 extraPlugins = config.extraPlugins,
\r
161 removePlugins = config.removePlugins;
\r
163 if ( extraPlugins )
\r
165 // Remove them first to avoid duplications.
\r
166 var removeRegex = new RegExp( '(?:^|,)(?:' + extraPlugins.replace( /\s*,\s*/g, '|' ) + ')(?=,|$)' , 'g' );
\r
167 plugins = plugins.replace( removeRegex, '' );
\r
169 plugins += ',' + extraPlugins;
\r
172 if ( removePlugins )
\r
174 removeRegex = new RegExp( '(?:^|,)(?:' + removePlugins.replace( /\s*,\s*/g, '|' ) + ')(?=,|$)' , 'g' );
\r
175 plugins = plugins.replace( removeRegex, '' );
\r
178 // Load all plugins defined in the "plugins" setting.
\r
179 CKEDITOR.plugins.load( plugins.split( ',' ), function( plugins )
\r
181 // The list of plugins.
\r
182 var pluginsArray = [];
\r
184 // The language code to get loaded for each plugin. Null
\r
185 // entries will be appended for plugins with no language files.
\r
186 var languageCodes = [];
\r
188 // The list of URLs to language files.
\r
189 var languageFiles = [];
\r
191 // Cache the loaded plugin names.
\r
192 editor.plugins = plugins;
\r
194 // Loop through all plugins, to build the list of language
\r
195 // files to get loaded.
\r
196 for ( var pluginName in plugins )
\r
198 var plugin = plugins[ pluginName ],
\r
199 pluginLangs = plugin.lang,
\r
200 pluginPath = CKEDITOR.plugins.getPath( pluginName ),
\r
203 // Set the plugin path in the plugin.
\r
204 plugin.path = pluginPath;
\r
206 // If the plugin has "lang".
\r
209 // Resolve the plugin language. If the current language
\r
210 // is not available, get the first one (default one).
\r
211 lang = ( CKEDITOR.tools.indexOf( pluginLangs, editor.langCode ) >= 0 ? editor.langCode : pluginLangs[ 0 ] );
\r
213 if ( !plugin.lang[ lang ] )
\r
215 // Put the language file URL into the list of files to
\r
217 languageFiles.push( CKEDITOR.getUrl( pluginPath + 'lang/' + lang + '.js' ) );
\r
221 CKEDITOR.tools.extend( editor.lang, plugin.lang[ lang ] );
\r
226 // Save the language code, so we know later which
\r
227 // language has been resolved to this plugin.
\r
228 languageCodes.push( lang );
\r
230 pluginsArray.push( plugin );
\r
233 // Load all plugin specific language files in a row.
\r
234 CKEDITOR.scriptLoader.load( languageFiles, function()
\r
236 // Initialize all plugins that have the "beforeInit" and "init" methods defined.
\r
237 var methods = [ 'beforeInit', 'init', 'afterInit' ];
\r
238 for ( var m = 0 ; m < methods.length ; m++ )
\r
240 for ( var i = 0 ; i < pluginsArray.length ; i++ )
\r
242 var plugin = pluginsArray[ i ];
\r
244 // Uses the first loop to update the language entries also.
\r
245 if ( m === 0 && languageCodes[ i ] && plugin.lang )
\r
246 CKEDITOR.tools.extend( editor.lang, plugin.lang[ languageCodes[ i ] ] );
\r
248 // Call the plugin method (beforeInit and init).
\r
249 if ( plugin[ methods[ m ] ] )
\r
250 plugin[ methods[ m ] ]( editor );
\r
254 // Load the editor skin.
\r
255 editor.fire( 'pluginsLoaded' );
\r
256 loadTheme( editor );
\r
261 var loadSkin = function( editor )
\r
263 CKEDITOR.skins.load( editor, 'editor', function()
\r
265 loadLang( editor );
\r
269 var loadTheme = function( editor )
\r
271 var theme = editor.config.theme;
\r
272 CKEDITOR.themes.load( theme, function()
\r
274 var editorTheme = editor.theme = CKEDITOR.themes.get( theme );
\r
275 editorTheme.path = CKEDITOR.themes.getPath( theme );
\r
276 editorTheme.build( editor );
\r
278 if ( editor.config.autoUpdateElement )
\r
279 attachToForm( editor );
\r
283 var attachToForm = function( editor )
\r
285 var element = editor.element;
\r
287 // If are replacing a textarea, we must
\r
288 if ( editor.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE && element.is( 'textarea' ) )
\r
290 var form = element.$.form && new CKEDITOR.dom.element( element.$.form );
\r
293 function onSubmit()
\r
295 editor.updateElement();
\r
297 form.on( 'submit',onSubmit );
\r
299 // Setup the submit function because it doesn't fire the
\r
301 if ( !form.$.submit.nodeName )
\r
303 form.$.submit = CKEDITOR.tools.override( form.$.submit, function( originalSubmit )
\r
307 editor.updateElement();
\r
309 // For IE, the DOM submit function is not a
\r
310 // function, so we need thid check.
\r
311 if ( originalSubmit.apply )
\r
312 originalSubmit.apply( this, arguments );
\r
319 // Remove 'submit' events registered on form element before destroying.(#3988)
\r
320 editor.on( 'destroy', function()
\r
322 form.removeListener( 'submit', onSubmit );
\r
328 function updateCommandsMode()
\r
331 commands = this._.commands,
\r
334 for ( var name in commands )
\r
336 command = commands[ name ];
\r
337 command[ command.startDisabled ? 'disable' : command.modes[ mode ] ? 'enable' : 'disable' ]();
\r
342 * Initializes the editor instance. This function is called by the editor
\r
343 * contructor (editor_basic.js).
\r
346 CKEDITOR.editor.prototype._init = function()
\r
348 // Get the properties that have been saved in the editor_base
\r
350 var element = CKEDITOR.dom.element.get( this._.element ),
\r
351 instanceConfig = this._.instanceConfig;
\r
352 delete this._.element;
\r
353 delete this._.instanceConfig;
\r
355 this._.commands = {};
\r
356 this._.styles = [];
\r
359 * The DOM element that has been replaced by this editor instance. This
\r
360 * element holds the editor data on load and post.
\r
361 * @name CKEDITOR.editor.prototype.element
\r
362 * @type CKEDITOR.dom.element
\r
364 * var editor = CKEDITOR.instances.editor1;
\r
365 * alert( <b>editor.element</b>.getName() ); "textarea"
\r
367 this.element = element;
\r
370 * The editor instance name. It hay be the replaced element id, name or
\r
371 * a default name using a progressive counter (editor1, editor2, ...).
\r
372 * @name CKEDITOR.editor.prototype.name
\r
375 * var editor = CKEDITOR.instances.editor1;
\r
376 * alert( <b>editor.name</b> ); "editor1"
\r
378 this.name = ( element && ( this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
\r
379 && ( element.getId() || element.getNameAtt() ) )
\r
382 if ( this.name in CKEDITOR.instances )
\r
383 throw '[CKEDITOR.editor] The instance "' + this.name + '" already exists.';
\r
386 * The configurations for this editor instance. It inherits all
\r
387 * settings defined in (@link CKEDITOR.config}, combined with settings
\r
388 * loaded from custom configuration files and those defined inline in
\r
389 * the page when creating the editor.
\r
390 * @name CKEDITOR.editor.prototype.config
\r
393 * var editor = CKEDITOR.instances.editor1;
\r
394 * alert( <b>editor.config.theme</b> ); "default" e.g.
\r
396 this.config = CKEDITOR.tools.prototypedCopy( CKEDITOR.config );
\r
399 * Namespace containing UI features related to this editor instance.
\r
400 * @name CKEDITOR.editor.prototype.ui
\r
401 * @type CKEDITOR.ui
\r
404 this.ui = new CKEDITOR.ui( this );
\r
407 * Controls the focus state of this editor instance. This property
\r
408 * is rarely used for normal API operations. It is mainly
\r
409 * destinated to developer adding UI elements to the editor interface.
\r
410 * @name CKEDITOR.editor.prototype.focusManager
\r
411 * @type CKEDITOR.focusManager
\r
414 this.focusManager = new CKEDITOR.focusManager( this );
\r
416 CKEDITOR.fire( 'instanceCreated', null, this );
\r
418 this.on( 'mode', updateCommandsMode, null, null, 1 );
\r
420 initConfig( this, instanceConfig );
\r
424 CKEDITOR.tools.extend( CKEDITOR.editor.prototype,
\r
425 /** @lends CKEDITOR.editor.prototype */
\r
428 * Adds a command definition to the editor instance. Commands added with
\r
429 * this function can be later executed with {@link #execCommand}.
\r
430 * @param {String} commandName The indentifier name of the command.
\r
431 * @param {CKEDITOR.commandDefinition} commandDefinition The command definition.
\r
433 * editorInstance.addCommand( 'sample',
\r
435 * exec : function( editor )
\r
437 * alert( 'Executing a command for the editor name "' + editor.name + '"!' );
\r
441 addCommand : function( commandName, commandDefinition )
\r
443 return this._.commands[ commandName ] = new CKEDITOR.command( this, commandDefinition );
\r
447 * Add a trunk of css text to the editor which will be applied to the wysiwyg editing document.
\r
448 * Note: This function should be called before editor is loaded to take effect.
\r
449 * @param css {String} CSS text.
\r
451 * editorInstance.addCss( 'body { background-color: grey; }' );
\r
453 addCss : function( css )
\r
455 this._.styles.push( css );
\r
459 * Destroys the editor instance, releasing all resources used by it.
\r
460 * If the editor replaced an element, the element will be recovered.
\r
461 * @param {Boolean} [noUpdate] If the instance is replacing a DOM
\r
462 * element, this parameter indicates whether or not to update the
\r
463 * element with the instance contents.
\r
465 * alert( CKEDITOR.instances.editor1 ); e.g "object"
\r
466 * <b>CKEDITOR.instances.editor1.destroy()</b>;
\r
467 * alert( CKEDITOR.instances.editor1 ); "undefined"
\r
469 destroy : function( noUpdate )
\r
472 this.updateElement();
\r
476 // -> currentMode.unload( holderElement );
\r
477 this._.modes[ this.mode ].unload( this.getThemeSpace( 'contents' ) );
\r
480 this.theme.destroy( this );
\r
488 if ( this.toolbox )
\r
490 toolbars = this.toolbox.toolbars;
\r
491 for ( ; index < toolbars.length ; index++ )
\r
493 items = toolbars[ index ].items;
\r
494 for ( j = 0 ; j < items.length ; j++ )
\r
496 instance = items[ j ];
\r
497 if ( instance.clickFn ) CKEDITOR.tools.removeFunction( instance.clickFn );
\r
498 if ( instance.keyDownFn ) CKEDITOR.tools.removeFunction( instance.keyDownFn );
\r
500 if ( instance.index ) CKEDITOR.ui.button._.instances[ instance.index ] = null;
\r
505 if ( this.contextMenu )
\r
506 CKEDITOR.tools.removeFunction( this.contextMenu._.functionId );
\r
508 if ( this._.filebrowserFn )
\r
509 CKEDITOR.tools.removeFunction( this._.filebrowserFn );
\r
511 this.fire( 'destroy' );
\r
512 CKEDITOR.remove( this );
\r
513 CKEDITOR.fire( 'instanceDestroyed', null, this );
\r
517 * Executes a command.
\r
518 * @param {String} commandName The indentifier name of the command.
\r
519 * @param {Object} [data] Data to be passed to the command
\r
520 * @returns {Boolean} "true" if the command has been successfuly
\r
521 * executed, otherwise "false".
\r
523 * editorInstance.execCommand( 'Bold' );
\r
525 execCommand : function( commandName, data )
\r
527 var command = this.getCommand( commandName );
\r
536 if ( command && command.state != CKEDITOR.TRISTATE_DISABLED )
\r
538 if ( this.fire( 'beforeCommandExec', eventData ) !== true )
\r
540 eventData.returnValue = command.exec( eventData.commandData );
\r
542 // Fire the 'afterCommandExec' immediately if command is synchronous.
\r
543 if ( !command.async && this.fire( 'afterCommandExec', eventData ) !== true )
\r
544 return eventData.returnValue;
\r
548 // throw 'Unknown command name "' + commandName + '"';
\r
553 * Gets one of the registered commands. Note that, after registering a
\r
554 * command definition with addCommand, it is transformed internally
\r
555 * into an instance of {@link CKEDITOR.command}, which will be then
\r
556 * returned by this function.
\r
557 * @param {String} commandName The name of the command to be returned.
\r
558 * This is the same used to register the command with addCommand.
\r
559 * @returns {CKEDITOR.command} The command object identified by the
\r
562 getCommand : function( commandName )
\r
564 return this._.commands[ commandName ];
\r
568 * Gets the editor data. The data will be in raw format. It is the same
\r
569 * data that is posted by the editor.
\r
571 * @returns (String) The editor data.
\r
573 * if ( CKEDITOR.instances.editor1.<b>getData()</b> == '' )
\r
574 * alert( 'There is no data available' );
\r
576 getData : function()
\r
578 this.fire( 'beforeGetData' );
\r
580 var eventData = this._.data;
\r
582 if ( typeof eventData != 'string' )
\r
584 var element = this.element;
\r
585 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
\r
586 eventData = element.is( 'textarea' ) ? element.getValue() : element.getHtml();
\r
591 eventData = { dataValue : eventData };
\r
593 // Fire "getData" so data manipulation may happen.
\r
594 this.fire( 'getData', eventData );
\r
596 return eventData.dataValue;
\r
599 getSnapshot : function()
\r
601 var data = this.fire( 'getSnapshot' );
\r
603 if ( typeof data != 'string' )
\r
605 var element = this.element;
\r
606 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
\r
607 data = element.is( 'textarea' ) ? element.getValue() : element.getHtml();
\r
613 loadSnapshot : function( snapshot )
\r
615 this.fire( 'loadSnapshot', snapshot );
\r
619 * Sets the editor data. The data must be provided in raw format (HTML).<br />
\r
621 * Note that this menthod is asynchronous. The "callback" parameter must
\r
622 * be used if interaction with the editor is needed after setting the data.
\r
623 * @param {String} data HTML code to replace the curent content in the
\r
625 * @param {Function} callback Function to be called after the setData
\r
628 * CKEDITOR.instances.editor1.<b>setData</b>( '<p>This is the editor data.</p>' );
\r
630 * CKEDITOR.instances.editor1.<b>setData</b>( '<p>Some other editor data.</p>', function()
\r
632 * this.checkDirty(); // true
\r
635 setData : function( data , callback )
\r
639 this.on( 'dataReady', function( evt )
\r
641 evt.removeListener();
\r
642 callback.call( evt.editor );
\r
646 // Fire "setData" so data manipulation may happen.
\r
647 var eventData = { dataValue : data };
\r
648 this.fire( 'setData', eventData );
\r
650 this._.data = eventData.dataValue;
\r
652 this.fire( 'afterSetData', eventData );
\r
656 * Inserts HTML into the currently selected position in the editor.
\r
657 * @param {String} data HTML code to be inserted into the editor.
\r
659 * CKEDITOR.instances.editor1.<b>insertHtml( '<p>This is a new paragraph.</p>' )</b>;
\r
661 insertHtml : function( data )
\r
663 this.fire( 'insertHtml', data );
\r
667 * Inserts an element into the currently selected position in the
\r
669 * @param {CKEDITOR.dom.element} element The element to be inserted
\r
672 * var element = CKEDITOR.dom.element.createFromHtml( '<img src="hello.png" border="0" title="Hello" />' );
\r
673 * CKEDITOR.instances.editor1.<b>insertElement( element )</b>;
\r
675 insertElement : function( element )
\r
677 this.fire( 'insertElement', element );
\r
680 checkDirty : function()
\r
682 return ( this.mayBeDirty && this._.previousValue !== this.getSnapshot() );
\r
685 resetDirty : function()
\r
687 if ( this.mayBeDirty )
\r
688 this._.previousValue = this.getSnapshot();
\r
692 * Updates the <textarea> element that has been replaced by the editor with
\r
693 * the current data available in the editor.
\r
695 * CKEDITOR.instances.editor1.updateElement();
\r
696 * alert( document.getElementById( 'editor1' ).value ); // The current editor data.
\r
698 updateElement : function()
\r
700 var element = this.element;
\r
701 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
\r
703 var data = this.getData();
\r
705 if ( this.config.htmlEncodeOutput )
\r
706 data = CKEDITOR.tools.htmlEncode( data );
\r
708 if ( element.is( 'textarea' ) )
\r
709 element.setValue( data );
\r
711 element.setHtml( data );
\r
716 CKEDITOR.on( 'loaded', function()
\r
718 // Run the full initialization for pending editors.
\r
719 var pending = CKEDITOR.editor._pending;
\r
722 delete CKEDITOR.editor._pending;
\r
724 for ( var i = 0 ; i < pending.length ; i++ )
\r
725 pending[ i ]._init();
\r
730 * Whether escape HTML when editor update original input element.
\r
731 * @name CKEDITOR.config.htmlEncodeOutput
\r
736 * config.htmlEncodeOutput = true;
\r
740 * Fired when a CKEDITOR instance is created, but still before initializing it.
\r
741 * To interact with a fully initialized instance, use the
\r
742 * {@link CKEDITOR#instanceReady} event instead.
\r
743 * @name CKEDITOR#instanceCreated
\r
745 * @param {CKEDITOR.editor} editor The editor instance that has been created.
\r
749 * Fired when a CKEDITOR instance is destroyed.
\r
750 * @name CKEDITOR#instanceDestroyed
\r
752 * @param {CKEDITOR.editor} editor The editor instance that has been destroyed.
\r
756 * Fired when all plugins are loaded and initialized into the editor instance.
\r
757 * @name CKEDITOR#pluginsLoaded
\r