aio-table 12.0.2 → 14.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. package/filter.css +282 -0
  2. package/index.css +9 -9
  3. package/index.d.ts +168 -52
  4. package/index.js +943 -253
  5. package/package.json +4 -2
package/index.js CHANGED
@@ -8,8 +8,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
11
- import { createContext, createRef, Fragment, useContext, useEffect, useRef, useState } from "react";
12
- import AIOInput, { AISelect } from "aio-input";
11
+ import { createContext, createRef, Fragment, useContext, useEffect, useMemo, useRef, useState } from "react";
12
+ import AIOInput, { AICheckbox, AIFormInput, AINumber, AIRadio, AISelect, AIText, AITime } from "aio-input";
13
13
  import * as UT from 'aio-utils';
14
14
  import usePopup from "aio-popup";
15
15
  import AIODate from "aio-date";
@@ -19,47 +19,72 @@ const Provider = (p) => _jsx(Context.Provider, { value: p.value, children: p.chi
19
19
  const useProvider = () => useContext(Context);
20
20
  const AIOTable = (props) => {
21
21
  const popup = usePopup();
22
- let [dom] = useState(createRef());
22
+ const fileClass = new UT.FileClass();
23
23
  let [searchValue, setSearchValue] = useState('');
24
- const [searchColumns, setSearchColumns] = useState([]);
25
- const [excelColumns, setExcelColumns] = useState([]);
26
- const [filterColumns, setFilterColumns] = useState([]);
27
- const [toggleColumns, setToggleColumns] = useState([]);
28
- const filterColumnsRef = useRef(filterColumns);
29
- filterColumnsRef.current = filterColumns;
30
- const rowsIndexDicRef = useRef({});
31
- const setRowsIndexDic = (rowsIndexDic) => rowsIndexDicRef.current = rowsIndexDic;
32
- const getRowsIndexDic = () => rowsIndexDicRef.current;
33
24
  const propsRef = useRef(props);
34
25
  propsRef.current = props;
35
- const pagingHook = UT.usePaging({ rows: props.value, paging: props.paging, onChange: props.onChangePaging });
36
- const getColumnOption = (key, cellDetails) => {
37
- if (key === 'id') {
38
- return cellDetails.column.id;
39
- }
40
- const columnValue = cellDetails.column[key];
41
- if (columnValue !== undefined) {
42
- const { getValue = {} } = props;
43
- if (getValue[columnValue]) {
44
- return getValue[columnValue](cellDetails);
45
- }
46
- return cellDetails.column[key];
47
- }
48
- const { columnOption } = props;
49
- const fn = (columnOption || {})[key] || (() => { });
50
- const res = fn(cellDetails);
51
- if (res !== undefined) {
52
- return res;
53
- }
54
- if (key === 'value') {
55
- return cellDetails.row[cellDetails.column.id];
56
- }
57
- const defaults = { show: true, title: '', justify: false, titleAttrs: {} };
58
- return defaults[key];
59
- };
60
- const tableHook = useTable(() => propsRef.current, () => props.paging, getColumnOption);
61
- const getIconRef = useRef(new UT.GetSvg());
62
- const getIcon = getIconRef.current.getIcon;
26
+ const pagingHook = usePaging({ rows: props.value, paging: props.paging, onChange: props.onChangePaging });
27
+ // const getColumnOption: I_getColumnOption<any> = (key, cellDetails) => {
28
+ // if (key === 'id') { return cellDetails.column.id }
29
+ // const columnValue = cellDetails.column[key]
30
+ // if (columnValue !== undefined) {
31
+ // const { getValue = {} } = props
32
+ // if (getValue[columnValue]) { return getValue[columnValue](cellDetails) }
33
+ // return cellDetails.column[key]
34
+ // }
35
+ // const { columnOption } = props;
36
+ // const fn = (columnOption || {})[key] || (() => { })
37
+ // const res = fn(cellDetails);
38
+ // if (res !== undefined) { return res }
39
+ // if (key === 'value') {
40
+ // return cellDetails.row[cellDetails.column.id]
41
+ // }
42
+ // const defaults: { [key in keyof I_table_column<any>]?: any } = { show: true, title: '', justify: false, titleAttrs: {} }
43
+ // return defaults[key]
44
+ // }
45
+ const tableHook = useTable(() => propsRef.current, () => props.paging);
46
+ const getColumn = (col) => {
47
+ const { columnOption = {} } = props;
48
+ const column = Object.assign({}, col);
49
+ for (let prop in columnOption) {
50
+ if (prop !== 'value') {
51
+ const fn = columnOption[prop];
52
+ if (fn) {
53
+ const res = fn({ column, row: undefined, rowIndex: 0 });
54
+ if (res !== undefined) {
55
+ column[prop] = res;
56
+ }
57
+ }
58
+ }
59
+ }
60
+ return Object.assign(Object.assign({}, column), col);
61
+ };
62
+ const getColumns = () => {
63
+ let { columns = [] } = props;
64
+ let searchColumns = [], excelColumns = [], filterColumns = [], toggleColumns = [];
65
+ let updatedColumns = columns.map((col, i) => {
66
+ const column = getColumn(col);
67
+ const { filter, search, excel, toggle, id, show = true } = column;
68
+ if (id === undefined) {
69
+ console.error(`aio-table error => missing column id in props.columns[${i}]`);
70
+ }
71
+ const fixedColumn = Object.assign(Object.assign({}, column), { show });
72
+ if (search) {
73
+ searchColumns.push(fixedColumn);
74
+ }
75
+ if (excel) {
76
+ excelColumns.push(fixedColumn);
77
+ }
78
+ if (filter) {
79
+ filterColumns.push(fixedColumn);
80
+ }
81
+ if (toggle) {
82
+ toggleColumns.push(fixedColumn);
83
+ }
84
+ return fixedColumn;
85
+ });
86
+ return { columns: updatedColumns, searchColumns, excelColumns, filterColumns, toggleColumns };
87
+ };
63
88
  const DragColumns = UT.useDrag((dragIndex, dropIndex, reOrder) => {
64
89
  const newColumns = reOrder(props.columns || [], dragIndex, dropIndex);
65
90
  if (props.onChangeColumns) {
@@ -71,11 +96,9 @@ const AIOTable = (props) => {
71
96
  return sort.getValue;
72
97
  }
73
98
  return (row) => {
74
- const cellDetail = tableHook.getCellDetail({ column, row });
75
- const type = getColumnOption('type', cellDetail) || 'text';
76
- const value = getColumnOption('value', cellDetail);
77
- const cellValue = tableHook.getCellValue(cellDetail, value);
78
- if (type === 'date') {
99
+ const { type = 'text' } = column;
100
+ const cellValue = tableHook.getCellValue({ row, column, rowIndex: 0 });
101
+ if (type === 'date' || type === 'jalaliDate') {
79
102
  const DATE = new AIODate();
80
103
  try {
81
104
  return DATE.getTime(cellValue);
@@ -91,75 +114,26 @@ const AIOTable = (props) => {
91
114
  let sorts = [];
92
115
  for (let i = 0; i < columns.length; i++) {
93
116
  const column = columns[i];
94
- const cellDetail = tableHook.getCellDetail({ column });
95
- const columnId = getColumnOption('id', cellDetail);
96
- const sortValue = getColumnOption('sort', cellDetail);
117
+ const columnId = column.id;
118
+ const sortValue = column.sort;
97
119
  const sort = sortValue === true ? { sortId: columnId } : sortValue;
98
120
  if (!sort) {
99
121
  continue;
100
122
  }
101
123
  let { active = false, dir = 'dec', sortId } = sort;
102
- let sortItem = { dir, title: sort.title || getColumnOption('title', cellDetail), sortId, active, getValue: getGetValue(sort, column) };
124
+ let sortItem = { dir, title: sort.title || column.title, sortId, active, getValue: getGetValue(sort, column) };
103
125
  sorts.push(sortItem);
104
126
  }
105
127
  return sorts;
106
128
  };
107
- const sortHook = UT.useSort({
129
+ const sortHook = useSort({
108
130
  fa: props.fa,
109
131
  sorts: [],
110
132
  rows: propsRef.current.value,
111
133
  onChangeRows: props.onChange,
112
134
  onChangeSort: props.onChangeSort,
113
135
  });
114
- const filterHook = UT.useFilter({
115
- columns: filterColumns,
116
- filter: props.filter || { onChange: () => { } },
117
- fa: props.fa,
118
- columnOption: {
119
- text: (column) => getColumnOption('title', tableHook.getCellDetail({ column })),
120
- id: (column) => getColumnOption('id', tableHook.getCellDetail({ column })),
121
- type: (column) => getColumnOption('type', tableHook.getCellDetail({ column })) || 'text',
122
- options: (column) => {
123
- const filter = getColumnOption('filter', tableHook.getCellDetail({ column }));
124
- return filter === null || filter === void 0 ? void 0 : filter.options;
125
- }
126
- }
127
- });
128
- function getColumns() {
129
- let { columns = [] } = props;
130
- let searchColumns = [], excelColumns = [], filterColumns = [], toggleColumns = [];
131
- let updatedColumns = columns.map((column, i) => {
132
- const cellDetail = tableHook.getCellDetail({ column });
133
- const filter = getColumnOption('filter', cellDetail);
134
- const search = getColumnOption('search', cellDetail);
135
- const excel = getColumnOption('excel', cellDetail);
136
- const toggle = getColumnOption('toggle', cellDetail);
137
- const id = getColumnOption('id', cellDetail);
138
- if (id === undefined) {
139
- console.error(`aio-table error => missing column id in props.columns[${i}]`);
140
- }
141
- if (search) {
142
- searchColumns.push(column);
143
- }
144
- if (excel) {
145
- excelColumns.push(column);
146
- }
147
- if (filter) {
148
- filterColumns.push(column);
149
- }
150
- if (toggle) {
151
- toggleColumns.push(column);
152
- }
153
- return column;
154
- });
155
- setSearchColumns(searchColumns);
156
- setExcelColumns(excelColumns);
157
- setFilterColumns(filterColumns);
158
- setToggleColumns(toggleColumns);
159
- return updatedColumns;
160
- }
161
136
  useEffect(() => {
162
- const columns = getColumns();
163
137
  sortHook.setSorts(getSorts(columns));
164
138
  }, []);
165
139
  useEffect(() => {
@@ -196,16 +170,12 @@ const AIOTable = (props) => {
196
170
  }
197
171
  else {
198
172
  for (let rowIndex = 0; rowIndex < props.value.length; rowIndex++) {
199
- const isFirst = rowIndex === 0;
200
- const isLast = rowIndex === props.value.length - 1;
201
173
  let row = props.value[rowIndex], json = {};
202
174
  for (let j = 0; j < excelColumns.length; j++) {
203
175
  const column = excelColumns[j];
204
- const cellDetail = tableHook.getCellDetail({ row, rowIndex, isFirst, isLast, column });
205
- const excel = getColumnOption('excel', cellDetail);
176
+ const { excel, title } = column;
206
177
  let key = '';
207
178
  if (excel === true) {
208
- const title = getColumnOption('title', cellDetail);
209
179
  if (typeof title === 'string') {
210
180
  key = title;
211
181
  }
@@ -219,13 +189,12 @@ const AIOTable = (props) => {
219
189
  else {
220
190
  continue;
221
191
  }
222
- const value = getColumnOption('value', cellDetail);
223
- json[key] = tableHook.getCellValue(cellDetail, value, '');
192
+ json[key] = tableHook.getCellValue({ row, rowIndex, column });
224
193
  }
225
194
  list.push(json);
226
195
  }
227
196
  }
228
- UT.ExportToExcel(list, { promptText: typeof props.excel === 'string' ? props.excel : 'Inter Excel File Name' });
197
+ fileClass.ExportToExcel(list, { promptText: typeof props.excel === 'string' ? props.excel : 'Inter Excel File Name' });
229
198
  }
230
199
  function getSearchedRows(rows) {
231
200
  if (props.onSearch !== true) {
@@ -234,15 +203,11 @@ const AIOTable = (props) => {
234
203
  if (!searchColumns.length || !searchValue) {
235
204
  return rows;
236
205
  }
237
- const rowsIndexDic = getRowsIndexDic();
238
- return UT.Search(rows, searchValue, (row) => {
239
- const { isFirst, isLast, rowIndex } = rowsIndexDic[(row)._id];
206
+ return UT.Search(rows, searchValue, (row, rowIndex) => {
240
207
  let str = '';
241
208
  for (let i = 0; i < searchColumns.length; i++) {
242
209
  let column = searchColumns[i];
243
- const cellDetail = tableHook.getCellDetail({ row, rowIndex, isFirst, isLast, column });
244
- const value = getColumnOption('value', cellDetail);
245
- let cellValue = tableHook.getCellValue(cellDetail, value, '');
210
+ let cellValue = tableHook.getCellValue({ row, rowIndex, column });
246
211
  if (cellValue) {
247
212
  str += cellValue + ' ';
248
213
  }
@@ -252,11 +217,6 @@ const AIOTable = (props) => {
252
217
  }
253
218
  function getRows() {
254
219
  const rows = props.value;
255
- const rowsIndexDic = {};
256
- for (let i = 0; i < props.value.length; i++) {
257
- rowsIndexDic[props.value[i]._id] = { rowIndex: i, isFirst: i === 0, isLast: i === rows.length - 1 };
258
- }
259
- setRowsIndexDic(rowsIndexDic);
260
220
  let searchedRows = getSearchedRows(rows);
261
221
  let sortedRows = sortHook.getSortedRows(searchedRows);
262
222
  let pagedRows = pagingHook.getPagedRows(sortedRows);
@@ -271,15 +231,23 @@ const AIOTable = (props) => {
271
231
  }
272
232
  }
273
233
  let ROWS = getRows();
274
- const { gap = [0, 1] } = props;
275
- let attrs = UT.AddToAttrs(props.attrs, { className: ['aio-table', props.className], style: Object.assign({ gap: gap[1] }, props.style), attrs: { ref: dom } });
234
+ const { columns, searchColumns, excelColumns, filterColumns, toggleColumns } = useMemo(() => getColumns(), [props.columns]);
276
235
  const context = {
277
- rootProps: props, excelColumns, filterColumns, tableHook, sortHook, ROWS, toggleColumns, filterHook,
278
- getRowsIndexDic, add, remove, search, exportToExcel, DragColumns, getIcon, popup, getColumnOption
236
+ rootProps: props, excelColumns, filterColumns, tableHook, sortHook, ROWS, toggleColumns, pagingHook,
237
+ add, remove, search, exportToExcel, DragColumns, popup, columns
279
238
  };
280
- return (_jsxs(Provider, { value: context, children: [_jsxs("div", Object.assign({}, attrs, { children: [_jsx(TableToolbar, {}), !!props.filter && filterHook.renderTags(), _jsxs("div", { className: 'aio-table-unit aio-table-scroll', style: { gap: gap[1] }, children: [_jsx(TableHeader, {}), _jsx(TableRows, {})] }), pagingHook.render()] })), popup.render()] }));
239
+ return (_jsxs(Provider, { value: context, children: [_jsx(AIOTableComponent, Object.assign({}, props)), popup.render()] }));
281
240
  };
282
241
  export default AIOTable;
242
+ const AIOTableComponent = (props) => {
243
+ const { pagingHook, rootProps } = useProvider();
244
+ const { filter } = rootProps;
245
+ let [dom] = useState(createRef());
246
+ const { gap = [] } = props;
247
+ let attrs = UT.AddToAttrs(props.attrs, { className: ['aio-table', props.className], style: Object.assign({ gap: gap[1] }, props.style), attrs: { ref: dom } });
248
+ const filterHook = useFilter();
249
+ return (_jsxs("div", Object.assign({}, attrs, { children: [_jsx(TableToolbar, { filterHook: filterHook }), !!filter && filterHook.renderTags(), _jsxs("div", { className: 'aio-table-unit aio-table-scroll', style: { gap: gap[1] }, children: [_jsx(TableHeader, {}), _jsx(TableRows, {})] }), pagingHook.render()] })));
250
+ };
283
251
  const TableRows = () => {
284
252
  let { ROWS, rootProps } = useProvider();
285
253
  let { rowOption = {}, rowsTemplate, placeholder = 'there is not any items' } = rootProps;
@@ -293,11 +261,7 @@ const TableRows = () => {
293
261
  content = rows.map((o, i) => {
294
262
  let { id = 'ailr' + Math.round(Math.random() * 10000000) } = o;
295
263
  o._id = o._id === undefined ? id : o._id;
296
- const rowDetail = {
297
- row: o, rowIndex: i,
298
- isFirst: i === 0,
299
- isLast: i === rows.length - 1
300
- };
264
+ const rowDetail = { row: o, rowIndex: i };
301
265
  let Row;
302
266
  if (rowTemplate) {
303
267
  Row = rowTemplate(rowDetail);
@@ -314,56 +278,53 @@ const TableRows = () => {
314
278
  else {
315
279
  return null;
316
280
  }
317
- const { gap = [0, 1] } = rootProps;
281
+ const { gap = [] } = rootProps;
318
282
  return _jsx("div", { className: 'aio-table-rows', style: { gap: gap[1] }, children: content });
319
283
  };
320
- const TableToolbar = () => {
321
- let { add, exportToExcel, search, rootProps, excelColumns, toggleColumns, getIcon, sortHook, getColumnOption, tableHook, filterHook } = useProvider();
322
- let { toolbarAttrs, toolbar, onAdd, onSearch, value, onChangeColumns, columns = [], fa } = rootProps;
284
+ const TableToolbar = ({ filterHook }) => {
285
+ let { add, exportToExcel, search, rootProps, excelColumns, toggleColumns, sortHook, columns } = useProvider();
286
+ let { toolbarAttrs, toolbar, onAdd, onSearch, value, onChangeColumns, fa, filter, inlineSort } = rootProps;
323
287
  const columnsRef = useRef(columns);
324
288
  columnsRef.current = columns;
325
289
  toolbarAttrs = UT.AddToAttrs(toolbarAttrs, { className: 'aio-table-toolbar' });
326
- if (!onAdd && !toolbar && !onSearch && !sortHook.sorts.length && !excelColumns.length && !toggleColumns.length && !rootProps.filter) {
290
+ if (!onAdd && !toolbar && !onSearch && !sortHook.sorts.length && !excelColumns.length && !toggleColumns.length && !filter) {
327
291
  return null;
328
292
  }
329
293
  function getAddText() {
330
294
  let { addText } = rootProps;
331
295
  if (!rootProps.addText) {
332
- return getIcon('mdiPlusThick', 0.8);
296
+ return new UT.GetSvg('em').getIcon('mdiPlusThick', 0.8);
333
297
  }
334
298
  return typeof addText === 'function' ? addText(value) : addText;
335
299
  }
336
- const getColumnOptionWrapper = (columnId, key) => {
337
- const columns = columnsRef.current || [];
338
- const column = columns.find((o) => o.id === columnId);
339
- return getColumnOption(key, tableHook.getCellDetail({ column }));
340
- };
341
- return (_jsxs("div", Object.assign({}, toolbarAttrs, { children: [toolbar && _jsx("div", { className: 'aio-table-toolbar-content', children: typeof toolbar === 'function' ? toolbar() : toolbar }), !!rootProps.filter && filterHook.renderButton({ className: 'aio-table-toolbar-button' }), !!onSearch && _jsx(AIOInput, { type: 'text', className: 'aio-table-search', onChange: (value) => search(value), after: getIcon('mdiMagnify', 0.7) }), !!sortHook.sorts.length && sortHook.renderSortButton({ className: 'aio-table-toolbar-button aio-table-sort-button' }), !!excelColumns.length && _jsx("div", { className: 'aio-table-toolbar-button aio-table-excel-button', onClick: () => exportToExcel(), children: getIcon('mdiFileExcel', 0.8) }), !!toggleColumns.length &&
300
+ return (_jsxs("div", Object.assign({}, toolbarAttrs, { children: [toolbar && _jsx("div", { className: 'aio-table-toolbar-content', children: typeof toolbar === 'function' ? toolbar() : toolbar }), !!filter && filterHook.renderButton(), !!onSearch && _jsx(AIOInput, { type: 'text', className: 'aio-table-search', onChange: (value) => search(value), after: new UT.GetSvg('em').getIcon('mdiMagnify', 0.7) }), !!sortHook.sorts.length && !inlineSort && sortHook.renderSortButton({ className: 'aio-table-toolbar-button aio-table-sort-button' }), !!excelColumns.length && _jsx("div", { className: 'aio-table-toolbar-button aio-table-excel-button', onClick: () => exportToExcel(), children: new UT.GetSvg('em').getIcon('mdiFileExcel', 0.8) }), !!toggleColumns.length &&
342
301
  _jsx(AISelect, { popover: {
343
302
  header: { title: fa ? 'نمایش ستون ها' : 'Show Columns', onClose: false },
344
- setAttrs: (key) => { if (key === 'header') {
345
- return { className: 'aio-table-dropdown-header' };
346
- } },
303
+ headerAttrs: { className: 'aio-table-dropdown-header' },
347
304
  limitTo: '.aio-table'
348
- }, className: 'aio-table-toolbar-button aio-table-toggle-button', text: getIcon('mdiEye', 0.8), justify: true, hideTags: true, options: toggleColumns.map((o) => (Object.assign(Object.assign({}, o), { show: true }))), caret: false, option: {
349
- text: (column) => getColumnOptionWrapper(column.id, 'title'),
350
- checked: (column) => getColumnOptionWrapper(column.id, 'show'),
305
+ }, className: 'aio-table-toolbar-button aio-table-toggle-button', text: new UT.GetSvg('em').getIcon('mdiEye', 0.8), justify: true, hideTags: true, options: toggleColumns.map((o) => (Object.assign({}, o))), caret: false, option: {
306
+ text: (column) => column.title,
307
+ checked: (column) => !!column.show,
351
308
  onClick: (column) => {
309
+ console.log(toggleColumns);
352
310
  if (!onChangeColumns) {
353
311
  return;
354
312
  }
355
313
  const columns = columnsRef.current || [];
356
- onChangeColumns(columns.map((o) => column.id === o.id ? Object.assign(Object.assign({}, o), { show: !getColumnOptionWrapper(o.id, 'show') }) : o));
314
+ const newColumns = columns.map((o) => column.id === o.id ? Object.assign(Object.assign({}, o), { show: !o.show }) : o);
315
+ onChangeColumns(newColumns);
357
316
  },
317
+ show: () => true,
318
+ justify: () => false,
358
319
  close: () => false
359
320
  } }), !!onAdd && _jsx("div", { className: 'aio-table-toolbar-button aio-table-add-button', onClick: () => add(), children: getAddText() })] })));
360
321
  };
361
322
  const TableHeader = () => {
362
- let { rootProps, getColumnOption, tableHook } = useProvider();
363
- let { headerAttrs, onRemove, gap = [0, 1], columns = [] } = rootProps;
323
+ let { rootProps, columns } = useProvider();
324
+ let { headerAttrs, onRemove, gap = [] } = rootProps;
364
325
  headerAttrs = UT.AddToAttrs(headerAttrs, { className: 'aio-table-header', style: { gap: gap[0] } });
365
326
  let Titles = columns.map((column, i) => {
366
- const columnId = getColumnOption('id', tableHook.getCellDetail({ column }));
327
+ const columnId = column.id;
367
328
  return _jsx(TableTitle, { column: column, isLast: i === columns.length - 1, colIndex: i }, columnId);
368
329
  });
369
330
  let RemoveTitle = !onRemove ? null : _jsx("div", { className: 'aio-table-remove-title' });
@@ -371,90 +332,91 @@ const TableHeader = () => {
371
332
  };
372
333
  const TableTitle = (p) => {
373
334
  const { column, colIndex } = p;
374
- let { tableHook, DragColumns, getColumnOption } = useProvider();
375
- const cellDetails = tableHook.getCellDetail({ column });
376
- if (!getColumnOption('show', cellDetails)) {
335
+ let { tableHook, DragColumns, sortHook, rootProps } = useProvider();
336
+ const { show = true, title } = column;
337
+ if (!show) {
377
338
  return null;
378
339
  }
379
- const title = getColumnOption('title', cellDetails);
380
340
  const attrs = Object.assign(Object.assign(Object.assign({}, tableHook.getTitleAttrs(column)), DragColumns.getDragAttrs(colIndex)), DragColumns.getDropAttrs(colIndex));
381
- return _jsx("div", Object.assign({}, attrs, { children: title }));
341
+ return (_jsxs("div", Object.assign({}, attrs, { children: [title, !!rootProps.inlineSort && sortHook.renderSortArrow(sortHook.getSortByColumn(column), true)] })));
382
342
  };
383
343
  const TableRow = (props) => {
384
344
  const { rowDetail } = props;
385
345
  const { row, rowIndex } = rowDetail;
386
346
  const rowId = row._id;
387
- let { remove, rootProps, tableHook, getIcon, getColumnOption } = useProvider();
388
- const { columns = [] } = rootProps;
389
- const isOdd = rowIndex % 2 === 0;
347
+ let { remove, rootProps, tableHook, columns } = useProvider();
390
348
  function getCells() {
391
349
  return columns.map((column) => {
392
- if (!getColumnOption('show', tableHook.getCellDetail(Object.assign(Object.assign({}, rowDetail), { column })))) {
350
+ const { show = true } = column;
351
+ if (!show) {
393
352
  return null;
394
353
  }
395
- const cellDetail = tableHook.getCellDetail(Object.assign(Object.assign({}, rowDetail), { column, change: (v) => tableHook.changeCell(tableHook.getCellDetail(Object.assign(Object.assign({}, rowDetail), { column })), v) }));
396
- const columnId = getColumnOption('id', cellDetail);
354
+ const columnId = column.id;
397
355
  const key = rowId + ' ' + columnId;
398
- const value = getColumnOption('value', cellDetail);
399
- const cellValue = tableHook.getCellValue(cellDetail, value);
400
- return (_jsx(TableCell, { cellDetail: cellDetail, cellValue: cellValue, isOdd: isOdd }, key));
356
+ return (_jsx(TableCell, { row: row, rowIndex: rowIndex, column: column }, key));
401
357
  });
402
358
  }
403
359
  let { onRemove } = rootProps;
404
- return (_jsx(_Fragment, { children: _jsxs("div", Object.assign({}, tableHook.getRowAttrs(props.rowDetail, isOdd), { children: [getCells(), onRemove ? _jsx("button", { className: 'aio-table-remove', onClick: () => remove(row, rowIndex), children: getIcon('mdiClose', 0.8) }) : null] }), rowId) }));
360
+ return (_jsx(_Fragment, { children: _jsxs("div", Object.assign({}, tableHook.getRowAttrs({ row, rowIndex }), { children: [getCells(), onRemove ? _jsx("button", { className: 'aio-table-remove', onClick: () => remove(row, rowIndex), children: new UT.GetSvg('em').getIcon('mdiClose', 0.8) }) : null] }), rowId) }));
405
361
  };
406
362
  const TableCell = (props) => {
407
- let { cellDetail, cellValue, isOdd } = props;
408
- const { row, column } = cellDetail;
409
- const { tableHook, rootProps, getColumnOption } = useProvider();
410
- const { columnOption } = rootProps;
411
- const type = getColumnOption('type', cellDetail);
412
- if (type === 'number') {
413
- cellValue = UT.SplitNumber(cellValue);
414
- }
415
- const before = getColumnOption('before', cellDetail);
416
- const after = getColumnOption('after', cellDetail);
417
- const subtext = getColumnOption('subtext', cellDetail);
363
+ var _a, _b;
364
+ let { row, column, rowIndex } = props;
365
+ const { tableHook, rootProps } = useProvider();
366
+ const { templates } = rootProps;
367
+ const { type = 'text', id } = column;
368
+ let cellValue = tableHook.getCellValue({ row, rowIndex, column });
369
+ if (type === 'date') {
370
+ cellValue = new AIODate().toObject(cellValue);
371
+ }
372
+ else if (type === 'jalaliDate') {
373
+ cellValue = new AIODate().toObject(cellValue, true);
374
+ }
418
375
  const rowId = row._id;
419
- const colId = getColumnOption('id', cellDetail);
376
+ const colId = id;
420
377
  const getTemplateValue = () => {
421
- const { getValue = {} } = rootProps;
422
- if (typeof column.template === 'function') {
423
- return column.template(cellDetail);
424
- }
425
- if (typeof column.template === 'string' && getValue[column.template]) {
426
- return getValue[column.template](cellDetail);
427
- }
428
- if ((columnOption || {}).template) {
429
- return columnOption.template(cellDetail);
430
- }
431
- const pattern = getColumnOption('pattern', cellDetail);
432
- if (pattern && cellValue) {
378
+ if (column.type === 'date') {
433
379
  try {
434
- if (type === 'date') {
435
- return new AIODate().getDateByPattern(cellValue, pattern);
436
- }
437
- else {
438
- return pattern.replace('{value}', cellValue);
439
- }
380
+ cellValue = new AIODate().toObject(cellValue);
440
381
  }
441
382
  catch (_a) { }
442
383
  }
384
+ else if (column.type === 'jalaliDate') {
385
+ try {
386
+ cellValue = new AIODate().toObject(cellValue, true);
387
+ }
388
+ catch (_b) { }
389
+ }
390
+ const cellTemplateValue = tableHook.getCellTemplateValue({ row, column, rowIndex, key: 'template' });
391
+ if (cellTemplateValue !== undefined) {
392
+ return cellTemplateValue;
393
+ }
394
+ return tableHook.getCellPatternValue({ row, column, rowIndex });
443
395
  };
444
396
  const templateValue = getTemplateValue();
445
- const beforeValue = tableHook.getCellValue(cellDetail, before, undefined);
446
- const afterValue = tableHook.getCellValue(cellDetail, after, undefined);
447
- const subtextValue = tableHook.getCellValue(cellDetail, subtext, undefined);
397
+ const before = tableHook.getCellTemplateValue({ row, column, rowIndex, key: 'before' });
398
+ const after = tableHook.getCellTemplateValue({ row, column, rowIndex, key: 'after' });
399
+ const subtext = tableHook.getCellTemplateValue({ row, column, rowIndex, key: 'subtext' });
448
400
  let content;
401
+ if (column.template) {
402
+ content = tableHook.getCellTemplateValue({ row, column, rowIndex, key: 'template' });
403
+ }
404
+ else if (column.pattern) {
405
+ content = tableHook.getCellPatternValue({ row, column, rowIndex });
406
+ }
449
407
  if (templateValue !== undefined) {
450
408
  content = templateValue;
451
409
  }
410
+ else if (column.type === 'number') {
411
+ content = UT.SplitNumber(cellValue);
412
+ }
452
413
  else {
453
414
  content = cellValue;
454
415
  }
455
- return (_jsx(Fragment, { children: _jsxs("div", Object.assign({}, tableHook.getCellAttrs(props.cellDetail, cellValue, isOdd, rootProps.striped), { children: [beforeValue !== undefined && _jsx("div", { className: "aio-table-cell-before", children: beforeValue }), _jsx("div", { className: `aio-table-cell-value${subtext !== undefined ? ' has-subtext' : ''}`, "data-subtext": subtextValue, children: content }), afterValue !== undefined && _jsx("div", { className: "aio-table-cell-after", children: afterValue })] })) }, rowId + ' ' + colId));
416
+ content = content !== undefined && content !== null && content !== '' ? _jsxs(_Fragment, { children: [(_a = column.prefix) !== null && _a !== void 0 ? _a : null, content, (_b = column.affix) !== null && _b !== void 0 ? _b : null] }) : content;
417
+ return (_jsx(Fragment, { children: _jsxs("div", Object.assign({}, tableHook.getCellAttrs({ row, column, rowIndex }), { children: [before !== undefined && _jsx("div", { className: "aio-table-cell-before", children: before }), _jsx("div", { className: `aio-table-cell-value${subtext !== undefined ? ' has-subtext' : ''}`, "data-subtext": subtext, children: content }), after !== undefined && _jsx("div", { className: "aio-table-cell-after", children: after })] })) }, rowId + ' ' + colId));
456
418
  };
457
- const useTable = (getProps, getPaging, getColumnOption) => {
419
+ const useTable = (getProps, getPaging) => {
458
420
  const DragRows = UT.useDrag((dragIndex, dropIndex, reOrder) => {
459
421
  const { onSwap, onChange, value: rows } = getProps();
460
422
  const newRows = reOrder(rows, dragIndex, dropIndex);
@@ -467,95 +429,823 @@ const useTable = (getProps, getPaging, getColumnOption) => {
467
429
  onChange(newRows);
468
430
  }
469
431
  }, 'aio-table-row');
470
- const getCellValue = (cellDetail, cellValue, def) => {
471
- const { getValue = {} } = getProps();
432
+ const fixIndexByPaging = (rowIndex) => {
472
433
  const paging = getPaging();
473
434
  if (paging) {
474
435
  let { number, size } = paging;
475
- cellDetail = Object.assign(Object.assign({}, cellDetail), { rowIndex: cellDetail.rowIndex + ((number - 1) * size) });
436
+ return rowIndex + ((number - 1) * size);
476
437
  }
477
- let type = typeof cellValue;
478
- if (type === 'string') {
479
- const { row } = cellDetail;
480
- if (row) { }
481
- let result = cellValue;
482
- if (getValue[cellValue]) {
483
- result = getValue[cellValue](cellDetail);
484
- }
485
- else if (cellValue.indexOf('row.') !== -1) {
438
+ return rowIndex;
439
+ };
440
+ const getCellValue = (p) => {
441
+ let { row, column, rowIndex } = p;
442
+ if (typeof column.value === 'string') {
443
+ let result = column.value;
444
+ if (result.indexOf('row.') !== -1) {
486
445
  try {
487
- eval(`result = ${cellValue}`);
488
- }
489
- catch (_a) {
490
- result = '';
446
+ eval(`result = ${result}`);
491
447
  }
448
+ catch (_a) { }
449
+ }
450
+ return result;
451
+ }
452
+ if (typeof column.value === 'function') {
453
+ return column.value({ row, rowIndex, column });
454
+ }
455
+ if (typeof column.value === 'undefined') {
456
+ const { columnOption } = getProps();
457
+ if (columnOption === null || columnOption === void 0 ? void 0 : columnOption.value) {
458
+ return columnOption.value({ row, column, rowIndex: fixIndexByPaging(rowIndex) });
459
+ }
460
+ }
461
+ };
462
+ const getTemplateParam = (p) => {
463
+ const value = getCellValue(p);
464
+ let dateObject = { year: 0, month: 0, day: 0, hour: 0, minute: 0, second: 0 };
465
+ if (p.column.type === 'date') {
466
+ try {
467
+ dateObject = new AIODate().toObject(value);
492
468
  }
493
- return result === undefined ? def : result;
469
+ catch (_a) { }
470
+ }
471
+ else if (p.column.type === 'jalaliDate') {
472
+ try {
473
+ dateObject = new AIODate().toObject(value, true);
474
+ }
475
+ catch (_b) { }
476
+ }
477
+ const rowIndex = fixIndexByPaging(p.rowIndex);
478
+ const change = (newRow) => changeCell(p.row, newRow);
479
+ return { value, dateObject, rowIndex, row: p.row, column: p.column, change };
480
+ };
481
+ const getCellTemplateValue = (p) => {
482
+ let { row, rowIndex, column, key } = p;
483
+ const { templates } = getProps();
484
+ if (!column[key]) {
485
+ return;
494
486
  }
495
- if (type === 'undefined') {
496
- return def;
487
+ const templateParam = getTemplateParam({ row, column, rowIndex });
488
+ if (typeof column[key] === 'function') {
489
+ const fn = column[key];
490
+ return fn(templateParam);
497
491
  }
498
- if (type === 'function') {
499
- return cellValue(cellDetail);
492
+ if (typeof column[key] === 'string') {
493
+ if (templates && templates[column[key]]) {
494
+ const fn = templates[column[key]];
495
+ return fn(templateParam);
496
+ }
497
+ else {
498
+ return column[key];
499
+ }
500
+ }
501
+ };
502
+ const getCellPatternValue = (p) => {
503
+ let { row, rowIndex, column } = p;
504
+ const cellValue = getCellValue({ row, column, rowIndex });
505
+ const { pattern } = column;
506
+ if (pattern && cellValue !== undefined) {
507
+ try {
508
+ if (column.type === 'date') {
509
+ return new AIODate().getDateByPattern(cellValue, pattern);
510
+ }
511
+ else if (column.type === 'jalaliDate') {
512
+ return new AIODate().getDateByPattern(cellValue, pattern, true);
513
+ }
514
+ else {
515
+ return pattern.replace('{value}', cellValue);
516
+ }
517
+ }
518
+ catch (_a) { }
500
519
  }
501
- return cellValue === undefined ? def : cellValue;
502
520
  };
503
- function changeCell(cellDetail, newRow) {
521
+ function changeCell(row, newRow) {
504
522
  if (newRow) { }
505
523
  const { onChange, value: propsValue } = getProps();
506
524
  if (!onChange) {
507
525
  return;
508
526
  }
509
- const rowId = cellDetail.row._id;
527
+ const rowId = row._id;
510
528
  onChange(propsValue.map((o) => o._id !== rowId ? o : newRow));
511
529
  }
512
- function getCellDetail(v) {
513
- const res = Object.assign({ column: {}, row: {}, rowIndex: 0, isFirst: false, isLast: false, date: [], change: () => { } }, v);
514
- const value = getColumnOption('value', res);
515
- const jalali = getColumnOption('jalali', res);
516
- const date = res.column.type === 'date' ? new AIODate().toArray(getCellValue(res, value), jalali) : [];
517
- return Object.assign(Object.assign(Object.assign({}, res), { date }), v);
518
- }
519
- const getCellAttrs = (cellDetail, cellValue, isOdd, striped) => {
520
- const { column } = cellDetail;
521
- const attrs = getCellValue(cellDetail, column.attrs, {});
522
- const justify = getColumnOption('justify', cellDetail);
523
- const width = getColumnOption('width', cellDetail);
524
- const minWidth = getColumnOption('minWidth', cellDetail);
530
+ const getCellAttrs = (p) => {
531
+ const { row, rowIndex, column } = p;
532
+ const { cellAttrs } = getProps();
533
+ const cellValue = getCellValue({ row, column, rowIndex });
534
+ const attrs = cellAttrs ? cellAttrs({ row, rowIndex: fixIndexByPaging(rowIndex), column, value: cellValue }) : {};
535
+ const { justify, width, minWidth } = column;
525
536
  const className = `aio-table-cell` + (justify ? ` aio-table-cell-justify` : '');
526
537
  let isNumber = false;
527
538
  try {
528
- if (!isNaN(+cellValue)) {
539
+ if (typeof cellValue === 'number' || column.type === 'number') {
540
+ isNumber = true;
541
+ }
542
+ else if (typeof cellValue === 'string' && !isNaN(+cellValue)) {
529
543
  isNumber = true;
530
544
  }
531
545
  }
532
546
  catch (_a) { }
533
547
  const style = { width, minWidth, flex: width ? undefined : 1, direction: isNumber ? 'ltr' : undefined };
534
- if (striped) {
535
- style.background = isOdd ? striped[0] : striped[1];
536
- }
537
- return UT.AddToAttrs(attrs, { className: [className, isOdd ? 'aio-table-cell-odd' : 'aio-table-cell-even'], style, attrs: { title: typeof cellValue === 'string' ? cellValue : undefined } });
548
+ return UT.AddToAttrs(attrs, { className: [className, rowIndex % 2 === 0 ? 'aio-table-cell-odd' : 'aio-table-cell-even'], style, attrs: { title: typeof cellValue === 'string' ? cellValue : undefined } });
538
549
  };
539
550
  const getTitleAttrs = (column) => {
540
- const cellDetail = getCellDetail({ column });
541
- const attrs = getColumnOption('titleAttrs', cellDetail);
542
- const justify = getColumnOption('justify', cellDetail);
543
- const width = getColumnOption('width', cellDetail);
544
- const minWidth = getColumnOption('minWidth', cellDetail);
551
+ const { justify, width, minWidth, title } = column;
552
+ const { titleAttrs } = getProps();
553
+ const attrs = titleAttrs ? titleAttrs(column) : {};
545
554
  const className = `aio-table-title` + (justify ? ` aio-table-title-justify` : '');
546
555
  const style = { width, minWidth, flex: width ? undefined : 1 };
547
- const title = getColumnOption('title', cellDetail);
548
556
  return UT.AddToAttrs(attrs, { className, style, attrs: { title: typeof title === 'string' ? title : undefined } });
549
557
  };
550
- const getRowAttrs = (rowDetail, isOdd) => {
551
- const { rowOption = {}, onSwap, gap = [0, 1] } = getProps();
558
+ const getRowAttrs = (p) => {
559
+ const { row, rowIndex } = p;
560
+ const { rowOption = {}, onSwap, gap = [] } = getProps();
552
561
  const { attrs: rowAttrs } = rowOption;
553
- const attrs = rowAttrs ? rowAttrs(rowDetail) : {};
554
- let obj = UT.AddToAttrs(attrs, { className: ['aio-table-row', isOdd ? 'aio-table-row-odd' : 'aio-table-row-even'], style: { gap: gap[0] } });
562
+ const attrs = rowAttrs ? rowAttrs({ row, rowIndex }) : {};
563
+ let obj = UT.AddToAttrs(attrs, { className: ['aio-table-row', rowIndex % 2 === 0 ? 'aio-table-row-odd' : 'aio-table-row-even'], style: { gap: gap[0] } });
555
564
  if (onSwap) {
556
- obj = Object.assign(Object.assign(Object.assign({}, obj), DragRows.getDragAttrs(rowDetail.rowIndex)), DragRows.getDropAttrs(rowDetail.rowIndex));
565
+ obj = Object.assign(Object.assign(Object.assign({}, obj), DragRows.getDragAttrs(rowIndex)), DragRows.getDropAttrs(rowIndex));
557
566
  }
558
567
  return obj;
559
568
  };
560
- return { getCellValue, getCellAttrs, getTitleAttrs, getRowAttrs, changeCell, getCellDetail };
569
+ return { getCellValue, getCellTemplateValue, getCellPatternValue, getCellAttrs, getTitleAttrs, getRowAttrs, changeCell };
570
+ };
571
+ export const useSort = (p) => {
572
+ let [sorts, setSorts] = useState(p.sorts);
573
+ const isInitSortExecutedRef = useRef(false);
574
+ const getSortedRows = (rows) => {
575
+ if (isInitSortExecutedRef.current) {
576
+ return rows;
577
+ }
578
+ if (p.onChangeSort) {
579
+ return rows;
580
+ }
581
+ let activeSorts = sorts.filter((sort) => sort.active !== false);
582
+ if (!activeSorts.length || !rows.length) {
583
+ return rows;
584
+ }
585
+ isInitSortExecutedRef.current = true;
586
+ let sortedRows = sortRows(rows, activeSorts);
587
+ if (p.onChangeRows) {
588
+ p.onChangeRows(sortedRows);
589
+ }
590
+ return sortedRows;
591
+ };
592
+ const sortRows = (rows, sorts) => {
593
+ if (!rows) {
594
+ return [];
595
+ }
596
+ if (!sorts || !sorts.length) {
597
+ return rows;
598
+ }
599
+ return rows.sort((a, b) => {
600
+ for (let i = 0; i < sorts.length; i++) {
601
+ let { dir, getValue } = sorts[i];
602
+ if (!getValue) {
603
+ return 0;
604
+ }
605
+ let aValue = getValue(a), bValue = getValue(b);
606
+ if (aValue < bValue) {
607
+ return -1 * (dir === 'dec' ? -1 : 1);
608
+ }
609
+ if (aValue > bValue) {
610
+ return 1 * (dir === 'dec' ? -1 : 1);
611
+ }
612
+ if (i === sorts.length - 1) {
613
+ return 0;
614
+ }
615
+ }
616
+ return 0;
617
+ });
618
+ };
619
+ const changeSort = (sortId, changeObject) => {
620
+ let newSorts = sorts.map((sort) => {
621
+ if (sort.sortId === sortId) {
622
+ let newSort = Object.assign(Object.assign({}, sort), changeObject);
623
+ return newSort;
624
+ }
625
+ return sort;
626
+ });
627
+ changeSorts(newSorts);
628
+ };
629
+ const changeSorts = (sorts) => __awaiter(void 0, void 0, void 0, function* () {
630
+ if (p.onChangeSort) {
631
+ let res = yield p.onChangeSort(sorts);
632
+ if (res !== false) {
633
+ setSorts(sorts);
634
+ }
635
+ }
636
+ else {
637
+ setSorts(sorts);
638
+ let activeSorts = sorts.filter((sort) => sort.active !== false);
639
+ if (activeSorts.length && !!p.onChangeRows) {
640
+ p.onChangeRows(sortRows(p.rows, activeSorts));
641
+ }
642
+ }
643
+ });
644
+ const renderSortArrow = (option, inline) => {
645
+ if (option === false) {
646
+ return null;
647
+ }
648
+ let { dir = 'dec', sortId, active } = option;
649
+ let path = dir === 'dec' ? 'mdiArrowDown' : 'mdiArrowUp';
650
+ if (inline && !active) {
651
+ path = 'mdiSortVariantOff';
652
+ }
653
+ return (_jsx("div", { className: 'aio-sort-arrow', onClick: (e) => {
654
+ e.stopPropagation();
655
+ if (!sortId) {
656
+ return;
657
+ }
658
+ let newActive = active, newDir = dir;
659
+ if (inline) {
660
+ if (active) {
661
+ if (dir === 'inc') {
662
+ newDir = 'dec';
663
+ newActive = true;
664
+ }
665
+ else {
666
+ newActive = false;
667
+ }
668
+ }
669
+ else {
670
+ newDir = 'inc';
671
+ newActive = true;
672
+ }
673
+ }
674
+ else {
675
+ newDir = dir === 'dec' ? 'inc' : 'dec';
676
+ }
677
+ changeSort(sortId, { dir: newDir, active: newActive });
678
+ }, children: new UT.GetSvg('em').getIcon(path, 0.8) }));
679
+ };
680
+ const getSortByColumn = (column) => {
681
+ const sort = column.sort;
682
+ if (!sort) {
683
+ return false;
684
+ }
685
+ const sortId = sort === true ? column.id : (sort.sortId === undefined ? column.id : sort.sortId);
686
+ const res = sorts.find((o) => o.sortId === sortId);
687
+ return res === undefined ? false : res;
688
+ };
689
+ const renderSortButton = (attrs, limitTo) => {
690
+ if (!sorts.length) {
691
+ return null;
692
+ }
693
+ const Attrs = UT.AddToAttrs(attrs, { className: 'aio-sort-button' });
694
+ return (_jsx(AISelect, { caret: false, options: sorts, justify: true, option: {
695
+ text: (option) => option.title, checked: (option) => !!option.active, close: () => false, value: (option) => option.sortId,
696
+ after: (option) => renderSortArrow(option),
697
+ onClick: (option) => changeSort(option.sortId, { active: !option.active })
698
+ }, popover: {
699
+ header: { title: p.fa ? 'مرتب سازی' : 'Sort', onClose: false },
700
+ headerAttrs: { className: 'aio-sort-header' },
701
+ limitTo: limitTo || '.aio-table'
702
+ }, attrs: Attrs, text: new UT.GetSvg('em').getIcon('mdiSort', 0.7), onSwap: (newSorts) => changeSorts(newSorts) }, 'sortbutton'));
703
+ };
704
+ return { sorts, setSorts, renderSortButton, getSortedRows, changeSort, changeSorts, renderSortArrow, getSortByColumn };
705
+ };
706
+ const usePaging = (p) => {
707
+ const timeoutRef = useRef(undefined);
708
+ const startRef = useRef(undefined);
709
+ const endRef = useRef(undefined);
710
+ const pagesRef = useRef(undefined);
711
+ const getPaging = (paging) => {
712
+ return paging ? fix(paging) : undefined;
713
+ };
714
+ function fix(paging) {
715
+ if (typeof p.onChange !== 'function') {
716
+ alert('aio-table error => in type table you set paging but forget to set onChangePaging function prop to aio input');
717
+ return { number: 0, size: 0 };
718
+ }
719
+ let { number, size = 20, length = 0, sizes = [1, 5, 10, 15, 20, 30, 50, 70, 100], serverSide } = paging;
720
+ if (!serverSide) {
721
+ length = p.rows.length;
722
+ }
723
+ if (sizes.indexOf(size) === -1) {
724
+ size = sizes[0];
725
+ }
726
+ let pages = Math.ceil(length / size);
727
+ number = number > pages ? pages : number;
728
+ number = number < 1 ? 1 : number;
729
+ let start = number - 3, end = number + 3;
730
+ startRef.current = start;
731
+ endRef.current = end;
732
+ pagesRef.current = pages;
733
+ return Object.assign(Object.assign({}, paging), { length, number, size, sizes });
734
+ }
735
+ const changePaging = (obj) => {
736
+ const paging = getPaging(p.paging);
737
+ if (!paging) {
738
+ return;
739
+ }
740
+ let newPaging = fix(Object.assign(Object.assign({}, paging), obj));
741
+ if (p.onChange) {
742
+ if (newPaging.serverSide) {
743
+ clearTimeout(timeoutRef.current);
744
+ timeoutRef.current = setTimeout(() => {
745
+ //be khatere fahme payine typescript majbooram dobare in shart ro bezanam
746
+ if (p.onChange) {
747
+ p.onChange(newPaging);
748
+ }
749
+ }, 800);
750
+ }
751
+ else {
752
+ p.onChange(newPaging);
753
+ }
754
+ }
755
+ };
756
+ const getPagedRows = (rows) => {
757
+ const paging = getPaging(p.paging);
758
+ if (!paging || paging.serverSide) {
759
+ return rows;
760
+ }
761
+ const { size, number } = paging;
762
+ return rows.slice((number - 1) * size, number * size);
763
+ };
764
+ function changeSizeButton(sizes, size) {
765
+ if (!sizes.length) {
766
+ return null;
767
+ }
768
+ return (_jsx(AISelect, { attrs: { className: 'aio-paging-button aio-paging-size' }, value: size, options: sizes, option: { text: 'option', value: 'option' }, justify: true, onChange: (value) => changePaging({ size: value }), popover: { fitHorizontal: true } }));
769
+ }
770
+ const render = () => {
771
+ const paging = getPaging(p.paging);
772
+ if (!paging) {
773
+ return null;
774
+ }
775
+ if (!p.rows.length) {
776
+ return null;
777
+ }
778
+ let { number, size, sizes } = paging;
779
+ let buttons = [];
780
+ let isFirst = true;
781
+ for (let i = startRef.current; i <= endRef.current; i++) {
782
+ if (i < 1 || i > pagesRef.current) {
783
+ buttons.push(_jsx("button", { className: 'aio-paging-button aio-paging-button-hidden', children: i }, i));
784
+ }
785
+ else {
786
+ let index;
787
+ if (isFirst) {
788
+ index = 1;
789
+ isFirst = false;
790
+ }
791
+ else if (i === Math.min(endRef.current, pagesRef.current)) {
792
+ index = pagesRef.current;
793
+ }
794
+ else {
795
+ index = i;
796
+ }
797
+ buttons.push(_jsx("button", { className: 'aio-paging-button' + (index === number ? ' active' : ''), onClick: () => changePaging({ number: index }), children: index }, index));
798
+ }
799
+ }
800
+ return (_jsxs("div", { className: 'aio-paging', children: [buttons, changeSizeButton(sizes || [], size)] }));
801
+ };
802
+ return { render, getPagedRows, changePaging };
803
+ };
804
+ const FilterContext = createContext({});
805
+ const FilterContextProvider = (props) => _jsx(FilterContext.Provider, { value: props.value, children: props.children });
806
+ const useFilterContext = () => useContext(FilterContext);
807
+ const useFilter = () => {
808
+ const { rootProps } = useProvider();
809
+ const { filter } = rootProps;
810
+ const { onChange } = filter || {};
811
+ const trans = (key) => {
812
+ const { fa } = rootProps;
813
+ const dic = {
814
+ 'less': fa ? 'کوچک تر از' : 'less than',
815
+ 'lessequal': fa ? 'کوچک تر مساوی' : 'equal or less than',
816
+ 'more': fa ? 'بزرگتر از' : 'more than',
817
+ 'moreequal': fa ? 'بزرگتر مساوی' : 'equal or more than',
818
+ 'equal': fa ? 'برابر' : 'equal',
819
+ 'notEqual': fa ? 'مخالف' : 'not equal',
820
+ 'contain': fa ? 'شامل باشد' : 'contain',
821
+ 'notContain': fa ? 'شامل نباشد' : 'not contain',
822
+ 'add': fa ? 'افزودن' : 'Add',
823
+ 'save': fa ? 'ذخیره' : 'Save',
824
+ 'edit': fa ? 'ویرایش' : 'Edit',
825
+ 'filters': fa ? 'افزودن فیلتر' : 'Add Filter',
826
+ 'remove': fa ? 'حذف' : 'Remove',
827
+ 'activate': fa ? 'اعمال' : 'Activate',
828
+ 'saved filters': fa ? 'مجموعه فیلتر های ذخیره شده' : 'Saved Filters',
829
+ 'current filters': fa ? 'فیلتر های جاری' : 'Current Filter',
830
+ 'remove saved filter': fa ? 'حذف فیلتر ذخیره شده' : 'Remove saved filter',
831
+ 'remove confirm': fa ? 'از حذف این آیتم اطمینان دارید؟' : 'are you sure to remove this item?',
832
+ 'cansel': fa ? 'لغو' : 'Cansel',
833
+ 'activate saved filter': fa ? 'اعمال فیلتر ذخیره شده' : 'activate saved filter',
834
+ 'activate confirm': fa ? 'از فعالسازی این آیتم اطمینان دارید؟' : 'are you sure to activate this item?',
835
+ 'saved empty': fa ? "مجموعه فیلتر ذخیره شده ای موجود نیست" : "There is not any saved filters",
836
+ 'inter filter name': fa ? 'نام فیلتر را برای ذخیره وارد کنید' : 'please inter filter name',
837
+ 'save filter': fa ? 'ذخیره فیلتر' : 'Save Filter'
838
+ };
839
+ return dic[key];
840
+ };
841
+ const popup = usePopup({ rtl: !!rootProps.fa });
842
+ const filterDataHook = useFilterData(popup, trans);
843
+ const renderButton = (attrs) => {
844
+ return (_jsxs(FilterContextProvider, { value: { trans, popup, filterDataHook, rootProps }, children: [_jsx(FilterButton, { attrs: attrs }), popup.render()] }));
845
+ };
846
+ const renderTags = () => {
847
+ return (_jsxs(FilterContextProvider, { value: { trans, popup, filterDataHook, rootProps }, children: [_jsx(FilterColumnsTags, { filterItems: filterDataHook.getData(), remove: true }), popup.render()] }));
848
+ };
849
+ return { renderButton, renderTags };
850
+ };
851
+ const FilterButton = ({ attrs }) => {
852
+ const { popup, trans, rootProps } = useFilterContext();
853
+ const { filter = {} } = rootProps;
854
+ const { modalAttrs } = filter;
855
+ const openModal = () => {
856
+ popup.addModal({
857
+ header: { title: trans('filters') },
858
+ position: 'fullscreen', body: _jsx(FilterModal, {}),
859
+ backdropAttrs: { className: 'aio-table-filter-modal aio-table-filter-modal-size' },
860
+ modalAttrs
861
+ });
862
+ };
863
+ const Attrs = UT.AddToAttrs(attrs, { className: 'aio-table-toolbar-button', attrs: { onClick: openModal } });
864
+ return (_jsx("button", Object.assign({}, Attrs, { children: new UT.GetSvg('em').getIcon('mdiFilter', 0.7) })));
865
+ };
866
+ const FilterColumnsTags = ({ filterItems, remove }) => {
867
+ const { filterDataHook } = useFilterContext();
868
+ const { filterColumns } = useProvider();
869
+ const getTags = () => {
870
+ var _a, _b;
871
+ const res = [];
872
+ for (let i = 0; i < filterItems.length; i++) {
873
+ const filterItem = filterItems[i];
874
+ const { columnId, value } = filterItem;
875
+ if (value === undefined || value === '' || !columnId) {
876
+ continue;
877
+ }
878
+ const column = filterColumns.find((o) => o.id === columnId);
879
+ if (!column) {
880
+ continue;
881
+ }
882
+ const { type = 'text' } = column;
883
+ let valuePresent = value;
884
+ const options = ((_a = column.filterInput) === null || _a === void 0 ? void 0 : _a.options) || [];
885
+ if (type === 'multiSelect') {
886
+ if (!value || !value.length) {
887
+ continue;
888
+ }
889
+ const texts = options.filter((o) => value.indexOf(o.value) !== -1).map((o) => o.text);
890
+ valuePresent = texts.join(' | ');
891
+ }
892
+ else if (type === 'select') {
893
+ const option = options.find((o) => o.value === value);
894
+ valuePresent = (option === null || option === void 0 ? void 0 : option.text) || '';
895
+ }
896
+ else if (type === 'date' || type === 'jalaliDate') {
897
+ const dateUnit = ((_b = column.filterInput) === null || _b === void 0 ? void 0 : _b.dateUnit) || { year: true, month: true, day: true };
898
+ const jalali = column.type === 'jalaliDate';
899
+ if (dateUnit === 'month') {
900
+ valuePresent = new AIODate().getDateByPattern(value, '{year}/{month}', jalali);
901
+ }
902
+ else if (dateUnit === 'day') {
903
+ valuePresent = new AIODate().getDateByPattern(value, '{year}/{month}/{day}', jalali);
904
+ }
905
+ else if (dateUnit === 'hour') {
906
+ valuePresent = new AIODate().getDateByPattern(value, '{year}/{month}/{day} {hour}:00 }', jalali);
907
+ }
908
+ else if (dateUnit === 'minute') {
909
+ valuePresent = new AIODate().getDateByPattern(value, '{year}/{month}/{day} {hour}:{minute}', jalali);
910
+ }
911
+ }
912
+ else if (type === 'number') {
913
+ valuePresent = UT.SplitNumber(valuePresent);
914
+ }
915
+ else if (type === 'checkbox') {
916
+ valuePresent = '';
917
+ }
918
+ res.push(_jsx(FilterTag, { column: column, filterItem: filterItem, valuePresent: valuePresent, onRemove: remove ? () => filterDataHook.removeFilterItem(filterItem) : undefined }, filterItem.id));
919
+ }
920
+ return res;
921
+ };
922
+ if (!filterItems.length) {
923
+ return null;
924
+ }
925
+ return (_jsx("div", { className: "aio-table-filter-tags", children: getTags() }));
926
+ };
927
+ const FilterTag = ({ filterItem, valuePresent, column, onRemove }) => {
928
+ var _a;
929
+ const { trans } = useFilterContext();
930
+ const { operator } = filterItem;
931
+ return (_jsxs("div", { className: "aio-table-filter-tag", onClick: onRemove, children: [_jsx("div", { className: "aio-table-filter-tag-column", children: ((_a = column.filterInput) === null || _a === void 0 ? void 0 : _a.label) || column.title || '' }), !!operator && _jsx("div", { className: "aio-table-filter-tag-operator", children: trans(operator) }), _jsx("div", { className: "aio-table-filter-tag-value", children: valuePresent }), !!onRemove && new UT.GetSvg('em').getIcon('mdiClose', 0.6)] }));
932
+ };
933
+ const SaveConfirm = ({ onSubmit }) => {
934
+ const { popup, filterDataHook, trans } = useFilterContext();
935
+ const [name, setName] = useState('');
936
+ return (_jsxs("div", { className: "aio-table-filter-save-modal", children: [_jsx(AIFormInput, { label: trans('inter filter name'), input: (_jsx(AIText, { options: filterDataHook.getSaves(), option: { text: (o) => o.name, value: (o) => o.name, onClick: (o) => setName(o) }, onChange: (newName, option) => setName(option ? option.text : newName) })) }), _jsxs("div", { className: "aio-table-filter-save-modal-footer", children: [_jsx("button", { className: "aio-table-filter-button aio-table-filter-active-button", disabled: !name, onClick: () => onSubmit(name), children: trans(name === filterDataHook.activeName ? 'edit' : 'save') }), _jsx("button", { className: "aio-table-filter-button", onClick: () => popup.removeModal(), children: trans('cansel') })] })] }));
937
+ };
938
+ const FilterSaveds = () => {
939
+ const { trans, filterDataHook } = useFilterContext();
940
+ const saves = filterDataHook.getSaves();
941
+ if (!saves.length) {
942
+ return (_jsx("div", { className: "aio-table-saved-filters-empty", children: trans('saved empty') }));
943
+ }
944
+ return (_jsx("div", { className: "aio-table-saved-filters", children: saves.map((o) => _jsx(FilterSaved, { item: o }, o.name)) }));
945
+ };
946
+ const FilterSaved = ({ item }) => {
947
+ const { rootProps } = useProvider();
948
+ const { trans, filterDataHook } = useFilterContext();
949
+ const { fa } = rootProps;
950
+ if (!item.name) {
951
+ return null;
952
+ }
953
+ const header_layout = () => _jsxs("div", { className: "aio-table-saved-filter-header", children: [" ", name_layout(), " ", remove_layout(), " ", activate_layout()] });
954
+ const name_layout = () => _jsx("div", { className: "aio-table-saved-filter-name", children: item.name });
955
+ const remove_layout = () => {
956
+ const attrs = { className: "aio-table-filter-remove-button", onClick: () => filterDataHook.removeSave(item), style: { [fa ? 'right' : 'left']: -4 } };
957
+ return (_jsx("div", Object.assign({}, attrs, { children: _jsx(FilterRemoveIcon, {}) })));
958
+ };
959
+ const activate_layout = () => {
960
+ return (_jsxs("div", { className: "aio-table-filter-button", onClick: () => filterDataHook.activeSave(item), children: [_jsx(FilterActiveIcon, {}), " ", trans('activate')] }));
961
+ };
962
+ const body_layout = () => {
963
+ return _jsx("div", { className: "aio-table-saved-filter-body", children: _jsx(FilterColumnsTags, { filterItems: item.data.filter((o) => o.value !== undefined && o.value !== '') }) });
964
+ };
965
+ return (_jsxs("div", { className: "aio-table-saved-filter", children: [header_layout(), " ", body_layout()] }));
966
+ };
967
+ const FilterModal = () => {
968
+ const { rootProps } = useProvider();
969
+ const { trans } = useFilterContext();
970
+ const { fa, filter } = rootProps;
971
+ const { getSavedItems } = filter || {};
972
+ const collections_layout = () => {
973
+ return (_jsxs("div", { className: "aio-table-filter-panel", children: [_jsx("div", { className: "aio-table-filter-panel-header", children: _jsx("div", { className: "aio-table-filter-panel-label", children: trans('saved filters') }) }), _jsx(FilterSaveds, {})] }));
974
+ };
975
+ return (_jsxs("div", { className: `aio-table-filter-body aio-table-filter-body-${fa ? 'rtl' : 'ltr'}`, children: [_jsx(CurrentFilters, {}), !!getSavedItems && collections_layout()] }));
976
+ };
977
+ const CurrentFilters = () => {
978
+ const { rootProps } = useProvider();
979
+ const { filterDataHook, trans } = useFilterContext();
980
+ const { filter } = rootProps;
981
+ const canSave = (!!filter && !!filter.setSavedItems);
982
+ const data = filterDataHook.getData();
983
+ return (_jsxs("div", { className: "aio-table-filter-panel", children: [_jsxs("div", { className: "aio-table-filter-panel-header", children: [_jsx("div", { className: "aio-table-filter-panel-label", children: trans('current filters') }), _jsxs("button", { className: "aio-table-filter-button", onClick: filterDataHook.addColumnsFilterItem, children: [_jsx(FilterAddIcon, {}), " ", trans('add')] }), !!canSave && !!data.length && _jsxs("button", { className: "aio-table-filter-button", onClick: filterDataHook.save, children: [_jsx(FilterSaveIcon, {}), " ", trans('save')] })] }), _jsx("div", { className: "aio-table-current-filters", children: data.map((columnsFilterItem) => _jsx(CurrentFilter, { filterItem: columnsFilterItem }, columnsFilterItem.id)) })] }));
984
+ };
985
+ const CurrentFilter = ({ filterItem }) => {
986
+ const { operator, columnId, value } = filterItem;
987
+ const { filterDataHook, trans } = useFilterContext();
988
+ const { filterColumns } = useProvider();
989
+ const [column] = useState(getColumn);
990
+ const [operators, setOperators] = useState(getOperators);
991
+ function getColumn() { return filterColumns.find((o) => o.id === columnId); }
992
+ useEffect(() => {
993
+ setOperators(getOperators());
994
+ }, [JSON.stringify(filterItem.columnId)]);
995
+ function getOperators() {
996
+ if (!column) {
997
+ return [];
998
+ }
999
+ const res = [];
1000
+ const operators = ['less', 'more', 'equal', 'notEqual', 'contain', 'notContain'];
1001
+ const dic = {
1002
+ text: { contain: true, notContain: true, equal: true, notEqual: true },
1003
+ number: { less: true, more: true, lessequal: true, moreequal: true, equal: true, notEqual: true },
1004
+ date: { less: true, more: true, lessequal: true, moreequal: true, equal: true, notEqual: true },
1005
+ jalaliDate: { less: true, more: true, lessequal: true, moreequal: true, equal: true, notEqual: true },
1006
+ select: { equal: true, notEqual: true },
1007
+ multiSelect: { contain: true, notContain: true },
1008
+ checkbox: {}
1009
+ };
1010
+ for (let i = 0; i < operators.length; i++) {
1011
+ const o = operators[i];
1012
+ if (!dic[column.type || 'text'][o]) {
1013
+ continue;
1014
+ }
1015
+ res.push(operators[i]);
1016
+ }
1017
+ return res;
1018
+ }
1019
+ const changeValue = (newValue) => {
1020
+ filterDataHook.changeColumnsFilterItem({ filterItem: filterItem, key: 'value', value: newValue, column });
1021
+ };
1022
+ const getValueInput = () => {
1023
+ var _a, _b;
1024
+ if (!column) {
1025
+ return null;
1026
+ }
1027
+ const type = ((_a = column.filterInput) === null || _a === void 0 ? void 0 : _a.type) || column.type || 'text';
1028
+ if (type === 'select') {
1029
+ return (_jsx(AISelect, Object.assign({}, column.filterInput, { value: value, onChange: (value) => changeValue(value), deSelect: true, popover: Object.assign({ fitHorizontal: true }, (_b = column.filterInput) === null || _b === void 0 ? void 0 : _b.popover) })));
1030
+ }
1031
+ if (type === 'multiSelect') {
1032
+ return (_jsx(AIRadio, Object.assign({ multiple: true }, column.filterInput, { value: value, onChange: (value) => changeValue(value), deSelect: true })));
1033
+ }
1034
+ if (type === 'text') {
1035
+ return _jsx(AIText, Object.assign({}, column.filterInput, { value: value, onChange: (value) => changeValue(value), deSelect: true }));
1036
+ }
1037
+ if (type === 'checkbox') {
1038
+ return _jsx(AICheckbox, Object.assign({}, column.filterInput, { value: value, onChange: (value) => changeValue(value), deSelect: true }));
1039
+ }
1040
+ if (type === 'number') {
1041
+ return _jsx(AINumber, Object.assign({}, column.filterInput, { value: value, onChange: (value) => changeValue(value), deSelect: true, inputAttrs: { style: { letterSpacing: 4 } }, justify: true }));
1042
+ }
1043
+ if (type === 'date' || type === 'jalaliDate') {
1044
+ return _jsx(TimeInput, Object.assign({}, column.filterInput, { filterItem: filterItem, onChange: (value) => changeValue(value), column: column, jalali: type === 'jalaliDate' }));
1045
+ }
1046
+ };
1047
+ return (_jsx(_Fragment, { children: _jsxs("div", { className: "aio-table-filter-row", children: [!!column &&
1048
+ _jsx(AISelect, { className: 'msf', options: filterColumns.map((o) => ({ text: o.title, value: o.id })), value: columnId, onChange: (columnId) => filterDataHook.changeColumnsFilterItem({ filterItem: filterItem, key: 'columnId', value: columnId, column }), popover: { fitHorizontal: true } }), !!column && !!operators.length &&
1049
+ _jsx(AISelect, { className: 'aio-table-filter-operator-select', options: operators, value: operator, option: { text: (operator) => trans(operator), value: (operator) => operator }, onChange: (operator) => filterDataHook.changeColumnsFilterItem({ filterItem: filterItem, key: 'operator', value: operator, column }), popover: { fitHorizontal: true } }), getValueInput(), _jsx("div", { className: "aio-table-filter-remove-button", onClick: () => filterDataHook.removeFilterItem(filterItem), children: _jsx(FilterRemoveIcon, {}) })] }) }));
1050
+ };
1051
+ const TimeInput = ({ filterItem, onChange, column, jalali }) => {
1052
+ const [unit] = useState(getUnit);
1053
+ function getUnit() {
1054
+ const { dateUnit = 'day' } = column.filterInput || {};
1055
+ const res = { year: true, month: true };
1056
+ if (dateUnit !== 'month') {
1057
+ res.day = true;
1058
+ if (dateUnit !== 'day') {
1059
+ res.hour = true;
1060
+ if (dateUnit !== 'hour') {
1061
+ res.minute = true;
1062
+ }
1063
+ }
1064
+ }
1065
+ return res;
1066
+ }
1067
+ const DATE = new AIODate();
1068
+ return (_jsx(AITime, { jalali: jalali, timeUnit: unit, value: filterItem.value, deSelect: true, onChange: (value) => onChange(value ? DATE.getTime(value) : undefined) }));
1069
+ };
1070
+ const FilterSaveIcon = () => new UT.GetSvg('em').getIcon('mdiSaveContent', 0.8);
1071
+ const FilterRemoveIcon = () => new UT.GetSvg('em').getIcon('mdiClose', 0.6);
1072
+ const FilterActiveIcon = () => new UT.GetSvg('em').getIcon('mdiCheckBold', 0.6);
1073
+ const FilterAddIcon = () => new UT.GetSvg('em').getIcon('mdiPlusThick', 0.7);
1074
+ const useFilterData = (popup, trans) => {
1075
+ const [activeName, setActiveName] = useState();
1076
+ const { filterColumns, rootProps } = useProvider();
1077
+ const { filter = {} } = rootProps;
1078
+ const [saves, setSaves] = useState([]);
1079
+ const savesRef = useRef(saves);
1080
+ savesRef.current = saves;
1081
+ const getSaves = () => savesRef.current;
1082
+ const [data, setData] = useState(getInitialData);
1083
+ function getInitialData() {
1084
+ if (filter.cache) {
1085
+ const storage = new UT.Storage(filter.cache);
1086
+ const data = storage.load('columnsFilterData', filter.items || []);
1087
+ if (filter.onChange) {
1088
+ filter.onChange(data);
1089
+ }
1090
+ return data;
1091
+ }
1092
+ else {
1093
+ return filter.items || [];
1094
+ }
1095
+ }
1096
+ const getData = () => dataRef.current;
1097
+ const dataRef = useRef(data);
1098
+ dataRef.current = data;
1099
+ const changeData = (newData) => __awaiter(void 0, void 0, void 0, function* () {
1100
+ if (filter.onChange) {
1101
+ const res = yield filter.onChange(newData);
1102
+ if (res === false) {
1103
+ return false;
1104
+ }
1105
+ }
1106
+ setData(newData);
1107
+ return true;
1108
+ });
1109
+ const changeSaves = (newSaves) => __awaiter(void 0, void 0, void 0, function* () {
1110
+ const { setSavedItems } = filter;
1111
+ if (!setSavedItems) {
1112
+ return false;
1113
+ }
1114
+ const res = yield setSavedItems(newSaves);
1115
+ if (res === false) {
1116
+ return false;
1117
+ }
1118
+ setSaves(newSaves);
1119
+ return true;
1120
+ });
1121
+ const save = () => {
1122
+ popup.addModal({
1123
+ header: { title: trans('save filter') }, position: 'center',
1124
+ body: (_jsx(SaveConfirm, { onSubmit: (name) => __awaiter(void 0, void 0, void 0, function* () {
1125
+ if (!name) {
1126
+ return;
1127
+ }
1128
+ const data = dataRef.current;
1129
+ const saves = savesRef.current;
1130
+ const newSave = { name, data };
1131
+ const isExist = !!saves.find((o) => o.name === name);
1132
+ let newSaves = [];
1133
+ if (isExist) {
1134
+ newSaves = saves.map((o) => o.name === name ? newSave : o);
1135
+ }
1136
+ else {
1137
+ newSaves = [...saves, newSave];
1138
+ }
1139
+ const res = yield changeSaves(newSaves);
1140
+ if (res) {
1141
+ popup.removeModal();
1142
+ }
1143
+ }) })),
1144
+ backdropAttrs: { className: 'aio-table-filter-modal' }
1145
+ });
1146
+ };
1147
+ const removeSave = (saveItem) => {
1148
+ popup.addConfirm({
1149
+ title: trans('remove saved filter'),
1150
+ text: trans('remove confirm'),
1151
+ submitText: trans('remove'),
1152
+ canselText: trans('cansel'),
1153
+ backdropAttrs: { className: 'aio-table-filter-modal' },
1154
+ submitAttrs: { className: 'aio-table-filter-button aio-table-filter-active-button' },
1155
+ canselAttrs: { className: 'aio-table-filter-button' },
1156
+ onSubmit: () => __awaiter(void 0, void 0, void 0, function* () { return yield changeSaves(savesRef.current.filter((o) => o.name !== saveItem.name)); })
1157
+ });
1158
+ };
1159
+ const activeSave = (saveItem) => __awaiter(void 0, void 0, void 0, function* () {
1160
+ const { onChange } = filter;
1161
+ if (!onChange) {
1162
+ return false;
1163
+ }
1164
+ changeData(saveItem.data);
1165
+ });
1166
+ const fetchSaves = () => __awaiter(void 0, void 0, void 0, function* () {
1167
+ const { getSavedItems } = filter;
1168
+ if (!getSavedItems) {
1169
+ return;
1170
+ }
1171
+ const saves = yield getSavedItems();
1172
+ if (saves) {
1173
+ setSaves(saves);
1174
+ }
1175
+ });
1176
+ useEffect(() => { fetchSaves(); }, []);
1177
+ const getDefaultFilter = (column) => {
1178
+ const { type = 'text' } = column;
1179
+ const { options } = (column === null || column === void 0 ? void 0 : column.filterInput) || {};
1180
+ let value = '';
1181
+ if (type === 'select' && !!options && !!options.length) {
1182
+ value = options[0].value;
1183
+ }
1184
+ const operatorDic = {
1185
+ text: 'contain', number: 'equal', date: 'equal', jalaliDate: 'equal', select: 'equal', multiSelect: 'contain', checkbox: false
1186
+ };
1187
+ const operator = column ? (operatorDic[type] === false ? undefined : operatorDic[type]) : undefined;
1188
+ return { value, operator, columnId: column === null || column === void 0 ? void 0 : column.id, id: 'a' + UT.GetRandomNumber(10000000, 99999999) };
1189
+ };
1190
+ const changeColumnsFilterItem = (p) => {
1191
+ const { key, value, filterItem, column } = p;
1192
+ const columnsFilterItems = dataRef.current;
1193
+ let newFilterItem;
1194
+ if (!!column && key === 'columnId') {
1195
+ const defaultFilter = getDefaultFilter(column);
1196
+ if (defaultFilter === false) {
1197
+ return;
1198
+ }
1199
+ newFilterItem = Object.assign(Object.assign({}, defaultFilter), { [key]: value });
1200
+ }
1201
+ else {
1202
+ newFilterItem = Object.assign(Object.assign({}, filterItem), { [key]: value });
1203
+ }
1204
+ const newFilterItems = columnsFilterItems.map((o) => o.id === filterItem.id ? newFilterItem : o);
1205
+ changeData(newFilterItems);
1206
+ };
1207
+ const addColumnsFilterItem = () => {
1208
+ const column = filterColumns[0];
1209
+ if (!column) {
1210
+ return;
1211
+ }
1212
+ const defaultFilter = getDefaultFilter(column);
1213
+ if (defaultFilter === false) {
1214
+ return;
1215
+ }
1216
+ const columnsFilterItems = dataRef.current;
1217
+ changeData([...columnsFilterItems, defaultFilter]);
1218
+ };
1219
+ const removeFilterItem = (filterItem) => {
1220
+ const columnsFilterItems = dataRef.current;
1221
+ changeData(columnsFilterItems.filter((o) => !!o && (o.id !== filterItem.id)));
1222
+ };
1223
+ return { getSaves, getData, save, removeSave, removeFilterItem, changeColumnsFilterItem, activeName, activeSave, addColumnsFilterItem };
561
1224
  };
1225
+ export function sortRows(rows, sorts) {
1226
+ if (!rows) {
1227
+ return [];
1228
+ }
1229
+ if (!sorts || !sorts.length) {
1230
+ return rows;
1231
+ }
1232
+ return rows.sort((a, b) => {
1233
+ for (let i = 0; i < sorts.length; i++) {
1234
+ let { dir, getValue } = sorts[i];
1235
+ if (!getValue) {
1236
+ return 0;
1237
+ }
1238
+ let aValue = getValue(a), bValue = getValue(b);
1239
+ if (aValue < bValue) {
1240
+ return -1 * (dir === 'dec' ? -1 : 1);
1241
+ }
1242
+ if (aValue > bValue) {
1243
+ return 1 * (dir === 'dec' ? -1 : 1);
1244
+ }
1245
+ if (i === sorts.length - 1) {
1246
+ return 0;
1247
+ }
1248
+ }
1249
+ return 0;
1250
+ });
1251
+ }