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 {!MenuButton} el The button to start the search from.
17 * @return {HTMLElement} The found element or null if not found.
19 function getContextMenuTarget(el) {
22 } while (el && !('contextMenu' in el));
27 * Creates a new menu button which is used to show the context menu for an
28 * ancestor that has a {@code contextMenu} property.
29 * @param {Object=} opt_propertyBag Optional properties.
31 * @extends {MenuButton}
33 var ContextMenuButton = cr.ui.define('button');
35 ContextMenuButton.prototype = {
36 __proto__: MenuButton.prototype,
39 * Override to return the contextMenu for the ancestor.
44 var target = getContextMenuTarget(this);
45 return target && target.contextMenu;
49 decorate: function() {
51 this.addEventListener('mouseup', this);
52 MenuButton.prototype.decorate.call(this);
56 handleEvent: function(e) {
59 // Menu buttons prevent focus changes.
60 var target = getContextMenuTarget(this);
65 // Stop mouseup to prevent selection changes.
69 MenuButton.prototype.handleEvent.call(this, e);
73 * Override MenuButton showMenu to allow the mousedown to be fully handled
74 * before the menu is shown. This is important in case the mousedown
75 * triggers command changes.
76 * @param {boolean} shouldSetFocus Whether the menu should be focused after
79 showMenu: function(shouldSetFocus) {
81 window.setTimeout(function() {
82 MenuButton.prototype.showMenu.call(self, shouldSetFocus);
89 ContextMenuButton: ContextMenuButton