| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.define('cr.ui', function() { | 5 cr.define('cr.ui', function() { |
| 6 | 6 |
| 7 /** @const */ var Menu = cr.ui.Menu; | 7 /** @const */ var Menu = cr.ui.Menu; |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Handles context menus. | 10 * Handles context menus. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 showMenu: function(e, menu) { | 32 showMenu: function(e, menu) { |
| 33 this.menu_ = menu; | 33 this.menu_ = menu; |
| 34 menu.updateCommands(e.currentTarget); | 34 menu.updateCommands(e.currentTarget); |
| 35 menu.hidden = false; | 35 menu.hidden = false; |
| 36 menu.contextElement = e.currentTarget; | 36 menu.contextElement = e.currentTarget; |
| 37 | 37 |
| 38 // when the menu is shown we steal all keyboard events. | 38 // when the menu is shown we steal all keyboard events. |
| 39 var doc = menu.ownerDocument; | 39 var doc = menu.ownerDocument; |
| 40 doc.addEventListener('keydown', this, true); | 40 doc.addEventListener('keydown', this, true); |
| 41 doc.addEventListener('mousedown', this, true); | 41 doc.addEventListener('mousedown', this, true); |
| 42 doc.addEventListener('blur', this, true); | 42 doc.addEventListener('focus', this, true); |
| 43 doc.defaultView.addEventListener('resize', this); | 43 doc.defaultView.addEventListener('resize', this); |
| 44 menu.addEventListener('contextmenu', this); | 44 menu.addEventListener('contextmenu', this); |
| 45 menu.addEventListener('activate', this); | 45 menu.addEventListener('activate', this); |
| 46 this.positionMenu_(e, menu); | 46 this.positionMenu_(e, menu); |
| 47 }, | 47 }, |
| 48 | 48 |
| 49 /** | 49 /** |
| 50 * Hide the currently shown menu. | 50 * Hide the currently shown menu. |
| 51 */ | 51 */ |
| 52 hideMenu: function() { | 52 hideMenu: function() { |
| 53 var menu = this.menu; | 53 var menu = this.menu; |
| 54 if (!menu) | 54 if (!menu) |
| 55 return; | 55 return; |
| 56 | 56 |
| 57 menu.hidden = true; | 57 menu.hidden = true; |
| 58 menu.contextElement = null; | 58 menu.contextElement = null; |
| 59 var doc = menu.ownerDocument; | 59 var doc = menu.ownerDocument; |
| 60 doc.removeEventListener('keydown', this, true); | 60 doc.removeEventListener('keydown', this, true); |
| 61 doc.removeEventListener('mousedown', this, true); | 61 doc.removeEventListener('mousedown', this, true); |
| 62 doc.removeEventListener('blur', this, true); | 62 doc.removeEventListener('focus', this, true); |
| 63 doc.defaultView.removeEventListener('resize', this); | 63 doc.defaultView.removeEventListener('resize', this); |
| 64 menu.removeEventListener('contextmenu', this); | 64 menu.removeEventListener('contextmenu', this); |
| 65 menu.removeEventListener('activate', this); | 65 menu.removeEventListener('activate', this); |
| 66 menu.selectedIndex = -1; | 66 menu.selectedIndex = -1; |
| 67 this.menu_ = null; | 67 this.menu_ = null; |
| 68 | 68 |
| 69 // On windows we might hide the menu in a right mouse button up and if | 69 // On windows we might hide the menu in a right mouse button up and if |
| 70 // that is the case we wait some short period before we allow the menu | 70 // that is the case we wait some short period before we allow the menu |
| 71 // to be shown again. | 71 // to be shown again. |
| 72 this.hideTimestamp_ = cr.isWindows ? Date.now() : 0; | 72 this.hideTimestamp_ = cr.isWindows ? Date.now() : 0; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 e.preventDefault(); | 139 e.preventDefault(); |
| 140 | 140 |
| 141 // If the menu is visible we let it handle all the keyboard events. | 141 // If the menu is visible we let it handle all the keyboard events. |
| 142 } else if (this.menu) { | 142 } else if (this.menu) { |
| 143 this.menu.handleKeyDown(e); | 143 this.menu.handleKeyDown(e); |
| 144 e.preventDefault(); | 144 e.preventDefault(); |
| 145 e.stopPropagation(); | 145 e.stopPropagation(); |
| 146 } | 146 } |
| 147 break; | 147 break; |
| 148 | 148 |
| 149 case 'focus': |
| 150 if (!this.menu.contains(e.target)) |
| 151 this.hideMenu(); |
| 152 break; |
| 153 |
| 149 case 'activate': | 154 case 'activate': |
| 150 case 'blur': | |
| 151 case 'resize': | 155 case 'resize': |
| 152 this.hideMenu(); | 156 this.hideMenu(); |
| 153 break; | 157 break; |
| 154 | 158 |
| 155 case 'contextmenu': | 159 case 'contextmenu': |
| 156 if ((!this.menu || !this.menu.contains(e.target)) && | 160 if ((!this.menu || !this.menu.contains(e.target)) && |
| 157 (!this.hideTimestamp_ || Date.now() - this.hideTimestamp_ > 50)) | 161 (!this.hideTimestamp_ || Date.now() - this.hideTimestamp_ > 50)) |
| 158 this.showMenu(e, e.currentTarget.contextMenu); | 162 this.showMenu(e, e.currentTarget.contextMenu); |
| 159 e.preventDefault(); | 163 e.preventDefault(); |
| 160 // Don't allow elements further up in the DOM to show their menus. | 164 // Don't allow elements further up in the DOM to show their menus. |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 * The singleton context menu handler. | 237 * The singleton context menu handler. |
| 234 * @type {!ContextMenuHandler} | 238 * @type {!ContextMenuHandler} |
| 235 */ | 239 */ |
| 236 var contextMenuHandler = new ContextMenuHandler; | 240 var contextMenuHandler = new ContextMenuHandler; |
| 237 | 241 |
| 238 // Export | 242 // Export |
| 239 return { | 243 return { |
| 240 contextMenuHandler: contextMenuHandler | 244 contextMenuHandler: contextMenuHandler |
| 241 }; | 245 }; |
| 242 }); | 246 }); |
| OLD | NEW |