-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[O2B-532] Create a reusable filtering system
- Loading branch information
1 parent
62357ac
commit 4fefe66
Showing
8 changed files
with
170 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* @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 { Observable } from '/js/src/index.js'; | ||
|
||
/** | ||
* @typedef FilteringItem | ||
* @property {FilterModel} model the model of the filter | ||
* @property {string} label the label of the filter | ||
*/ | ||
|
||
/** | ||
* Model representing a filtering system, including filter inputs visibility, filters values and so on | ||
*/ | ||
export class FilteringModel extends Observable { | ||
/** | ||
* Constructor | ||
* | ||
* @param {Object<string, FilteringItem>} filters the filters with their label and model | ||
*/ | ||
constructor(filters) { | ||
super(); | ||
|
||
this._visualChange$ = new Observable(); | ||
|
||
this._filters = filters; | ||
this._filterModels = Object.values(filters).map(({ model }) => model); | ||
for (const model of this._filterModels) { | ||
model.bubbleTo(this); | ||
model.visualChange$?.bubbleTo(this._visualChange$); | ||
} | ||
} | ||
|
||
/** | ||
* Reset the filters | ||
* | ||
* @return {void} | ||
*/ | ||
reset() { | ||
for (const model of this._filterModels) { | ||
model.reset(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the normalized value of all the filters, without null values | ||
* | ||
* @return {object} the normalized values | ||
*/ | ||
get normalized() { | ||
const ret = {}; | ||
for (const [filterKey, { model: filter }] of Object.entries(this._filters)) { | ||
if (filter && !filter.isEmpty) { | ||
ret[filterKey] = filter.normalized; | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
/** | ||
* States if there is currently at least one filter active | ||
* | ||
* @return {boolean} true if at least one filter is active | ||
*/ | ||
isAnyFilterActive() { | ||
for (const model of this._filterModels) { | ||
if (!model.isEmpty) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Returns the observable notified any time there is a visual change which has no impact on the actual filtering | ||
* | ||
* @return {Observable} the filters visibility observable | ||
*/ | ||
get visualChange$() { | ||
return this._visualChange$; | ||
} | ||
|
||
/** | ||
* Return the filter model for a given key | ||
* | ||
* @param {string} key the key of the filtering model | ||
* @return {FilteringModel} the filtering model | ||
*/ | ||
get(key) { | ||
if (!(key in this._filters)) { | ||
throw new Error(`No filter found with key ${key}`); | ||
} | ||
|
||
return this._filters[key].model; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 0 additions & 34 deletions
34
lib/public/components/Filters/common/filters/remoteDataTagFilter.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters