X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fmenu%2Fplugin.js;h=02b0ea309cece93fc99a68621e0ae6d229616539;hb=48b1db88210b4160dce439c6e3e32e14af8c106b;hp=1ea12b21515bf5671e19ef42f37d1e3ba5f0b4ee;hpb=039a051ccf3901311661022a30afd60fc38130c9;p=ckeditor.git diff --git a/_source/plugins/menu/plugin.js b/_source/plugins/menu/plugin.js index 1ea12b2..02b0ea3 100644 --- a/_source/plugins/menu/plugin.js +++ b/_source/plugins/menu/plugin.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -49,10 +49,11 @@ CKEDITOR.plugins.add( 'menu', $ : function( editor, definition ) { definition = this._.definition = definition || {}; - this.id = 'cke_' + CKEDITOR.tools.getNextNumber(); + this.id = CKEDITOR.tools.getNextId(); this.editor = editor; this.items = []; + this._.listeners = []; this._.level = definition.level || 1; @@ -71,6 +72,84 @@ CKEDITOR.plugins.add( 'menu', _ : { + onShow : function() + { + var selection = this.editor.getSelection(); + + // Selection will be unavailable after menu shows up + // in IE, lock it now. + if ( CKEDITOR.env.ie ) + selection && selection.lock(); + + var element = selection && selection.getStartElement(), + listeners = this._.listeners, + includedItems = []; + + this.removeAll(); + // Call all listeners, filling the list of items to be displayed. + for ( var i = 0 ; i < listeners.length ; i++ ) + { + var listenerItems = listeners[ i ]( element, selection ); + + if ( listenerItems ) + { + for ( var itemName in listenerItems ) + { + var item = this.editor.getMenuItem( itemName ); + + if ( item ) + { + item.state = listenerItems[ itemName ]; + this.add( item ); + } + } + } + } + }, + + onClick : function( item ) + { + this.hide(); + + if ( item.onClick ) + item.onClick(); + else if ( item.command ) + this.editor.execCommand( item.command ); + }, + + onEscape : function( keystroke ) + { + var parent = this.parent; + // 1. If it's sub-menu, restore the last focused item + // of upper level menu. + // 2. In case of a top-menu, close it. + if ( parent ) + { + parent._.panel.hideChild(); + // Restore parent block item focus. + var parentBlock = parent._.panel._.panel._.currentBlock, + parentFocusIndex = parentBlock._.focusIndex; + parentBlock._.markItem( parentFocusIndex ); + } + else if ( keystroke == 27 ) + { + this.hide(); + this.editor.focus(); + } + return false; + }, + + onHide : function() + { + if ( CKEDITOR.env.ie ) + { + var selection = this.editor.getSelection(); + selection && selection.unlock(); + } + + this.onHide && this.onHide(); + }, + showSubMenu : function( index ) { var menu = this._.subMenu, @@ -98,9 +177,7 @@ CKEDITOR.plugins.add( 'menu', menu = this._.subMenu = new CKEDITOR.menu( this.editor, CKEDITOR.tools.extend( {}, this._.definition, { level : this._.level + 1 }, true ) ); menu.parent = this; - menu.onClick = CKEDITOR.tools.bind( this.onClick, this ); - // Sub menu use their own scope for binding onEscape. - menu.onEscape = this.onEscape; + menu._.onClick = CKEDITOR.tools.bind( this._.onClick, this ); } // Add all submenu items to the menu. @@ -142,6 +219,17 @@ CKEDITOR.plugins.add( 'menu', show : function( offsetParent, corner, offsetX, offsetY ) { + // Not for sub menu. + if ( !this.parent ) + { + this._.onShow(); + // Don't menu with zero items. + if ( ! this.items.length ) + return; + } + + corner = corner || ( this.editor.lang.dir == 'rtl' ? 2 : 1 ); + var items = this.items, editor = this.editor, panel = this._.panel, @@ -157,14 +245,14 @@ CKEDITOR.plugins.add( 'menu', panel.onEscape = CKEDITOR.tools.bind( function( keystroke ) { - if ( this.onEscape && this.onEscape( keystroke ) === false ) + if ( this._.onEscape( keystroke ) === false ) return false; }, this ); panel.onHide = CKEDITOR.tools.bind( function() { - this.onHide && this.onHide(); + this._.onHide && this._.onHide(); }, this ); @@ -213,7 +301,7 @@ CKEDITOR.plugins.add( 'menu', if ( item.getItems ) this._.showSubMenu( index ); else - this.onClick && this.onClick( item ); + this._.onClick( item ); }, this ); } @@ -247,6 +335,8 @@ CKEDITOR.plugins.add( 'menu', // Inject the HTML inside the panel. element.setHtml( output.join( '' ) ); + CKEDITOR.ui.fire( 'ready', this ); + // Show the panel. if ( this.parent ) this.parent._.panel.showAsChild( panel, this.id, offsetParent, corner, offsetX, offsetY ); @@ -256,8 +346,14 @@ CKEDITOR.plugins.add( 'menu', editor.fire( 'menuShow', [ panel ] ); }, + addListener : function( listenerFn ) + { + this._.listeners.push( listenerFn ); + }, + hide : function() { + this._.onHide && this._.onHide(); this._.panel && this._.panel.hide(); } } @@ -277,47 +373,45 @@ CKEDITOR.plugins.add( 'menu', 0; }); } -})(); - -CKEDITOR.menuItem = CKEDITOR.tools.createClass( -{ - $ : function( editor, name, definition ) + CKEDITOR.menuItem = CKEDITOR.tools.createClass( { - CKEDITOR.tools.extend( this, definition, - // Defaults - { - order : 0, - className : 'cke_button_' + name - }); + $ : function( editor, name, definition ) + { + CKEDITOR.tools.extend( this, definition, + // Defaults + { + order : 0, + className : 'cke_button_' + name + }); - // Transform the group name into its order number. - this.group = editor._.menuGroups[ this.group ]; + // Transform the group name into its order number. + this.group = editor._.menuGroups[ this.group ]; - this.editor = editor; - this.name = name; - }, + this.editor = editor; + this.name = name; + }, - proto : - { - render : function( menu, index, output ) + proto : { - var id = menu.id + String( index ), - state = ( typeof this.state == 'undefined' ) ? CKEDITOR.TRISTATE_OFF : this.state; + render : function( menu, index, output ) + { + var id = menu.id + String( index ), + state = ( typeof this.state == 'undefined' ) ? CKEDITOR.TRISTATE_OFF : this.state; - var classes = ' cke_' + ( - state == CKEDITOR.TRISTATE_ON ? 'on' : - state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' : - 'off' ); + var classes = ' cke_' + ( + state == CKEDITOR.TRISTATE_ON ? 'on' : + state == CKEDITOR.TRISTATE_DISABLED ? 'disabled' : + 'off' ); - var htmlLabel = this.label; + var htmlLabel = this.label; - if ( this.className ) - classes += ' ' + this.className; + if ( this.className ) + classes += ' ' + this.className; var hasSubMenu = this.getItems; output.push( - '' + + '' + '' ); } - output.push( - htmlLabel, - '' + - '' + - '' ); + output.push( + htmlLabel, + '' + + '' + + '' ); } - } -}); + } + }); + +})(); + /** * The amount of time, in milliseconds, the editor waits before showing submenu