Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | // Copyright 2017 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 | Snippets.SnippetsQuickOpen = class extends QuickOpen.FilteredListWidget.Provider { |
| 6 | constructor() { |
| 7 | super(); |
| 8 | /** @type {!Array<!Workspace.UISourceCode>} */ |
| 9 | this._snippets = []; |
| 10 | } |
| 11 | /** |
| 12 | * @override |
| 13 | * @param {?number} itemIndex |
| 14 | * @param {string} promptValue |
| 15 | */ |
| 16 | selectItem(itemIndex, promptValue) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 17 | if (itemIndex === null) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 18 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 19 | } |
Alexey Kozyatinskiy | 3a2eb28 | 2018-08-21 16:22:02 | [diff] [blame] | 20 | Snippets.evaluateScriptSnippet(this._snippets[itemIndex]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | /** |
| 24 | * @override |
| 25 | * @param {string} query |
| 26 | * @return {string} |
| 27 | */ |
| 28 | notFoundText(query) { |
| 29 | return Common.UIString('No snippets found.'); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @override |
| 34 | */ |
| 35 | attach() { |
Alexey Kozyatinskiy | 3a2eb28 | 2018-08-21 16:22:02 | [diff] [blame] | 36 | this._snippets = Snippets.project.uiSourceCodes(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @override |
| 41 | */ |
| 42 | detach() { |
| 43 | this._snippets = []; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | /** |
| 48 | * @override |
| 49 | * @return {number} |
| 50 | */ |
| 51 | itemCount() { |
| 52 | return this._snippets.length; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @override |
| 57 | * @param {number} itemIndex |
| 58 | * @return {string} |
| 59 | */ |
| 60 | itemKeyAt(itemIndex) { |
| 61 | return this._snippets[itemIndex].name(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @override |
| 66 | * @param {number} itemIndex |
| 67 | * @param {string} query |
| 68 | * @param {!Element} titleElement |
| 69 | * @param {!Element} subtitleElement |
| 70 | */ |
| 71 | renderItem(itemIndex, query, titleElement, subtitleElement) { |
Alexey Kozyatinskiy | 3a2eb28 | 2018-08-21 16:22:02 | [diff] [blame] | 72 | titleElement.textContent = unescape(this._snippets[itemIndex].name()); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 73 | titleElement.classList.add('monospace'); |
| 74 | QuickOpen.FilteredListWidget.highlightRanges(titleElement, query, true); |
| 75 | } |
| 76 | }; |