blob: ea82e8129c5f16d1b8e8d9bdee6e498d2ab0afa6 [file] [log] [blame]
Tim van der Lippe0830b3d2019-10-03 13:20:071// Copyright 2019 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
Paul Lewis17e384e2020-01-08 15:46:515import * as Common from '../common/common.js';
Simon Zünd0ca08ce2020-08-11 07:33:156import * as Root from '../root/root.js';
Tim van der Lipped1a00aa2020-08-19 16:03:567
Paul Lewis9950e182019-12-16 16:06:078import {ActionDelegate} from './ActionDelegate.js'; // eslint-disable-line no-unused-vars
Tim van der Lipped1a00aa2020-08-19 16:03:569import {Context} from './Context.js';
Paul Lewis9950e182019-12-16 16:06:0710
Simon Zünd0ca08ce2020-08-11 07:33:1511class ActionRuntimeExtensionDescriptor extends // eslint-disable-line no-unused-vars
12 Root.Runtime.RuntimeExtensionDescriptor {
13 constructor() {
14 super();
15
16 /** @type {string|null} */
17 this.iconClass;
18
19 /** @type {string|null} */
20 this.toggledIconClass;
21
22 /** @type {boolean|null} */
23 this.toggleWithRedColor;
24
25 /** @type {string|null} */
26 this.category;
27
28 /** @type {string|null} */
29 this.tags;
30
31 /** @type {boolean|null} */
32 this.toggleable;
33
34 /**
Alex Rudenkoc8950582020-10-08 07:10:1535 * @type {!Array<{
Simon Zünd0ca08ce2020-08-11 07:33:1536 * value: boolean,
37 * title: string,
Alex Rudenkoc8950582020-10-08 07:10:1538 * raw: undefined,
39 * }>|undefined}
Simon Zünd0ca08ce2020-08-11 07:33:1540 */
41 this.options;
42 }
43}
Sigurd Schneider46da7db2020-05-20 13:45:1144
Paul Lewis17e384e2020-01-08 15:46:5145export class Action extends Common.ObjectWrapper.ObjectWrapper {
Tim van der Lippe0830b3d2019-10-03 13:20:0746 /**
47 * @param {!Root.Runtime.Extension} extension
48 */
49 constructor(extension) {
50 super();
51 this._extension = extension;
Simon Zünd0ca08ce2020-08-11 07:33:1552 /** @type {boolean} */
Tim van der Lippe0830b3d2019-10-03 13:20:0753 this._enabled = true;
Simon Zünd0ca08ce2020-08-11 07:33:1554 /** @type {boolean} */
Tim van der Lippe0830b3d2019-10-03 13:20:0755 this._toggled = false;
56 }
57
58 /**
59 * @return {string}
60 */
61 id() {
Simon Zünd0ca08ce2020-08-11 07:33:1562 return this._actionDescriptor().actionId || '';
Tim van der Lippe0830b3d2019-10-03 13:20:0763 }
64
65 /**
Tim van der Lippe6d51bf02020-03-18 12:15:1466 * @return {!Root.Runtime.Extension}
Tim van der Lippe0830b3d2019-10-03 13:20:0767 */
68 extension() {
69 return this._extension;
70 }
71
72 /**
73 * @return {!Promise.<boolean>}
74 */
Jack Lynchf9f8e4b2020-04-02 19:04:1075 async execute() {
76 if (!this._extension.canInstantiate()) {
77 return false;
Tim van der Lippe0830b3d2019-10-03 13:20:0778 }
Jack Lynchf9f8e4b2020-04-02 19:04:1079 const delegate = /** @type {!ActionDelegate} */ (await this._extension.instance());
80 const actionId = this.id();
Tim van der Lipped1a00aa2020-08-19 16:03:5681 return delegate.handleAction(Context.instance(), actionId);
Tim van der Lippe0830b3d2019-10-03 13:20:0782 }
83
84 /**
85 * @return {string}
86 */
87 icon() {
Simon Zünd0ca08ce2020-08-11 07:33:1588 return this._actionDescriptor().iconClass || '';
Tim van der Lippe0830b3d2019-10-03 13:20:0789 }
90
91 /**
92 * @return {string}
93 */
94 toggledIcon() {
Simon Zünd0ca08ce2020-08-11 07:33:1595 return this._actionDescriptor().toggledIconClass || '';
Tim van der Lippe0830b3d2019-10-03 13:20:0796 }
97
98 /**
99 * @return {boolean}
100 */
101 toggleWithRedColor() {
Simon Zünd0ca08ce2020-08-11 07:33:15102 return !!this._actionDescriptor().toggleWithRedColor;
Tim van der Lippe0830b3d2019-10-03 13:20:07103 }
104
105 /**
106 * @param {boolean} enabled
107 */
108 setEnabled(enabled) {
109 if (this._enabled === enabled) {
110 return;
111 }
112
113 this._enabled = enabled;
114 this.dispatchEventToListeners(Events.Enabled, enabled);
115 }
116
117 /**
118 * @return {boolean}
119 */
120 enabled() {
121 return this._enabled;
122 }
123
124 /**
125 * @return {string}
126 */
127 category() {
Simon Zünd0ca08ce2020-08-11 07:33:15128 return ls`${this._actionDescriptor().category || ''}`;
Tim van der Lippe0830b3d2019-10-03 13:20:07129 }
130
131 /**
132 * @return {string}
133 */
134 tags() {
Simon Zünd0ca08ce2020-08-11 07:33:15135 return this._actionDescriptor().tags || '';
Tim van der Lippe0830b3d2019-10-03 13:20:07136 }
137
138 /**
139 * @return {boolean}
140 */
141 toggleable() {
Simon Zünd0ca08ce2020-08-11 07:33:15142 return !!this._actionDescriptor().toggleable;
Tim van der Lippe0830b3d2019-10-03 13:20:07143 }
144
145 /**
146 * @return {string}
147 */
148 title() {
149 let title = this._extension.title() || '';
Simon Zünd0ca08ce2020-08-11 07:33:15150 const options = this._actionDescriptor().options;
Tim van der Lippe0830b3d2019-10-03 13:20:07151 if (options) {
152 for (const pair of options) {
Simon Zünd0ca08ce2020-08-11 07:33:15153 if (pair.value !== this._toggled) {
154 title = ls`${pair.title}`;
Tim van der Lippe0830b3d2019-10-03 13:20:07155 }
156 }
157 }
Lorne Mitchelld65e09c2019-10-09 17:34:50158 return title;
Tim van der Lippe0830b3d2019-10-03 13:20:07159 }
160
161 /**
162 * @return {boolean}
163 */
164 toggled() {
165 return this._toggled;
166 }
167
168 /**
169 * @param {boolean} toggled
170 */
171 setToggled(toggled) {
172 console.assert(this.toggleable(), 'Shouldn\'t be toggling an untoggleable action', this.id());
173 if (this._toggled === toggled) {
174 return;
175 }
176
177 this._toggled = toggled;
178 this.dispatchEventToListeners(Events.Toggled, toggled);
179 }
Simon Zünd0ca08ce2020-08-11 07:33:15180
181 /**
182 * @return {!ActionRuntimeExtensionDescriptor}
183 */
184 _actionDescriptor() {
185 return /** @type {!ActionRuntimeExtensionDescriptor} */ (this._extension.descriptor());
186 }
Tim van der Lippe0830b3d2019-10-03 13:20:07187}
188
189/** @enum {symbol} */
Paul Lewis9950e182019-12-16 16:06:07190export const Events = {
Tim van der Lippe0830b3d2019-10-03 13:20:07191 Enabled: Symbol('Enabled'),
192 Toggled: Symbol('Toggled')
193};