ActionsProvider.sys.mjs (3642B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /** 6 * A provider that matches the urlbar input to built in actions. 7 */ 8 export class ActionsProvider { 9 /** 10 * Unique name for the provider. 11 * 12 * @abstract 13 * @returns {string} 14 */ 15 get name() { 16 return "ActionsProviderBase"; 17 } 18 19 /** 20 * Whether this provider should be invoked for the given context. 21 * If this method returns false, the providers manager won't start a query 22 * with this provider, to save on resources. 23 * 24 * @param {UrlbarQueryContext} _queryContext The query context object. 25 * @returns {boolean} Whether this provider should be invoked for the search. 26 * @abstract 27 */ 28 isActive(_queryContext) { 29 throw new Error("Not implemented."); 30 } 31 32 /** 33 * Query for actions based on the current users input. 34 * 35 * @param {UrlbarQueryContext} _queryContext The query context object. 36 * @returns {Promise<ActionsResult[]>} 37 * @abstract 38 */ 39 async queryActions(_queryContext) { 40 throw new Error("Not implemented."); 41 } 42 43 /** 44 * Pick an action. 45 * 46 * @param {UrlbarQueryContext} _queryContext The query context object. 47 * @param {UrlbarController} _controller The urlbar controller. 48 * @param {Element} _element The element that was selected. 49 * @abstract 50 */ 51 pickAction(_queryContext, _controller, _element) { 52 throw new Error("Not implemented."); 53 } 54 } 55 56 /** 57 * Class used to create an Actions Result. 58 */ 59 export class ActionsResult { 60 /** 61 * @type {string} 62 * The name of the `ActionsProvider` that provided this actions result. 63 */ 64 providerName; 65 66 #key; 67 #l10nId; 68 #l10nArgs; 69 #icon; 70 #dataset; 71 #onPick; 72 #onSelection; 73 #engine; 74 75 /** 76 * @param {object} options 77 * An option object. 78 * @param {string} options.key 79 * A string key used to distinguish between different actions. 80 * @param {string} options.l10nId 81 * The id of the l10n string displayed in the action button. 82 * @param {{[arg: string]: any}} [options.l10nArgs] 83 * Arguments passed to construct the above string 84 * @param {string} options.icon 85 * The icon displayed in the button. 86 * @param {{[key: string]: any}} [options.dataset] 87 * An object of properties we set on the action button that 88 * can be used to pass data when it is selected. 89 * @param {(context: UrlbarQueryContext, controller: UrlbarController) => void} options.onPick 90 * A callback function called when the result has been picked. 91 * @param {(result: UrlbarResult, resultElement: Element) => void} [options.onSelection] 92 * A callback function called when the result has been selected. 93 * @param {string} [options.engine] 94 * The name of an installed engine if the action prompts search mode. 95 */ 96 constructor({ 97 key, 98 l10nId, 99 l10nArgs, 100 icon, 101 dataset, 102 onPick, 103 onSelection, 104 engine, 105 }) { 106 this.#key = key; 107 this.#l10nId = l10nId; 108 this.#l10nArgs = l10nArgs; 109 this.#icon = icon; 110 this.#dataset = dataset; 111 this.#onPick = onPick; 112 this.#onSelection = onSelection; 113 this.#engine = engine; 114 } 115 116 get key() { 117 return this.#key; 118 } 119 120 get l10nId() { 121 return this.#l10nId; 122 } 123 124 get l10nArgs() { 125 return this.#l10nArgs; 126 } 127 128 get icon() { 129 return this.#icon; 130 } 131 132 get dataset() { 133 return this.#dataset; 134 } 135 136 get onPick() { 137 return this.#onPick; 138 } 139 140 get onSelection() { 141 return this.#onSelection; 142 } 143 144 get engine() { 145 return this.#engine; 146 } 147 }