-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortElements.js
68 lines (60 loc) · 3.01 KB
/
sortElements.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Sorts elements in a `.sortable-items` container by an `.sort-${property-name}` element text content, when clicked on an `.sort-${property-name}` element outside the container.
*
* Available sorting directions: `default`, `asc`, `desc`.
*
* Available data types for comparison: `string`, `int`.
*
* @param {object} type - data types `{ string: ['name', 'model'], int: ['quantity', 'formatedPrice'] }`.
*/
class SortElements {
constructor(type) {
(type.int ??= []).push('defaultSortId');
this.type = type;
this.initActionHandler();
}
initActionHandler() {
document.addEventListener('click', e => {
const sortBtn = e.target.closest('[class*="sort-"]');
if (sortBtn && !sortBtn.closest('.sortable-items'))
this.sortElements(this.getContainer(sortBtn), this.getProp(sortBtn), this.toggleSort(sortBtn));
});
}
getContainer(sortBtn) {
let container, parent = sortBtn.parentElement;
do {
parent = parent.parentElement;
container = parent.querySelector('.sortable-items');
} while (!container);
return container;
}
getProp(sortBtn) {
let propName;
[...sortBtn.classList].some(str => propName = str.startsWith('sort-') ? str.substring(5) : '');
return propName;
}
toggleSort(sortBtn) {
const
sorts = ['asc', 'desc'],
curSort = [...sortBtn.classList].reduce((acc, cur) => sorts.includes(cur) ? cur : acc, ''),
toggle = (el, sort) => sort === '' ? el.classList.add('asc') : (sort === 'asc' ? el.classList.replace(...sorts) : el.classList.remove('desc'));
[...sortBtn.parentElement.children].forEach(el => el === sortBtn ? toggle(el, curSort) : el.classList.remove(...sorts));
return sorts[sorts.indexOf(curSort) + 1];
}
sortElements(container, prop, sort) {
prop = sort ? prop : 'defaultSortId';
if (!this.isExistDefaultSort(container)) this.createDefaultSort(container);
container.replaceChildren(...[...container.children].sort(this.getCompareFn(prop, sort)));
}
getCompareFn(prop, sort) {
sort = sort ? sort : 'asc';
const [first, second] = sort === 'asc' ? ['a', 'b'] : ['b', 'a'];
if (this.type.string.includes(prop))
return (a, b) => this.getValue(eval(first), prop).localeCompare(this.getValue(eval(second), prop));
if (this.type.int.includes(prop))
return (a, b) => parseInt(this.getValue(eval(first), prop).replace(/ /g, '')) - parseInt(this.getValue(eval(second), prop).replace(/ /g, ''));
}
isExistDefaultSort = container => typeof container.firstElementChild.dataset.defaultSortId === 'string';
createDefaultSort = container => [...container.children].forEach((el, i) => el.dataset.defaultSortId = i);
getValue = (el, prop) => prop === 'defaultSortId' ? el.dataset.defaultSortId : (el.classList.contains(`sort-${prop}`) ? el.textContent : el.querySelector(`.sort-${prop}`).textContent);
}