JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.1
[ckeditor.git] / _source / plugins / button / plugin.js
index f16e192..7655229 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -16,7 +16,7 @@ CKEDITOR.plugins.add( 'button',
  * @constant\r
  * @example\r
  */\r
-CKEDITOR.UI_BUTTON = 1;\r
+CKEDITOR.UI_BUTTON = 'button';\r
 \r
 /**\r
  * Represents a button UI element. This class should not be called directly. To\r
@@ -55,10 +55,10 @@ CKEDITOR.ui.button.handler =
        }\r
 };\r
 \r
+( function()\r
+{\r
 CKEDITOR.ui.button.prototype =\r
 {\r
-       canGroup : true,\r
-\r
        /**\r
         * Renders the button.\r
         * @param {CKEDITOR.editor} editor The editor instance which this button is\r
@@ -70,11 +70,10 @@ CKEDITOR.ui.button.prototype =
        render : function( editor, output )\r
        {\r
                var env = CKEDITOR.env,\r
-                       id = this._.id = 'cke_' + CKEDITOR.tools.getNextNumber(),\r
+                       id = this._.id = CKEDITOR.tools.getNextId(),\r
                        classes = '',\r
                        command = this.command, // Get the command name.\r
-                       clickFn,\r
-                       index;\r
+                       clickFn;\r
 \r
                this._.editor = editor;\r
 \r
@@ -90,20 +89,70 @@ CKEDITOR.ui.button.prototype =
                        },\r
                        execute : function()\r
                        {\r
-                               this.button.click( editor );\r
+                               // IE 6 needs some time before execution (#7922)\r
+                               if ( CKEDITOR.env.ie && CKEDITOR.env.version < 7 )\r
+                                       CKEDITOR.tools.setTimeout( function(){ this.button.click( editor ); }, 0, this );\r
+                               else\r
+                                       this.button.click( editor );\r
                        }\r
                };\r
 \r
+               var keydownFn = CKEDITOR.tools.addFunction( function( ev )\r
+                       {\r
+                               if ( instance.onkey )\r
+                               {\r
+                                       ev = new CKEDITOR.dom.event( ev );\r
+                                       return ( instance.onkey( instance, ev.getKeystroke() ) !== false );\r
+                               }\r
+                       });\r
+\r
+               var focusFn = CKEDITOR.tools.addFunction( function( ev )\r
+                       {\r
+                               var retVal;\r
+\r
+                               if ( instance.onfocus )\r
+                                         retVal = ( instance.onfocus( instance, new CKEDITOR.dom.event( ev ) ) !== false );\r
+\r
+                               // FF2: prevent focus event been bubbled up to editor container, which caused unexpected editor focus.\r
+                               if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )\r
+                                         ev.preventBubble();\r
+                               return retVal;\r
+                       });\r
+\r
                instance.clickFn = clickFn = CKEDITOR.tools.addFunction( instance.execute, instance );\r
 \r
-               instance.index = index = CKEDITOR.ui.button._.instances.push( instance ) - 1;\r
 \r
+               // Indicate a mode sensitive button.\r
                if ( this.modes )\r
                {\r
-                       editor.on( 'mode', function()\r
+                       var modeStates = {};\r
+\r
+                       function updateState()\r
+                       {\r
+                               // "this" is a CKEDITOR.ui.button instance.\r
+\r
+                               var mode = editor.mode;\r
+\r
+                               if ( mode )\r
+                               {\r
+                                       // Restore saved button state.\r
+                                       var state = this.modes[ mode ] ? modeStates[ mode ] != undefined ? modeStates[ mode ] :\r
+                                                       CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;\r
+\r
+                                       this.setState( editor.readOnly && !this.readOnly ? CKEDITOR.TRISTATE_DISABLED : state );\r
+                               }\r
+                       }\r
+\r
+                       editor.on( 'beforeModeUnload', function()\r
                                {\r
-                                       this.setState( this.modes[ editor.mode ] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED );\r
-                               }, this);\r
+                                       if ( editor.mode && this._.state != CKEDITOR.TRISTATE_DISABLED )\r
+                                               modeStates[ editor.mode ] = this._.state;\r
+                               }, this );\r
+\r
+                       editor.on( 'mode', updateState, this);\r
+\r
+                       // If this button is sensitive to readOnly state, update it accordingly.\r
+                       !this.readOnly && editor.on( 'readOnly', updateState, this);\r
                }\r
                else if ( command )\r
                {\r
@@ -131,7 +180,7 @@ CKEDITOR.ui.button.prototype =
                        classes += ' ' + this.className;\r
 \r
                output.push(\r
-                       '<span class="cke_button">',\r
+                       '<span class="cke_button' + ( this.icon && this.icon.indexOf( '.png' ) == -1 ? ' cke_noalphafix' : '' ) + '">',\r
                        '<a id="', id, '"' +\r
                                ' class="', classes, '"',\r
                                env.gecko && env.version >= 10900 && !env.hc  ? '' : '" href="javascript:void(\''+ ( this.title || '' ).replace( "'", '' )+ '\')"',\r
@@ -160,9 +209,10 @@ CKEDITOR.ui.button.prototype =
                }\r
 \r
                output.push(\r
-                               ' onkeydown="return CKEDITOR.ui.button._.keydown(', index, ', event);"' +\r
-                               ' onfocus="return CKEDITOR.ui.button._.focus(', index, ', event);"' +\r
-                               ' onclick="CKEDITOR.tools.callFunction(', clickFn, ', this); return false;">' +\r
+                                       ' onkeydown="return CKEDITOR.tools.callFunction(', keydownFn, ', event);"' +\r
+                                       ' onfocus="return CKEDITOR.tools.callFunction(', focusFn,', event);" ' +\r
+                                       ( CKEDITOR.env.ie ? 'onclick="return false;" onmouseup' : 'onclick' ) +         // #188\r
+                                               '="CKEDITOR.tools.callFunction(', clickFn, ', this); return false;">' +\r
                                        '<span class="cke_icon"' );\r
 \r
                if ( this.icon )\r
@@ -221,44 +271,12 @@ CKEDITOR.ui.button.prototype =
        }\r
 };\r
 \r
-/**\r
- * Handles a button click.\r
- * @private\r
- */\r
-CKEDITOR.ui.button._ =\r
-{\r
-       instances : [],\r
-\r
-       keydown : function( index, ev )\r
-       {\r
-               var instance = CKEDITOR.ui.button._.instances[ index ];\r
-\r
-               if ( instance.onkey )\r
-               {\r
-                       ev = new CKEDITOR.dom.event( ev );\r
-                       return ( instance.onkey( instance, ev.getKeystroke() ) !== false );\r
-               }\r
-       },\r
-\r
-       focus : function( index, ev )\r
-       {\r
-               var instance = CKEDITOR.ui.button._.instances[ index ],\r
-                       retVal;\r
-\r
-               if ( instance.onfocus )\r
-                       retVal = ( instance.onfocus( instance, new CKEDITOR.dom.event( ev ) ) !== false );\r
-\r
-               // FF2: prevent focus event been bubbled up to editor container, which caused unexpected editor focus.\r
-               if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )\r
-                       ev.preventBubble();\r
-               return retVal;\r
-       }\r
-};\r
+})();\r
 \r
 /**\r
  * Adds a button definition to the UI elements list.\r
- * @param {String} The button name.\r
- * @param {Object} The button definition.\r
+ * @param {String} name The button name.\r
+ * @param {Object} definition The button definition.\r
  * @example\r
  * editorInstance.ui.addButton( 'MyBold',\r
  *     {\r
@@ -270,8 +288,3 @@ CKEDITOR.ui.prototype.addButton = function( name, definition )
 {\r
        this.add( name, CKEDITOR.UI_BUTTON, definition );\r
 };\r
-\r
-CKEDITOR.on( 'reset', function()\r
-       {\r
-               CKEDITOR.ui.button._.instances = [];\r
-       });\r