X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fmenu%2Fplugin.js;h=731fed4afac65f7ba24aa3b7a72e6a9ab7585ca2;hb=4e70ea24db840898be8cc21c950363a52a2a6aba;hp=74fbb4f3e59c51fe70df32aedda405e2b4ff02b2;hpb=c9fdde67e6384bd5a66adc2b3bba5c4ce9db56c7;p=ckeditor.git diff --git a/_source/plugins/menu/plugin.js b/_source/plugins/menu/plugin.js index 74fbb4f..731fed4 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.command || this.editor.getCommand( item.command ).state ) ) + { + 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 ); @@ -190,15 +278,15 @@ CKEDITOR.plugins.add( 'menu', this._.itemOverFn = CKEDITOR.tools.addFunction( function( index ) { clearTimeout( this._.showSubTimeout ); - this._.showSubTimeout = CKEDITOR.tools.setTimeout( this._.showSubMenu, editor.config.menu_subMenuDelay, this, [ index ] ); + this._.showSubTimeout = CKEDITOR.tools.setTimeout( this._.showSubMenu, editor.config.menu_subMenuDelay || 400, this, [ index ] ); }, - this); + this ); this._.itemOutFn = CKEDITOR.tools.addFunction( function( index ) { clearTimeout( this._.showSubTimeout ); }, - this); + this ); this._.itemClickFn = CKEDITOR.tools.addFunction( function( index ) { @@ -213,16 +301,16 @@ CKEDITOR.plugins.add( 'menu', if ( item.getItems ) this._.showSubMenu( index ); else - this.onClick && this.onClick( item ); + this._.onClick( item ); }, - this); + this ); } // Put the items in the right order. sortItems( items ); - var chromeRoot = editor.container.getChild( 1 ); - var mixedContentClass = chromeRoot.hasClass( 'cke_mixed_dir_content' ) ? ' cke_mixed_dir_content' : ''; + var chromeRoot = editor.container.getChild( 1 ), + mixedContentClass = chromeRoot.hasClass( 'cke_mixed_dir_content' ) ? ' cke_mixed_dir_content' : ''; // Build the HTML that composes the menu and its items. var output = [ '