-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathFilterableOverviewPageModel.js
More file actions
128 lines (115 loc) · 3.83 KB
/
Copy pathFilterableOverviewPageModel.js
File metadata and controls
128 lines (115 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* @license
* Copyright CERN and copyright holders of ALICE O2. This software is
* distributed under the terms of the GNU General Public License v3 (GPL
* Version 3), copied verbatim in the file "COPYING".
*
* See http://alice-o2.web.cern.ch/license for full licensing information.
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
import { buildUrl } from '/js/src/index.js';
import { OverviewPageModel } from './OverviewModel.js';
import { FilteringModel } from '../components/Filters/common/FilteringModel.js';
/**
* Base model for a filterable overview page
*
* @template T the type of data displayed in the overview page
*/
export class FilterableOverviewPageModel extends OverviewPageModel {
/**
* Constructor
*/
constructor(router, pageIdentifier, filters) {
super();
this._filteringModel = new FilteringModel(router, filters, this._warnings);
this._filteringModel.pageIdentifier = pageIdentifier;
this._filteringModel.visualChange$.bubbleTo(this);
this._filteringModel.observe(() => this._applyFilters());
this._sortModel.observe(() => this._applyFilters());
this._debouncedLoad = (_time) => {}; // Abstract, does nothing on purpose
this._fetchInstantly = true;
}
/**
* Builds a url string from filters and a base string
*
* @param {string} base the base string from which the endpoint will be built
* @return {string}
*/
buildRootEndpoint(base) {
return buildUrl(base, { filter: this.getFilterParams() });
}
/**
* Sets the fetchInstantly boolean
* @param {boolean} bool the value to set
* @return {void}
*/
set fetchInstantly(bool) {
this._fetchInstantly = bool;
}
/**
* Returns all filtering, sorting and pagination settings to their default values
* @param {boolean} [fetch = true] whether to refetch all data after filters have been reset
* @return {void}
*/
reset(fetch = true) {
super.reset();
this.resetFiltering(fetch);
}
/**
* Reset all filtering models
* @param {boolean} fetch Whether to refetch all data after filters have been reset
* @param {boolean} [clearUrl=false] if true filters will be removed from the url
* @return {void}
*/
resetFiltering(fetch = true, clearUrl = false) {
this._filteringModel.reset(false, clearUrl);
if (fetch) {
this._applyFilters(true);
}
}
/**
* Checks if any filter value has been modified from their default (empty)
* @return {Boolean} If any filter is active
*/
isAnyFilterActive() {
return this._filteringModel.isAnyFilterActive();
}
/**
* Apply the current filtering and update the remote data list
*
* @param {boolean} now if true, filtering will be applied now without debouncing
*
* @return {void}
*/
_applyFilters() {
this._pagination.silentlySetCurrentPage(1);
this._fetchInstantly ? this.load() : this._debouncedLoad();
}
/**
* Set underlying FilteringModel's filters from the query parameters in the URL
*
* @param {boolean} notify if the FilteringModel should notify it's observers after finishing setting the filters
*/
setFilterFromURL(notify) {
this._filteringModel.setFilterFromURL(notify);
}
/**
* Return the filtering model
*
* @return {FilteringModel} the filtering model
*/
get filteringModel() {
return this._filteringModel;
}
/**
* Return filter params of base model
*
* @return {object} filter
*/
getFilterParams() {
return this._filteringModel.normalized;
}
}