UNPKG

@vuesax-alpha/nightly

Version:
67 lines (62 loc) 2.03 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); require('../../../hooks/index.js'); var index = require('../../../hooks/use-namespace/index.js'); 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 = index.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); }; exports.getPage = getPage; exports.sortData = sortData; exports.toggleSelectAll = toggleSelectAll; //# sourceMappingURL=utils.js.map