2020-03-23 18:10:42 +01:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ActionSelectors {
|
|
|
|
constructor() {
|
2020-06-26 15:20:15 +02:00
|
|
|
this.parentElement = document.querySelector('.form-check-parent');
|
|
|
|
this.parentForm = document.getElementById('bulkActions');
|
2020-03-23 18:10:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
watchCheckboxes(parentElement) {
|
2020-06-26 15:20:15 +02:00
|
|
|
document
|
|
|
|
.querySelectorAll('.child-hidden-input')
|
|
|
|
.forEach((element) => element.remove());
|
|
|
|
|
|
|
|
document.querySelectorAll('.form-check-child').forEach((child) => {
|
2020-03-23 18:10:42 +01:00
|
|
|
if (parentElement.checked) {
|
|
|
|
child.checked = parentElement.checked;
|
2020-06-26 15:20:15 +02:00
|
|
|
this.processChildItem(
|
|
|
|
child,
|
|
|
|
document.getElementById('bulkActions')
|
|
|
|
);
|
2020-03-23 18:10:42 +01:00
|
|
|
} else {
|
|
|
|
child.checked = false;
|
|
|
|
document
|
2020-06-26 15:20:15 +02:00
|
|
|
.querySelectorAll('.child-hidden-input')
|
|
|
|
.forEach((element) => element.remove());
|
2020-03-23 18:10:42 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
processChildItem(element, parent, options = {}) {
|
2020-06-26 15:20:15 +02:00
|
|
|
if (options.hasOwnProperty('single')) {
|
2020-03-23 18:10:42 +01:00
|
|
|
document
|
2020-06-26 15:20:15 +02:00
|
|
|
.querySelectorAll('.child-hidden-input')
|
|
|
|
.forEach((element) => element.remove());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (element.checked === false) {
|
|
|
|
let inputs = document.querySelectorAll('input.child-hidden-input');
|
|
|
|
|
|
|
|
for (let i of inputs) {
|
|
|
|
if (i.value == element.dataset.value) i.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2020-03-23 18:10:42 +01:00
|
|
|
}
|
|
|
|
|
2020-06-26 15:20:15 +02:00
|
|
|
let _temp = document.createElement('INPUT');
|
2020-03-23 18:10:42 +01:00
|
|
|
|
2020-06-26 15:20:15 +02:00
|
|
|
_temp.setAttribute('name', 'invoices[]');
|
|
|
|
_temp.setAttribute('value', element.dataset.value);
|
|
|
|
_temp.setAttribute('class', 'child-hidden-input');
|
2020-03-23 18:10:42 +01:00
|
|
|
_temp.hidden = true;
|
|
|
|
|
|
|
|
parent.append(_temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
handle() {
|
2020-06-26 15:20:15 +02:00
|
|
|
this.parentElement.addEventListener('click', () => {
|
2020-03-23 18:10:42 +01:00
|
|
|
this.watchCheckboxes(this.parentElement);
|
|
|
|
});
|
|
|
|
|
2020-06-26 15:20:15 +02:00
|
|
|
for (let child of document.querySelectorAll('.form-check-child')) {
|
|
|
|
child.addEventListener('click', () => {
|
2020-03-23 18:10:42 +01:00
|
|
|
this.processChildItem(child, this.parentForm);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @handle **/
|
|
|
|
new ActionSelectors().handle();
|