/*
Select plugin
Note: Only for single select option
Usage example:
```
```
*/
let svg_tick = `
`;
function toggleSelectOptions(elem, state) {
elem.classList.remove("invalid");
try { elem.parentElement.querySelector('.errTxt').remove();} catch (error) {}
let options = elem.querySelector('.options');
const pos = elem.getBoundingClientRect();
const windowWidth = document.getElementsByTagName("body")[0].clientHeight;
if(pos.y + 250 > windowWidth) {
options.style.bottom = '40px';
} else { options.style.bottom = null }
options.style.display = state != 'close' ? getComputedStyle(options).display == 'none' ? 'block': 'none' : 'none';
}
let selectElements = document.querySelectorAll('.custom-select').forEach(element => {
let value = element.getAttribute('data-default')
element.childNodes[0].nodeValue = value;
element.querySelector(`.options span[data-value="${value.toLowerCase()}"]`).
innerHTML = `${value} ${svg_tick}`;
element.addEventListener('click', (e) => {if (e.target === element) toggleSelectOptions(element)});
element.addEventListener('focusout', (e) => {if (e.target === element) toggleSelectOptions(element, 'close')});
});
function singleSelectClickHandler(elem) {
let selectDiv = elem.closest('.custom-select');
let selectedOption = selectDiv.querySelector('span[selected]');
let input = document.querySelector(`[name="${selectDiv.getAttribute('data-input')}"]`);
if (!elem.hasAttribute('selected')) {
if (selectedOption != null) {
selectedOption.removeAttribute('selected');
selectedOption.querySelector('svg').remove();
}
elem.setAttribute('selected', '');
elem.innerHTML = `${elem.innerText} ${svg_tick}`
// Code where value is inserted to input
input.value = elem.getAttribute('data-value') ? elem.getAttribute('data-value') : '';
selectDiv.childNodes[0].nodeValue = elem.innerText;
} else {
elem.removeAttribute('selected');
elem.querySelector('svg').remove();
selectDiv.childNodes[0].nodeValue = selectDiv.getAttribute('data-defaultIfNone');
input.value = "";
}
selectDiv.blur();
}
let singleSelectOptions = document.querySelectorAll('.custom-select:not([data-multiple="1"])>.options span');
for (let i = 0; i < singleSelectOptions.length; i++) {
singleSelectOptions[i].addEventListener('click', () => {singleSelectClickHandler(singleSelectOptions[i])});
singleSelectOptions[i].setAttribute('id', 'option-'+i.toString());
}
/*
Toggle switch plugin
Usage example:
```
```
*/
document.querySelectorAll('[data-isCheckbox]:not([data-value="all"]) label').forEach(checkBoxLabel => {
checkBoxLabel.addEventListener('click', () => {
let checkBox = checkBoxLabel.parentElement;
let helperInput = checkBox.parentElement.querySelector('input[type="checkbox"]');
let mainInput = document.getElementsByName(checkBox.getAttribute('data-input'))[0];
if (helperInput.checked == true) {
mainInput.value = mainInput.value.replace(checkBox.getAttribute('data-value') + ',', '');
} else {
mainInput.value += checkBox.getAttribute('data-value') + ',';
}
});
})