Consolidate list-view code
Throughout the admin section, there are (at least) three distinct list
views that behave very similarly. This change helps share code between
them:
Create a list-view element that handles the filtering and navigation,
and styles <content></content>, which is the same among lists.
Change-Id: I1037c43df7be58aa00843d29cae50495e089826d
diff --git a/polygerrit-ui/app/elements/shared/gr-list-view/gr-list-view.html b/polygerrit-ui/app/elements/shared/gr-list-view/gr-list-view.html
new file mode 100644
index 0000000..78073dc
--- /dev/null
+++ b/polygerrit-ui/app/elements/shared/gr-list-view/gr-list-view.html
@@ -0,0 +1,107 @@
+<!--
+Copyright (C) 2017 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.
+-->
+
+<link rel="import" href="../../../behaviors/base-url-behavior/base-url-behavior.html">
+<link rel="import" href="../../../behaviors/gr-url-encoding-behavior.html">
+<link rel="import" href="../../../bower_components/polymer/polymer.html">
+
+<link rel="import" href="../../../styles/shared-styles.html">
+
+<dom-module id="gr-list-view">
+ <template>
+ <style include="shared-styles">
+ #filterContainer {
+ margin: 1em;
+ }
+ #filter {
+ font-size: 1em;
+ max-width: 25em;
+ }
+ a {
+ color: var(--default-text-color);
+ text-decoration: none;
+ }
+ a:hover {
+ text-decoration: underline;
+ }
+ nav {
+ padding: .5em 0;
+ text-align: center;
+ }
+ nav a {
+ display: inline-block;
+ }
+ nav a:first-of-type {
+ margin-right: .5em;
+ }
+ ::content {
+ display: flex;
+ flex-direction: column;
+ }
+ ::content tr.table {
+ border-bottom: 1px solid #eee;
+ }
+ ::content #list {
+ border-collapse: collapse;
+ width: 100%;
+ }
+ ::content td {
+ flex-shrink: 0;
+ padding: .3em .5em;
+ }
+ ::content th {
+ background-color: #ddd;
+ border-bottom: 1px solid #eee;
+ font-weight: bold;
+ padding: .3em .5em;
+ text-align: left;
+ }
+ ::content a {
+ color: var(--default-text-color);
+ text-decoration: none;
+ }
+ ::content a:hover {
+ text-decoration: underline;
+ }
+ ::content .description {
+ width: 70%;
+ }
+ ::content .loading {
+ color: #666;
+ padding: 1em var(--default-horizontal-margin);
+ }
+ </style>
+ <div id="filterContainer">
+ <label>Filter:</label>
+ <input is="iron-input"
+ type="text"
+ id="filter"
+ bind-value="{{_filter}}"
+ on-input="_onValueChange">
+ </div>
+ <content></content>
+ <nav>
+ <a id="prevArrow"
+ href$="[[_computeNavLink(offset, -1, itemsPerPage, filter)]]"
+ hidden$="[[_hidePrevArrow(offset)]]" hidden>← Prev</a>
+ <a id="nextArrow"
+ href$="[[_computeNavLink(offset, 1, itemsPerPage, filter)]]"
+ hidden$="[[_hideNextArrow(loading, items)]]" hidden>
+ Next →</a>
+ </nav>
+ </template>
+ <script src="gr-list-view.js"></script>
+</dom-module>
diff --git a/polygerrit-ui/app/elements/shared/gr-list-view/gr-list-view.js b/polygerrit-ui/app/elements/shared/gr-list-view/gr-list-view.js
new file mode 100644
index 0000000..b5da125
--- /dev/null
+++ b/polygerrit-ui/app/elements/shared/gr-list-view/gr-list-view.js
@@ -0,0 +1,77 @@
+// Copyright (C) 2017 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 REQUEST_DEBOUNCE_INTERVAL_MS = 200;
+
+ Polymer({
+ is: 'gr-list-view',
+
+ properties: {
+ items: Array,
+ itemsPerPage: Number,
+ filter: String,
+ offset: Number,
+ loading: Boolean,
+ path: String,
+ },
+
+ behaviors: [
+ Gerrit.BaseUrlBehavior,
+ Gerrit.URLEncodingBehavior,
+ ],
+
+ listeners: {
+ 'next-page': '_handleNextPage',
+ 'previous-page': '_handlePreviousPage',
+ },
+
+ _onValueChange(e) {
+ this.debounce('reload', () => {
+ if (e.target.value) {
+ return page.show(`${this.path}/q/filter:` +
+ this.encodeURL(e.target.value, false));
+ }
+ page.show(this.path);
+ }, REQUEST_DEBOUNCE_INTERVAL_MS);
+ },
+
+ _computeNavLink(offset, direction, projectsPerPage, filter) {
+ // Offset could be a string when passed from the router.
+ offset = +(offset || 0);
+ const newOffset = Math.max(0, offset + (projectsPerPage * direction));
+ let href = this.getBaseUrl() + this.path;
+ if (filter) {
+ href += '/q/filter:' + filter;
+ }
+ if (newOffset > 0) {
+ href += ',' + newOffset;
+ }
+ return href;
+ },
+
+ _hidePrevArrow(offset) {
+ return offset === 0;
+ },
+
+ _hideNextArrow(loading, projects) {
+ let lastPage = false;
+ if (projects.length < this.itemsPerPage + 1) {
+ lastPage = true;
+ }
+ return loading || lastPage || !projects || !projects.length;
+ },
+ });
+})();
diff --git a/polygerrit-ui/app/elements/shared/gr-list-view/gr-list-view_test.html b/polygerrit-ui/app/elements/shared/gr-list-view/gr-list-view_test.html
new file mode 100644
index 0000000..10ef4e1
--- /dev/null
+++ b/polygerrit-ui/app/elements/shared/gr-list-view/gr-list-view_test.html
@@ -0,0 +1,112 @@
+<!DOCTYPE html>
+<!--
+Copyright (C) 2017 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.
+-->
+
+<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
+<title>gr-list-view</title>
+<script src="../../../bower_components/page/page.js"></script>
+<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
+<script src="../../../bower_components/web-component-tester/browser.js"></script>
+
+<link rel="import" href="../../../bower_components/iron-test-helpers/iron-test-helpers.html">
+<link rel="import" href="gr-list-view.html">
+
+<script>void(0);</script>
+
+<test-fixture id="basic">
+ <template>
+ <gr-list-view></gr-list-view>
+ </template>
+</test-fixture>
+
+<script>
+ suite('gr-list-view tests', () => {
+ let element;
+ let sandbox;
+
+ setup(() => {
+ sandbox = sinon.sandbox.create();
+ element = fixture('basic');
+ });
+
+ teardown(() => {
+ sandbox.restore();
+ });
+
+ test('_computeNavLink', () => {
+ const offset = 25;
+ const projectsPerPage = 25;
+ const filter = 'test';
+ element.path = '/admin/projects';
+
+ sandbox.stub(element, 'getBaseUrl', () => '');
+
+ assert.equal(
+ element._computeNavLink(offset, 1, projectsPerPage, filter),
+ '/admin/projects/q/filter:test,50');
+
+ assert.equal(
+ element._computeNavLink(offset, -1, projectsPerPage, filter),
+ '/admin/projects/q/filter:test');
+
+ assert.equal(
+ element._computeNavLink(offset, 1, projectsPerPage, null),
+ '/admin/projects,50');
+
+ assert.equal(
+ element._computeNavLink(offset, -1, projectsPerPage, null),
+ '/admin/projects');
+ });
+
+ test('_onValueChange', done => {
+ element.path = '/admin/projects';
+ sandbox.stub(page, 'show', url => {
+ assert.equal(url, '/admin/projects/q/filter:test');
+ done();
+ });
+ const e = {target: {value: 'test'}};
+ element._onValueChange(e);
+ });
+
+ test('next button', done => {
+ element.itemsPerPage = 25;
+ projects = new Array(26);
+
+ flush(() => {
+ let loading;
+ assert.isFalse(element._hideNextArrow(loading, projects));
+ loading = true;
+ assert.isTrue(element._hideNextArrow(loading, projects));
+ loading = false;
+ assert.isFalse(element._hideNextArrow(loading, projects));
+ element._projects = [];
+ assert.isTrue(element._hideNextArrow(loading, element._projects));
+ projects = new Array(4);
+ assert.isTrue(element._hideNextArrow(loading, projects));
+ done();
+ });
+ });
+
+ test('prev button', () => {
+ flush(() => {
+ let offset = 0;
+ assert.isTrue(element._hidePrevArrow(offset));
+ offset = 5;
+ assert.isFalse(element._hidePrevArrow(offset));
+ });
+ });
+ });
+</script>