frontend/.pnpm-store/v3/files/ad/1f35138f2f17669721a44684ff3131db7a4703628b46c31fca5aacd45cb5481891242c4ee4d93704df47e8b1a311cefd1616d00a7e158f8aa9288911cc51d7

100 lines
2.9 KiB
Plaintext

import Context from './core/Context';
import Rows from './core/Handlers/Rows';
import Pages from './core/Handlers/Pages';
import GlobalSearch from './core/Handlers/GlobalSearch';
import Filters from './core/Handlers/Filters';
export default class DataHandler {
context;
rows;
pages;
globalSearch;
filters;
i18n;
constructor(data = [], params = { rowsPerPage: null }) {
this.i18n = this.translate(params.i18n);
this.context = new Context(data, params);
this.rows = new Rows(this.context);
this.pages = new Pages(this.context);
this.globalSearch = new GlobalSearch(this.context);
this.filters = new Filters(this.context);
}
setRows(data) {
this.context.rawRows.set(data);
}
getRows() {
return this.context.rows;
}
getRowCount() {
return this.context.rowCount;
}
getRowsPerPage() {
return this.context.rowsPerPage;
}
sort(orderBy) {
this.setPage(1);
this.rows.sort(orderBy);
}
sortAsc(orderBy) {
this.setPage(1);
this.rows.sortAsc(orderBy);
}
sortDesc(orderBy) {
this.setPage(1);
this.rows.sortDesc(orderBy);
}
getSorted() {
return this.context.sorted;
}
search(value, scope = null) {
this.globalSearch.set(value, scope);
}
clearSearch() {
this.globalSearch.remove();
}
filter(value, filterBy) {
return this.filters.set(value, filterBy);
}
getPages(params = { ellipsis: false }) {
if (params.ellipsis) {
return this.context.pagesWithEllipsis;
}
return this.context.pages;
}
getPageCount() {
return this.context.pageCount;
}
getPageNumber() {
return this.context.pageNumber;
}
setPage(value) {
switch (value) {
case 'previous': return this.pages.previous();
case 'next': return this.pages.next();
default: return this.pages.goTo(value);
}
}
getTriggerChange() {
return this.context.triggerChange;
}
translate(i18n) {
return { ...{
search: 'Search...',
show: 'Show',
entries: 'entries',
filter: 'Filter',
rowCount: 'Showing {start} to {end} of {total} entries',
noRows: 'No entries to found',
previous: 'Previous',
next: 'Next'
}, ...i18n };
}
/**
* Deprecated
* use setRows() instead
*/
update(data) {
console.log('%c%s', 'color:#e65100;background:#fff3e0;font-size:12px;border-radius:4px;padding:4px;text-align:center;', `DataHandler.update(data) method is deprecated. Please use DataHandler.setRows(data) instead`);
this.context.rawRows.set(data);
}
}