Nation Notes module contributed by Z&H Healthcare.
[openemr.git] / library / custom_template / ckeditor / _source / core / editor.js
blobbd66d933064f1f92f7b83db9d373ce499266d8e5
1 /*
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
4 */
6 /**
7  * @fileOverview Defines the {@link CKEDITOR.editor} class, which represents an
8  *              editor instance.
9  */
11 (function()
13         // The counter for automatic instance names.
14         var nameCounter = 0;
16         var getNewName = function()
17         {
18                 var name = 'editor' + ( ++nameCounter );
19                 return ( CKEDITOR.instances && CKEDITOR.instances[ name ] ) ? getNewName() : name;
20         };
22         // ##### START: Config Privates
24         // These function loads custom configuration files and cache the
25         // CKEDITOR.editorConfig functions defined on them, so there is no need to
26         // download them more than once for several instances.
27         var loadConfigLoaded = {};
28         var loadConfig = function( editor )
29         {
30                 var customConfig = editor.config.customConfig;
32                 // Check if there is a custom config to load.
33                 if ( !customConfig )
34                         return false;
36                 customConfig = CKEDITOR.getUrl( customConfig );
38                 var loadedConfig = loadConfigLoaded[ customConfig ] || ( loadConfigLoaded[ customConfig ] = {} );
40                 // If the custom config has already been downloaded, reuse it.
41                 if ( loadedConfig.fn )
42                 {
43                         // Call the cached CKEDITOR.editorConfig defined in the custom
44                         // config file for the editor instance depending on it.
45                         loadedConfig.fn.call( editor, editor.config );
47                         // If there is no other customConfig in the chain, fire the
48                         // "configLoaded" event.
49                         if ( CKEDITOR.getUrl( editor.config.customConfig ) == customConfig || !loadConfig( editor ) )
50                                 editor.fireOnce( 'customConfigLoaded' );
51                 }
52                 else
53                 {
54                         // Load the custom configuration file.
55                         CKEDITOR.scriptLoader.load( customConfig, function()
56                                 {
57                                         // If the CKEDITOR.editorConfig function has been properly
58                                         // defined in the custom configuration file, cache it.
59                                         if ( CKEDITOR.editorConfig )
60                                                 loadedConfig.fn = CKEDITOR.editorConfig;
61                                         else
62                                                 loadedConfig.fn = function(){};
64                                         // Call the load config again. This time the custom
65                                         // config is already cached and so it will get loaded.
66                                         loadConfig( editor );
67                                 });
68                 }
70                 return true;
71         };
73         var initConfig = function( editor, instanceConfig )
74         {
75                 // Setup the lister for the "customConfigLoaded" event.
76                 editor.on( 'customConfigLoaded', function()
77                         {
78                                 if ( instanceConfig )
79                                 {
80                                         // Register the events that may have been set at the instance
81                                         // configuration object.
82                                         if ( instanceConfig.on )
83                                         {
84                                                 for ( var eventName in instanceConfig.on )
85                                                 {
86                                                         editor.on( eventName, instanceConfig.on[ eventName ] );
87                                                 }
88                                         }
90                                         // Overwrite the settings from the in-page config.
91                                         CKEDITOR.tools.extend( editor.config, instanceConfig, true );
93                                         delete editor.config.on;
94                                 }
96                                 onConfigLoaded( editor );
97                         });
99                 // The instance config may override the customConfig setting to avoid
100                 // loading the default ~/config.js file.
101                 if ( instanceConfig && instanceConfig.customConfig != undefined )
102                         editor.config.customConfig = instanceConfig.customConfig;
104                 // Load configs from the custom configuration files.
105                 if ( !loadConfig( editor ) )
106                         editor.fireOnce( 'customConfigLoaded' );
107         };
109         // ##### END: Config Privates
111         var onConfigLoaded = function( editor )
112         {
113                 // Set config related properties.
115                 var skin = editor.config.skin.split( ',' ),
116                         skinName = skin[ 0 ],
117                         skinPath = CKEDITOR.getUrl( skin[ 1 ] || (
118                                 '_source/' +    // @Packager.RemoveLine
119                                 'skins/' + skinName + '/' ) );
121                 /**
122                  * The name of the skin used by this editor instance. The skin name can
123                  * be set though the {@link CKEDITOR.config.skin} setting.
124                  * @name CKEDITOR.editor.prototype.skinName
125                  * @type String
126                  * @example
127                  * alert( editor.skinName );  // "kama" (e.g.)
128                  */
129                 editor.skinName = skinName;
131                 /**
132                  * The full URL of the skin directory.
133                  * @name CKEDITOR.editor.prototype.skinPath
134                  * @type String
135                  * @example
136                  * alert( editor.skinPath );  // "http://example.com/ckeditor/skins/kama/" (e.g.)
137                  */
138                 editor.skinPath = skinPath;
140                 /**
141                  * The CSS class name used for skin identification purposes.
142                  * @name CKEDITOR.editor.prototype.skinClass
143                  * @type String
144                  * @example
145                  * alert( editor.skinClass );  // "cke_skin_kama" (e.g.)
146                  */
147                 editor.skinClass = 'cke_skin_' + skinName;
149                 /**
150                  * The <a href="http://en.wikipedia.org/wiki/Tabbing_navigation">tabbing
151                  * navigation</a> order that has been calculated for this editor
152                  * instance. This can be set by the {@link CKEDITOR.config.tabIndex}
153                  * setting or taken from the "tabindex" attribute of the
154                  * {@link #element} associated to the editor.
155                  * @name CKEDITOR.editor.prototype.tabIndex
156                  * @type Number
157                  * @default 0 (zero)
158                  * @example
159                  * alert( editor.tabIndex );  // "0" (e.g.)
160                  */
161                 editor.tabIndex = editor.config.tabIndex || editor.element.getAttribute( 'tabindex' ) || 0;
163                 // Fire the "configLoaded" event.
164                 editor.fireOnce( 'configLoaded' );
166                 // Load language file.
167                 loadSkin( editor );
168         };
170         var loadLang = function( editor )
171         {
172                 CKEDITOR.lang.load( editor.config.language, editor.config.defaultLanguage, function( languageCode, lang )
173                         {
174                                 /**
175                                  * The code for the language resources that have been loaded
176                                  * for the user internface elements of this editor instance.
177                                  * @name CKEDITOR.editor.prototype.langCode
178                                  * @type String
179                                  * @example
180                                  * alert( editor.langCode );  // "en" (e.g.)
181                                  */
182                                 editor.langCode = languageCode;
184                                 /**
185                                  * An object holding all language strings used by the editor
186                                  * interface.
187                                  * @name CKEDITOR.editor.prototype.lang
188                                  * @type CKEDITOR.lang
189                                  * @example
190                                  * alert( editor.lang.bold );  // "Negrito" (e.g. if language is Portuguese)
191                                  */
192                                 // As we'll be adding plugin specific entries that could come
193                                 // from different language code files, we need a copy of lang,
194                                 // not a direct reference to it.
195                                 editor.lang = CKEDITOR.tools.prototypedCopy( lang );
197                                 // We're not able to support RTL in Firefox 2 at this time.
198                                 if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 && editor.lang.dir == 'rtl' )
199                                         editor.lang.dir = 'ltr';
201                                 var config = editor.config;
202                                 config.contentsLangDirection == 'ui' && ( config.contentsLangDirection = editor.lang.dir );
204                                 loadPlugins( editor );
205                         });
206         };
208         var loadPlugins = function( editor )
209         {
210                 var config                      = editor.config,
211                         plugins                 = config.plugins,
212                         extraPlugins    = config.extraPlugins,
213                         removePlugins   = config.removePlugins;
215                 if ( extraPlugins )
216                 {
217                         // Remove them first to avoid duplications.
218                         var removeRegex = new RegExp( '(?:^|,)(?:' + extraPlugins.replace( /\s*,\s*/g, '|' ) + ')(?=,|$)' , 'g' );
219                         plugins = plugins.replace( removeRegex, '' );
221                         plugins += ',' + extraPlugins;
222                 }
224                 if ( removePlugins )
225                 {
226                         removeRegex = new RegExp( '(?:^|,)(?:' + removePlugins.replace( /\s*,\s*/g, '|' ) + ')(?=,|$)' , 'g' );
227                         plugins = plugins.replace( removeRegex, '' );
228                 }
230                 // Load the Adobe AIR plugin conditionally.
231                 CKEDITOR.env.air && ( plugins += ',adobeair' );
233                 // Load all plugins defined in the "plugins" setting.
234                 CKEDITOR.plugins.load( plugins.split( ',' ), function( plugins )
235                         {
236                                 // The list of plugins.
237                                 var pluginsArray = [];
239                                 // The language code to get loaded for each plugin. Null
240                                 // entries will be appended for plugins with no language files.
241                                 var languageCodes = [];
243                                 // The list of URLs to language files.
244                                 var languageFiles = [];
246                                 /**
247                                  * And object holding references to all plugins used by this
248                                  * editor istance.
249                                  * @name CKEDITOR.editor.prototype.plugins
250                                  * @type Object
251                                  * @example
252                                  * alert( editor.plugins.dialog.path );  // "http://example.com/ckeditor/plugins/dialog/" (e.g.)
253                                  */
254                                 editor.plugins = plugins;
256                                 // Loop through all plugins, to build the list of language
257                                 // files to get loaded.
258                                 for ( var pluginName in plugins )
259                                 {
260                                         var plugin = plugins[ pluginName ],
261                                                 pluginLangs = plugin.lang,
262                                                 pluginPath = CKEDITOR.plugins.getPath( pluginName ),
263                                                 lang = null;
265                                         // Set the plugin path in the plugin.
266                                         plugin.path = pluginPath;
268                                         // If the plugin has "lang".
269                                         if ( pluginLangs )
270                                         {
271                                                 // Resolve the plugin language. If the current language
272                                                 // is not available, get the first one (default one).
273                                                 lang = ( CKEDITOR.tools.indexOf( pluginLangs, editor.langCode ) >= 0 ? editor.langCode : pluginLangs[ 0 ] );
275                                                 if ( !plugin.lang[ lang ] )
276                                                 {
277                                                         // Put the language file URL into the list of files to
278                                                         // get downloaded.
279                                                         languageFiles.push( CKEDITOR.getUrl( pluginPath + 'lang/' + lang + '.js' ) );
280                                                 }
281                                                 else
282                                                 {
283                                                         CKEDITOR.tools.extend( editor.lang, plugin.lang[ lang ] );
284                                                         lang = null;
285                                                 }
286                                         }
288                                         // Save the language code, so we know later which
289                                         // language has been resolved to this plugin.
290                                         languageCodes.push( lang );
292                                         pluginsArray.push( plugin );
293                                 }
295                                 // Load all plugin specific language files in a row.
296                                 CKEDITOR.scriptLoader.load( languageFiles, function()
297                                         {
298                                                 // Initialize all plugins that have the "beforeInit" and "init" methods defined.
299                                                 var methods = [ 'beforeInit', 'init', 'afterInit' ];
300                                                 for ( var m = 0 ; m < methods.length ; m++ )
301                                                 {
302                                                         for ( var i = 0 ; i < pluginsArray.length ; i++ )
303                                                         {
304                                                                 var plugin = pluginsArray[ i ];
306                                                                 // Uses the first loop to update the language entries also.
307                                                                 if ( m === 0 && languageCodes[ i ] && plugin.lang )
308                                                                         CKEDITOR.tools.extend( editor.lang, plugin.lang[ languageCodes[ i ] ] );
310                                                                 // Call the plugin method (beforeInit and init).
311                                                                 if ( plugin[ methods[ m ] ] )
312                                                                         plugin[ methods[ m ] ]( editor );
313                                                         }
314                                                 }
316                                                 // Load the editor skin.
317                                                 editor.fire( 'pluginsLoaded' );
318                                                 loadTheme( editor );
319                                         });
320                         });
321         };
323         var loadSkin = function( editor )
324         {
325                 CKEDITOR.skins.load( editor, 'editor', function()
326                         {
327                                 loadLang( editor );
328                         });
329         };
331         var loadTheme = function( editor )
332         {
333                 var theme = editor.config.theme;
334                 CKEDITOR.themes.load( theme, function()
335                         {
336                                 /**
337                                  * The theme used by this editor instance.
338                                  * @name CKEDITOR.editor.prototype.theme
339                                  * @type CKEDITOR.theme
340                                  * @example
341                                  * alert( editor.theme );  "http://example.com/ckeditor/themes/default/" (e.g.)
342                                  */
343                                 var editorTheme = editor.theme = CKEDITOR.themes.get( theme );
344                                 editorTheme.path = CKEDITOR.themes.getPath( theme );
345                                 editorTheme.build( editor );
347                                 if ( editor.config.autoUpdateElement )
348                                         attachToForm( editor );
349                         });
350         };
352         var attachToForm = function( editor )
353         {
354                 var element = editor.element;
356                 // If are replacing a textarea, we must
357                 if ( editor.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE && element.is( 'textarea' ) )
358                 {
359                         var form = element.$.form && new CKEDITOR.dom.element( element.$.form );
360                         if ( form )
361                         {
362                                 function onSubmit()
363                                 {
364                                         editor.updateElement();
365                                 }
366                                 form.on( 'submit',onSubmit );
368                                 // Setup the submit function because it doesn't fire the
369                                 // "submit" event.
370                                 if ( !form.$.submit.nodeName && !form.$.submit.length )
371                                 {
372                                         form.$.submit = CKEDITOR.tools.override( form.$.submit, function( originalSubmit )
373                                                 {
374                                                         return function()
375                                                                 {
376                                                                         editor.updateElement();
378                                                                         // For IE, the DOM submit function is not a
379                                                                         // function, so we need thid check.
380                                                                         if ( originalSubmit.apply )
381                                                                                 originalSubmit.apply( this, arguments );
382                                                                         else
383                                                                                 originalSubmit();
384                                                                 };
385                                                 });
386                                 }
388                                 // Remove 'submit' events registered on form element before destroying.(#3988)
389                                 editor.on( 'destroy', function()
390                                 {
391                                         form.removeListener( 'submit', onSubmit );
392                                 } );
393                         }
394                 }
395         };
397         function updateCommandsMode()
398         {
399                 var command,
400                         commands = this._.commands,
401                         mode = this.mode;
403                 for ( var name in commands )
404                 {
405                         command = commands[ name ];
406                         command[ command.startDisabled ? 'disable' : command.modes[ mode ] ? 'enable' : 'disable' ]();
407                 }
408         }
410         /**
411          * Initializes the editor instance. This function is called by the editor
412          * contructor (editor_basic.js).
413          * @private
414          */
415         CKEDITOR.editor.prototype._init = function()
416                 {
417                         // Get the properties that have been saved in the editor_base
418                         // implementation.
419                         var element                     = CKEDITOR.dom.element.get( this._.element ),
420                                 instanceConfig  = this._.instanceConfig;
421                         delete this._.element;
422                         delete this._.instanceConfig;
424                         this._.commands = {};
425                         this._.styles = [];
427                         /**
428                          * The DOM element that has been replaced by this editor instance. This
429                          * element holds the editor data on load and post.
430                          * @name CKEDITOR.editor.prototype.element
431                          * @type CKEDITOR.dom.element
432                          * @example
433                          * var editor = CKEDITOR.instances.editor1;
434                          * alert( <b>editor.element</b>.getName() );  "textarea"
435                          */
436                         this.element = element;
438                         /**
439                          * The editor instance name. It hay be the replaced element id, name or
440                          * a default name using a progressive counter (editor1, editor2, ...).
441                          * @name CKEDITOR.editor.prototype.name
442                          * @type String
443                          * @example
444                          * var editor = CKEDITOR.instances.editor1;
445                          * alert( <b>editor.name</b> );  "editor1"
446                          */
447                         this.name = ( element && ( this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
448                                                         && ( element.getId() || element.getNameAtt() ) )
449                                                 || getNewName();
451                         if ( this.name in CKEDITOR.instances )
452                                 throw '[CKEDITOR.editor] The instance "' + this.name + '" already exists.';
454                         /**
455                          * A unique random string assigned to each editor instance in the page.
456                          * @name CKEDITOR.editor.prototype.id
457                          * @type String
458                          */
459                         this.id = CKEDITOR.tools.getNextId();
461                         /**
462                          * The configurations for this editor instance. It inherits all
463                          * settings defined in (@link CKEDITOR.config}, combined with settings
464                          * loaded from custom configuration files and those defined inline in
465                          * the page when creating the editor.
466                          * @name CKEDITOR.editor.prototype.config
467                          * @type Object
468                          * @example
469                          * var editor = CKEDITOR.instances.editor1;
470                          * alert( <b>editor.config.theme</b> );  "default" e.g.
471                          */
472                         this.config = CKEDITOR.tools.prototypedCopy( CKEDITOR.config );
474                         /**
475                          * Namespace containing UI features related to this editor instance.
476                          * @name CKEDITOR.editor.prototype.ui
477                          * @type CKEDITOR.ui
478                          * @example
479                          */
480                         this.ui = new CKEDITOR.ui( this );
482                         /**
483                          * Controls the focus state of this editor instance. This property
484                          * is rarely used for normal API operations. It is mainly
485                          * destinated to developer adding UI elements to the editor interface.
486                          * @name CKEDITOR.editor.prototype.focusManager
487                          * @type CKEDITOR.focusManager
488                          * @example
489                          */
490                         this.focusManager = new CKEDITOR.focusManager( this );
492                         CKEDITOR.fire( 'instanceCreated', null, this );
494                         this.on( 'mode', updateCommandsMode, null, null, 1 );
496                         initConfig( this, instanceConfig );
497                 };
498 })();
500 CKEDITOR.tools.extend( CKEDITOR.editor.prototype,
501         /** @lends CKEDITOR.editor.prototype */
502         {
503                 /**
504                  * Adds a command definition to the editor instance. Commands added with
505                  * this function can be later executed with {@link #execCommand}.
506                  * @param {String} commandName The indentifier name of the command.
507                  * @param {CKEDITOR.commandDefinition} commandDefinition The command definition.
508                  * @example
509                  * editorInstance.addCommand( 'sample',
510                  * {
511                  *     exec : function( editor )
512                  *     {
513                  *         alert( 'Executing a command for the editor name "' + editor.name + '"!' );
514                  *     }
515                  * });
516                  */
517                 addCommand : function( commandName, commandDefinition )
518                 {
519                         return this._.commands[ commandName ] = new CKEDITOR.command( this, commandDefinition );
520                 },
522                 /**
523                  * Add a trunk of css text to the editor which will be applied to the wysiwyg editing document.
524                  * Note: This function should be called before editor is loaded to take effect.
525                  * @param css {String} CSS text.
526                  * @example
527                  * editorInstance.addCss( 'body { background-color: grey; }' );
528                  */
529                 addCss : function( css )
530                 {
531                         this._.styles.push( css );
532                 },
534                 /**
535                  * Destroys the editor instance, releasing all resources used by it.
536                  * If the editor replaced an element, the element will be recovered.
537                  * @param {Boolean} [noUpdate] If the instance is replacing a DOM
538                  *              element, this parameter indicates whether or not to update the
539                  *              element with the instance contents.
540                  * @example
541                  * alert( CKEDITOR.instances.editor1 );  e.g "object"
542                  * <b>CKEDITOR.instances.editor1.destroy()</b>;
543                  * alert( CKEDITOR.instances.editor1 );  "undefined"
544                  */
545                 destroy : function( noUpdate )
546                 {
547                         if ( !noUpdate )
548                                 this.updateElement();
550                         this.fire( 'destroy' );
551                         this.theme && this.theme.destroy( this );
553                         CKEDITOR.remove( this );
554                         CKEDITOR.fire( 'instanceDestroyed', null, this );
555                 },
557                 /**
558                  * Executes a command.
559                  * @param {String} commandName The indentifier name of the command.
560                  * @param {Object} [data] Data to be passed to the command
561                  * @returns {Boolean} "true" if the command has been successfuly
562                  *              executed, otherwise "false".
563                  * @example
564                  * editorInstance.execCommand( 'Bold' );
565                  */
566                 execCommand : function( commandName, data )
567                 {
568                         var command = this.getCommand( commandName );
570                         var eventData =
571                         {
572                                 name: commandName,
573                                 commandData: data,
574                                 command: command
575                         };
577                         if ( command && command.state != CKEDITOR.TRISTATE_DISABLED )
578                         {
579                                 if ( this.fire( 'beforeCommandExec', eventData ) !== true )
580                                 {
581                                         eventData.returnValue = command.exec( eventData.commandData );
583                                         // Fire the 'afterCommandExec' immediately if command is synchronous.
584                                         if ( !command.async && this.fire( 'afterCommandExec', eventData ) !== true )
585                                                 return eventData.returnValue;
586                                 }
587                         }
589                         // throw 'Unknown command name "' + commandName + '"';
590                         return false;
591                 },
593                 /**
594                  * Gets one of the registered commands. Note that, after registering a
595                  * command definition with addCommand, it is transformed internally
596                  * into an instance of {@link CKEDITOR.command}, which will be then
597                  * returned by this function.
598                  * @param {String} commandName The name of the command to be returned.
599                  * This is the same used to register the command with addCommand.
600                  * @returns {CKEDITOR.command} The command object identified by the
601                  * provided name.
602                  */
603                 getCommand : function( commandName )
604                 {
605                         return this._.commands[ commandName ];
606                 },
608                 /**
609                  * Gets the editor data. The data will be in raw format. It is the same
610                  * data that is posted by the editor.
611                  * @type String
612                  * @returns (String) The editor data.
613                  * @example
614                  * if ( CKEDITOR.instances.editor1.<b>getData()</b> == '' )
615                  *     alert( 'There is no data available' );
616                  */
617                 getData : function()
618                 {
619                         this.fire( 'beforeGetData' );
621                         var eventData = this._.data;
623                         if ( typeof eventData != 'string' )
624                         {
625                                 var element = this.element;
626                                 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
627                                         eventData = element.is( 'textarea' ) ? element.getValue() : element.getHtml();
628                                 else
629                                         eventData = '';
630                         }
632                         eventData = { dataValue : eventData };
634                         // Fire "getData" so data manipulation may happen.
635                         this.fire( 'getData', eventData );
637                         return eventData.dataValue;
638                 },
640                 /**
641                  * Gets the "raw data" currently available in the editor. This is a
642                  * fast method which return the data as is, without processing, so it's
643                  * not recommended to use it on resulting pages. It can be used instead
644                  * combined with the {@link #loadSnapshot} so one can automatic save
645                  * the editor data from time to time while the user is using the
646                  * editor, to avoid data loss, without risking performance issues.
647                  * @example
648                  * alert( editor.getSnapshot() );
649                  */
650                 getSnapshot : function()
651                 {
652                         var data = this.fire( 'getSnapshot' );
654                         if ( typeof data != 'string' )
655                         {
656                                 var element = this.element;
657                                 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
658                                         data = element.is( 'textarea' ) ? element.getValue() : element.getHtml();
659                         }
661                         return data;
662                 },
664                 /**
665                  * Loads "raw data" in the editor. This data is loaded with processing
666                  * straight to the editing area. It should not be used as a way to load
667                  * any kind of data, but instead in combination with
668                  * {@link #getSnapshot} produced data.
669                  * @example
670                  * var data = editor.getSnapshot();
671                  * editor.<b>loadSnapshot( data )</b>;
672                  */
673                 loadSnapshot : function( snapshot )
674                 {
675                         this.fire( 'loadSnapshot', snapshot );
676                 },
678                 /**
679                  * Sets the editor data. The data must be provided in raw format (HTML).<br />
680                  * <br />
681                  * Note that this menthod is asynchronous. The "callback" parameter must
682                  * be used if interaction with the editor is needed after setting the data.
683                  * @param {String} data HTML code to replace the curent content in the
684                  *              editor.
685                  * @param {Function} callback Function to be called after the setData
686                  *              is completed.
687                  *@param {Boolean} internal Whether suppress  any event firing when copying data internally inside editor.
688                  * @example
689                  * CKEDITOR.instances.editor1.<b>setData</b>( '&lt;p&gt;This is the editor data.&lt;/p&gt;' );
690                  * @example
691                  * CKEDITOR.instances.editor1.<b>setData</b>( '&lt;p&gt;Some other editor data.&lt;/p&gt;', function()
692                  *     {
693                  *         this.checkDirty();    // true
694                  *     });
695                  */
696                 setData : function( data , callback, internal )
697                 {
698                         if( callback )
699                         {
700                                 this.on( 'dataReady', function( evt )
701                                 {
702                                         evt.removeListener();
703                                         callback.call( evt.editor );
704                                 } );
705                         }
707                         // Fire "setData" so data manipulation may happen.
708                         var eventData = { dataValue : data };
709                         !internal && this.fire( 'setData', eventData );
711                         this._.data = eventData.dataValue;
713                         !internal && this.fire( 'afterSetData', eventData );
714                 },
716                 /**
717                  * Inserts HTML into the currently selected position in the editor.
718                  * @param {String} data HTML code to be inserted into the editor.
719                  * @example
720                  * CKEDITOR.instances.editor1.<b>insertHtml( '&lt;p&gt;This is a new paragraph.&lt;/p&gt;' )</b>;
721                  */
722                 insertHtml : function( data )
723                 {
724                         this.fire( 'insertHtml', data );
725                 },
727                 /**
728                  * Insert text content into the currently selected position in the
729                  * editor, in WYSIWYG mode, styles of the selected element will be applied to the inserted text,
730                  * spaces around the text will be leaving untouched.
731                  * <strong>Note:</strong> two subsequent line-breaks will introduce one paragraph, which element depends on {@link CKEDITOR.config.enterMode};
732                  * A single line-break will be instead translated into one &lt;br /&gt;.
733                  * @since 3.5
734                  * @param {String} text Text to be inserted into the editor.
735                  * @example
736                  * CKEDITOR.instances.editor1.<b>insertText( ' line1 \n\n line2' )</b>;
737                  */
738                 insertText : function( text )
739                 {
740                         this.fire( 'insertText', text );
741                 },
743                 /**
744                  * Inserts an element into the currently selected position in the
745                  * editor.
746                  * @param {CKEDITOR.dom.element} element The element to be inserted
747                  *              into the editor.
748                  * @example
749                  * var element = CKEDITOR.dom.element.createFromHtml( '&lt;img src="hello.png" border="0" title="Hello" /&gt;' );
750                  * CKEDITOR.instances.editor1.<b>insertElement( element )</b>;
751                  */
752                 insertElement : function( element )
753                 {
754                         this.fire( 'insertElement', element );
755                 },
757                 /**
758                  * Checks whether the current editor contents present changes when
759                  * compared to the contents loaded into the editor at startup, or to
760                  * the contents available in the editor when {@link #resetDirty} has
761                  * been called.
762                  * @returns {Boolean} "true" is the contents present changes.
763                  * @example
764                  * function beforeUnload( e )
765                  * {
766                  *     if ( CKEDITOR.instances.editor1.<b>checkDirty()</b> )
767                  *              return e.returnValue = "You'll loose the changes made in the editor.";
768                  * }
769                  *
770                  * if ( window.addEventListener )
771                  *     window.addEventListener( 'beforeunload', beforeUnload, false );
772                  * else
773                  *     window.attachEvent( 'onbeforeunload', beforeUnload );
774                  */
775                 checkDirty : function()
776                 {
777                         return ( this.mayBeDirty && this._.previousValue !== this.getSnapshot() );
778                 },
780                 /**
781                  * Resets the "dirty state" of the editor so subsequent calls to
782                  * {@link #checkDirty} will return "false" if the user will not make
783                  * further changes to the contents.
784                  * @example
785                  * alert( editor.checkDirty() );  // "true" (e.g.)
786                  * editor.<b>resetDirty()</b>;
787                  * alert( editor.checkDirty() );  // "false"
788                  */
789                 resetDirty : function()
790                 {
791                         if ( this.mayBeDirty )
792                                 this._.previousValue = this.getSnapshot();
793                 },
795                 /**
796                  * Updates the &lt;textarea&gt; element that has been replaced by the editor with
797                  * the current data available in the editor.
798                  * @example
799                  * CKEDITOR.instances.editor1.updateElement();
800                  * alert( document.getElementById( 'editor1' ).value );  // The current editor data.
801                  */
802                 updateElement : function()
803                 {
804                         var element = this.element;
805                         if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
806                         {
807                                 var data = this.getData();
809                                 if ( this.config.htmlEncodeOutput )
810                                         data = CKEDITOR.tools.htmlEncode( data );
812                                 if ( element.is( 'textarea' ) )
813                                         element.setValue( data );
814                                 else
815                                         element.setHtml( data );
816                         }
817                 }
818         });
820 CKEDITOR.on( 'loaded', function()
821         {
822                 // Run the full initialization for pending editors.
823                 var pending = CKEDITOR.editor._pending;
824                 if ( pending )
825                 {
826                         delete CKEDITOR.editor._pending;
828                         for ( var i = 0 ; i < pending.length ; i++ )
829                                 pending[ i ]._init();
830                 }
831         });
834  * Whether escape HTML when editor update original input element.
835  * @name CKEDITOR.config.htmlEncodeOutput
836  * @since 3.1
837  * @type Boolean
838  * @default false
839  * @example
840  * config.htmlEncodeOutput = true;
841  */
844  * Fired when a CKEDITOR instance is created, but still before initializing it.
845  * To interact with a fully initialized instance, use the
846  * {@link CKEDITOR#instanceReady} event instead.
847  * @name CKEDITOR#instanceCreated
848  * @event
849  * @param {CKEDITOR.editor} editor The editor instance that has been created.
850  */
853  * Fired when a CKEDITOR instance is destroyed.
854  * @name CKEDITOR#instanceDestroyed
855  * @event
856  * @param {CKEDITOR.editor} editor The editor instance that has been destroyed.
857  */
860  * Fired when all plugins are loaded and initialized into the editor instance.
861  * @name CKEDITOR.editor#pluginsLoaded
862  * @event
863  * @param {CKEDITOR.editor} editor This editor instance.
864  */
867  * Fired before the command execution when {@link #execCommand} is called.
868  * @name CKEDITOR.editor#beforeCommandExec
869  * @event
870  * @param {CKEDITOR.editor} editor This editor instance.
871  * @param {String} data.name The command name.
872  * @param {Object} data.commandData The data to be sent to the command. This
873  *              can be manipulated by the event listener.
874  * @param {CKEDITOR.command} data.command The command itself.
875  */
878  * Fired after the command execution when {@link #execCommand} is called.
879  * @name CKEDITOR.editor#afterCommandExec
880  * @event
881  * @param {CKEDITOR.editor} editor This editor instance.
882  * @param {String} data.name The command name.
883  * @param {Object} data.commandData The data sent to the command.
884  * @param {CKEDITOR.command} data.command The command itself.
885  * @param {Object} data.returnValue The value returned by the command execution.
886  */
889  * Fired every custom configuration file is loaded, before the final
890  * configurations initialization.<br />
891  * <br />
892  * Custom configuration files can be loaded thorugh the
893  * {@link CKEDITOR.config.customConfig} setting. Several files can be loading
894  * by chaning this setting.
895  * @name CKEDITOR.editor#customConfigLoaded
896  * @event
897  * @param {CKEDITOR.editor} editor This editor instance.
898  * @example
899  */
902  * Fired once the editor configuration is ready (loaded and processed).
903  * @name CKEDITOR.editor#configLoaded
904  * @event
905  * @param {CKEDITOR.editor} editor This editor instance.
906  * @example
907  * if( editor.config.fullPage )
908  *     alert( 'This is a full page editor' );
909  */
912  * Fired when this editor instance is destroyed. The editor at this
913  * point isn't usable and this event should be used to perform clean up
914  * in any plugin.
915  * @name CKEDITOR.editor#destroy
916  * @event
917  */
920  * Internal event to get the current data.
921  * @name CKEDITOR.editor#beforeGetData
922  * @event
923  */
926  * Internal event to perform the #getSnapshot call.
927  * @name CKEDITOR.editor#getSnapshot
928  * @event
929  */
932  * Internal event to perform the #loadSnapshot call.
933  * @name CKEDITOR.editor#loadSnapshot
934  * @event
935  */
939  * Event fired before the #getData call returns allowing additional manipulation.
940  * @name CKEDITOR.editor#getData
941  * @event
942  * @param {CKEDITOR.editor} editor This editor instance.
943  * @param {String} data.dataValue The data that will be returned.
944  */
947  * Event fired before the #setData call is executed allowing additional manipulation.
948  * @name CKEDITOR.editor#setData
949  * @event
950  * @param {CKEDITOR.editor} editor This editor instance.
951  * @param {String} data.dataValue The data that will be used.
952  */
955  * Event fired at the end of the #setData call is executed. Usually it's better to use the
956  * {@link CKEDITOR.editor.prototype.dataReady} event.
957  * @name CKEDITOR.editor#afterSetData
958  * @event
959  * @param {CKEDITOR.editor} editor This editor instance.
960  * @param {String} data.dataValue The data that has been set.
961  */
964  * Internal event to perform the #insertHtml call
965  * @name CKEDITOR.editor#insertHtml
966  * @event
967  * @param {CKEDITOR.editor} editor This editor instance.
968  * @param {String} data The HTML to insert.
969  */
972  * Internal event to perform the #insertText call
973  * @name CKEDITOR.editor#insertText
974  * @event
975  * @param {CKEDITOR.editor} editor This editor instance.
976  * @param {String} text The text to insert.
977  */
980  * Internal event to perform the #insertElement call
981  * @name CKEDITOR.editor#insertElement
982  * @event
983  * @param {CKEDITOR.editor} editor This editor instance.
984  * @param {Object} element The element to insert.
985  */