Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK-6409 - Clinical case can not be created with panel lock ON: value is passed as ON instead of True to OpenCGA #943

Merged
merged 9 commits into from
Jul 9, 2024
6 changes: 0 additions & 6 deletions src/webcomponents/commons/forms/data-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -954,26 +954,20 @@ export default class DataForm extends LitElement {
_createToggleSwitchElement(element) {
const value = this.getValue(element.field);
const disabled = this._getBooleanValue(element.display?.disabled, false, element);
const activeClassName = element.display?.activeClassName ?? element.display?.activeClass ?? "";
const inactiveClassName = element.display?.inactiveClassName ?? element.display?.inactiveClass ?? "";

const content = html`
<div class="">
<toggle-switch
.disabled="${disabled}"
.value="${value}"
.onText="${element.display?.onText}"
.offText="${element.display?.offText}"
.activeClass="${activeClassName}"
.inactiveClass="${inactiveClassName}"
.classes="${this._isUpdated(element) ? "updated" : ""}"
@filterChange="${e => this.onFilterChange(element, e.detail.value)}">
</toggle-switch>
</div>
`;

return this._createElementTemplate(element, value, content);

}

_createToggleButtonsElement(element) {
Expand Down
180 changes: 22 additions & 158 deletions src/webcomponents/commons/forms/toggle-switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,11 @@ import {LitElement, html} from "lit";
import UtilsNew from "../../../core/utils-new.js";
import LitUtils from "../utils/lit-utils.js";


/**
* Usage:
* <toggle-switch .value="true" .onText="YES" .offText="NO"></toggle-switch>
*/
export default class ToggleSwitch extends LitElement {

constructor() {
super();

// Set status and init private properties
this._init();
this.#init();
}

createRenderRoot() {
Expand All @@ -47,179 +40,50 @@ export default class ToggleSwitch extends LitElement {
offText: {
type: String
},
activeClass: {
type: String
},
inactiveClass: {
type: String
},
disabled: {
type: Boolean
},
classes: {
type: String
type: String,
},
_value: {
type: Boolean,
state: true
}
};
}

_init() {
#init() {
this._prefix = UtilsNew.randomString(8);

// Default values
this.onText = "ON";
this.offText = "OFF";
this.activeClass = "btn-primary";
this.inactiveClass = "btn-light";
this.classes = "";
}

// updated(changedProperties) {
// if (changedProperties.has("value")) {
// this._propertyObserver();
// this._value = this.value;
// }
// if (changedProperties.has("onText")) {
// this.onText = this.onText ? this.onText : "ON";
// // this._propertyObserver();
// }
// if (changedProperties.has("offText")) {
// this.offText = this.offText ? this.offText : "OFF";
// // this._propertyObserver();
// }
// if (changedProperties.has("activeClass")) {
// this.activeClass = this.activeClass ? this.activeClass : "btn-primary";
// this._propertyObserver();
// }
// if (changedProperties.has("inactiveClass")) {
// this.inactiveClass = this.inactiveClass ? this.inactiveClass : "btn-light";
// this._propertyObserver();
// }
// }

firstUpdated(changedProperties) {
if (changedProperties.has("value")) {
this.propertyObserver();
}
}

update(changedProperties) {
// if (changedProperties.has("value")) {
// this.propertyObserver();
// }

if (changedProperties.has("onText")) {
this.onText = this.onText ? this.onText : "ON";
}

if (changedProperties.has("offText")) {
this.offText = this.offText ? this.offText : "OFF";
}
super.update(changedProperties);
}

propertyObserver() {
// const val = this.value?.toString();
// if (val !== "OFF" && val !== "false") {
// this._value = true;
// } else {
// this._value = false;
// }
this._value = this.value;
}

_propertyObserver() {
if (typeof this.value !== "undefined" && this.activeClass && this.inactiveClass) {
if (this.value) {
this._onClass = this.activeClass + " active";
this._offClass = this.inactiveClass;
} else {
this._onClass = this.inactiveClass;
this._offClass = this.activeClass + " active";
}
this.requestUpdate();
}
}

onToggleClick(buttonId) {
// Check if there is anything to do
if ((this.value && buttonId === "ON") || (!this.value && buttonId === "OFF")) {
return;
}

// Support several classes
const activeClasses = this.activeClass.split(" ");
const inactiveClasses = this.inactiveClass.split(" ");

// Fetch and reset buttons status
const buttons = this.getElementsByClassName("btn-toggle-" + this._prefix);
Array.from(buttons).forEach(button => {
button.classList.remove(...activeClasses, ...inactiveClasses, "active");
});
let onIndex = 0;
let offIndex = 1;
if (buttons[0].dataset.id === "OFF") {
onIndex = 1;
offIndex = 0;
}

// Set proper classes
this.value = buttonId === "ON";
if (this.value) {
buttons[onIndex].classList.add(...activeClasses, "active");
buttons[offIndex].classList.add(...inactiveClasses);
} else {
buttons[onIndex].classList.add(...inactiveClasses);
buttons[offIndex].classList.add(...activeClasses, "active");
}

// Set the field status
this.onFilterChange();
}

onFilterChange(val) {
LitUtils.dispatchCustomEvent(this, "filterChange", val);
}

render() {
return html`
<fieldset ?disabled="${this.disabled}">
<fieldset .disabled="${this.disabled}">
<div class="btn-group" role="group">
<input class="btn-check" type="radio" ?checked="${this._value}"
name="${this._prefix}BtnRadio" id="${this._prefix}onBtnRadio"
@click=${() => this.onFilterChange("ON")} autocomplete="off">
<label class="btn btn-outline-primary" for="${this._prefix}onBtnRadio">
${this.onText}
<input
class="btn-check"
type="radio"
.checked="${this.value}"
name="${this._prefix}BtnRadio"
id="${this._prefix}onBtnRadio"
@click=${() => this.onFilterChange(true)}>
<label class="btn btn-outline-primary ${this.classes}" for="${this._prefix}onBtnRadio">
${this.onText || "ON"}
</label>

<input type="radio" class="btn-check" ?checked="${!this._value}"
name="${this._prefix}BtnRadio" id="${this._prefix}offBtnRadio"
@click=${() => this.onFilterChange("OFF")} autocomplete="off">
<label class="btn btn-outline-primary" for="${this._prefix}offBtnRadio">
${this.offText}
<input
type="radio"
class="btn-check"
.checked="${!this.value}"
name="${this._prefix}BtnRadio"
id="${this._prefix}offBtnRadio"
@click=${() => this.onFilterChange(false)}>
<label class="btn btn-outline-primary ${this.classes}" for="${this._prefix}offBtnRadio">
${this.offText || "OFF"}
</label>
</div>
</fieldset>
<!-- <button
class="btn ${this._onClass} btn-toggle-${this._prefix} ${this.classes}"
data-id="ON"
?disabled="${this.disabled}"
type="button"
@click="${e => this.onToggleClick("ON", e)}">
${this.onText}
</button>
<button
class="btn ${this._offClass} btn-toggle-${this._prefix} ${this.classes}"
data-id="OFF"
?disabled="${this.disabled}"
type="button"
@click="${e => this.onToggleClick("OFF", e)}">
${this.offText}
</button> -->

`;
}

Expand Down
Loading