From f2cb22dcf730bac846478ca39aa1e7dcb37dc0cf Mon Sep 17 00:00:00 2001 From: Wyatt Allen Date: Mon, 27 Aug 2018 10:03:48 -0700 Subject: [PATCH] Add repository/branch picker element Add a new element that simplifies user selection of a repository and branch pair named gr-repo-branch-picker. Also introduces a wrapper around gr-autocomplete that provides additional visual styling named gr-labeled-autocomplete. Change-Id: I434690b249fd4632989bbdd73cad6f5229399ced --- .../gr-labeled-autocomplete.html | 72 +++++++++ .../gr-labeled-autocomplete.js | 73 +++++++++ .../gr-labeled-autocomplete_test.html | 58 ++++++++ .../gr-repo-branch-picker.html | 60 ++++++++ .../gr-repo-branch-picker.js | 109 ++++++++++++++ .../gr-repo-branch-picker_test.html | 140 ++++++++++++++++++ polygerrit-ui/app/test/index.html | 2 + 7 files changed, 514 insertions(+) create mode 100644 polygerrit-ui/app/elements/shared/gr-labeled-autocomplete/gr-labeled-autocomplete.html create mode 100644 polygerrit-ui/app/elements/shared/gr-labeled-autocomplete/gr-labeled-autocomplete.js create mode 100644 polygerrit-ui/app/elements/shared/gr-labeled-autocomplete/gr-labeled-autocomplete_test.html create mode 100644 polygerrit-ui/app/elements/shared/gr-repo-branch-picker/gr-repo-branch-picker.html create mode 100644 polygerrit-ui/app/elements/shared/gr-repo-branch-picker/gr-repo-branch-picker.js create mode 100644 polygerrit-ui/app/elements/shared/gr-repo-branch-picker/gr-repo-branch-picker_test.html diff --git a/polygerrit-ui/app/elements/shared/gr-labeled-autocomplete/gr-labeled-autocomplete.html b/polygerrit-ui/app/elements/shared/gr-labeled-autocomplete/gr-labeled-autocomplete.html new file mode 100644 index 0000000000..58253016bb --- /dev/null +++ b/polygerrit-ui/app/elements/shared/gr-labeled-autocomplete/gr-labeled-autocomplete.html @@ -0,0 +1,72 @@ + + + + + + + + + diff --git a/polygerrit-ui/app/elements/shared/gr-labeled-autocomplete/gr-labeled-autocomplete.js b/polygerrit-ui/app/elements/shared/gr-labeled-autocomplete/gr-labeled-autocomplete.js new file mode 100644 index 0000000000..cb3d54672c --- /dev/null +++ b/polygerrit-ui/app/elements/shared/gr-labeled-autocomplete/gr-labeled-autocomplete.js @@ -0,0 +1,73 @@ +/** + * @license + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +(function() { + 'use strict'; + + Polymer({ + is: 'gr-labeled-autocomplete', + + /** + * Fired when a value is chosen. + * + * @event commit + */ + + properties: { + + /** + * Used just like the query property of gr-autocomplete. + * + * @type {function(string): Promise} + */ + query: { + type: Function, + value() { + return function() { + return Promise.resolve([]); + }; + }, + }, + + text: { + type: String, + value: '', + notify: true, + }, + label: String, + placeholder: String, + disabled: Boolean, + + _autocompleteThreshold: { + type: Number, + value: 0, + readOnly: true, + }, + }, + + _handleTriggerTap() { + this.$.autocomplete.focus(); + }, + + setText(text) { + this.$.autocomplete.setText(text); + }, + + clear() { + this.setText(''); + }, + }); +})(); diff --git a/polygerrit-ui/app/elements/shared/gr-labeled-autocomplete/gr-labeled-autocomplete_test.html b/polygerrit-ui/app/elements/shared/gr-labeled-autocomplete/gr-labeled-autocomplete_test.html new file mode 100644 index 0000000000..f7632d3d36 --- /dev/null +++ b/polygerrit-ui/app/elements/shared/gr-labeled-autocomplete/gr-labeled-autocomplete_test.html @@ -0,0 +1,58 @@ + + + +gr-labeled-autocomplete + + + + + + + + + + + + + diff --git a/polygerrit-ui/app/elements/shared/gr-repo-branch-picker/gr-repo-branch-picker.html b/polygerrit-ui/app/elements/shared/gr-repo-branch-picker/gr-repo-branch-picker.html new file mode 100644 index 0000000000..d794dd648d --- /dev/null +++ b/polygerrit-ui/app/elements/shared/gr-repo-branch-picker/gr-repo-branch-picker.html @@ -0,0 +1,60 @@ + + + + + + + + + + + + + diff --git a/polygerrit-ui/app/elements/shared/gr-repo-branch-picker/gr-repo-branch-picker.js b/polygerrit-ui/app/elements/shared/gr-repo-branch-picker/gr-repo-branch-picker.js new file mode 100644 index 0000000000..e2298c3d37 --- /dev/null +++ b/polygerrit-ui/app/elements/shared/gr-repo-branch-picker/gr-repo-branch-picker.js @@ -0,0 +1,109 @@ +/** + * @license + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +(function() { + 'use strict'; + + const SUGGESTIONS_LIMIT = 15; + const REF_PREFIX = 'refs/heads/'; + + Polymer({ + is: 'gr-repo-branch-picker', + + properties: { + repo: { + type: String, + notify: true, + observer: '_repoChanged', + }, + branch: { + type: String, + notify: true, + }, + _branchDisabled: Boolean, + _query: { + type: Function, + value() { + return this._getRepoBranchesSuggestions.bind(this); + }, + }, + _repoQuery: { + type: Function, + value() { + return this._getRepoSuggestions.bind(this); + }, + }, + }, + + behaviors: [ + Gerrit.URLEncodingBehavior, + ], + + attached() { + if (this.repo) { + this.$.repoInput.setText(this.repo); + } + }, + + ready() { + this._branchDisabled = !this.repo; + }, + + _getRepoBranchesSuggestions(input) { + if (!this.repo) { return Promise.resolve([]); } + if (input.startsWith(REF_PREFIX)) { + input = input.substring(REF_PREFIX.length); + } + return this.$.restAPI.getRepoBranches(input, this.repo, SUGGESTIONS_LIMIT) + .then(this._branchResponseToSuggestions.bind(this)); + }, + + _getRepoSuggestions(input) { + return this.$.restAPI.getRepos(input, SUGGESTIONS_LIMIT) + .then(this._repoResponseToSuggestions.bind(this)); + }, + + _repoResponseToSuggestions(res) { + return res.map(repo => ({ + name: repo.name, + value: this.singleDecodeURL(repo.id), + })); + }, + + _branchResponseToSuggestions(res) { + return Object.keys(res).map(key => { + let branch = res[key].ref; + if (branch.startsWith(REF_PREFIX)) { + branch = branch.substring(REF_PREFIX.length); + } + return {name: branch, value: branch}; + }); + }, + + _repoCommitted(e) { + this.repo = e.detail.value; + }, + + _branchCommitted(e) { + this.branch = e.detail.value; + }, + + _repoChanged() { + this.$.branchInput.clear(); + this._branchDisabled = !this.repo; + }, + }); +})(); diff --git a/polygerrit-ui/app/elements/shared/gr-repo-branch-picker/gr-repo-branch-picker_test.html b/polygerrit-ui/app/elements/shared/gr-repo-branch-picker/gr-repo-branch-picker_test.html new file mode 100644 index 0000000000..989e838181 --- /dev/null +++ b/polygerrit-ui/app/elements/shared/gr-repo-branch-picker/gr-repo-branch-picker_test.html @@ -0,0 +1,140 @@ + + + +gr-repo-branch-picker + + + + + + + + + + + + + diff --git a/polygerrit-ui/app/test/index.html b/polygerrit-ui/app/test/index.html index 3ec55dd951..a489627588 100644 --- a/polygerrit-ui/app/test/index.html +++ b/polygerrit-ui/app/test/index.html @@ -167,12 +167,14 @@ limitations under the License. 'shared/gr-js-api-interface/gr-plugin-endpoints_test.html', 'shared/gr-js-api-interface/gr-plugin-rest-api_test.html', 'shared/gr-fixed-panel/gr-fixed-panel_test.html', + 'shared/gr-labeled-autocomplete/gr-labeled-autocomplete_test.html', 'shared/gr-lib-loader/gr-lib-loader_test.html', 'shared/gr-limited-text/gr-limited-text_test.html', 'shared/gr-linked-chip/gr-linked-chip_test.html', 'shared/gr-linked-text/gr-linked-text_test.html', 'shared/gr-list-view/gr-list-view_test.html', 'shared/gr-page-nav/gr-page-nav_test.html', + 'shared/gr-repo-branch-picker/gr-repo-branch-picker_test.html', 'shared/gr-rest-api-interface/gr-auth_test.html', 'shared/gr-rest-api-interface/gr-rest-api-interface_test.html', 'shared/gr-rest-api-interface/gr-reviewer-updates-parser_test.html',