JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.2
[ckeditor.git] / _source / core / editor.js
index d9017c0..a8921db 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -33,6 +33,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                if ( !customConfig )\r
                        return false;\r
 \r
+               customConfig = CKEDITOR.getUrl( customConfig );\r
+\r
                var loadedConfig = loadConfigLoaded[ customConfig ] || ( loadConfigLoaded[ customConfig ] = {} );\r
 \r
                // If the custom config has already been downloaded, reuse it.\r
@@ -44,7 +46,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        // If there is no other customConfig in the chain, fire the\r
                        // "configLoaded" event.\r
-                       if ( editor.config.customConfig == customConfig || !loadConfig( editor ) )\r
+                       if ( CKEDITOR.getUrl( editor.config.customConfig ) == customConfig || !loadConfig( editor ) )\r
                                editor.fireOnce( 'customConfigLoaded' );\r
                }\r
                else\r
@@ -113,25 +115,80 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                var skin = editor.config.skin.split( ',' ),\r
                        skinName = skin[ 0 ],\r
                        skinPath = CKEDITOR.getUrl( skin[ 1 ] || (\r
+                               '_source/' +    // @Packager.RemoveLine\r
                                'skins/' + skinName + '/' ) );\r
 \r
+               /**\r
+                * The name of the skin used by this editor instance. The skin name can\r
+                * be set though the {@link CKEDITOR.config.skin} setting.\r
+                * @name CKEDITOR.editor.prototype.skinName\r
+                * @type String\r
+                * @example\r
+                * alert( editor.skinName );  // "kama" (e.g.)\r
+                */\r
                editor.skinName = skinName;\r
+\r
+               /**\r
+                * The full URL of the skin directory.\r
+                * @name CKEDITOR.editor.prototype.skinPath\r
+                * @type String\r
+                * @example\r
+                * alert( editor.skinPath );  // "http://example.com/ckeditor/skins/kama/" (e.g.)\r
+                */\r
                editor.skinPath = skinPath;\r
+\r
+               /**\r
+                * The CSS class name used for skin identification purposes.\r
+                * @name CKEDITOR.editor.prototype.skinClass\r
+                * @type String\r
+                * @example\r
+                * alert( editor.skinClass );  // "cke_skin_kama" (e.g.)\r
+                */\r
                editor.skinClass = 'cke_skin_' + skinName;\r
 \r
+               /**\r
+                * The <a href="http://en.wikipedia.org/wiki/Tabbing_navigation">tabbing\r
+                * navigation</a> order that has been calculated for this editor\r
+                * instance. This can be set by the {@link CKEDITOR.config.tabIndex}\r
+                * setting or taken from the "tabindex" attribute of the\r
+                * {@link #element} associated to the editor.\r
+                * @name CKEDITOR.editor.prototype.tabIndex\r
+                * @type Number\r
+                * @default 0 (zero)\r
+                * @example\r
+                * alert( editor.tabIndex );  // "0" (e.g.)\r
+                */\r
+               editor.tabIndex = editor.config.tabIndex || editor.element.getAttribute( 'tabindex' ) || 0;\r
+\r
                // Fire the "configLoaded" event.\r
                editor.fireOnce( 'configLoaded' );\r
 \r
                // Load language file.\r
-               loadLang( editor );\r
+               loadSkin( editor );\r
        };\r
 \r
        var loadLang = function( editor )\r
        {\r
                CKEDITOR.lang.load( editor.config.language, editor.config.defaultLanguage, function( languageCode, lang )\r
                        {\r
+                               /**\r
+                                * The code for the language resources that have been loaded\r
+                                * for the user internface elements of this editor instance.\r
+                                * @name CKEDITOR.editor.prototype.langCode\r
+                                * @type String\r
+                                * @example\r
+                                * alert( editor.langCode );  // "en" (e.g.)\r
+                                */\r
                                editor.langCode = languageCode;\r
 \r
+                               /**\r
+                                * An object holding all language strings used by the editor\r
+                                * interface.\r
+                                * @name CKEDITOR.editor.prototype.lang\r
+                                * @type CKEDITOR.lang\r
+                                * @example\r
+                                * alert( editor.lang.bold );  // "Negrito" (e.g. if language is Portuguese)\r
+                                */\r
                                // As we'll be adding plugin specific entries that could come\r
                                // from different language code files, we need a copy of lang,\r
                                // not a direct reference to it.\r
@@ -141,6 +198,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 && editor.lang.dir == 'rtl' )\r
                                        editor.lang.dir = 'ltr';\r
 \r
+                               var config = editor.config;\r
+                               config.contentsLangDirection == 'ui' && ( config.contentsLangDirection = editor.lang.dir );\r
+\r
                                loadPlugins( editor );\r
                        });\r
        };\r
@@ -180,7 +240,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                // The list of URLs to language files.\r
                                var languageFiles = [];\r
 \r
-                               // Cache the loaded plugin names.\r
+                               /**\r
+                                * And object holding references to all plugins used by this\r
+                                * editor istance.\r
+                                * @name CKEDITOR.editor.prototype.plugins\r
+                                * @type Object\r
+                                * @example\r
+                                * alert( editor.plugins.dialog.path );  // "http://example.com/ckeditor/plugins/dialog/" (e.g.)\r
+                                */\r
                                editor.plugins = plugins;\r
 \r
                                // Loop through all plugins, to build the list of language\r
@@ -245,7 +312,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                // Load the editor skin.\r
                                                editor.fire( 'pluginsLoaded' );\r
-                                               loadSkin( editor );\r
+                                               loadTheme( editor );\r
                                        });\r
                        });\r
        };\r
@@ -254,7 +321,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        {\r
                CKEDITOR.skins.load( editor, 'editor', function()\r
                        {\r
-                               loadTheme( editor );\r
+                               loadLang( editor );\r
                        });\r
        };\r
 \r
@@ -263,6 +330,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                var theme = editor.config.theme;\r
                CKEDITOR.themes.load( theme, function()\r
                        {\r
+                               /**\r
+                                * The theme used by this editor instance.\r
+                                * @name CKEDITOR.editor.prototype.theme\r
+                                * @type CKEDITOR.theme\r
+                                * @example\r
+                                * alert( editor.theme );  "http://example.com/ckeditor/themes/default/" (e.g.)\r
+                                */\r
                                var editorTheme = editor.theme = CKEDITOR.themes.get( theme );\r
                                editorTheme.path = CKEDITOR.themes.getPath( theme );\r
                                editorTheme.build( editor );\r
@@ -326,7 +400,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                for ( var name in commands )\r
                {\r
                        command = commands[ name ];\r
-                       command[ command.modes[ mode ] ? 'enable' : 'disable' ]();\r
+                       command[ command.startDisabled ? 'disable' : command.modes[ mode ] ? 'enable' : 'disable' ]();\r
                }\r
        }\r
 \r
@@ -375,6 +449,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                throw '[CKEDITOR.editor] The instance "' + this.name + '" already exists.';\r
 \r
                        /**\r
+                        * A unique random string assigned to each editor instance in the page.\r
+                        * @name CKEDITOR.editor.prototype.id\r
+                        * @type String\r
+                        */\r
+                       this.id = CKEDITOR.tools.getNextId();\r
+\r
+                       /**\r
                         * The configurations for this editor instance. It inherits all\r
                         * settings defined in (@link CKEDITOR.config}, combined with settings\r
                         * loaded from custom configuration files and those defined inline in\r
@@ -435,6 +516,13 @@ CKEDITOR.tools.extend( CKEDITOR.editor.prototype,
                        return this._.commands[ commandName ] = new CKEDITOR.command( this, commandDefinition );\r
                },\r
 \r
+               /**\r
+                * Add a trunk of css text to the editor which will be applied to the wysiwyg editing document.\r
+                * Note: This function should be called before editor is loaded to take effect.\r
+                * @param css {String} CSS text.\r
+                * @example\r
+                * editorInstance.addCss( 'body { background-color: grey; }' );\r
+                */\r
                addCss : function( css )\r
                {\r
                        this._.styles.push( css );\r
@@ -456,9 +544,46 @@ CKEDITOR.tools.extend( CKEDITOR.editor.prototype,
                        if ( !noUpdate )\r
                                this.updateElement();\r
 \r
+                       if ( this.mode )\r
+                       {\r
+                               // ->           currentMode.unload( holderElement );\r
+                               this._.modes[ this.mode ].unload( this.getThemeSpace( 'contents' ) );\r
+                       }\r
+\r
                        this.theme.destroy( this );\r
+\r
+                       var toolbars,\r
+                               index = 0,\r
+                               j,\r
+                               items,\r
+                               instance;\r
+\r
+                       if ( this.toolbox )\r
+                       {\r
+                               toolbars = this.toolbox.toolbars;\r
+                               for ( ; index < toolbars.length ; index++ )\r
+                               {\r
+                                       items = toolbars[ index ].items;\r
+                                       for ( j = 0 ; j < items.length ; j++ )\r
+                                       {\r
+                                               instance = items[ j ];\r
+                                               if ( instance.clickFn ) CKEDITOR.tools.removeFunction( instance.clickFn );\r
+                                               if ( instance.keyDownFn ) CKEDITOR.tools.removeFunction( instance.keyDownFn );\r
+\r
+                                               if ( instance.index ) CKEDITOR.ui.button._.instances[ instance.index ] = null;\r
+                                       }\r
+                               }\r
+                       }\r
+\r
+                       if ( this.contextMenu )\r
+                               CKEDITOR.tools.removeFunction( this.contextMenu._.functionId );\r
+\r
+                       if ( this._.filebrowserFn )\r
+                               CKEDITOR.tools.removeFunction( this._.filebrowserFn );\r
+\r
                        this.fire( 'destroy' );\r
                        CKEDITOR.remove( this );\r
+                       CKEDITOR.fire( 'instanceDestroyed', null, this );\r
                },\r
 \r
                /**\r
@@ -544,6 +669,16 @@ CKEDITOR.tools.extend( CKEDITOR.editor.prototype,
                        return eventData.dataValue;\r
                },\r
 \r
+               /**\r
+                * Gets the "raw data" currently available in the editor. This is a\r
+                * fast method which return the data as is, without processing, so it's\r
+                * not recommended to use it on resulting pages. It can be used instead\r
+                * combined with the {@link #loadSnapshot} so one can automatic save\r
+                * the editor data from time to time while the user is using the\r
+                * editor, to avoid data loss, without risking performance issues.\r
+                * @example\r
+                * alert( editor.getSnapshot() );\r
+                */\r
                getSnapshot : function()\r
                {\r
                        var data = this.fire( 'getSnapshot' );\r
@@ -558,19 +693,48 @@ CKEDITOR.tools.extend( CKEDITOR.editor.prototype,
                        return data;\r
                },\r
 \r
+               /**\r
+                * Loads "raw data" in the editor. This data is loaded with processing\r
+                * straight to the editing area. It should not be used as a way to load\r
+                * any kind of data, but instead in combination with\r
+                * {@link #getSnapshot} produced data.\r
+                * @example\r
+                * var data = editor.getSnapshot();\r
+                * editor.<b>loadSnapshot( data )</b>;\r
+                */\r
                loadSnapshot : function( snapshot )\r
                {\r
                        this.fire( 'loadSnapshot', snapshot );\r
                },\r
 \r
                /**\r
-                * Sets the editor data. The data must be provided in raw format.\r
-                * @param {String} data HTML code to replace the curent content in the editor.\r
+                * Sets the editor data. The data must be provided in raw format (HTML).<br />\r
+                * <br />\r
+                * Note that this menthod is asynchronous. The "callback" parameter must\r
+                * be used if interaction with the editor is needed after setting the data.\r
+                * @param {String} data HTML code to replace the curent content in the\r
+                *              editor.\r
+                * @param {Function} callback Function to be called after the setData\r
+                *              is completed.\r
+                * @example\r
+                * CKEDITOR.instances.editor1.<b>setData</b>( '&lt;p&gt;This is the editor data.&lt;/p&gt;' );\r
                 * @example\r
-                * CKEDITOR.instances.editor1.<b>setData( '&lt;p&gt;This is the editor data.&lt;/p&gt;' )</b>;\r
+                * CKEDITOR.instances.editor1.<b>setData</b>( '&lt;p&gt;Some other editor data.&lt;/p&gt;', function()\r
+                *     {\r
+                *         this.checkDirty();    // true\r
+                *     });\r
                 */\r
-               setData : function( data )\r
+               setData : function( data , callback )\r
                {\r
+                       if( callback )\r
+                       {\r
+                               this.on( 'dataReady', function( evt )\r
+                               {\r
+                                       evt.removeListener();\r
+                                       callback.call( evt.editor );\r
+                               } );\r
+                       }\r
+\r
                        // Fire "setData" so data manipulation may happen.\r
                        var eventData = { dataValue : data };\r
                        this.fire( 'setData', eventData );\r
@@ -605,11 +769,38 @@ CKEDITOR.tools.extend( CKEDITOR.editor.prototype,
                        this.fire( 'insertElement', element );\r
                },\r
 \r
+               /**\r
+                * Checks whether the current editor contents present changes when\r
+                * compared to the contents loaded into the editor at startup, or to\r
+                * the contents available in the editor when {@link #resetDirty} has\r
+                * been called.\r
+                * @returns {Boolean} "true" is the contents present changes.\r
+                * @example\r
+                * function beforeUnload( e )\r
+                * {\r
+                *     if ( CKEDITOR.instances.editor1.<b>checkDirty()</b> )\r
+                *              return e.returnValue = "You'll loose the changes made in the editor.";\r
+                * }\r
+                *\r
+                * if ( window.addEventListener )\r
+                *     window.addEventListener( 'beforeunload', beforeUnload, false );\r
+                * else\r
+                *     window.attachEvent( 'onbeforeunload', beforeUnload );\r
+                */\r
                checkDirty : function()\r
                {\r
                        return ( this.mayBeDirty && this._.previousValue !== this.getSnapshot() );\r
                },\r
 \r
+               /**\r
+                * Resets the "dirty state" of the editor so subsequent calls to\r
+                * {@link #checkDirty} will return "false" if the user will not make\r
+                * further changes to the contents.\r
+                * @example\r
+                * alert( editor.checkDirty() );  // "true" (e.g.)\r
+                * editor.<b>resetDirty()</b>;\r
+                * alert( editor.checkDirty() );  // "false"\r
+                */\r
                resetDirty : function()\r
                {\r
                        if ( this.mayBeDirty )\r
@@ -628,10 +819,15 @@ CKEDITOR.tools.extend( CKEDITOR.editor.prototype,
                        var element = this.element;\r
                        if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )\r
                        {\r
+                               var data = this.getData();\r
+\r
+                               if ( this.config.htmlEncodeOutput )\r
+                                       data = CKEDITOR.tools.htmlEncode( data );\r
+\r
                                if ( element.is( 'textarea' ) )\r
-                                       element.setValue( this.getData() );\r
+                                       element.setValue( data );\r
                                else\r
-                                       element.setHtml( this.getData() );\r
+                                       element.setHtml( data );\r
                        }\r
                }\r
        });\r
@@ -648,3 +844,81 @@ CKEDITOR.on( 'loaded', function()
                                pending[ i ]._init();\r
                }\r
        });\r
+\r
+/**\r
+ * Whether escape HTML when editor update original input element.\r
+ * @name CKEDITOR.config.htmlEncodeOutput\r
+ * @since 3.1\r
+ * @type Boolean\r
+ * @default false\r
+ * @example\r
+ * config.htmlEncodeOutput = true;\r
+ */\r
+\r
+/**\r
+ * Fired when a CKEDITOR instance is created, but still before initializing it.\r
+ * To interact with a fully initialized instance, use the\r
+ * {@link CKEDITOR#instanceReady} event instead.\r
+ * @name CKEDITOR#instanceCreated\r
+ * @event\r
+ * @param {CKEDITOR.editor} editor The editor instance that has been created.\r
+ */\r
+\r
+/**\r
+ * Fired when a CKEDITOR instance is destroyed.\r
+ * @name CKEDITOR#instanceDestroyed\r
+ * @event\r
+ * @param {CKEDITOR.editor} editor The editor instance that has been destroyed.\r
+ */\r
+\r
+/**\r
+ * Fired when all plugins are loaded and initialized into the editor instance.\r
+ * @name CKEDITOR#pluginsLoaded\r
+ * @event\r
+ * @param {CKEDITOR.editor} editor The editor instance that has been destroyed.\r
+ */\r
+\r
+/**\r
+ * Fired before the command execution when {@link #execCommand} is called.\r
+ * @name CKEDITOR.editor#beforeCommandExec\r
+ * @event\r
+ * @param {CKEDITOR.editor} editor This editor instance.\r
+ * @param {String} data.name The command name.\r
+ * @param {Object} data.commandData The data to be sent to the command. This\r
+ *             can be manipulated by the event listener.\r
+ * @param {CKEDITOR.command} data.command The command itself.\r
+ */\r
+\r
+/**\r
+ * Fired after the command execution when {@link #execCommand} is called.\r
+ * @name CKEDITOR.editor#afterCommandExec\r
+ * @event\r
+ * @param {CKEDITOR.editor} editor This editor instance.\r
+ * @param {String} data.name The command name.\r
+ * @param {Object} data.commandData The data sent to the command.\r
+ * @param {CKEDITOR.command} data.command The command itself.\r
+ * @param {Object} data.returnValue The value returned by the command execution.\r
+ */\r
+\r
+/**\r
+ * Fired every custom configuration file is loaded, before the final\r
+ * configurations initialization.<br />\r
+ * <br />\r
+ * Custom configuration files can be loaded thorugh the\r
+ * {@link CKEDITOR.config.customConfig} setting. Several files can be loading\r
+ * by chaning this setting.\r
+ * @name CKEDITOR.editor#customConfigLoaded\r
+ * @event\r
+ * @param {CKEDITOR.editor} editor This editor instance.\r
+ * @example\r
+ */\r
+\r
+/**\r
+ * Fired once the editor configuration is ready (loaded and processed).\r
+ * @name CKEDITOR.editor#configLoaded\r
+ * @event\r
+ * @param {CKEDITOR.editor} editor This editor instance.\r
+ * @example\r
+ * if( editor.config.fullPage )\r
+ *     alert( 'This is a full page editor' );\r
+ */\r