diff --git a/src/webcomponents/commons/filters/study-filter.js b/src/webcomponents/commons/filters/study-filter.js index f67790f99c..952c768db1 100644 --- a/src/webcomponents/commons/filters/study-filter.js +++ b/src/webcomponents/commons/filters/study-filter.js @@ -14,7 +14,7 @@ * limitations under the License. */ -import {LitElement, html} from "lit"; +import {LitElement, html, nothing} from "lit"; import UtilsNew from "../../../core/utils-new.js"; import LitUtils from "../utils/lit-utils.js"; import "../forms/select-field-filter.js"; @@ -24,7 +24,6 @@ export default class StudyFilter extends LitElement { constructor() { super(); - this.#init(); } @@ -35,103 +34,139 @@ export default class StudyFilter extends LitElement { static get properties() { return { opencgaSession: { - type: Object + type: Object, }, value: { type: String, }, + config: { + type: Object, + }, }; } #init() { this._prefix = UtilsNew.randomString(8); - this.operator = ","; - this.selectedStudies = []; - this.differentStudies = []; + this._studies = []; + this._operator = ","; + this._selection = []; + this._config = this.getDefaultConfig(); } update(changedProperties) { if (changedProperties.has("opencgaSession")) { - if (this.opencgaSession?.project?.studies?.length) { - this.differentStudies = this.opencgaSession.project.studies.filter(study => this.opencgaSession.study.id !== study.id); - } + this.opencgaSessionObserver(); } - if (changedProperties.has("opencgaSession") || changedProperties.has("value")) { - this.selectedStudies = Array.from(new Set([ - this.opencgaSession.study.fqn, - ...(this.value || "").split(this.operator).filter(v => !!v), - ])); + this.valueObserver(); + } + if (changedProperties.has("config")) { + this._config = { + ...this.getDefaultConfig(), + ...this.config, + }; } - super.update(changedProperties); } - updated(changedProperties) { - if (changedProperties.has("opencgaSession")) { - $(".selectpicker", this).selectpicker("refresh"); + opencgaSessionObserver() { + this._studies = []; + if (this.opencgaSession?.project?.studies?.length) { + // 1. Add current study as the first element and mark it as disabled + this._studies.push({ + name: this.opencgaSession.study.name, + id: this.opencgaSession.study.fqn, + selected: true, + disabled: true, + }); + // 2. Add other studies to the studies dropdown + this.opencgaSession.project.studies.forEach(study => { + if (study.fqn !== this.opencgaSession.study.fqn) { + this._studies.push({ + name: study.name, + id: study.fqn, + }); + } + }); } - $(".selectpicker", this).selectpicker("val", this.selectedStudies); } - filterChange() { - let querystring; - // AND or OR operators - if (this.operator !== "!") { - querystring = [...this.selectedStudies.map(study => `${study}`)].join(this.operator); - } else { - // NOT operator (not visible/not implemented) - querystring = [...this.selectedStudies.map(study => `${this.operator}${study}`)].join(";"); - } - LitUtils.dispatchCustomEvent(this, "filterChange", querystring); + valueObserver() { + // 1. Reset the operator value. If the current value does not contain ';', maintain the current selected operator + this._operator = (this.value || "").indexOf(";") > -1 ? ";" : this._operator; + // 2. Reset the selection + this._selection = Array.from(new Set([ + this.opencgaSession.study.fqn, + ...(this.value || "").split(this._operator).filter(v => !!v), + ])); } - onChangeOperator(e) { - this.operator = e.target.value; - this.filterChange(); + onStudyChange(event) { + // 1. Split values returned from select-field-filter and remove empty items + // Note: select-field-filter returns values joined with a comma character + const values = (event.detail.value || "") + .split(",") + .filter(value => !!value); + // 2. Trigger 'filterChange' event with the values joined with the current operator + LitUtils.dispatchCustomEvent(this, "filterChange", values.join(this._operator)); } - onChangeSelectedStudy() { - const selected = $(".selectpicker", this).selectpicker("val"); - // Active study is always the first element - this.selectedStudies = [this.opencgaSession.study.fqn, ...selected]; - this.requestUpdate(); - this.filterChange(); + onOperatorChange(event) { + // 1. Save the new operator value + this._operator = event.target.value || ","; + // 2. Trigger the 'filterChange' event + LitUtils.dispatchCustomEvent(this, "filterChange", this._selection.join(this._operator)); } render() { - // Check Project exists - if (!this.opencgaSession && !this.opencgaSession.project) { - return html` -