blob: 10081047b0bb55fd786e714c5cb195b130f9aec4 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// Copyright 2014 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.
Paul Lewis9950e182019-12-16 16:06:074
Simon Zünd45870102020-08-11 09:23:415import * as Root from '../root/root.js'; // eslint-disable-line no-unused-vars
Tim van der Lippeee97fa32020-04-23 15:20:566
Paul Lewis9950e182019-12-16 16:06:077import {Action} from './Action.js';
8import {Context} from './Context.js'; // eslint-disable-line no-unused-vars
9
Tim van der Lippe177c0b22020-08-19 14:56:0210/** @type {!ActionRegistry} */
11let actionRegistryInstance;
12
Paul Lewis9950e182019-12-16 16:06:0713export class ActionRegistry {
Tim van der Lipped1a00aa2020-08-19 16:03:5614 /**
15 * @private
16 */
Blink Reformat4c46d092018-04-07 15:32:3717 constructor() {
Paul Lewis9950e182019-12-16 16:06:0718 /** @type {!Map.<string, !Action>} */
Blink Reformat4c46d092018-04-07 15:32:3719 this._actionsById = new Map();
20 this._registerActions();
21 }
22
Tim van der Lippe177c0b22020-08-19 14:56:0223 /**
24 * @param {{forceNew: ?boolean}} opts
25 */
26 static instance(opts = {forceNew: null}) {
27 const {forceNew} = opts;
28 if (!actionRegistryInstance || forceNew) {
29 actionRegistryInstance = new ActionRegistry();
30 }
31
32 return actionRegistryInstance;
33 }
34
Blink Reformat4c46d092018-04-07 15:32:3735 _registerActions() {
Tim van der Lippe7f2002c2020-09-28 14:54:0136 Root.Runtime.Runtime.instance().extensions('action').forEach(registerExtension, this);
Blink Reformat4c46d092018-04-07 15:32:3737
38 /**
Tim van der Lippe99e59b82019-09-30 20:00:5939 * @param {!Root.Runtime.Extension} extension
Paul Lewis9950e182019-12-16 16:06:0740 * @this {ActionRegistry}
Blink Reformat4c46d092018-04-07 15:32:3741 */
42 function registerExtension(extension) {
Simon Zünd45870102020-08-11 09:23:4143 const actionId = extension.descriptor().actionId;
44 if (!actionId) {
45 console.error(`No actionId provided for extension ${extension.descriptor().name}`);
46 return;
47 }
Blink Reformat4c46d092018-04-07 15:32:3748 console.assert(!this._actionsById.get(actionId));
49
Paul Lewis9950e182019-12-16 16:06:0750 const action = new Action(extension);
Tim van der Lippe1d6e57a2019-09-30 11:55:3451 if (!action.category() || action.title()) {
Blink Reformat4c46d092018-04-07 15:32:3752 this._actionsById.set(actionId, action);
Tim van der Lippe1d6e57a2019-09-30 11:55:3453 } else {
Blink Reformat4c46d092018-04-07 15:32:3754 console.error(`Category actions require a title for command menu: ${actionId}`);
Tim van der Lippe1d6e57a2019-09-30 11:55:3455 }
Jack Lynch3b8d1782020-04-13 17:49:3156 if (!extension.canInstantiate()) {
57 action.setEnabled(false);
58 }
Blink Reformat4c46d092018-04-07 15:32:3759 }
60 }
61
62 /**
Paul Lewis9950e182019-12-16 16:06:0763 * @return {!Array.<!Action>}
Blink Reformat4c46d092018-04-07 15:32:3764 */
65 availableActions() {
Tim van der Lipped1a00aa2020-08-19 16:03:5666 return this.applicableActions([...this._actionsById.keys()], Context.instance());
Blink Reformat4c46d092018-04-07 15:32:3767 }
68
69 /**
Jack Lynch575e9fb2020-03-26 22:20:5170 * @return {!Array.<!Action>}
71 */
72 actions() {
73 return [...this._actionsById.values()];
74 }
75
76 /**
Blink Reformat4c46d092018-04-07 15:32:3777 * @param {!Array.<string>} actionIds
Paul Lewis9950e182019-12-16 16:06:0778 * @param {!Context} context
79 * @return {!Array.<!Action>}
Blink Reformat4c46d092018-04-07 15:32:3780 */
81 applicableActions(actionIds, context) {
82 const extensions = [];
Simon Zünd45870102020-08-11 09:23:4183 for (const actionId of actionIds) {
Blink Reformat4c46d092018-04-07 15:32:3784 const action = this._actionsById.get(actionId);
Jack Lynch3b8d1782020-04-13 17:49:3185 if (action && action.enabled()) {
Tim van der Lippe0830b3d2019-10-03 13:20:0786 extensions.push(action.extension());
Tim van der Lippe1d6e57a2019-09-30 11:55:3487 }
Simon Zünd45870102020-08-11 09:23:4188 }
Simon Zünda0d40622020-02-12 13:16:4289 return [...context.applicableExtensions(extensions)].map(extensionToAction.bind(this));
Blink Reformat4c46d092018-04-07 15:32:3790
91 /**
Tim van der Lippe99e59b82019-09-30 20:00:5992 * @param {!Root.Runtime.Extension} extension
Paul Lewis9950e182019-12-16 16:06:0793 * @return {!Action}
94 * @this {ActionRegistry}
Blink Reformat4c46d092018-04-07 15:32:3795 */
96 function extensionToAction(extension) {
Simon Zünd45870102020-08-11 09:23:4197 const actionId = /** @type {string} */ (extension.descriptor().actionId);
98 return /** @type {!Action} */ (this.action(actionId));
Blink Reformat4c46d092018-04-07 15:32:3799 }
100 }
101
102 /**
103 * @param {string} actionId
Paul Lewis9950e182019-12-16 16:06:07104 * @return {?Action}
Blink Reformat4c46d092018-04-07 15:32:37105 */
106 action(actionId) {
107 return this._actionsById.get(actionId) || null;
108 }
Tim van der Lippe0830b3d2019-10-03 13:20:07109}