nopaque/web/app/static/js/modules/nopaque.InteractionElement.js

67 lines
2.0 KiB
JavaScript
Raw Normal View History

2020-06-19 10:30:05 +00:00
class InteractionElement {
constructor(htmlId="",
checkStatus=true,
2020-06-19 10:30:05 +00:00
disabledBefore=true,
disabledAfter=false,
hideBefore=true,
hideAfter=false) {
2020-06-25 15:44:55 +00:00
this.htmlId = htmlId;
this.element = (htmlId) => {this.element = document.getElementById(htmlId);}
this.checkStatus = checkStatus;
this.callbacks = {};
this.disabledBefore = disabledBefore;
this.disabledAfter = disabledAfter;
this.hideBefore = hideBefore;
this.hideAfter = hideAfter;
this.element(this.htmlId);
2020-06-19 10:30:05 +00:00
}
setCallback(trigger, callback, bindThis, args=[]) {
this.callbacks[trigger] = {
2020-06-25 15:44:55 +00:00
"function": callback,
"bindThis": bindThis,
"args": args
2020-06-19 10:30:05 +00:00
};
}
bindThisToCallback(trigger) {
let callback = this.callbacks[trigger];
let boundedCallback = callback["function"].bind(callback.bindThis);
return boundedCallback;
}
2020-07-20 07:31:27 +00:00
}
class InteractionElements {
constructor() {
this.interactions = [];
}
addInteractions (interactionsArray) {
this.interactions.push(...interactionsArray);
}
2020-07-20 07:31:27 +00:00
onChangeExecute() {
// checks if a change for every interactionElement happens and executes
// the callbacks accordingly
2020-07-20 07:31:27 +00:00
for (let interaction of this.interactions) {
if (interaction.checkStatus) {
interaction.element.addEventListener("change", (event) => {
if (event.target.checked) {
let f_on = interaction.bindThisToCallback("on");
let args_on = interaction.callbacks.on.args;
f_on(...args_on);
} else if (!event.target.checked){
let f_off = interaction.bindThisToCallback("off");
let args_off = interaction.callbacks.off.args;
f_off(...args_off);
}
});
} else {
continue
}
};
}
}
2020-08-20 14:03:37 +00:00
// export Classes
export { InteractionElement, InteractionElements };