Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 4 | |
Simon Zünd | 4587010 | 2020-08-11 09:23:41 | [diff] [blame] | 5 | import * as Root from '../root/root.js'; // eslint-disable-line no-unused-vars |
Tim van der Lippe | ee97fa3 | 2020-04-23 15:20:56 | [diff] [blame] | 6 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 7 | import {Action} from './Action.js'; |
| 8 | import {Context} from './Context.js'; // eslint-disable-line no-unused-vars |
| 9 | |
Tim van der Lippe | 177c0b2 | 2020-08-19 14:56:02 | [diff] [blame] | 10 | /** @type {!ActionRegistry} */ |
| 11 | let actionRegistryInstance; |
| 12 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 13 | export class ActionRegistry { |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 14 | /** |
| 15 | * @private |
| 16 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 17 | constructor() { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 18 | /** @type {!Map.<string, !Action>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 19 | this._actionsById = new Map(); |
| 20 | this._registerActions(); |
| 21 | } |
| 22 | |
Tim van der Lippe | 177c0b2 | 2020-08-19 14:56:02 | [diff] [blame] | 23 | /** |
| 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 Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 35 | _registerActions() { |
Tim van der Lippe | 7f2002c | 2020-09-28 14:54:01 | [diff] [blame] | 36 | Root.Runtime.Runtime.instance().extensions('action').forEach(registerExtension, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 37 | |
| 38 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 | [diff] [blame] | 39 | * @param {!Root.Runtime.Extension} extension |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 40 | * @this {ActionRegistry} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 41 | */ |
| 42 | function registerExtension(extension) { |
Simon Zünd | 4587010 | 2020-08-11 09:23:41 | [diff] [blame] | 43 | const actionId = extension.descriptor().actionId; |
| 44 | if (!actionId) { |
| 45 | console.error(`No actionId provided for extension ${extension.descriptor().name}`); |
| 46 | return; |
| 47 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 48 | console.assert(!this._actionsById.get(actionId)); |
| 49 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 50 | const action = new Action(extension); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 51 | if (!action.category() || action.title()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 52 | this._actionsById.set(actionId, action); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 53 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 54 | console.error(`Category actions require a title for command menu: ${actionId}`); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 55 | } |
Jack Lynch | 3b8d178 | 2020-04-13 17:49:31 | [diff] [blame] | 56 | if (!extension.canInstantiate()) { |
| 57 | action.setEnabled(false); |
| 58 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 63 | * @return {!Array.<!Action>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 64 | */ |
| 65 | availableActions() { |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 66 | return this.applicableActions([...this._actionsById.keys()], Context.instance()); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
Jack Lynch | 575e9fb | 2020-03-26 22:20:51 | [diff] [blame] | 70 | * @return {!Array.<!Action>} |
| 71 | */ |
| 72 | actions() { |
| 73 | return [...this._actionsById.values()]; |
| 74 | } |
| 75 | |
| 76 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 77 | * @param {!Array.<string>} actionIds |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 78 | * @param {!Context} context |
| 79 | * @return {!Array.<!Action>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 80 | */ |
| 81 | applicableActions(actionIds, context) { |
| 82 | const extensions = []; |
Simon Zünd | 4587010 | 2020-08-11 09:23:41 | [diff] [blame] | 83 | for (const actionId of actionIds) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 84 | const action = this._actionsById.get(actionId); |
Jack Lynch | 3b8d178 | 2020-04-13 17:49:31 | [diff] [blame] | 85 | if (action && action.enabled()) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 86 | extensions.push(action.extension()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 87 | } |
Simon Zünd | 4587010 | 2020-08-11 09:23:41 | [diff] [blame] | 88 | } |
Simon Zünd | a0d4062 | 2020-02-12 13:16:42 | [diff] [blame] | 89 | return [...context.applicableExtensions(extensions)].map(extensionToAction.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 90 | |
| 91 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 | [diff] [blame] | 92 | * @param {!Root.Runtime.Extension} extension |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 93 | * @return {!Action} |
| 94 | * @this {ActionRegistry} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 95 | */ |
| 96 | function extensionToAction(extension) { |
Simon Zünd | 4587010 | 2020-08-11 09:23:41 | [diff] [blame] | 97 | const actionId = /** @type {string} */ (extension.descriptor().actionId); |
| 98 | return /** @type {!Action} */ (this.action(actionId)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @param {string} actionId |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 104 | * @return {?Action} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 105 | */ |
| 106 | action(actionId) { |
| 107 | return this._actionsById.get(actionId) || null; |
| 108 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 109 | } |