Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(898)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/quick_open/QuickOpen.js

Issue 2679483002: DevTools: Create extensible QuickOpen control (Closed)
Patch Set: rename Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // 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
5 QuickOpen.QuickOpen = class extends QuickOpen.FilteredListWidget.Delegate {
6 constructor() {
7 super();
8 /** @type {!Array<{
9 * prefix: string,
10 * instance: function():!Promise<!QuickOpen.QuickOpenProvider>
11 * }>} */
12 this._descriptors = [];
13 self.runtime.extensions(QuickOpen.QuickOpenProvider).forEach(this._addProvid er.bind(this));
14 this._provider = this._loadingProvider = new QuickOpen.QuickOpen.Loading();
15 this._prefix = null;
16 this._query = '';
17 this._disposed = false;
18 }
19
20 /**
21 * @param {string} query
22 */
23 static show(query) {
24 var filteredItemSelectionDialog = new QuickOpen.FilteredListWidget(new this( ), this._history);
25 filteredItemSelectionDialog.showAsDialog();
26 filteredItemSelectionDialog.setQuery(query);
27 }
28
29 /**
30 * @param {!Runtime.Extension} extension
31 */
32 _addProvider(extension) {
33 var descriptor = {
34 prefix: extension.descriptor().prefix,
35 provider: null,
36 instance: extension.instance.bind(extension)
37 };
38 var index = this._descriptors.lowerBound(descriptor, (a, b) => b.prefix.leng th - a.prefix.length);
39 this._descriptors.splice(index, 0, descriptor);
40 }
41
42 _updateProvider() {
43 var descriptor = this._descriptors.find(descriptor => this._query.startsWith (descriptor.prefix));
44 if (!descriptor || this._prefix === descriptor.prefix)
45 return;
46
47 this._prefix = descriptor.prefix;
48 if (descriptor.provider) {
49 this._setProvider(descriptor.provider);
50 return;
51 }
52 descriptor.instance().then(instance => {
53 if (this._disposed)
54 instance.dispose();
55 descriptor.provider = instance;
56 if (this._prefix !== descriptor.prefix)
57 return;
58 this._setProvider(instance);
59 this._providerLoadedForTest(instance);
60 });
61 this._setProvider(this._loadingProvider);
62 }
63
64 _setProvider(provider) {
65 if (provider === this._provider)
66 return;
67 this._provider = provider;
68 this._provider.queryChanged(this._query);
69 this.refresh();
70 }
71
72 /**
73 * @param {string} query
74 * @return {string}
75 */
76 _cleanQuery(query) {
77 return query.substring((this._prefix || '').length);
78 }
79
80 /**
81 * @override
82 * @param {string} query
83 */
84 queryChanged(query) {
85 this._query = query;
86 this._updateProvider();
87 this._provider.queryChanged(this._cleanQuery(query));
88 }
89
90 /**
91 * @param {!QuickOpen.QuickOpenProvider} provider
92 */
93 _providerLoadedForTest(provider) {
94 }
95
96 /**
97 * @override
98 * @param {string} query
99 * @return {string}
100 */
101 notFoundText(query) {
102 return this._provider.notFoundText(this._cleanQuery(query));
103 }
104
105 /**
106 * @override
107 * @return {number}
108 */
109 itemCount() {
110 return this._provider.itemCount();
111 }
112
113 /**
114 * @override
115 * @param {number} itemIndex
116 * @return {string}
117 */
118 itemKeyAt(itemIndex) {
119 return this._provider.itemKeyAt(itemIndex);
120 }
121
122 /**
123 * @override
124 * @param {number} itemIndex
125 * @param {string} query
126 * @return {number}
127 */
128 itemScoreAt(itemIndex, query) {
129 return this._provider.itemScoreAt(itemIndex, query);
130 }
131
132 /**
133 * @override
134 * @param {number} itemIndex
135 * @param {string} query
136 * @param {!Element} titleElement
137 * @param {!Element} subtitleElement
138 */
139 renderItem(itemIndex, query, titleElement, subtitleElement) {
140 this._provider.renderItem(itemIndex, this._cleanQuery(query), titleElement, subtitleElement);
141 }
142
143 /**
144 * @override
145 * @return {boolean}
146 */
147 renderAsTwoRows() {
148 return this._provider.renderAsTwoRows();
149 }
150
151 /**
152 * @override
153 * @param {null|number} itemIndex
154 * @param {string} promptValue
155 */
156 selectItem(itemIndex, promptValue) {
157 this._provider.selectItem(itemIndex, this._cleanQuery(promptValue));
158 }
159
160 /**
161 * @override
162 * @param {string} query
163 * @return {string}
164 */
165 rewriteQuery(query) {
166 return this._provider.rewriteQuery(this._cleanQuery(query));
167 }
168
169 /**
170 * @override
171 */
172 dispose() {
173 this._disposed = true;
174 for (var descriptor of this._descriptors) {
175 if (descriptor.provider)
176 descriptor.provider.dispose();
177 }
178 super.dispose();
179 }
180 };
181
182 QuickOpen.QuickOpen._history = [];
183
184 QuickOpen.QuickOpenProvider = class extends QuickOpen.FilteredListWidget.Delegat e {};
pfeldman 2017/03/02 22:42:27 That's still unpretty.
185
186 /**
187 * @implements {UI.ActionDelegate}
188 */
189 QuickOpen.QuickOpen.ShowActionDelegate = class {
190 /**
191 * @override
192 * @param {!UI.Context} context
193 * @param {string} actionId
194 * @return {boolean}
195 */
196 handleAction(context, actionId) {
197 switch (actionId) {
198 case 'quickOpen.show':
199 QuickOpen.QuickOpen.show('');
200 return true;
201 }
202 return false;
203 }
204 };
205
206 QuickOpen.QuickOpen.Loading = class extends QuickOpen.QuickOpenProvider {
207 /**
208 * @override
209 */
210 notFoundText() {
211 return Common.UIString('Loading...');
212 }
213 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698