blob: f364c016c3a2eb923a6d89f538cd91be99bde618 [file] [log] [blame]
Dave Borowitz8cdc76b2018-03-26 10:04:27 -04001/**
2 * @license
3 * Copyright (C) 2017 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 */
Milutin Kristofic848f16c2020-08-13 14:25:41 +020017import '@polymer/iron-input/iron-input';
18import '@polymer/iron-icon/iron-icon';
19import '../../../styles/shared-styles';
20import '../gr-button/gr-button';
Milutin Kristofic848f16c2020-08-13 14:25:41 +020021import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin';
22import {PolymerElement} from '@polymer/polymer/polymer-element';
23import {htmlTemplate} from './gr-list-view_html';
24import {encodeURL, getBaseUrl} from '../../../utils/url-util';
25import {page} from '../../../utils/page-wrapper-utils';
Milutin Kristofic332cfbf2020-11-18 16:41:49 +010026import {property, customElement} from '@polymer/decorators';
Ben Rohlfse07f8182020-12-07 10:09:20 +010027import {fireEvent} from '../../../utils/event-util';
Becky Siegel56fbb552017-06-05 11:18:41 -070028
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010029const REQUEST_DEBOUNCE_INTERVAL_MS = 200;
Becky Siegel56fbb552017-06-05 11:18:41 -070030
Milutin Kristofic848f16c2020-08-13 14:25:41 +020031declare global {
32 interface HTMLElementTagNameMap {
33 'gr-list-view': GrListView;
Dmitrii Filippov3fd2b102019-11-15 16:16:46 +010034 }
Milutin Kristofic848f16c2020-08-13 14:25:41 +020035}
36
Ben Rohlfs04fc1ca2021-02-13 13:06:53 +010037const DEBOUNCER_RELOAD = 'reload';
38
Milutin Kristofic848f16c2020-08-13 14:25:41 +020039@customElement('gr-list-view')
Ben Rohlfsd94011e2021-03-08 23:37:58 +010040class GrListView extends LegacyElementMixin(PolymerElement) {
Milutin Kristofic848f16c2020-08-13 14:25:41 +020041 static get template() {
42 return htmlTemplate;
43 }
44
45 @property({type: Boolean})
46 createNew?: boolean;
47
48 @property({type: Array})
49 items?: unknown[];
50
51 @property({type: Number})
52 itemsPerPage = 25;
53
Milutin Kristofic332cfbf2020-11-18 16:41:49 +010054 @property({type: String, observer: '_filterChanged'})
Milutin Kristofic848f16c2020-08-13 14:25:41 +020055 filter?: string;
56
57 @property({type: Number})
58 offset?: number;
59
60 @property({type: Boolean})
61 loading?: boolean;
62
63 @property({type: String})
64 path?: string;
Dmitrii Filippov3fd2b102019-11-15 16:16:46 +010065
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010066 /** @override */
67 detached() {
68 super.detached();
Ben Rohlfs04fc1ca2021-02-13 13:06:53 +010069 this.cancelDebouncer(DEBOUNCER_RELOAD);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010070 }
71
Milutin Kristofic332cfbf2020-11-18 16:41:49 +010072 _filterChanged(newFilter?: string, oldFilter?: string) {
73 // newFilter can be empty string and then !newFilter === true
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010074 if (!newFilter && !oldFilter) {
75 return;
76 }
77
78 this._debounceReload(newFilter);
79 }
80
Milutin Kristofic332cfbf2020-11-18 16:41:49 +010081 _debounceReload(filter?: string) {
Milutin Kristofic848f16c2020-08-13 14:25:41 +020082 this.debounce(
Ben Rohlfs04fc1ca2021-02-13 13:06:53 +010083 DEBOUNCER_RELOAD,
Milutin Kristofic848f16c2020-08-13 14:25:41 +020084 () => {
85 if (this.path) {
86 if (filter) {
87 return page.show(
88 `${this.path}/q/filter:` + encodeURL(filter, false)
89 );
90 }
91 return page.show(this.path);
92 }
93 },
94 REQUEST_DEBOUNCE_INTERVAL_MS
95 );
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010096 }
97
98 _createNewItem() {
Ben Rohlfse07f8182020-12-07 10:09:20 +010099 fireEvent(this, 'create-clicked');
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100100 }
101
Milutin Kristofic848f16c2020-08-13 14:25:41 +0200102 _computeNavLink(
103 offset: number,
104 direction: number,
105 itemsPerPage: number,
106 filter: string,
107 path: string
108 ) {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100109 // Offset could be a string when passed from the router.
110 offset = +(offset || 0);
Milutin Kristofic848f16c2020-08-13 14:25:41 +0200111 const newOffset = Math.max(0, offset + itemsPerPage * direction);
Dmitrii Filippov0049afe2020-07-08 14:08:17 +0200112 let href = getBaseUrl() + path;
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100113 if (filter) {
Dmitrii Filippove3c09ae2020-07-10 11:39:50 +0200114 href += '/q/filter:' + encodeURL(filter, false);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100115 }
116 if (newOffset > 0) {
Milutin Kristofic848f16c2020-08-13 14:25:41 +0200117 href += `,${newOffset}`;
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100118 }
119 return href;
120 }
121
Milutin Kristofic848f16c2020-08-13 14:25:41 +0200122 _computeCreateClass(createNew?: boolean) {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100123 return createNew ? 'show' : '';
124 }
125
Milutin Kristofic848f16c2020-08-13 14:25:41 +0200126 _hidePrevArrow(loading?: boolean, offset?: number) {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100127 return loading || offset === 0;
128 }
129
Milutin Kristofic848f16c2020-08-13 14:25:41 +0200130 _hideNextArrow(loading?: boolean, items?: unknown[]) {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100131 if (loading || !items || !items.length) {
132 return true;
133 }
134 const lastPage = items.length < this.itemsPerPage + 1;
135 return lastPage;
136 }
137
138 // TODO: fix offset (including itemsPerPage)
139 // to either support a decimal or make it go to the nearest
140 // whole number (e.g 3).
Milutin Kristofic848f16c2020-08-13 14:25:41 +0200141 _computePage(offset: number, itemsPerPage: number) {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100142 return offset / itemsPerPage + 1;
143 }
144}