X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fmenu%2Fplugin.js;fp=_source%2Fplugins%2Fmenu%2Fplugin.js;h=5d37ffda020723f252a92c96819f7fb8227f90d3;hb=9afde8772159bd3436f1f5b7862960307710ae5a;hp=1ea12b21515bf5671e19ef42f37d1e3ba5f0b4ee;hpb=614511639979907ceb0da3614122a4d8eb963ad4;p=ckeditor.git diff --git a/_source/plugins/menu/plugin.js b/_source/plugins/menu/plugin.js index 1ea12b2..5d37ffd 100644 --- a/_source/plugins/menu/plugin.js +++ b/_source/plugins/menu/plugin.js @@ -53,6 +53,7 @@ CKEDITOR.plugins.add( 'menu', 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,42 +373,40 @@ 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; @@ -329,25 +423,25 @@ CKEDITOR.menuItem = CKEDITOR.tools.createClass( ( state == CKEDITOR.TRISTATE_DISABLED ? 'aria-disabled="true"' : '' ) + ( state == CKEDITOR.TRISTATE_ON ? 'aria-pressed="true"' : '' ) ); - // Some browsers don't cancel key events in the keydown but in the - // keypress. - // TODO: Check if really needed for Gecko+Mac. - if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) ) - { - output.push( - ' onkeypress="return false;"' ); - } + // Some browsers don't cancel key events in the keydown but in the + // keypress. + // TODO: Check if really needed for Gecko+Mac. + if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) ) + { + output.push( + ' onkeypress="return false;"' ); + } - // With Firefox, we need to force the button to redraw, otherwise it - // will remain in the focus state. - if ( CKEDITOR.env.gecko ) - { - output.push( - ' onblur="this.style.cssText = this.style.cssText;"' ); - } + // With Firefox, we need to force the button to redraw, otherwise it + // will remain in the focus state. + if ( CKEDITOR.env.gecko ) + { + output.push( + ' onblur="this.style.cssText = this.style.cssText;"' ); + } - var offset = ( this.iconOffset || 0 ) * -16; - output.push( + var offset = ( this.iconOffset || 0 ) * -16; + output.push( // ' onkeydown="return CKEDITOR.ui.button._.keydown(', index, ', event);"' + ' onmouseover="CKEDITOR.tools.callFunction(', menu._.itemOverFn, ',', index, ');"' + ' onmouseout="CKEDITOR.tools.callFunction(', menu._.itemOutFn, ',', index, ');"' + @@ -371,14 +465,17 @@ CKEDITOR.menuItem = CKEDITOR.tools.createClass( '' ); } - output.push( - htmlLabel, - '' + - '' + - '' ); + output.push( + htmlLabel, + '' + + '' + + '' ); } - } -}); + } + }); + +})(); + /** * The amount of time, in milliseconds, the editor waits before showing submenu