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));
41 * Returns associated command.
42 * @return {cr.ui.Command} associated command.
44 CommandButton.prototype.getCommand = function() {
49 * Associates command with this button.
50 * @param {string|cr.ui.Command} command Command id, or command object to
51 * associate with this button.
53 CommandButton.prototype.setCommand = function(command) {
55 this.command_.removeEventListener('labelChange', this);
56 this.command_.removeEventListener('disabledChange', this);
57 this.command_.removeEventListener('hiddenChange', this);
60 if (typeof command == 'string' && command[0] == '#') {
61 command = this.ownerDocument.getElementById(command.slice(1));
62 cr.ui.decorate(command, cr.ui.Command);
65 this.command_ = command;
68 this.setAttribute('command', '#' + command.id);
70 this.setLabel(command.label);
71 this.disabled = command.disabled;
72 this.hidden = command.hidden;
74 this.command_.addEventListener('labelChange', this);
75 this.command_.addEventListener('disabledChange', this);
76 this.command_.addEventListener('hiddenChange', this);
81 * Returns button label
82 * @return {string} Button label.
84 CommandButton.prototype.getLabel = function() {
85 return this.textContent;
90 * @param {string} label New button label.
92 CommandButton.prototype.setLabel = function(label) {
93 this.textContent = label;
97 * Handles click event and dispatches associated command.
98 * @param {Event} e The mouseup event object.
101 CommandButton.prototype.handleClick_ = function(e) {
102 if (!this.disabled && this.command_)
103 this.command_.execute(this);
107 * Handles changes to the associated command.
108 * @param {Event} e The event object.
110 CommandButton.prototype.handleEvent = function(e) {
112 case 'disabledChange':
113 this.disabled = this.command_.disabled;
116 this.hidden = this.command_.hidden;
119 this.setLabel(this.command_.label);
125 * Whether the button is disabled or not.
128 cr.defineProperty(CommandButton, 'disabled',
129 cr.PropertyKind.BOOL_ATTR);
132 * Whether the button is hidden or not.
135 cr.defineProperty(CommandButton, 'hidden',
136 cr.PropertyKind.BOOL_ATTR);