Chromium Code Reviews| Index: third_party/WebKit/Source/devtools/front_end/quick_open/QuickOpen.js |
| diff --git a/third_party/WebKit/Source/devtools/front_end/quick_open/QuickOpen.js b/third_party/WebKit/Source/devtools/front_end/quick_open/QuickOpen.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f2ca01b5ba33991115e0c68f4d483b523b249add |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/devtools/front_end/quick_open/QuickOpen.js |
| @@ -0,0 +1,213 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +QuickOpen.QuickOpen = class extends QuickOpen.FilteredListWidget.Delegate { |
| + constructor() { |
| + super(); |
| + /** @type {!Array<{ |
| + * prefix: string, |
| + * instance: function():!Promise<!QuickOpen.QuickOpenProvider> |
| + * }>} */ |
| + this._descriptors = []; |
| + self.runtime.extensions(QuickOpen.QuickOpenProvider).forEach(this._addProvider.bind(this)); |
| + this._provider = this._loadingProvider = new QuickOpen.QuickOpen.Loading(); |
| + this._prefix = null; |
| + this._query = ''; |
| + this._disposed = false; |
| + } |
| + |
| + /** |
| + * @param {string} query |
| + */ |
| + static show(query) { |
| + var filteredItemSelectionDialog = new QuickOpen.FilteredListWidget(new this(), this._history); |
| + filteredItemSelectionDialog.showAsDialog(); |
| + filteredItemSelectionDialog.setQuery(query); |
| + } |
| + |
| + /** |
| + * @param {!Runtime.Extension} extension |
| + */ |
| + _addProvider(extension) { |
| + var descriptor = { |
| + prefix: extension.descriptor().prefix, |
| + provider: null, |
| + instance: extension.instance.bind(extension) |
| + }; |
| + var index = this._descriptors.lowerBound(descriptor, (a, b) => b.prefix.length - a.prefix.length); |
| + this._descriptors.splice(index, 0, descriptor); |
| + } |
| + |
| + _updateProvider() { |
| + var descriptor = this._descriptors.find(descriptor => this._query.startsWith(descriptor.prefix)); |
| + if (!descriptor || this._prefix === descriptor.prefix) |
| + return; |
| + |
| + this._prefix = descriptor.prefix; |
| + if (descriptor.provider) { |
| + this._setProvider(descriptor.provider); |
| + return; |
| + } |
| + descriptor.instance().then(instance => { |
| + if (this._disposed) |
| + instance.dispose(); |
| + descriptor.provider = instance; |
| + if (this._prefix !== descriptor.prefix) |
| + return; |
| + this._setProvider(instance); |
| + this._providerLoadedForTest(instance); |
| + }); |
| + this._setProvider(this._loadingProvider); |
| + } |
| + |
| + _setProvider(provider) { |
| + if (provider === this._provider) |
| + return; |
| + this._provider = provider; |
| + this._provider.queryChanged(this._query); |
| + this.refresh(); |
| + } |
| + |
| + /** |
| + * @param {string} query |
| + * @return {string} |
| + */ |
| + _cleanQuery(query) { |
| + return query.substring((this._prefix || '').length); |
| + } |
| + |
| + /** |
| + * @override |
| + * @param {string} query |
| + */ |
| + queryChanged(query) { |
| + this._query = query; |
| + this._updateProvider(); |
| + this._provider.queryChanged(this._cleanQuery(query)); |
| + } |
| + |
| + /** |
| + * @param {!QuickOpen.QuickOpenProvider} provider |
| + */ |
| + _providerLoadedForTest(provider) { |
| + } |
| + |
| + /** |
| + * @override |
| + * @param {string} query |
| + * @return {string} |
| + */ |
| + notFoundText(query) { |
| + return this._provider.notFoundText(this._cleanQuery(query)); |
| + } |
| + |
| + /** |
| + * @override |
| + * @return {number} |
| + */ |
| + itemCount() { |
| + return this._provider.itemCount(); |
| + } |
| + |
| + /** |
| + * @override |
| + * @param {number} itemIndex |
| + * @return {string} |
| + */ |
| + itemKeyAt(itemIndex) { |
| + return this._provider.itemKeyAt(itemIndex); |
| + } |
| + |
| + /** |
| + * @override |
| + * @param {number} itemIndex |
| + * @param {string} query |
| + * @return {number} |
| + */ |
| + itemScoreAt(itemIndex, query) { |
| + return this._provider.itemScoreAt(itemIndex, query); |
| + } |
| + |
| + /** |
| + * @override |
| + * @param {number} itemIndex |
| + * @param {string} query |
| + * @param {!Element} titleElement |
| + * @param {!Element} subtitleElement |
| + */ |
| + renderItem(itemIndex, query, titleElement, subtitleElement) { |
| + this._provider.renderItem(itemIndex, this._cleanQuery(query), titleElement, subtitleElement); |
| + } |
| + |
| + /** |
| + * @override |
| + * @return {boolean} |
| + */ |
| + renderAsTwoRows() { |
| + return this._provider.renderAsTwoRows(); |
| + } |
| + |
| + /** |
| + * @override |
| + * @param {null|number} itemIndex |
| + * @param {string} promptValue |
| + */ |
| + selectItem(itemIndex, promptValue) { |
| + this._provider.selectItem(itemIndex, this._cleanQuery(promptValue)); |
| + } |
| + |
| + /** |
| + * @override |
| + * @param {string} query |
| + * @return {string} |
| + */ |
| + rewriteQuery(query) { |
| + return this._provider.rewriteQuery(this._cleanQuery(query)); |
| + } |
| + |
| + /** |
| + * @override |
| + */ |
| + dispose() { |
| + this._disposed = true; |
| + for (var descriptor of this._descriptors) { |
| + if (descriptor.provider) |
| + descriptor.provider.dispose(); |
| + } |
| + super.dispose(); |
| + } |
| +}; |
| + |
| +QuickOpen.QuickOpen._history = []; |
| + |
| +QuickOpen.QuickOpenProvider = class extends QuickOpen.FilteredListWidget.Delegate {}; |
|
pfeldman
2017/03/02 22:42:27
That's still unpretty.
|
| + |
| +/** |
| + * @implements {UI.ActionDelegate} |
| + */ |
| +QuickOpen.QuickOpen.ShowActionDelegate = class { |
| + /** |
| + * @override |
| + * @param {!UI.Context} context |
| + * @param {string} actionId |
| + * @return {boolean} |
| + */ |
| + handleAction(context, actionId) { |
| + switch (actionId) { |
| + case 'quickOpen.show': |
| + QuickOpen.QuickOpen.show(''); |
| + return true; |
| + } |
| + return false; |
| + } |
| +}; |
| + |
| +QuickOpen.QuickOpen.Loading = class extends QuickOpen.QuickOpenProvider { |
| + /** |
| + * @override |
| + */ |
| + notFoundText() { |
| + return Common.UIString('Loading...'); |
| + } |
| +}; |