blob: 686553f47984033fcbb354d6eda608937fc53570 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// Copyright 2016 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
Tim van der Lippe63416c12020-02-04 12:04:455import * as UI from '../ui/ui.js'; // eslint-disable-line no-unused-vars
6
Paul Lewisc9eb5182020-01-15 14:41:557import {FilteredListWidget, Provider} from './FilteredListWidget.js';
8
9export const history = [];
Paul Lewis8c546e22019-11-25 15:46:2810
11/**
12 * @unrestricted
13 */
14export class QuickOpenImpl {
Blink Reformat4c46d092018-04-07 15:32:3715 constructor() {
16 this._prefix = null;
17 this._query = '';
Paul Lewisc9eb5182020-01-15 14:41:5518 /** @type {!Map<string, function():!Promise<!Provider>>} */
Blink Reformat4c46d092018-04-07 15:32:3719 this._providers = new Map();
20 /** @type {!Array<string>} */
21 this._prefixes = [];
22 this._filteredListWidget = null;
Paul Lewisc9eb5182020-01-15 14:41:5523 self.runtime.extensions(Provider).forEach(this._addProvider.bind(this));
Blink Reformat4c46d092018-04-07 15:32:3724 this._prefixes.sort((a, b) => b.length - a.length);
25 }
26
27 /**
28 * @param {string} query
29 */
30 static show(query) {
31 const quickOpen = new this();
Paul Lewisc9eb5182020-01-15 14:41:5532 const filteredListWidget = new FilteredListWidget(null, history, quickOpen._queryChanged.bind(quickOpen));
Blink Reformat4c46d092018-04-07 15:32:3733 quickOpen._filteredListWidget = filteredListWidget;
Anubha Mathurc080e242019-09-20 19:11:5234 filteredListWidget.setPlaceholder(
35 ls`Type '?' to see available commands`, ls`Type question mark to see available commands`);
Blink Reformat4c46d092018-04-07 15:32:3736 filteredListWidget.showAsDialog();
37 filteredListWidget.setQuery(query);
38 }
39
40 /**
Tim van der Lippe99e59b82019-09-30 20:00:5941 * @param {!Root.Runtime.Extension} extension
Blink Reformat4c46d092018-04-07 15:32:3742 */
43 _addProvider(extension) {
44 const prefix = extension.descriptor()['prefix'];
45 this._prefixes.push(prefix);
46 this._providers.set(
Paul Lewisc9eb5182020-01-15 14:41:5547 prefix, /** @type {function():!Promise<!Provider>} */
Blink Reformat4c46d092018-04-07 15:32:3748 (extension.instance.bind(extension)));
49 }
50
51 /**
52 * @param {string} query
53 */
54 _queryChanged(query) {
55 const prefix = this._prefixes.find(prefix => query.startsWith(prefix));
Tim van der Lippe1d6e57a2019-09-30 11:55:3456 if (typeof prefix !== 'string' || this._prefix === prefix) {
Blink Reformat4c46d092018-04-07 15:32:3757 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3458 }
Blink Reformat4c46d092018-04-07 15:32:3759
60 this._prefix = prefix;
61 this._filteredListWidget.setPrefix(prefix);
62 this._filteredListWidget.setProvider(null);
63 this._providers.get(prefix)().then(provider => {
Tim van der Lippe1d6e57a2019-09-30 11:55:3464 if (this._prefix !== prefix) {
Blink Reformat4c46d092018-04-07 15:32:3765 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3466 }
Blink Reformat4c46d092018-04-07 15:32:3767 this._filteredListWidget.setProvider(provider);
68 this._providerLoadedForTest(provider);
69 });
70 }
71
72 /**
Paul Lewisc9eb5182020-01-15 14:41:5573 * @param {!Provider} provider
Blink Reformat4c46d092018-04-07 15:32:3774 */
75 _providerLoadedForTest(provider) {
76 }
Paul Lewis8c546e22019-11-25 15:46:2877}
Blink Reformat4c46d092018-04-07 15:32:3778
79/**
Tim van der Lippe63416c12020-02-04 12:04:4580 * @implements {UI.ActionDelegate.ActionDelegate}
Blink Reformat4c46d092018-04-07 15:32:3781 */
Paul Lewis8c546e22019-11-25 15:46:2882export class ShowActionDelegate {
Blink Reformat4c46d092018-04-07 15:32:3783 /**
84 * @override
Tim van der Lippe63416c12020-02-04 12:04:4585 * @param {!UI.Context.Context} context
Blink Reformat4c46d092018-04-07 15:32:3786 * @param {string} actionId
87 * @return {boolean}
88 */
89 handleAction(context, actionId) {
90 switch (actionId) {
91 case 'quickOpen.show':
Paul Lewis8c546e22019-11-25 15:46:2892 QuickOpenImpl.show('');
Blink Reformat4c46d092018-04-07 15:32:3793 return true;
94 }
95 return false;
96 }
Paul Lewis8c546e22019-11-25 15:46:2897}