| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 /** | 4 /** |
| 5 * @unrestricted | 5 * @unrestricted |
| 6 */ | 6 */ |
| 7 UI.DropDownMenu = class extends Common.Object { | 7 UI.DropDownMenu = class extends Common.ObjectBase { |
| 8 /** | 8 /** |
| 9 * @param {!Element} element | 9 * @param {!Element} element |
| 10 */ | 10 */ |
| 11 constructor(element) { | 11 constructor(element) { |
| 12 super(); | 12 super(); |
| 13 /** @type {!Array.<!UI.DropDownMenu.Item>} */ | 13 /** @type {!Array.<!UI.DropDownMenu.Item>} */ |
| 14 this._items = []; | 14 this._items = []; |
| 15 | 15 |
| 16 element.addEventListener('mousedown', this._onMouseDown.bind(this)); | 16 element.addEventListener('mousedown', this._onMouseDown.bind(this)); |
| 17 } | 17 } |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * @param {!Event} event | 20 * @param {!Event} event |
| 21 */ | 21 */ |
| 22 _onMouseDown(event) { | 22 _onMouseDown(event) { |
| 23 if (event.which !== 1) | 23 if (event.which !== 1) |
| 24 return; | 24 return; |
| 25 var menu = new UI.ContextMenu(event); | 25 var menu = new UI.ContextMenu(event); |
| 26 for (var item of this._items) | 26 for (var item of this._items) |
| 27 menu.appendCheckboxItem(item.title, this._itemHandler.bind(this, item.id),
item.id === this._selectedItemId); | 27 menu.appendCheckboxItem(item.title, this._itemHandler.bind(this, item.id),
item.id === this._selectedItemId); |
| 28 menu.show(); | 28 menu.show(); |
| 29 } | 29 } |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * @param {string} id | 32 * @param {string} id |
| 33 */ | 33 */ |
| 34 _itemHandler(id) { | 34 _itemHandler(id) { |
| 35 this.dispatchEventToListeners(UI.DropDownMenu.Events.ItemSelected, id); | 35 this.dispatchEventToListeners(new UI.DropDownMenu.ItemSelectedEvent(id)); |
| 36 } | 36 } |
| 37 | 37 |
| 38 /** | 38 /** |
| 39 * @param {string} id | 39 * @param {string} id |
| 40 * @param {string} title | 40 * @param {string} title |
| 41 */ | 41 */ |
| 42 addItem(id, title) { | 42 addItem(id, title) { |
| 43 this._items.push({id: id, title: title}); | 43 this._items.push({id: id, title: title}); |
| 44 } | 44 } |
| 45 | 45 |
| 46 /** | 46 /** |
| 47 * @param {string} id | 47 * @param {string} id |
| 48 */ | 48 */ |
| 49 selectItem(id) { | 49 selectItem(id) { |
| 50 this._selectedItemId = id; | 50 this._selectedItemId = id; |
| 51 } | 51 } |
| 52 | 52 |
| 53 clear() { | 53 clear() { |
| 54 this._items = []; | 54 this._items = []; |
| 55 delete this._selectedItemId; | 55 delete this._selectedItemId; |
| 56 } | 56 } |
| 57 }; | 57 }; |
| 58 | 58 |
| 59 /** @typedef {{id: string, title: string}} */ | 59 /** @typedef {{id: string, title: string}} */ |
| 60 UI.DropDownMenu.Item; | 60 UI.DropDownMenu.Item; |
| 61 | 61 |
| 62 /** @enum {symbol} */ | 62 UI.DropDownMenu.ItemSelectedEvent = class { |
| 63 UI.DropDownMenu.Events = { | 63 /** |
| 64 ItemSelected: Symbol('ItemSelected') | 64 * @param {string} id |
| 65 */ |
| 66 constructor(id) { |
| 67 this.id = id; |
| 68 } |
| 65 }; | 69 }; |
| OLD | NEW |