File: D:/HostingSpaces/SBogers10/shop.komma.nl/resources/js/kms/attributes/vatScenarioController.js
export class VatScenarioController {
/**
* @param {HTMLElement} wrapper
* */
constructor(wrapper) {
if(!this.setWrapper(wrapper)) return;
if(!this.setKey()) return;
if(!this.setValuePart()) return;
if(!this.setRealInput()) return;
if(!this.setNumericRealInput()) return;
if(!this.setNumericWholeInput()) return;
if(!this.setNumericFractionInput()) return;
if(!this.setSelectInput()) return;
this.updateRealInput = this.updateRealInput.bind(this);
this.controlListeners();
this.updateRealInput();
}
/**
* @param {boolean} enable
*/
controlListeners(enable = true) {
//We cannot add a listener to the numeric real input because it's value is set by the numeric.js controller.
//Javascript does not emit / dispatch (change) events when you set a value by javascript. This is to prevent loops for example.
//For this reason we add a click event listener on the wrapper.
this.wrapper.removeEventListener('click', this.updateRealInput);
this.wholeInput.removeEventListener('change', this.updateRealInput);
this.wholeInput.removeEventListener('keydown', this.updateRealInput);
this.fractionInput.removeEventListener('change', this.updateRealInput);
this.fractionInput.removeEventListener('keydown', this.updateRealInput);
this.selectInput.removeEventListener('change', this.updateRealInput);
if(enable) {
this.wrapper.addEventListener('click', this.updateRealInput);
this.wholeInput.addEventListener('change', this.updateRealInput);
this.wholeInput.addEventListener('keydown', this.updateRealInput);
this.fractionInput.addEventListener('change', this.updateRealInput);
this.fractionInput.addEventListener('keydown', this.updateRealInput);
this.selectInput.addEventListener('change', this.updateRealInput);
}
}
/**
* @param {HTMLElement} wrapper
* @return {boolean}
*/
setWrapper(wrapper) {
if (wrapper === undefined) {
console.error('VatScenarioPrice Missing the expected element that represents the VatScenario attribute wrapper.');
return false;
}
this.wrapper = wrapper;
return true;
}
/**
* @return {boolean}
*/
setKey()
{
if(!('key' in this.wrapper.dataset)) {
console.error('VatScenarioPrice Missing the key dataset property on the vatScenarioPrice attribute wrapper');
return false;
}
this.key = this.wrapper.dataset.key;
return true;
}
/**
* @return {boolean}
*/
setValuePart()
{
if(!('valuePart' in this.wrapper.dataset)) {
console.error('VatScenarioPrice Missing the valuePart dataset property on the vatScenarioPrice attribute wrapper');
return false;
}
this.valuePart = this.wrapper.dataset.valuePart;
return true;
}
/**
* @return {boolean}
*/
setRealInput()
{
let realInput = this.wrapper.querySelector('#'+this.key);
if (!realInput) {
console.error('VatScenarioPrice Did not find a required input of type checkbox in the VatScenarioPrice attribute wrapper.');
return false;
}
this.realInput = realInput;
return true;
}
updateRealInput() {
console.log('change', this.realInput);
this.realInput.value = [
this.numericRealInput.value,
this.selectInput.value,
].join('|');
}
//Retrieve some inputs from the numeric attribute. Because we cannot get the numeric controller of the numeric attribute.
//We assume they exists, because otherwise the numeric attribute controller will let you know they are missing.
/**
* @return {boolean}
*/
setNumericRealInput()
{
this.numericRealInput = this.wrapper.querySelector('#VatScenarioPrice-'+this.valuePart+'_numeric');
return true;
}
/**
* @return {boolean}
*/
setNumericWholeInput()
{
this.wholeInput = this.wrapper.querySelector('#VatScenarioPrice-'+this.valuePart+'_numeric_whole');
return true;
}
/**
* @return {boolean}
*/
setNumericFractionInput()
{
this.fractionInput = this.wrapper.querySelector('#VatScenarioPrice-'+this.valuePart+'_numeric_fraction');
return true;
}
/**
* @return {boolean}
*/
setSelectInput()
{
this.selectInput = this.wrapper.querySelector('.select-menu');
return true;
}
}