OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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. |
| 4 |
| 5 cr.define('cr.ui', function() { |
| 6 const Command = cr.ui.Command; |
| 7 |
| 8 /** |
| 9 * Creates a new menu item element. |
| 10 * @param {Object=} opt_propertyBag Optional properties. |
| 11 * @constructor |
| 12 * @extends {HTMLButtonElement} |
| 13 */ |
| 14 var MenuItem = cr.ui.define('button'); |
| 15 |
| 16 /** |
| 17 * Creates a new menu separator element. |
| 18 * @return {cr.ui.MenuItem} |
| 19 */ |
| 20 MenuItem.createSeparator = function() { |
| 21 var el = cr.doc.createElement('hr'); |
| 22 MenuItem.decorate(el); |
| 23 return el; |
| 24 }; |
| 25 |
| 26 MenuItem.prototype = { |
| 27 __proto__: HTMLButtonElement.prototype, |
| 28 |
| 29 /** |
| 30 * Initializes the menu item. |
| 31 */ |
| 32 decorate: function() { |
| 33 var commandId; |
| 34 if ((commandId = this.getAttribute('command'))) |
| 35 this.command = commandId; |
| 36 |
| 37 this.addEventListener('mouseup', this.handleMouseUp_); |
| 38 }, |
| 39 |
| 40 /** |
| 41 * The command associated with this menu item. If this is set to a string |
| 42 * of the form "#element-id" then the element is looked up in the document |
| 43 * of the command. |
| 44 * @type {cr.ui.Command} |
| 45 */ |
| 46 command_: null, |
| 47 get command() { |
| 48 return this.command_; |
| 49 }, |
| 50 set command(command) { |
| 51 if (this.command_) { |
| 52 this.command_.removeEventListener('labelChange', this); |
| 53 this.command_.removeEventListener('disabledChange', this); |
| 54 this.command_.removeEventListener('hiddenChange', this); |
| 55 } |
| 56 |
| 57 if (typeof command == 'string' && command[0] == '#') { |
| 58 command = this.ownerDocument.getElementById(command.slice(1)); |
| 59 cr.ui.decorate(command, Command); |
| 60 } |
| 61 |
| 62 this.command_ = command; |
| 63 if (command) { |
| 64 if (command.id) |
| 65 this.setAttribute('command', '#' + command.id); |
| 66 |
| 67 this.label = command.label; |
| 68 this.disabled = command.disabled; |
| 69 this.hidden = command.hidden; |
| 70 |
| 71 this.command_.addEventListener('labelChange', this); |
| 72 this.command_.addEventListener('disabledChange', this); |
| 73 this.command_.addEventListener('hiddenChange', this); |
| 74 } |
| 75 }, |
| 76 |
| 77 /** |
| 78 * The text label. |
| 79 * @type {string} |
| 80 */ |
| 81 get label() { |
| 82 return this.textContent; |
| 83 }, |
| 84 set label(label) { |
| 85 this.textContent = label; |
| 86 }, |
| 87 |
| 88 /** |
| 89 * @return {boolean} Whether the menu item is a separator. |
| 90 */ |
| 91 isSeparator: function() { |
| 92 return this.tagName == 'HR'; |
| 93 }, |
| 94 |
| 95 /** |
| 96 * Handles mouseup events. This dispatches an active event and if there |
| 97 * is an assiciated command then that is executed. |
| 98 * @param {Event} The mouseup event object. |
| 99 * @private |
| 100 */ |
| 101 handleMouseUp_: function(e) { |
| 102 if (!this.disabled && !this.isSeparator()) { |
| 103 // Dispatch command event followed by executing the command object. |
| 104 if (cr.dispatchSimpleEvent(this, 'activate', true, true)) { |
| 105 var command = this.command; |
| 106 if (command) |
| 107 command.execute(); |
| 108 } |
| 109 } |
| 110 }, |
| 111 |
| 112 /** |
| 113 * Handles changes to the associated command. |
| 114 * @param {Event} e The event object. |
| 115 */ |
| 116 handleEvent: function(e) { |
| 117 switch (e.type) { |
| 118 case 'disabledChange': |
| 119 this.disabled = this.command.disabled; |
| 120 break; |
| 121 case 'hiddenChange': |
| 122 this.hidden = this.command.hidden; |
| 123 break; |
| 124 case 'labelChange': |
| 125 this.label = this.command.label; |
| 126 break; |
| 127 } |
| 128 } |
| 129 }; |
| 130 |
| 131 /** |
| 132 * Whether the menu item is hidden or not. |
| 133 * @type {boolean} |
| 134 */ |
| 135 cr.defineProperty(MenuItem, 'hidden', cr.PropertyKind.BOOL_ATTR); |
| 136 |
| 137 /** |
| 138 * Whether the menu item is selected or not. |
| 139 * @type {boolean} |
| 140 */ |
| 141 cr.defineProperty(MenuItem, 'selected', cr.PropertyKind.BOOL_ATTR); |
| 142 |
| 143 // Export |
| 144 return { |
| 145 MenuItem: MenuItem |
| 146 }; |
| 147 }); |
OLD | NEW |