Becky Siegel | 8b220de | 2018-04-04 17:11:06 -0700 | [diff] [blame^] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright (C) 2016 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | (function() { |
| 18 | 'use strict'; |
| 19 | |
| 20 | const MAX_AUTOCOMPLETE_RESULTS = 10; |
| 21 | const SELF_EXPRESSION = 'self'; |
| 22 | const ME_EXPRESSION = 'me'; |
| 23 | |
| 24 | Polymer({ |
| 25 | is: 'gr-smart-search', |
| 26 | |
| 27 | properties: { |
| 28 | searchQuery: String, |
| 29 | _config: Object, |
| 30 | _projectSuggestions: { |
| 31 | type: Function, |
| 32 | value() { |
| 33 | return this._fetchProjects.bind(this); |
| 34 | }, |
| 35 | }, |
| 36 | _groupSuggestions: { |
| 37 | type: Function, |
| 38 | value() { |
| 39 | return this._fetchGroups.bind(this); |
| 40 | }, |
| 41 | }, |
| 42 | _accountSuggestions: { |
| 43 | type: Function, |
| 44 | value() { |
| 45 | return this._fetchAccounts.bind(this); |
| 46 | }, |
| 47 | }, |
| 48 | }, |
| 49 | |
| 50 | behaviors: [ |
| 51 | Gerrit.AnonymousNameBehavior, |
| 52 | ], |
| 53 | |
| 54 | attached() { |
| 55 | this.$.restAPI.getConfig().then(cfg => { |
| 56 | this._config = cfg; |
| 57 | }); |
| 58 | }, |
| 59 | |
| 60 | _handleSearch(e) { |
| 61 | const input = e.detail.inputVal; |
| 62 | if (input) { |
| 63 | Gerrit.Nav.navigateToSearchQuery(input); |
| 64 | } |
| 65 | }, |
| 66 | |
| 67 | _accountOrAnon(name) { |
| 68 | return this.getUserName(this._serverConfig, name, false); |
| 69 | }, |
| 70 | |
| 71 | /** |
| 72 | * Fetch from the API the predicted projects. |
| 73 | * @param {string} predicate - The first part of the search term, e.g. |
| 74 | * 'project' |
| 75 | * @param {string} expression - The second part of the search term, e.g. |
| 76 | * 'gerr' |
| 77 | * @return {!Promise} This returns a promise that resolves to an array of |
| 78 | * strings. |
| 79 | */ |
| 80 | _fetchProjects(predicate, expression) { |
| 81 | return this.$.restAPI.getSuggestedProjects( |
| 82 | expression, |
| 83 | MAX_AUTOCOMPLETE_RESULTS) |
| 84 | .then(projects => { |
| 85 | if (!projects) { return []; } |
| 86 | const keys = Object.keys(projects); |
| 87 | return keys.map(key => predicate + ':' + key); |
| 88 | }); |
| 89 | }, |
| 90 | |
| 91 | /** |
| 92 | * Fetch from the API the predicted groups. |
| 93 | * @param {string} predicate - The first part of the search term, e.g. |
| 94 | * 'ownerin' |
| 95 | * @param {string} expression - The second part of the search term, e.g. |
| 96 | * 'polyger' |
| 97 | * @return {!Promise} This returns a promise that resolves to an array of |
| 98 | * strings. |
| 99 | */ |
| 100 | _fetchGroups(predicate, expression) { |
| 101 | if (expression.length === 0) { return Promise.resolve([]); } |
| 102 | return this.$.restAPI.getSuggestedGroups( |
| 103 | expression, |
| 104 | MAX_AUTOCOMPLETE_RESULTS) |
| 105 | .then(groups => { |
| 106 | if (!groups) { return []; } |
| 107 | const keys = Object.keys(groups); |
| 108 | return keys.map(key => predicate + ':' + key); |
| 109 | }); |
| 110 | }, |
| 111 | |
| 112 | /** |
| 113 | * Fetch from the API the predicted accounts. |
| 114 | * @param {string} predicate - The first part of the search term, e.g. |
| 115 | * 'owner' |
| 116 | * @param {string} expression - The second part of the search term, e.g. |
| 117 | * 'kasp' |
| 118 | * @return {!Promise} This returns a promise that resolves to an array of |
| 119 | * strings. |
| 120 | */ |
| 121 | _fetchAccounts(predicate, expression) { |
| 122 | if (expression.length === 0) { return Promise.resolve([]); } |
| 123 | return this.$.restAPI.getSuggestedAccounts( |
| 124 | expression, |
| 125 | MAX_AUTOCOMPLETE_RESULTS) |
| 126 | .then(accounts => { |
| 127 | if (!accounts) { return []; } |
| 128 | return accounts.map(acct => acct.email ? |
| 129 | `${predicate}:${acct.email}` : |
| 130 | `${predicate}:"${this._accountOrAnon(acct)}"`); |
| 131 | }).then(accounts => { |
| 132 | // When the expression supplied is a beginning substring of 'self', |
| 133 | // add it as an autocomplete option. |
| 134 | if (SELF_EXPRESSION.startsWith(expression)) { |
| 135 | return accounts.concat([predicate + ':' + SELF_EXPRESSION]); |
| 136 | } else if (ME_EXPRESSION.startsWith(expression)) { |
| 137 | return accounts.concat([predicate + ':' + ME_EXPRESSION]); |
| 138 | } else { |
| 139 | return accounts; |
| 140 | } |
| 141 | }); |
| 142 | }, |
| 143 | }); |
| 144 | })(); |