JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.2
[ckeditor.git] / _source / plugins / contextmenu / plugin.js
index 1c83546..8d7cb8d 100644 (file)
@@ -25,7 +25,7 @@ CKEDITOR.plugins.contextMenu = CKEDITOR.tools.createClass(
 {\r
        $ : function( editor )\r
        {\r
-               this.id = 'cke_' + CKEDITOR.tools.getNextNumber();\r
+               this.id = CKEDITOR.tools.getNextId();\r
                this.editor = editor;\r
                this._.listeners = [];\r
                this._.functionId = CKEDITOR.tools.addFunction( function( commandName )\r
@@ -35,6 +35,18 @@ CKEDITOR.plugins.contextMenu = CKEDITOR.tools.createClass(
                                editor.execCommand( commandName );\r
                        },\r
                        this);\r
+\r
+               this.definition =\r
+               {\r
+                       panel:\r
+                       {\r
+                               className : editor.skinClass + ' cke_contextmenu',\r
+                               attributes :\r
+                               {\r
+                                       'aria-label' : editor.lang.contextmenu.options\r
+                               }\r
+                       }\r
+               };\r
        },\r
 \r
        _ :\r
@@ -51,7 +63,7 @@ CKEDITOR.plugins.contextMenu = CKEDITOR.tools.createClass(
                        }\r
                        else\r
                        {\r
-                               menu = this._.menu = new CKEDITOR.menu( editor );\r
+                               menu = this._.menu = new CKEDITOR.menu( editor, this.definition );\r
                                menu.onClick = CKEDITOR.tools.bind( function( item )\r
                                {\r
                                        menu.hide();\r
@@ -63,9 +75,26 @@ CKEDITOR.plugins.contextMenu = CKEDITOR.tools.createClass(
 \r
                                }, this );\r
 \r
-                               menu.onEscape = function()\r
+                               menu.onEscape = function( keystroke )\r
                                {\r
-                                       editor.focus();\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
+                                       {\r
+                                               this.hide();\r
+                                               editor.focus();\r
+                                       }\r
+                                       return false;\r
                                };\r
                        }\r
 \r
@@ -118,13 +147,14 @@ CKEDITOR.plugins.contextMenu = CKEDITOR.tools.createClass(
        {\r
                addTarget : function( element, nativeContextMenuOnCtrl )\r
                {\r
-                       // Opera doesn't support 'contextmenu' event, we have duo approaches employed here:\r
-                       // 1. Inherit the 'button override' hack we introduced in v2 (#4530), while this require the Opera browser\r
-                       //  option 'Allow script to detect context menu/right click events' to be always turned on.\r
+\r
+                       // For browsers (Opera <=10a) that doesn't  support 'contextmenu' event, we have duo approaches employed here:\r
+                       // 1. Inherit the 'button override' hack we introduced in v2 (#4530) (In Opera browser, this require the\r
+                       //  option 'Allow script to detect context menu/right click events' to be always turned on).\r
                        // 2. Considering the fact that ctrl/meta key is not been occupied\r
                        //  for multiple range selecting (like Gecko), we use this key\r
                        //  combination as a fallback for triggering context-menu. (#4530)\r
-                       if ( CKEDITOR.env.opera )\r
+                       if ( CKEDITOR.env.opera && !( 'oncontextmenu' in document.body ) )\r
                        {\r
                                var contextMenuOverrideButton;\r
                                element.on( 'mousedown', function( evt )\r
@@ -138,7 +168,7 @@ CKEDITOR.plugins.contextMenu = CKEDITOR.tools.createClass(
                                        }\r
 \r
                                        if ( nativeContextMenuOnCtrl\r
-                                                && ( evt.$.ctrlKey || evt.$.metaKey ) )\r
+                                                && ( CKEDITOR.env.mac ? evt.$.metaKey : evt.$.ctrlKey ) )\r
                                                return;\r
 \r
                                        var target = evt.getTarget();\r
@@ -176,16 +206,9 @@ CKEDITOR.plugins.contextMenu = CKEDITOR.tools.createClass(
                                        if ( nativeContextMenuOnCtrl &&\r
                                             // Safari on Windows always show 'ctrlKey' as true in 'contextmenu' event,\r
                                                // which make this property unreliable. (#4826)\r
-                                            ( CKEDITOR.env.webkit ? holdCtrlKey : domEvent.$.ctrlKey || domEvent.$.metaKey ) )\r
+                                            ( CKEDITOR.env.webkit ? holdCtrlKey : ( CKEDITOR.env.mac ? domEvent.$.metaKey : domEvent.$.ctrlKey ) ) )\r
                                                return;\r
 \r
-                                       // Selection will be unavailable after context menu shows up\r
-                                       // in IE, lock it now.\r
-                                       if ( CKEDITOR.env.ie )\r
-                                       {\r
-                                               var selection = this.editor.getSelection();\r
-                                               selection && selection.lock();\r
-                                       }\r
 \r
                                        // Cancel the browser context menu.\r
                                        domEvent.preventDefault();\r
@@ -196,18 +219,31 @@ CKEDITOR.plugins.contextMenu = CKEDITOR.tools.createClass(
 \r
                                        CKEDITOR.tools.setTimeout( function()\r
                                                {\r
-                                                       this._.onMenu( offsetParent, null, offsetX, offsetY );\r
+                                                       this.show( offsetParent, null, offsetX, offsetY );\r
                                                },\r
                                                0, this );\r
                                },\r
                                this );\r
 \r
+                       if ( CKEDITOR.env.opera )\r
+                       {\r
+                               // 'contextmenu' event triggered by Windows menu key is unpreventable,\r
+                               // cancel the key event itself. (#6534)\r
+                               element.on( 'keypress' , function ( evt )\r
+                               {\r
+                                       var domEvent = evt.data;\r
+\r
+                                       if ( domEvent.$.keyCode === 0 )\r
+                                               domEvent.preventDefault();\r
+                               });\r
+                       }\r
+\r
                        if ( CKEDITOR.env.webkit )\r
                        {\r
                                var holdCtrlKey,\r
                                        onKeyDown = function( event )\r
                                        {\r
-                                               holdCtrlKey = event.data.$.ctrlKey || event.data.$.metaKey;\r
+                                               holdCtrlKey = CKEDITOR.env.mac ? event.data.$.metaKey : event.data.$.ctrlKey ;\r
                                        },\r
                                        resetOnKeyUp = function()\r
                                        {\r
@@ -228,6 +264,15 @@ CKEDITOR.plugins.contextMenu = CKEDITOR.tools.createClass(
                show : function( offsetParent, corner, offsetX, offsetY )\r
                {\r
                        this.editor.focus();\r
+\r
+                       // Selection will be unavailable after context menu shows up\r
+                       // in IE, lock it now.\r
+                       if ( CKEDITOR.env.ie )\r
+                       {\r
+                               var selection = this.editor.getSelection();\r
+                               selection && selection.lock();\r
+                       }\r
+\r
                        this._.onMenu( offsetParent || CKEDITOR.document.getDocumentElement(), corner, offsetX || 0, offsetY || 0 );\r
                }\r
        }\r