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');
Array.from(selectElements).forEach(element => {
element.childNodes[0].nodeValue = (element.hasAttribute('data-multiple') ? element.getAttribute('data-placeholder') : element.getAttribute('data-default'));
element.addEventListener('click', (e) => {if (e.target === element) toggleSelectOptions(element)});
element.addEventListener('focusout', (e) => {if (e.target === element) toggleSelectOptions(element, 'close')});
});
function removeSelectOption(elem, optionId) {
let option = document.querySelector('#'+optionId);
let selectDiv = option.closest('.custom-select');
let input = document.querySelector(`[name="${selectDiv.getAttribute('data-input')}"]`);
elem.parentElement.remove();
option.removeAttribute('selected');
option.querySelector('svg').remove();
input.value = input.value.replace(option.getAttribute('data-value') ? option.getAttribute('data-value') + "," : '', '');
}
function multiSelectClickHandler(elem) {
let selectDiv = elem.closest('.custom-select');
let input = document.querySelector(`[name="${selectDiv.getAttribute('data-input')}"]`);
if (!elem.hasAttribute('selected')) {
document.querySelector('#'+elem.closest(".custom-select").getAttribute("data-showDivId")).innerHTML +=
`${elem.innerText}
`;
elem.setAttribute('selected', '');
elem.innerHTML = `${elem.innerText}
`
// Code where value in inserted into input
input.value += elem.getAttribute('data-value') ? elem.getAttribute('data-value') + "," : '';
} else {
// similar to removeSelectOption method
document.querySelector('#'+elem.getAttribute('id')+'-selected').remove();
elem.removeAttribute('selected');
elem.querySelector('svg').remove();
input.value = input.value.replace(elem.getAttribute('data-value') ? elem.getAttribute('data-value') + "," : '', '');
}
}
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}
`
// 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-default');
input.value = "";
}
selectDiv.blur();
}
let multiSelectOptions = document.querySelectorAll('.custom-select[data-multiple="1"]>.options span');
for (let i = 0; i < multiSelectOptions.length; i++) {
multiSelectOptions[i].addEventListener('click', () => {multiSelectClickHandler(multiSelectOptions[i])});
multiSelectOptions[i].setAttribute('id', 'option-'+i.toString());
}
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());
}
function selectAllHandler(elem) {
let options = elem.parentElement.querySelectorAll('.custom-select[data-multiple="1"]>.options span');
Array.from(options).forEach((option) => {
try {
let selectedShownElem = document.getElementById(option.getAttribute('id')+'-selected');
removeSelectOption(selectedShownElem.querySelector('span'), option.getAttribute('id'))
} catch (err) {}
if (elem.innerText == 'select all') { option.click() };
});
elem.innerText = elem.innerText == 'select all' ? 'deselect all' : 'select all'
}
// On settings form submit
function submitSettings() {
let form = document.settings;
document.querySelectorAll('.errTxt').forEach(e => e.remove());
for(let i = 0; i < form.elements.length; i++) {
let input = form.elements[i];
if (input.value == "" && input.hasAttribute('required')) {
let elem = input.parentElement.querySelector('[takeInput]')
let errTxt = document.createElement('p')
errTxt.classList.add('errTxt')
errTxt.innerText = 'This setting can\'t be empty!!'
elem.classList.add('invalid');
elem.parentElement.insertBefore(errTxt, elem);
}
}
return false;
}
document.querySelectorAll('.settings-sidebar .set-name').forEach(filter => {
let target = filter.getAttribute('data-detailId');
filter.addEventListener('click', () => {
try {document.querySelector('.set-name.active').classList.remove('active');} catch(e){}
filter.classList.add('active');
if (target == 'all') {
document.querySelectorAll('.set-item').forEach((elem) => {
elem.style.display = 'block';
})
return;
}
document.querySelectorAll('.set-item[data-id="'+target+'"]').forEach((elem) => {
elem.style.display = 'block'
})
document.querySelectorAll('.set-item:not([data-id="'+target+'"])').forEach((elem) => {
elem.style.display = 'none'
})
})
})
function fadeOut(element) {
var op = 1; // initial opacity
var timer = setInterval(function () {
if (op <= 0.1){
clearInterval(timer);
element.style.display = 'none';
element.classList.add('fade');
}
element.style.opacity = op;
element.style.filter = 'alpha(opacity=' + op * 100 + ")";
op -= op * 0.1;
}, 50);
}
function fadeIn(element) {
var op = 0.1; // initial opacity
element.style.display = 'block';
var timer = setInterval(function () {
if (op >= 1){
clearInterval(timer);
}
element.style.opacity = op;
element.style.filter = 'alpha(opacity=' + op * 100 + ")";
op += op * 0.1;
}, 10);
}