JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.1
[ckeditor.git] / _source / plugins / menu / plugin.js
index 74fbb4f..0165d31 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
@@ -14,17 +14,36 @@ CKEDITOR.plugins.add( 'menu',
                for ( var i = 0 ; i < groups.length ; i++ )\r
                        groupsOrder[ groups[ i ] ] = i + 1;\r
 \r
+               /**\r
+                * Registers an item group to the editor context menu in order to make it\r
+                * possible to associate it with menu items later.\r
+                * @name CKEDITOR.editor.prototype.addMenuGroup\r
+                * @param {String} name Specify a group name.\r
+                * @param {Number} [order=100] Define the display sequence of this group\r
+                *      inside the menu. A smaller value gets displayed first.\r
+                */\r
                editor.addMenuGroup = function( name, order )\r
                        {\r
                                groupsOrder[ name ] = order || 100;\r
                        };\r
 \r
+               /**\r
+                * Adds an item from the specified definition to the editor context menu.\r
+                * @name CKEDITOR.editor.prototype.addMenuItem\r
+                * @param {String} name The menu item name.\r
+                * @param {CKEDITOR.menu.definition} definition The menu item definition.\r
+                */\r
                editor.addMenuItem = function( name, definition )\r
                        {\r
                                if ( groupsOrder[ definition.group ] )\r
                                        menuItems[ name ] = new CKEDITOR.menuItem( this, name, definition );\r
                        };\r
 \r
+               /**\r
+                * Adds one or more items from the specified definition array to the editor context menu.\r
+                * @name CKEDITOR.editor.prototype.addMenuItems\r
+                * @param {Array} definitions List of definitions for each menu item as if {@link CKEDITOR.editor.addMenuItem} is called.\r
+                */\r
                editor.addMenuItems = function( definitions )\r
                        {\r
                                for ( var itemName in definitions )\r
@@ -33,10 +52,27 @@ CKEDITOR.plugins.add( 'menu',
                                }\r
                        };\r
 \r
+               /**\r
+                * Retrieves a particular menu item definition from the editor context menu.\r
+                * @name CKEDITOR.editor.prototype.getMenuItem\r
+                * @param {String} name The name of the desired menu item.\r
+                * @return {CKEDITOR.menu.definition}\r
+                */\r
                editor.getMenuItem = function( name )\r
                        {\r
                                return menuItems[ name ];\r
                        };\r
+\r
+               /**\r
+                * Removes a particular menu item added before from the editor context menu.\r
+                * @name CKEDITOR.editor.prototype.removeMenuItem\r
+                * @param {String} name The name of the desired menu item.\r
+                * @since 3.6.1\r
+                */\r
+               editor.removeMenuItem = function( name )\r
+                       {\r
+                               delete menuItems[ name ];\r
+                       };\r
        },\r
 \r
        requires : [ 'floatpanel' ]\r
@@ -49,10 +85,11 @@ CKEDITOR.plugins.add( 'menu',
                $ : function( editor, definition )\r
                {\r
                        definition = this._.definition = definition || {};\r
-                       this.id = 'cke_' + CKEDITOR.tools.getNextNumber();\r
+                       this.id = CKEDITOR.tools.getNextId();\r
 \r
                        this.editor = editor;\r
                        this.items = [];\r
+                       this._.listeners = [];\r
 \r
                        this._.level = definition.level || 1;\r
 \r
@@ -71,6 +108,82 @@ CKEDITOR.plugins.add( 'menu',
 \r
                _ :\r
                {\r
+                       onShow : function()\r
+                       {\r
+                               var selection = this.editor.getSelection();\r
+\r
+                               // Selection will be unavailable after menu shows up\r
+                               // in IE, lock it now.\r
+                               if ( CKEDITOR.env.ie )\r
+                                       selection && selection.lock();\r
+\r
+                               var element = selection && selection.getStartElement(),\r
+                                       listeners = this._.listeners,\r
+                                       includedItems = [];\r
+\r
+                               this.removeAll();\r
+                               // Call all listeners, filling the list of items to be displayed.\r
+                               for ( var i = 0 ; i < listeners.length ; i++ )\r
+                               {\r
+                                       var listenerItems = listeners[ i ]( element, selection );\r
+\r
+                                       if ( listenerItems )\r
+                                       {\r
+                                               for ( var itemName in listenerItems )\r
+                                               {\r
+                                                       var item = this.editor.getMenuItem( itemName );\r
+\r
+                                                       if ( item && ( !item.command || this.editor.getCommand( item.command ).state ) )\r
+                                                       {\r
+                                                               item.state = listenerItems[ itemName ];\r
+                                                               this.add( item );\r
+                                                       }\r
+                                               }\r
+                                       }\r
+                               }\r
+                       },\r
+\r
+                       onClick : function( item )\r
+                       {\r
+                               this.hide( false );\r
+\r
+                               if ( item.onClick )\r
+                                       item.onClick();\r
+                               else if ( item.command )\r
+                                       this.editor.execCommand( item.command );\r
+                       },\r
+\r
+                       onEscape : function( keystroke )\r
+                       {\r
+                               var parent = this.parent;\r
+                               // 1. If it's sub-menu, restore the last focused item\r
+                               // of upper level menu.\r
+                               // 2. In case of a top-menu, close it.\r
+                               if ( parent )\r
+                               {\r
+                                       parent._.panel.hideChild();\r
+                                       // Restore parent block item focus.\r
+                                       var parentBlock = parent._.panel._.panel._.currentBlock,\r
+                                               parentFocusIndex =  parentBlock._.focusIndex;\r
+                                       parentBlock._.markItem( parentFocusIndex );\r
+                               }\r
+                               else if ( keystroke == 27 )\r
+                                       this.hide();\r
+\r
+                               return false;\r
+                       },\r
+\r
+                       onHide : function()\r
+                       {\r
+                               if ( CKEDITOR.env.ie )\r
+                               {\r
+                                       var selection = this.editor.getSelection();\r
+                                       selection && selection.unlock();\r
+                               }\r
+\r
+                               this.onHide && this.onHide();\r
+                       },\r
+\r
                        showSubMenu : function( index )\r
                        {\r
                                var menu = this._.subMenu,\r
@@ -98,9 +211,7 @@ CKEDITOR.plugins.add( 'menu',
                                        menu = this._.subMenu = new CKEDITOR.menu( this.editor,\r
                                                                   CKEDITOR.tools.extend( {}, this._.definition, { level : this._.level + 1 }, true ) );\r
                                        menu.parent = this;\r
-                                       menu.onClick = CKEDITOR.tools.bind( this.onClick, this );\r
-                                       // Sub menu use their own scope for binding onEscape.\r
-                                       menu.onEscape = this.onEscape;\r
+                                       menu._.onClick = CKEDITOR.tools.bind( this._.onClick, this );\r
                                }\r
 \r
                                // Add all submenu items to the menu.\r
@@ -142,6 +253,17 @@ CKEDITOR.plugins.add( 'menu',
 \r
                        show : function( offsetParent, corner, offsetX, offsetY )\r
                        {\r
+                               // Not for sub menu.\r
+                               if ( !this.parent )\r
+                               {\r
+                                       this._.onShow();\r
+                                       // Don't menu with zero items.\r
+                                       if ( ! this.items.length )\r
+                                               return;\r
+                               }\r
+\r
+                               corner = corner || ( this.editor.lang.dir == 'rtl' ? 2 : 1 );\r
+\r
                                var items = this.items,\r
                                        editor = this.editor,\r
                                        panel = this._.panel,\r
@@ -157,14 +279,14 @@ CKEDITOR.plugins.add( 'menu',
 \r
                                        panel.onEscape = CKEDITOR.tools.bind( function( keystroke )\r
                                        {\r
-                                               if ( this.onEscape && this.onEscape( keystroke ) === false )\r
+                                               if ( this._.onEscape( keystroke ) === false )\r
                                                        return false;\r
                                        },\r
                                        this );\r
 \r
                                        panel.onHide = CKEDITOR.tools.bind( function()\r
                                        {\r
-                                               this.onHide && this.onHide();\r
+                                               this._.onHide && this._.onHide();\r
                                        },\r
                                        this );\r
 \r
@@ -177,8 +299,9 @@ CKEDITOR.plugins.add( 'menu',
                                        keys[ 9 ]       = 'next';                                       // TAB\r
                                        keys[ 38 ]      = 'prev';                                       // ARROW-UP\r
                                        keys[ CKEDITOR.SHIFT + 9 ]      = 'prev';       // SHIFT + TAB\r
-                                       keys[ 32 ]      = 'click';                                      // SPACE\r
-                                       keys[ ( editor.lang.dir == 'rtl' ? 37 : 39 ) ]  = 'click';  // ARROW-RIGHT/ARROW-LEFT(rtl)\r
+                                       keys[ ( editor.lang.dir == 'rtl' ? 37 : 39 ) ]= CKEDITOR.env.ie ? 'mouseup' : 'click';  // ARROW-RIGHT/ARROW-LEFT(rtl)\r
+                                       keys[ 32 ]      = CKEDITOR.env.ie ? 'mouseup' : 'click';                                        // SPACE\r
+                                       CKEDITOR.env.ie && ( keys[ 13 ] = 'mouseup' );          // Manage ENTER, since onclick is blocked in IE (#8041).\r
 \r
                                        element = this._.element = block.element;\r
                                        element.addClass( editor.skinClass );\r
@@ -190,15 +313,15 @@ CKEDITOR.plugins.add( 'menu',
                                        this._.itemOverFn = CKEDITOR.tools.addFunction( function( index )\r
                                                {\r
                                                        clearTimeout( this._.showSubTimeout );\r
-                                                       this._.showSubTimeout = CKEDITOR.tools.setTimeout( this._.showSubMenu, editor.config.menu_subMenuDelay, this, [ index ] );\r
+                                                       this._.showSubTimeout = CKEDITOR.tools.setTimeout( this._.showSubMenu, editor.config.menu_subMenuDelay || 400, this, [ index ] );\r
                                                },\r
-                                               this);\r
+                                               this );\r
 \r
                                        this._.itemOutFn = CKEDITOR.tools.addFunction( function( index )\r
                                                {\r
                                                        clearTimeout( this._.showSubTimeout );\r
                                                },\r
-                                               this);\r
+                                               this );\r
 \r
                                        this._.itemClickFn = CKEDITOR.tools.addFunction( function( index )\r
                                                {\r
@@ -213,16 +336,16 @@ CKEDITOR.plugins.add( 'menu',
                                                        if ( item.getItems )\r
                                                                this._.showSubMenu( index );\r
                                                        else\r
-                                                               this.onClick && this.onClick( item );\r
+                                                               this._.onClick( item );\r
                                                },\r
-                                               this);\r
+                                               this );\r
                                }\r
 \r
                                // Put the items in the right order.\r
                                sortItems( items );\r
 \r
-                               var chromeRoot = editor.container.getChild( 1 );\r
-                               var mixedContentClass = chromeRoot.hasClass( 'cke_mixed_dir_content' ) ? ' cke_mixed_dir_content' : '';\r
+                               var chromeRoot = editor.container.getChild( 1 ),\r
+                                       mixedContentClass = chromeRoot.hasClass( 'cke_mixed_dir_content' ) ? ' cke_mixed_dir_content' : '';\r
 \r
                                // Build the HTML that composes the menu and its items.\r
                                var output = [ '<div class="cke_menu' + mixedContentClass + '" role="presentation">' ];\r
@@ -247,6 +370,8 @@ CKEDITOR.plugins.add( 'menu',
                                // Inject the HTML inside the panel.\r
                                element.setHtml( output.join( '' ) );\r
 \r
+                               CKEDITOR.ui.fire( 'ready', this );\r
+\r
                                // Show the panel.\r
                                if ( this.parent )\r
                                        this.parent._.panel.showAsChild( panel, this.id, offsetParent, corner, offsetX, offsetY );\r
@@ -256,9 +381,15 @@ CKEDITOR.plugins.add( 'menu',
                                editor.fire( 'menuShow', [ panel ] );\r
                        },\r
 \r
-                       hide : function()\r
+                       addListener : function( listenerFn )\r
                        {\r
-                               this._.panel && this._.panel.hide();\r
+                               this._.listeners.push( listenerFn );\r
+                       },\r
+\r
+                       hide : function( returnFocus )\r
+                       {\r
+                               this._.onHide && this._.onHide();\r
+                               this._.panel && this._.panel.hide( returnFocus );\r
                        }\r
                }\r
        });\r
@@ -277,47 +408,45 @@ CKEDITOR.plugins.add( 'menu',
                                        0;\r
                        });\r
        }\r
-})();\r
-\r
-CKEDITOR.menuItem = CKEDITOR.tools.createClass(\r
-{\r
-       $ : function( editor, name, definition )\r
+       CKEDITOR.menuItem = CKEDITOR.tools.createClass(\r
        {\r
-               CKEDITOR.tools.extend( this, definition,\r
-                       // Defaults\r
-                       {\r
-                               order : 0,\r
-                               className : 'cke_button_' + name\r
-                       });\r
+               $ : function( editor, name, definition )\r
+               {\r
+                       CKEDITOR.tools.extend( this, definition,\r
+                               // Defaults\r
+                               {\r
+                                       order : 0,\r
+                                       className : 'cke_button_' + name\r
+                               });\r
 \r
-               // Transform the group name into its order number.\r
-               this.group = editor._.menuGroups[ this.group ];\r
+                       // Transform the group name into its order number.\r
+                       this.group = editor._.menuGroups[ this.group ];\r
 \r
-               this.editor = editor;\r
-               this.name = name;\r
-       },\r
+                       this.editor = editor;\r
+                       this.name = name;\r
+               },\r
 \r
-       proto :\r
-       {\r
-               render : function( menu, index, output )\r
+               proto :\r
                {\r
-                       var id = menu.id + String( index ),\r
-                               state = ( typeof this.state == 'undefined' ) ? CKEDITOR.TRISTATE_OFF : this.state;\r
+                       render : function( menu, index, output )\r
+                       {\r
+                               var id = menu.id + String( index ),\r
+                                       state = ( typeof this.state == 'undefined' ) ? CKEDITOR.TRISTATE_OFF : this.state;\r
 \r
-                       var classes = ' cke_' + (\r
-                               state == CKEDITOR.TRISTATE_ON ? 'on' :\r
-                               state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :\r
-                               'off' );\r
+                               var classes = ' cke_' + (\r
+                                       state == CKEDITOR.TRISTATE_ON ? 'on' :\r
+                                       state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' :\r
+                                       'off' );\r
 \r
-                       var htmlLabel = this.label;\r
+                               var htmlLabel = this.label;\r
 \r
-                       if ( this.className )\r
-                               classes += ' ' + this.className;\r
+                               if ( this.className )\r
+                                       classes += ' ' + this.className;\r
 \r
                        var hasSubMenu = this.getItems;\r
 \r
                        output.push(\r
-                               '<span class="cke_menuitem">' +\r
+                               '<span class="cke_menuitem' + ( this.icon && this.icon.indexOf( '.png' ) == -1 ? ' cke_noalphafix' : '' ) + '">' +\r
                                '<a id="', id, '"' +\r
                                        ' class="', classes, '" href="javascript:void(\'', ( this.label || '' ).replace( "'", '' ), '\')"' +\r
                                        ' title="', this.label, '"' +\r
@@ -329,29 +458,30 @@ CKEDITOR.menuItem = CKEDITOR.tools.createClass(
                                        ( state == CKEDITOR.TRISTATE_DISABLED ? 'aria-disabled="true"' : '' ) +\r
                                        ( state == CKEDITOR.TRISTATE_ON ? 'aria-pressed="true"' : '' ) );\r
 \r
-                       // Some browsers don't cancel key events in the keydown but in the\r
-                       // keypress.\r
-                       // TODO: Check if really needed for Gecko+Mac.\r
-                       if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) )\r
-                       {\r
-                               output.push(\r
-                                       ' onkeypress="return false;"' );\r
-                       }\r
+                               // Some browsers don't cancel key events in the keydown but in the\r
+                               // keypress.\r
+                               // TODO: Check if really needed for Gecko+Mac.\r
+                               if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) )\r
+                               {\r
+                                       output.push(\r
+                                               ' onkeypress="return false;"' );\r
+                               }\r
 \r
-                       // With Firefox, we need to force the button to redraw, otherwise it\r
-                       // will remain in the focus state.\r
-                       if ( CKEDITOR.env.gecko )\r
-                       {\r
-                               output.push(\r
-                                       ' onblur="this.style.cssText = this.style.cssText;"' );\r
-                       }\r
+                               // With Firefox, we need to force the button to redraw, otherwise it\r
+                               // will remain in the focus state.\r
+                               if ( CKEDITOR.env.gecko )\r
+                               {\r
+                                       output.push(\r
+                                               ' onblur="this.style.cssText = this.style.cssText;"' );\r
+                               }\r
 \r
-                       var offset = ( this.iconOffset || 0 ) * -16;\r
-                       output.push(\r
+                               var offset = ( this.iconOffset || 0 ) * -16;\r
+                               output.push(\r
 //                                     ' onkeydown="return CKEDITOR.ui.button._.keydown(', index, ', event);"' +\r
                                        ' onmouseover="CKEDITOR.tools.callFunction(', menu._.itemOverFn, ',', index, ');"' +\r
-                                       ' onmouseout="CKEDITOR.tools.callFunction(', menu._.itemOutFn, ',', index, ');"' +\r
-                                       ' onclick="CKEDITOR.tools.callFunction(', menu._.itemClickFn, ',', index, '); return false;"' +\r
+                                       ' onmouseout="CKEDITOR.tools.callFunction(', menu._.itemOutFn, ',', index, ');" ' +\r
+                                       ( CKEDITOR.env.ie ? 'onclick="return false;" onmouseup' : 'onclick' ) +         // #188\r
+                                               '="CKEDITOR.tools.callFunction(', menu._.itemClickFn, ',', index, '); return false;"' +\r
                                        '>' +\r
                                                '<span class="cke_icon_wrapper"><span class="cke_icon"' +\r
                                                        ( this.icon ? ' style="background-image:url(' + CKEDITOR.getUrl( this.icon ) + ');background-position:0 ' + offset + 'px;"'\r
@@ -371,18 +501,21 @@ CKEDITOR.menuItem = CKEDITOR.tools.createClass(
                                                        '</span>' );\r
                        }\r
 \r
-                       output.push(\r
-                                                       htmlLabel,\r
-                                               '</span>' +\r
-                               '</a>' +\r
-                               '</span>' );\r
+                               output.push(\r
+                                                               htmlLabel,\r
+                                                       '</span>' +\r
+                                       '</a>' +\r
+                                       '</span>' );\r
                }\r
-       }\r
-});\r
+               }\r
+       });\r
+\r
+})();\r
+\r
 \r
 /**\r
- * The amount of time, in milliseconds, the editor waits before showing submenu\r
- * options when moving the mouse over options that contains submenus, like the\r
+ * The amount of time, in milliseconds, the editor waits before displaying submenu\r
+ * options when moving the mouse over options that contain submenus, like the\r
  * "Cell Properties" entry for tables.\r
  * @type Number\r
  * @default 400\r
@@ -390,12 +523,11 @@ CKEDITOR.menuItem = CKEDITOR.tools.createClass(
  * // Remove the submenu delay.\r
  * config.menu_subMenuDelay = 0;\r
  */\r
-CKEDITOR.config.menu_subMenuDelay = 400;\r
 \r
 /**\r
  * A comma separated list of items group names to be displayed in the context\r
- * menu. The items order will reflect the order in this list if no priority\r
- * has been definted in the groups.\r
+ * menu. The order of items will reflect the order specified in this list if\r
+ * no priority was defined in the groups.\r
  * @type String\r
  * @default 'clipboard,form,tablecell,tablecellproperties,tablerow,tablecolumn,table,anchor,link,image,flash,checkbox,radio,textfield,hiddenfield,imagebutton,button,select,textarea'\r
  * @example\r