Commit 40ea4a14 authored by Luciano Pacheco's avatar Luciano Pacheco Committed by Commit Bot

[Files app] ES6 class for ComboButton

|cr.define| used for this element doesn't support inheritance properly
[1], so to make the inheritance work with |cr.define| parent element
|cr.ui.MenuButton| added a static |decorate| method which is called by
the rest of |cr| framework and in this method inject back the prototype
in the same way it happens in the |cr.ui.define| [2].

[1] - https://cs.chromium.org/chromium/src/ui/webui/resources/js/cr.js?l=269-281
[2] - https://cs.chromium.org/chromium/src/ui/webui/resources/js/cr/ui.js?l=97-100

Test: Manually checked that the component is working in addition to normal integration tests
Bug: 778674
Change-Id: I9531c517c6a31b01e23fb42c3d8aec70081b8712
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1614725
Commit-Queue: François Degros <fdegros@chromium.org>
Auto-Submit: Luciano Pacheco <lucmult@chromium.org>
Reviewed-by: default avatarFrançois Degros <fdegros@chromium.org>
Cr-Commit-Position: refs/heads/master@{#660634}
parent f9dea821
...@@ -4,31 +4,37 @@ ...@@ -4,31 +4,37 @@
/** /**
* @fileoverview This implements a combobutton control. * @fileoverview This implements a combobutton control.
* TODO(yawano): Migrate combobutton to Polymer element.
*/ */
cr.define('cr.ui', () => { cr.define('cr.ui', () => {
/** /**
* Creates a new combobutton element. * Creates a new combo button element.
* @param {Object=} opt_propertyBag Optional properties.
* @constructor
* @extends {cr.ui.MenuButton}
*/ */
const ComboButton = cr.ui.define(cr.ui.MenuButton); class ComboButton extends cr.ui.MenuButton {
/**
* @param {Object=} opt_propertyBag Optional properties.
*/
constructor(opt_propertyBag) {
super(opt_propertyBag);
/** @private {?cr.ui.MenuItem} */
this.defaultItem_ = null;
ComboButton.prototype = { /** @private {?Element} */
__proto__: cr.ui.MenuButton.prototype, this.trigger_ = null;
defaultItem_: null, /** @private {?Element} */
this.actionNode_ = null;
}
/** /**
* Truncates drop-down list. * Truncates drop-down list.
*/ */
clear: function() { clear() {
this.menu.clear(); this.menu.clear();
this.multiple = false; this.multiple = false;
}, }
addDropDownItem: function(item) { addDropDownItem(item) {
this.multiple = true; this.multiple = true;
const menuitem = this.menu.addMenuItem(item); const menuitem = this.menu.addMenuItem(item);
...@@ -46,30 +52,41 @@ cr.define('cr.ui', () => { ...@@ -46,30 +52,41 @@ cr.define('cr.ui', () => {
menuitem.style.fontWeight = 'bold'; menuitem.style.fontWeight = 'bold';
} }
return menuitem; return menuitem;
}, }
/** /**
* Adds separator to drop-down list. * Adds separator to drop-down list.
*/ */
addSeparator: function() { addSeparator() {
this.menu.addSeparator(); this.menu.addSeparator();
}, }
/** /**
* Default item to fire on combobox click * Default item to fire on combobox click
*/ */
get defaultItem() { get defaultItem() {
return this.defaultItem_; return this.defaultItem_;
}, }
set defaultItem(defaultItem) { set defaultItem(defaultItem) {
this.defaultItem_ = defaultItem; this.defaultItem_ = defaultItem;
this.actionNode_.textContent = defaultItem.label || ''; this.actionNode_.textContent = defaultItem.label || '';
}, }
/**
* cr.ui.decorate expects a static |decorate| method.
*
* @param {HTMLElement} el Element to be ComboButton.
* @public
*/
static decorate(el) {
el.__proto__ = cr.ui.ComboButton.prototype;
el.decorate();
}
/** /**
* Initializes the element. * Initializes the element.
*/ */
decorate: function() { decorate() {
cr.ui.MenuButton.prototype.decorate.call(this); cr.ui.MenuButton.prototype.decorate.call(this);
this.classList.add('combobutton'); this.classList.add('combobutton');
...@@ -118,12 +135,12 @@ cr.define('cr.ui', () => { ...@@ -118,12 +135,12 @@ cr.define('cr.ui', () => {
// and move it down to trigger_. // and move it down to trigger_.
this.removeEventListener('mousedown', this); this.removeEventListener('mousedown', this);
this.trigger_.addEventListener('mousedown', this); this.trigger_.addEventListener('mousedown', this);
}, }
/** /**
* Handles the keydown event for the menu button. * Handles the keydown event for the menu button.
*/ */
handleKeyDown: function(e) { handleKeyDown(e) {
switch (e.key) { switch (e.key) {
case 'ArrowDown': case 'ArrowDown':
case 'ArrowUp': case 'ArrowUp':
...@@ -136,17 +153,17 @@ cr.define('cr.ui', () => { ...@@ -136,17 +153,17 @@ cr.define('cr.ui', () => {
this.hideMenu(); this.hideMenu();
break; break;
} }
}, }
handleTriggerClicked_: function(event) { handleTriggerClicked_(event) {
event.stopPropagation(); event.stopPropagation();
}, }
handleMenuActivate_: function(event) { handleMenuActivate_(event) {
this.dispatchSelectEvent(event.target.data); this.dispatchSelectEvent(event.target.data);
}, }
handleButtonClick_: function(event) { handleButtonClick_(event) {
if (this.multiple) { if (this.multiple) {
// When there are multiple choices just show/hide menu. // When there are multiple choices just show/hide menu.
if (this.isMenuShown()) { if (this.isMenuShown()) {
...@@ -160,22 +177,22 @@ cr.define('cr.ui', () => { ...@@ -160,22 +177,22 @@ cr.define('cr.ui', () => {
this.blur(); this.blur();
this.dispatchSelectEvent(this.defaultItem_); this.dispatchSelectEvent(this.defaultItem_);
} }
}, }
handleMenuShow_: function() { handleMenuShow_() {
this.filesToggleRipple_.activated = true; this.filesToggleRipple_.activated = true;
}, }
handleMenuHide_: function() { handleMenuHide_() {
this.filesToggleRipple_.activated = false; this.filesToggleRipple_.activated = false;
}, }
dispatchSelectEvent: function(item) { dispatchSelectEvent(item) {
const selectEvent = new Event('select'); const selectEvent = new Event('select');
selectEvent.item = item; selectEvent.item = item;
this.dispatchEvent(selectEvent); this.dispatchEvent(selectEvent);
} }
}; }
cr.defineProperty(ComboButton, 'disabled', cr.PropertyKind.BOOL_ATTR); cr.defineProperty(ComboButton, 'disabled', cr.PropertyKind.BOOL_ATTR);
cr.defineProperty(ComboButton, 'multiple', cr.PropertyKind.BOOL_ATTR); cr.defineProperty(ComboButton, 'multiple', cr.PropertyKind.BOOL_ATTR);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment