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
3 // found in the LICENSE file.
6 * @fileoverview This implements a special button that is useful for showing a
10 cr.define('cr.ui', function() {
11 /** @const */ var MenuButton = cr.ui.MenuButton;
14 * Helper function for ContextMenuButton to find the first ancestor of the
15 * button that has a context menu.
16 * @param {!cr.ui.MenuButton} button The button to start the search from.
17 * @return {HTMLElement} The found element or null if not found.
19 function getContextMenuTarget(button) {
23 } while (el && !('contextMenu' in el));
24 return el ? assertInstanceof(el, HTMLElement) : null;
28 * Creates a new menu button which is used to show the context menu for an
29 * ancestor that has a {@code contextMenu} property.
30 * @param {Object=} opt_propertyBag Optional properties.
32 * @extends {cr.ui.MenuButton}
34 var ContextMenuButton = cr.ui.define('button');
36 ContextMenuButton.prototype = {
37 __proto__: MenuButton.prototype,
40 * Override to return the contextMenu for the ancestor.
45 var target = getContextMenuTarget(this);
46 return target && target.contextMenu;
50 decorate: function() {
52 this.addEventListener('mouseup', this);
53 MenuButton.prototype.decorate.call(this);
57 handleEvent: function(e) {
60 // Menu buttons prevent focus changes.
61 var target = getContextMenuTarget(this);
66 // Stop mouseup to prevent selection changes.
70 MenuButton.prototype.handleEvent.call(this, e);
74 * Override MenuButton showMenu to allow the mousedown to be fully handled
75 * before the menu is shown. This is important in case the mousedown
76 * triggers command changes.
77 * @param {boolean} shouldSetFocus Whether the menu should be focused after
80 showMenu: function(shouldSetFocus) {
82 window.setTimeout(function() {
83 MenuButton.prototype.showMenu.call(self, shouldSetFocus);
90 ContextMenuButton: ContextMenuButton