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.
8 * @fileoverview This implements a common button control, bound to command.
12 * Creates a new button element.
13 * @param {Object=} opt_propertyBag Optional properties.
15 * @extends {HTMLDivElement}
17 var CommandButton = cr.ui.define('button');
20 CommandButton.prototype.__proto__ = HTMLButtonElement.prototype;
27 CommandButton.prototype.command_ = null;
30 * Initializes the menu item.
32 CommandButton.prototype.decorate = function() {
34 if ((commandId = this.getAttribute('command')))
35 this.setCommand(commandId);
37 this.addEventListener('click', this.handleClick_.bind(this));
38 this.addEventListener('keypress', this.handleKeyPress_.bind(this));
42 * Returns associated command.
43 * @return {cr.ui.Command} associated command.
45 CommandButton.prototype.getCommand = function() {
50 * Associates command with this button.
51 * @param {string|cr.ui.Command} command Command id, or command object to
52 * associate with this button.
54 CommandButton.prototype.setCommand = function(command) {
56 this.command_.removeEventListener('labelChange', this);
57 this.command_.removeEventListener('disabledChange', this);
58 this.command_.removeEventListener('hiddenChange', this);
61 if (typeof command == 'string' && command[0] == '#') {
62 command = this.ownerDocument.getElementById(command.slice(1));
63 cr.ui.decorate(command, cr.ui.Command);
66 this.command_ = command;
69 this.setAttribute('command', '#' + command.id);
71 this.setLabel(command.label);
72 this.disabled = command.disabled;
73 this.hidden = command.hidden;
75 this.command_.addEventListener('labelChange', this);
76 this.command_.addEventListener('disabledChange', this);
77 this.command_.addEventListener('hiddenChange', this);
82 * Returns button label
83 * @return {string} Button label.
85 CommandButton.prototype.getLabel = function() {
86 return this.textContent;
91 * @param {string} label New button label.
93 CommandButton.prototype.setLabel = function(label) {
94 this.textContent = label;
98 * Handles click event and dispatches associated command.
99 * @param {Event} e The mouseup event object.
102 CommandButton.prototype.handleClick_ = function(e) {
103 if (!this.disabled && this.command_)
104 this.command_.execute(this);
108 * Handles keypress event and dispatches associated command.
109 * @param {Event} e The mouseup event object.
112 CommandButton.prototype.handleKeyPress_ = function(e) {
113 if (!this.command_) return;
115 switch (util.getKeyModifiers(e) + e.keyCode) {
118 this.command_.execute(this);
124 * Handles changes to the associated command.
125 * @param {Event} e The event object.
127 CommandButton.prototype.handleEvent = function(e) {
129 case 'disabledChange':
130 this.disabled = this.command_.disabled;
133 this.hidden = this.command_.hidden;
136 this.setLabel(this.command_.label);
142 * Whether the button is disabled or not.
145 cr.defineProperty(CommandButton, 'disabled',
146 cr.PropertyKind.BOOL_ATTR);
149 * Whether the button is hidden or not.
152 cr.defineProperty(CommandButton, 'hidden',
153 cr.PropertyKind.BOOL_ATTR);