UNPKG

@vuesax-alpha/nightly

Version:
61 lines (58 loc) 1.91 kB
import '../../../hooks/index.mjs'; import { useNamespace } from '../../../hooks/use-namespace/index.mjs'; const toggleSelectAll = (selected, data) => { if (selected.length !== data.length) { return [...data]; } return []; }; const sortData = (event, data, sortKey, type = "desc") => { const _sortKey = String(sortKey); let sortType = type; const ns = useNamespace("table"); const el = event.target; data = [...data].sort(returnOriginalIndex); if (!el) return []; if (el.dataset[`sortType${_sortKey}`] === "desc") { sortType = "asc"; } else if (el.dataset[`sortType${_sortKey}`] === "asc") { sortType = void 0; } if (sortType === "desc") { data.forEach((item, index) => { item[`originalIndex${_sortKey}`] = index; }); } el.dataset[`sortType${_sortKey}`] = sortType; el.dataset["sortType"] = sortType; el.dataset["sortKey"] = `sortType${_sortKey}`; const parent = el.closest(ns.e("tr")); const thsort = parent == null ? void 0 : parent.querySelectorAll("th.is-sort"); thsort == null ? void 0 : thsort.forEach((th) => { if (th !== el) { th.dataset.sortType = null; th.dataset[th.dataset[`sortKey`]] = null; } }); function compare(a, b) { if (a[_sortKey] < b[_sortKey]) { return sortType !== "desc" ? 1 : -1; } if (a[_sortKey] > b[_sortKey]) { return sortType !== "desc" ? -1 : 1; } return 0; } function returnOriginalIndex(a, b) { return a[`originalIndex${_sortKey}`] - b[`originalIndex${_sortKey}`]; } return sortType !== void 0 ? [...data].sort(compare) : [...data].sort(returnOriginalIndex); }; const getPage = (data, page, maxItems = 5) => { const max = Math.ceil(page * maxItems); const min = max - maxItems; return data.filter((_, index) => index >= min && index < max); }; export { getPage, sortData, toggleSelectAll }; //# sourceMappingURL=utils.mjs.map