2018-12-07 19:33:32 +01:00
|
|
|
/**
|
|
|
|
* ListSortControl
|
|
|
|
* Manages the logic for the control which provides list sorting options.
|
|
|
|
*/
|
|
|
|
class ListSortControl {
|
|
|
|
|
|
|
|
constructor(elem) {
|
|
|
|
this.elem = elem;
|
2019-05-05 15:43:26 +02:00
|
|
|
this.menu = elem.querySelector('ul');
|
2018-12-07 19:33:32 +01:00
|
|
|
|
|
|
|
this.sortInput = elem.querySelector('[name="sort"]');
|
|
|
|
this.orderInput = elem.querySelector('[name="order"]');
|
|
|
|
this.form = elem.querySelector('form');
|
|
|
|
|
2019-05-05 15:43:26 +02:00
|
|
|
this.menu.addEventListener('click', event => {
|
2018-12-07 19:33:32 +01:00
|
|
|
if (event.target.closest('[data-sort-value]') !== null) {
|
|
|
|
this.sortOptionClick(event);
|
|
|
|
}
|
2019-05-05 15:43:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.elem.addEventListener('click', event => {
|
2018-12-07 19:33:32 +01:00
|
|
|
if (event.target.closest('[data-sort-dir]') !== null) {
|
|
|
|
this.sortDirectionClick(event);
|
|
|
|
}
|
2019-05-05 15:43:26 +02:00
|
|
|
});
|
2018-12-07 19:33:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sortOptionClick(event) {
|
|
|
|
const sortOption = event.target.closest('[data-sort-value]');
|
|
|
|
this.sortInput.value = sortOption.getAttribute('data-sort-value');
|
|
|
|
event.preventDefault();
|
|
|
|
this.form.submit();
|
|
|
|
}
|
|
|
|
|
|
|
|
sortDirectionClick(event) {
|
|
|
|
const currentDir = this.orderInput.value;
|
|
|
|
const newDir = (currentDir === 'asc') ? 'desc' : 'asc';
|
|
|
|
this.orderInput.value = newDir;
|
|
|
|
event.preventDefault();
|
|
|
|
this.form.submit();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ListSortControl;
|