@azrico/reactgrid 1.2.0 → 1.2.2

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.
@@ -1,491 +1,481 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.AzGrid = AzGrid;
7
- exports.default = void 0;
8
- require("@inovua/reactdatagrid-enterprise/index.css");
9
- require("@inovua/reactdatagrid-enterprise/theme/default-dark.css");
10
- require("@inovua/reactdatagrid-enterprise/theme/amber-dark.css");
11
- require("../themes/base_styles.css");
12
- require("../themes/grid.css");
13
- var _controls = require("@azrico/controls");
14
- var _react = _interopRequireDefault(require("react"));
15
- var _helpers = require("../func/helpers");
16
- var _BoolEditor = _interopRequireDefault(require("@inovua/reactdatagrid-community/BoolEditor"));
17
- var _NumericEditor = _interopRequireDefault(require("@inovua/reactdatagrid-community/NumericEditor"));
18
- var _reactdatagridEnterprise = _interopRequireDefault(require("@inovua/reactdatagrid-enterprise"));
19
- var _SelectEditor = _interopRequireDefault(require("@inovua/reactdatagrid-community/SelectEditor"));
20
- var _clsx = _interopRequireDefault(require("clsx"));
21
- var _object = require("@azrico/object");
22
- var _reactUseSize = require("react-use-size");
23
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24
- /* -------------------------------------------------------------------------- */
25
- // const defaultfilterTypes: FilterTypes = {
26
- // string: {
27
- // type: 'string',
28
- // emptyValue: '',
29
- // operators: [
30
- // {
31
- // name: 'contains',
32
- // fn: ({ value, filterValue, data }) => {
33
- // console.log('fn');
34
- // return !filterValue ? true : value.indexOf(filterValue) != -1;
35
- // },
36
- // },
37
- // ],
38
- // },
39
- // };
40
- const defaultOptions = {
41
- //filterTypes: defaultfilterTypes,
42
- remoteFilter: false,
43
- remoteSort: false,
44
- editStartEvent: 'click',
45
- rowReorderColumn: false,
46
- resizable: true,
47
- columnUserSelect: true,
48
- rowIndexColumn: true,
49
- rowHeight: 35,
50
- maxRowHeight: 40,
51
- minRowHeight: 30
52
- };
53
- const forcedOptions = {
54
- idProperty: '__table_id',
55
- nodesProperty: '__children'
56
- };
57
- const defaultColumnOptions = {
58
- draggable: false,
59
- lockable: false,
60
- editable: false
61
- };
62
- const dynamic_option_names = ['selected', 'loading', 'limit', 'skip'];
63
- /* -------------------------------------------------------------------------- */
64
- function AzGrid(props) {
65
- /* -------------------------------------------------------------------------- */
66
- /* Columns */
67
- /* -------------------------------------------------------------------------- */
68
- const cached_columns = _react.default.useMemo(() => {
69
- return ApplyColumnData(props);
70
- }, [props.columns, props.leftColumns, props.rightColumns, props.editableColumns, props.columnExtensions, props.customColumns]);
71
- /* -------------------------------------------------------------------------- */
72
- /* Row changes */
73
- /* -------------------------------------------------------------------------- */
74
- const cached_data = _react.default.useRef();
75
- const last_cache_info = _react.default.useRef();
76
- const dataSource = _react.default.useCallback(async dt => {
77
- let result_data = undefined;
78
- let isUsingCache = false;
79
- const cacheInfo = {
80
- ds: props.dataSource,
81
- skip: dt.skip,
82
- limit: dt.limit
83
- };
84
- //check if we can used cached data
85
- //if the datasource is changed since last render, we should invalidate cache
86
- if (!validateCache(last_cache_info.current, cacheInfo, props.looseCacheCheck)) {
87
- cached_data.current = undefined;
88
- last_cache_info.current = cacheInfo;
89
- }
90
- if (cached_data.current) {
91
- //cache is valid, use the cache
92
- isUsingCache = true;
93
- result_data = cached_data.current;
94
- } else {
95
- //cache is invalid, use the datasource again
96
- isUsingCache = false;
97
- if (typeof props.dataSource === 'function') {
98
- result_data = await props.dataSource(dt);
99
- } else result_data = props.dataSource;
100
- }
101
- //making sure the datasource is a valid object :
102
- if (!result_data) result_data = [];
103
- if (Array.isArray(result_data) || typeof result_data !== 'object') result_data = {
104
- data: result_data
105
- };
106
- if (!Array.isArray(result_data.data)) result_data.data = [result_data.data];
107
- //no need to fix parent/child when using cache (it should be already fixed)
108
- if (!isUsingCache && props.allowChildRows) result_data.data = (0, _helpers.fix_parent_child_relations)(result_data.data);
109
- if (!result_data.count) result_data.count = result_data.data.length;
110
- //update cache
111
- cached_data.current = result_data;
112
- return result_data;
113
- }, [props.dataSource]);
114
- const base_gridOptions = _react.default.useMemo(() => {
115
- const firstcolumn = cached_columns[0];
116
- const licenseKey = props.licenseKey || get_default_key();
117
- const events = {
118
- onSkipChange: props.onSkipChange,
119
- onLimitChange: props.onLimitChange,
120
- onEditComplete: props.onEditComplete
121
- };
122
- const extra_ops = {
123
- onSelectionChange: props.onSelectionChange,
124
- licenseKey: licenseKey,
125
- editable: props.editable,
126
- treeColumn: props.allowChildRows ? firstcolumn.name : undefined,
127
- renderRowDetails: props.RowDetail,
128
- theme: props.dark ? 'amber-dark' : 'default-light'
129
- };
130
- return {
131
- ...defaultOptions,
132
- ...events,
133
- ...extra_ops
134
- };
135
- }, [cached_columns, props.allowChildRows, props.editable, props.RowDetail, props.onEditComplete]);
136
- const static_gridOptions = _react.default.useMemo(() => {
137
- return {
138
- ...base_gridOptions,
139
- ...props.gridOptions,
140
- ...forcedOptions
141
- };
142
- }, [props.gridOptions, base_gridOptions, forcedOptions]);
143
- const dynamic_gridOptions = _react.default.useMemo(() => {
144
- const res = {};
145
- dynamic_option_names.map(r => {
146
- res[r] = props[r];
147
- });
148
- return res;
149
- }, dynamic_option_names.map(r => props[r]));
150
- return _react.default.createElement(DataGridEventsWrapper, {
151
- ...props,
152
- static_gridOptions: static_gridOptions,
153
- dynamic_gridOptions: dynamic_gridOptions,
154
- columns: cached_columns,
155
- dataSource: dataSource,
156
- highlightedRow: props.highlightedRow
157
- });
158
- }
159
- /* -------------------------------------------------------------------------- */
1
+ import '@inovua/reactdatagrid-enterprise/index.css';
2
+ import '@inovua/reactdatagrid-enterprise/theme/default-dark.css';
3
+ import '@inovua/reactdatagrid-enterprise/theme/amber-dark.css';
4
+ import '../themes/base_styles.css';
5
+ import '../themes/grid.css';
6
+ import { ContextMenu } from '@azrico/controls';
7
+ import React from 'react';
8
+ import { fix_parent_child_relations, func_selection } from '../func/helpers';
9
+ import BoolEditor from '@inovua/reactdatagrid-community/BoolEditor';
10
+ import NumericEditor from '@inovua/reactdatagrid-community/NumericEditor';
11
+ import ReactDataGrid from '@inovua/reactdatagrid-enterprise';
12
+ import SelectEditor from '@inovua/reactdatagrid-community/SelectEditor';
13
+ import clsx from 'clsx';
14
+ import { object_equals } from '@azrico/object';
15
+ import { useComponentSize } from 'react-use-size';
16
+ /* -------------------------------------------------------------------------- */
17
+ // const defaultfilterTypes: FilterTypes = {
18
+ // string: {
19
+ // type: 'string',
20
+ // emptyValue: '',
21
+ // operators: [
22
+ // {
23
+ // name: 'contains',
24
+ // fn: ({ value, filterValue, data }) => {
25
+ // console.log('fn');
26
+ // return !filterValue ? true : value.indexOf(filterValue) != -1;
27
+ // },
28
+ // },
29
+ // ],
30
+ // },
31
+ // };
32
+ const defaultOptions = {
33
+ //filterTypes: defaultfilterTypes,
34
+ remoteFilter: false,
35
+ remoteSort: false,
36
+ editStartEvent: 'click',
37
+ rowReorderColumn: false,
38
+ resizable: true,
39
+ columnUserSelect: true,
40
+ rowIndexColumn: true,
41
+ rowHeight: 35,
42
+ maxRowHeight: 40,
43
+ minRowHeight: 30,
44
+ };
45
+ const forcedOptions = {
46
+ idProperty: '__table_id',
47
+ nodesProperty: '__children',
48
+ };
49
+ const defaultColumnOptions = {
50
+ draggable: false,
51
+ lockable: false,
52
+ editable: false,
53
+ };
54
+ const dynamic_option_names = ['selected', 'loading', 'limit', 'skip'];
55
+ /* -------------------------------------------------------------------------- */
56
+ export function AzGrid(props) {
57
+ /* -------------------------------------------------------------------------- */
58
+ /* Columns */
59
+ /* -------------------------------------------------------------------------- */
60
+ const cached_columns = React.useMemo(() => {
61
+ return ApplyColumnData(props);
62
+ }, [
63
+ props.columns,
64
+ props.leftColumns,
65
+ props.rightColumns,
66
+ props.editableColumns,
67
+ props.columnExtensions,
68
+ props.customColumns,
69
+ ]);
70
+ /* -------------------------------------------------------------------------- */
71
+ /* Row changes */
72
+ /* -------------------------------------------------------------------------- */
73
+ const cached_data = React.useRef();
74
+ const last_cache_info = React.useRef();
75
+ const dataSource = React.useCallback(async (dt) => {
76
+ let result_data = undefined;
77
+ let isUsingCache = false;
78
+ const cacheInfo = { ds: props.dataSource, skip: dt.skip, limit: dt.limit };
79
+ //check if we can used cached data
80
+ //if the datasource is changed since last render, we should invalidate cache
81
+ if (!validateCache(last_cache_info.current, cacheInfo, props.looseCacheCheck)) {
82
+ cached_data.current = undefined;
83
+ last_cache_info.current = cacheInfo;
84
+ }
85
+ if (cached_data.current) {
86
+ //cache is valid, use the cache
87
+ isUsingCache = true;
88
+ result_data = cached_data.current;
89
+ }
90
+ else {
91
+ //cache is invalid, use the datasource again
92
+ isUsingCache = false;
93
+ if (typeof props.dataSource === 'function') {
94
+ result_data = await props.dataSource(dt);
95
+ }
96
+ else
97
+ result_data = props.dataSource;
98
+ }
99
+ //making sure the datasource is a valid object :
100
+ if (!result_data)
101
+ result_data = [];
102
+ if (Array.isArray(result_data) || typeof result_data !== 'object')
103
+ result_data = { data: result_data };
104
+ if (!Array.isArray(result_data.data))
105
+ result_data.data = [result_data.data];
106
+ //no need to fix parent/child when using cache (it should be already fixed)
107
+ if (!isUsingCache && props.allowChildRows)
108
+ result_data.data = fix_parent_child_relations(result_data.data);
109
+ if (!result_data.count)
110
+ result_data.count = result_data.data.length;
111
+ //update cache
112
+ cached_data.current = result_data;
113
+ return result_data;
114
+ }, [props.dataSource]);
115
+ const base_gridOptions = React.useMemo(() => {
116
+ const firstcolumn = cached_columns[0];
117
+ const licenseKey = props.licenseKey || get_default_key();
118
+ const events = {
119
+ onSkipChange: props.onSkipChange,
120
+ onLimitChange: props.onLimitChange,
121
+ onEditComplete: props.onEditComplete,
122
+ };
123
+ const extra_ops = {
124
+ onSelectionChange: props.onSelectionChange,
125
+ licenseKey: licenseKey,
126
+ editable: props.editable,
127
+ treeColumn: props.allowChildRows ? firstcolumn.name : undefined,
128
+ renderRowDetails: props.RowDetail,
129
+ theme: props.dark ? 'amber-dark' : 'default-light',
130
+ };
131
+ return { ...defaultOptions, ...events, ...extra_ops };
132
+ }, [
133
+ cached_columns,
134
+ props.allowChildRows,
135
+ props.editable,
136
+ props.RowDetail,
137
+ props.onEditComplete,
138
+ ]);
139
+ const static_gridOptions = React.useMemo(() => {
140
+ return { ...base_gridOptions, ...props.gridOptions, ...forcedOptions };
141
+ }, [props.gridOptions, base_gridOptions, forcedOptions]);
142
+ const dynamic_gridOptions = React.useMemo(() => {
143
+ const res = {};
144
+ dynamic_option_names.map((r) => {
145
+ res[r] = props[r];
146
+ });
147
+ return res;
148
+ }, dynamic_option_names.map((r) => props[r]));
149
+ return (React.createElement(DataGridEventsWrapper, { ...props, static_gridOptions: static_gridOptions, dynamic_gridOptions: dynamic_gridOptions, columns: cached_columns, dataSource: dataSource, highlightedRow: props.highlightedRow }));
150
+ }
151
+ /* -------------------------------------------------------------------------- */
160
152
  /**
161
153
  * Wrapper for menu events
162
154
  * @param props
163
155
  * @returns
164
- */
165
- function DataGridEventsWrapper(props) {
166
- /* -------------------------------------------------------------------------- */
167
- const click_data = _react.default.useRef(null);
168
- const [menuAnchorEl, set_menuAnchorEl] = _react.default.useState(null);
169
- const [menuRow, set_menuRow] = _react.default.useState(null);
170
- /* --------------------------------- events --------------------------------- */
171
- const onRowContextMenu = _react.default.useCallback((rowProps, event) => {
172
- if (_helpers.func_selection.has_selection()) return;
173
- update_menuRow(rowProps.data);
174
- update_menuAnchorEl(_helpers.func_selection.getVirtualElement(event.clientX, event.clientY));
175
- event.preventDefault();
176
- }, []);
177
- const onRowClick = _react.default.useCallback((rowProps, event) => {
178
- const nowDate = Date.now();
179
- const lastclick = click_data.current[get_row_id(rowProps)];
180
- if (lastclick) {
181
- const diffTime = Math.abs(nowDate - lastclick);
182
- if (diffTime < 400) {
183
- click_data.current[get_row_id(rowProps)] = null;
184
- return props.onRowDoubleClick && props.onRowDoubleClick(rowProps.rowIndex, rowProps.data);
185
- }
186
- }
187
- click_data.current[get_row_id(rowProps)] = nowDate;
188
- return props.onRowClick && props.onRowClick(rowProps.rowIndex, rowProps.data);
189
- }, [props.onRowClick, props.onRowDoubleClick]);
190
- _react.default.useEffect(() => {
191
- click_data.current = {};
192
- }, [props.dataSource]);
193
- /* ------------------------------- ContextMenu ------------------------------ */
194
- const update_menuAnchorEl = _react.default.useCallback(el => {
195
- if (el) {
196
- set_menuAnchorEl(el);
197
- } else set_menuAnchorEl(null);
198
- }, []);
199
- const update_menuRow = _react.default.useCallback(r => {
200
- if (r) {
201
- set_menuRow(r);
202
- } else set_menuRow(null);
203
- }, []);
204
- /**
205
- * Merge click events into grid options
206
- */
207
- const static_gridOptions = _react.default.useMemo(() => {
208
- const events = {
209
- onRowClick: onRowClick,
210
- onRowContextMenu: onRowContextMenu
211
- };
212
- return {
213
- ...props.static_gridOptions,
214
- ...events
215
- };
216
- }, [props.static_gridOptions, onRowClick]);
217
- return _react.default.createElement(DataGridSizeWrapper, {
218
- ...props,
219
- static_gridOptions: static_gridOptions
220
- }, _react.default.createElement(_controls.ContextMenu, {
221
- menuAnchorEl: menuAnchorEl,
222
- set_menuAnchorEl: update_menuAnchorEl,
223
- menuRow: menuRow,
224
- items: props.contextMenuItems
225
- }), props.children);
226
- }
227
- function DataGridSizeWrapper(props) {
228
- // console.log(props);
229
- if (props.delayedSize) {
230
- return _react.default.createElement(DataGridDelayedSize, {
231
- ...props
232
- });
233
- } else {
234
- return _react.default.createElement(DataGridFlexSize, {
235
- ...props
236
- });
237
- }
238
- }
239
- function DataGridDelayedSize(props) {
240
- const gridRef = _react.default.useRef(null);
241
- const {
242
- ref,
243
- height,
244
- width
245
- } = (0, _reactUseSize.useComponentSize)();
246
- return _react.default.createElement("div", {
247
- ref: ref,
248
- className: (0, _clsx.default)(props.className, 'azgrid-nooverflow', 'azgrid-filler')
249
- }, _react.default.createElement(_reactdatagridEnterprise.default, {
250
- ...props.static_gridOptions,
251
- ...props.dynamic_gridOptions,
252
- handle: ref => gridRef.current = ref ? ref.current : null,
253
- className: 'azgrid-delayedresize',
254
- style: {
255
- height: height,
256
- width: width
257
- },
258
- columns: props.columns,
259
- dataSource: props.dataSource
260
- }), props.children);
261
- }
262
- function DataGridFlexSize(props) {
263
- const gridRef = _react.default.useRef(null);
264
- return _react.default.createElement("div", {
265
- className: (0, _clsx.default)(props.className, 'azgrid-nooverflow', 'azgrid-filler')
266
- }, _react.default.createElement(_reactdatagridEnterprise.default, {
267
- ...props.static_gridOptions,
268
- ...props.dynamic_gridOptions,
269
- handle: ref => gridRef.current = ref ? ref.current : null,
270
- className: 'azgrid-filler',
271
- columns: props.columns,
272
- dataSource: props.dataSource
273
- }), props.children);
274
- }
275
- /* -------------------------------------------------------------------------- */
276
- /* -------------------------------------------------------------------------- */
277
- /* -------------------------------------------------------------------------- */
278
- /* -------------------------------------------------------------------------- */
156
+ */
157
+ function DataGridEventsWrapper(props) {
158
+ /* -------------------------------------------------------------------------- */
159
+ const click_data = React.useRef(null);
160
+ const [menuAnchorEl, set_menuAnchorEl] = React.useState(null);
161
+ const [menuRow, set_menuRow] = React.useState(null);
162
+ /* --------------------------------- events --------------------------------- */
163
+ const onRowContextMenu = React.useCallback((rowProps, event) => {
164
+ if (func_selection.has_selection())
165
+ return;
166
+ update_menuRow(rowProps.data);
167
+ update_menuAnchorEl(func_selection.getVirtualElement(event.clientX, event.clientY));
168
+ event.preventDefault();
169
+ }, []);
170
+ const onRowClick = React.useCallback((rowProps, event) => {
171
+ const nowDate = Date.now();
172
+ const lastclick = click_data.current[get_row_id(rowProps)];
173
+ if (lastclick) {
174
+ const diffTime = Math.abs(nowDate - lastclick);
175
+ if (diffTime < 400) {
176
+ click_data.current[get_row_id(rowProps)] = null;
177
+ return (props.onRowDoubleClick &&
178
+ props.onRowDoubleClick(rowProps.rowIndex, rowProps.data));
179
+ }
180
+ }
181
+ click_data.current[get_row_id(rowProps)] = nowDate;
182
+ return props.onRowClick && props.onRowClick(rowProps.rowIndex, rowProps.data);
183
+ }, [props.onRowClick, props.onRowDoubleClick]);
184
+ React.useEffect(() => {
185
+ click_data.current = {};
186
+ }, [props.dataSource]);
187
+ /* ------------------------------- ContextMenu ------------------------------ */
188
+ const update_menuAnchorEl = React.useCallback((el) => {
189
+ if (el) {
190
+ set_menuAnchorEl(el);
191
+ }
192
+ else
193
+ set_menuAnchorEl(null);
194
+ }, []);
195
+ const update_menuRow = React.useCallback((r) => {
196
+ if (r) {
197
+ set_menuRow(r);
198
+ }
199
+ else
200
+ set_menuRow(null);
201
+ }, []);
202
+ /**
203
+ * Merge click events into grid options
204
+ */
205
+ const static_gridOptions = React.useMemo(() => {
206
+ const events = {
207
+ onRowClick: onRowClick,
208
+ onRowContextMenu: onRowContextMenu,
209
+ };
210
+ return { ...props.static_gridOptions, ...events };
211
+ }, [props.static_gridOptions, onRowClick]);
212
+ return (React.createElement(DataGridSizeWrapper, { ...props, static_gridOptions: static_gridOptions },
213
+ React.createElement(ContextMenu, { menuAnchorEl: menuAnchorEl, set_menuAnchorEl: update_menuAnchorEl, menuRow: menuRow, items: props.contextMenuItems }),
214
+ props.children));
215
+ }
216
+ function DataGridSizeWrapper(props) {
217
+ // console.log(props);
218
+ if (props.delayedSize) {
219
+ return React.createElement(DataGridDelayedSize, { ...props });
220
+ }
221
+ else {
222
+ return React.createElement(DataGridFlexSize, { ...props });
223
+ }
224
+ }
225
+ function DataGridDelayedSize(props) {
226
+ const gridRef = React.useRef(null);
227
+ const { ref, height, width } = useComponentSize();
228
+ return (React.createElement("div", { ref: ref, className: clsx(props.className, 'azgrid-nooverflow', 'azgrid-filler') },
229
+ React.createElement(ReactDataGrid, { ...props.static_gridOptions, ...props.dynamic_gridOptions, handle: (ref) => (gridRef.current = ref ? ref.current : null), className: 'azgrid-delayedresize', style: {
230
+ height: height,
231
+ width: width,
232
+ }, columns: props.columns, dataSource: props.dataSource }),
233
+ props.children));
234
+ }
235
+ function DataGridFlexSize(props) {
236
+ const gridRef = React.useRef(null);
237
+ return (React.createElement("div", { className: clsx(props.className, 'azgrid-nooverflow', 'azgrid-filler') },
238
+ React.createElement(ReactDataGrid, { ...props.static_gridOptions, ...props.dynamic_gridOptions, handle: (ref) => (gridRef.current = ref ? ref.current : null), className: 'azgrid-filler', columns: props.columns, dataSource: props.dataSource }),
239
+ props.children));
240
+ }
241
+ /* -------------------------------------------------------------------------- */
242
+ /* -------------------------------------------------------------------------- */
243
+ /* -------------------------------------------------------------------------- */
244
+ /* -------------------------------------------------------------------------- */
279
245
  /**
280
246
  * merges together all different props for columns
281
247
  * to build a single final column data array
282
248
  * @param props
283
249
  * @returns
284
- */
285
- function ApplyColumnData(props) {
286
- const final_columns = [];
287
- let mcols = props.columns || [];
288
- let mcolExts = props.columnExtensions || [];
289
- let rightColumns = props.rightColumns || [];
290
- let leftColumns = props.leftColumns || [];
291
- let editableColumns = props.editableColumns || [];
292
- let customColumns = props.customColumns || [];
293
- if (!Array.isArray(mcols)) return [];
294
- /* ------------------------------- fix columns ------------------------------ */
295
- mcols.map(r => {
296
- if (r == null) return null;
297
- if (typeof r === 'string') return {
298
- field: r,
299
- header: r
300
- };
301
- if (typeof r === 'object') {
302
- let final_cl = {
303
- name: get_col_name(r),
304
- header: get_col_header(r),
305
- width: r.width || undefined
306
- };
307
- final_cl = {
308
- ...defaultColumnOptions,
309
- ...r,
310
- ...final_cl
311
- };
312
- return final_cl;
313
- }
314
- return null;
315
- }).forEach(r => {
316
- if (r != null) final_columns.push(r);
317
- });
318
- /* ---------------------------- apply extensions ---------------------------- */
319
- if (mcolExts && Array.isArray(mcolExts)) for (let index = 0; index < mcolExts.length; index++) {
320
- const ext = mcolExts[index];
321
- const ext_cl = get_col_name(ext);
322
- if (!ext_cl) {
323
- console.warn('ColumnExtension must have a name');
324
- continue;
325
- }
326
- const mcol = final_columns.find(s => get_col_name(s) === ext_cl);
327
- if (mcol) {
328
- for (const key in ext) {
329
- if (!mcol[key] && key != 'name') mcol[key] = ext[key];
330
- }
331
- }
332
- // if (ext.widthGrow) mcol.widthGrow = ext.widthGrow;
333
- }
334
- /* ------------------------------- frozen cols ------------------------------ */
335
- if (Array.isArray(leftColumns)) {
336
- for (let index = 0; index < leftColumns.length; index++) {
337
- const r = get_col_name(leftColumns[index]);
338
- const mcol = final_columns.find(s => get_col_name(s) === r);
339
- if (mcol) {
340
- mcol.locked = 'start';
341
- }
342
- }
343
- }
344
- if (Array.isArray(rightColumns)) {
345
- for (let index = 0; index < rightColumns.length; index++) {
346
- const r = get_col_name(rightColumns[index]);
347
- const mcol = final_columns.find(s => get_col_name(s) === r);
348
- if (mcol) {
349
- mcol.locked = 'end';
350
- }
351
- }
352
- }
353
- /* -------------------------------- ediatable ------------------------------- */
354
- if (Array.isArray(editableColumns)) {
355
- for (let index = 0; index < editableColumns.length; index++) {
356
- const r = get_col_name(editableColumns[index]);
357
- const mcol = final_columns.find(s => get_col_name(s) === r);
358
- if (mcol) {
359
- mcol.editable = true;
360
- }
361
- }
362
- }
363
- for (let index = 0; index < final_columns.length; index++) {
364
- const col = final_columns[index];
365
- if (col.type) {
366
- switch (col.type) {
367
- case 'combo':
368
- case 'select':
369
- //@ts-ignore
370
- col.editor = _SelectEditor.default;
371
- break;
372
- case 'number':
373
- case 'numberic':
374
- //@ts-ignore
375
- col.editor = _NumericEditor.default;
376
- break;
377
- case 'truefalse':
378
- case 'bool':
379
- //@ts-ignore
380
- col.editor = _BoolEditor.default;
381
- break;
382
- }
383
- if (col.editable != false) col.editable = true;
384
- delete col.type;
385
- }
386
- //@ts-ignore
387
- if (!col.editorProps) col.editorProps = {};
388
- if (col.items) {
389
- //@ts-ignore
390
- if (!col.editorProps.idProperty) col.editorProps.idProperty = 'id';
391
- //@ts-ignore
392
- if (!col.editorProps.collapseOnSelect) col.editorProps.collapseOnSelect = true;
393
- //@ts-ignore
394
- col.editorProps.dataSource = to_datasource(col.items, col.editorProps.idProperty);
395
- delete col.items;
396
- }
397
- }
398
- /* -------------------------------------------------------------------------- */
399
- if (Array.isArray(customColumns)) {
400
- for (let index = 0; index < customColumns.length; index++) {
401
- const r = customColumns[index];
402
- const rn = get_col_name(r);
403
- const mcol = final_columns.find(s => get_col_name(s) === rn);
404
- if (mcol) {
405
- //@ts-ignore
406
- mcol.render = r.render;
407
- }
408
- }
409
- }
410
- /* ------------------------------ apply renames ----------------------------- */
411
- for (let index = 0; index < final_columns.length; index++) {
412
- const col = final_columns[index];
413
- if (!col.name) {
414
- col.name = get_col_name(col);
415
- delete col.field;
416
- delete col.key;
417
- delete col.columnName;
418
- if (col.flex && !col.defaultFlex) col.defaultFlex = col.flex;
419
- }
420
- }
421
- /* -------------------------------------------------------------------------- */
422
- //console.log(final_columns);
423
- return final_columns;
424
- }
425
- function to_datasource(arr, idProperty, valueAsID = true) {
426
- const res = [];
427
- let index = 0;
428
- if (Array.isArray(arr)) {
429
- arr.map(s => {
430
- let elem = {};
431
- if (valueAsID) elem[idProperty] = String(s).toLowerCase();else elem[idProperty] = '_cid_' + index;
432
- if (typeof s === 'object') elem = {
433
- ...elem,
434
- ...s
435
- };else elem['label'] = s;
436
- res.push(elem);
437
- index++;
438
- });
439
- }
440
- return res;
441
- }
250
+ */
251
+ function ApplyColumnData(props) {
252
+ const final_columns = [];
253
+ let mcols = props.columns || [];
254
+ let mcolExts = props.columnExtensions || [];
255
+ let rightColumns = props.rightColumns || [];
256
+ let leftColumns = props.leftColumns || [];
257
+ let editableColumns = props.editableColumns || [];
258
+ let customColumns = props.customColumns || [];
259
+ if (!Array.isArray(mcols))
260
+ return [];
261
+ /* ------------------------------- fix columns ------------------------------ */
262
+ mcols
263
+ .map((r) => {
264
+ if (r == null)
265
+ return null;
266
+ if (typeof r === 'string')
267
+ return { field: r, header: r };
268
+ if (typeof r === 'object') {
269
+ let final_cl = {
270
+ name: get_col_name(r),
271
+ header: get_col_header(r),
272
+ width: r.width || undefined,
273
+ };
274
+ final_cl = {
275
+ ...defaultColumnOptions,
276
+ ...r,
277
+ ...final_cl,
278
+ };
279
+ return final_cl;
280
+ }
281
+ return null;
282
+ })
283
+ .forEach((r) => {
284
+ if (r != null)
285
+ final_columns.push(r);
286
+ });
287
+ /* ---------------------------- apply extensions ---------------------------- */
288
+ if (mcolExts && Array.isArray(mcolExts))
289
+ for (let index = 0; index < mcolExts.length; index++) {
290
+ const ext = mcolExts[index];
291
+ const ext_cl = get_col_name(ext);
292
+ if (!ext_cl) {
293
+ console.warn('ColumnExtension must have a name');
294
+ continue;
295
+ }
296
+ const mcol = final_columns.find((s) => get_col_name(s) === ext_cl);
297
+ if (mcol) {
298
+ for (const key in ext) {
299
+ if (!mcol[key] && key != 'name')
300
+ mcol[key] = ext[key];
301
+ }
302
+ }
303
+ // if (ext.widthGrow) mcol.widthGrow = ext.widthGrow;
304
+ }
305
+ /* ------------------------------- frozen cols ------------------------------ */
306
+ if (Array.isArray(leftColumns)) {
307
+ for (let index = 0; index < leftColumns.length; index++) {
308
+ const r = get_col_name(leftColumns[index]);
309
+ const mcol = final_columns.find((s) => get_col_name(s) === r);
310
+ if (mcol) {
311
+ mcol.locked = 'start';
312
+ }
313
+ }
314
+ }
315
+ if (Array.isArray(rightColumns)) {
316
+ for (let index = 0; index < rightColumns.length; index++) {
317
+ const r = get_col_name(rightColumns[index]);
318
+ const mcol = final_columns.find((s) => get_col_name(s) === r);
319
+ if (mcol) {
320
+ mcol.locked = 'end';
321
+ }
322
+ }
323
+ }
324
+ /* -------------------------------- ediatable ------------------------------- */
325
+ if (Array.isArray(editableColumns)) {
326
+ for (let index = 0; index < editableColumns.length; index++) {
327
+ const r = get_col_name(editableColumns[index]);
328
+ const mcol = final_columns.find((s) => get_col_name(s) === r);
329
+ if (mcol) {
330
+ mcol.editable = true;
331
+ }
332
+ }
333
+ }
334
+ for (let index = 0; index < final_columns.length; index++) {
335
+ const col = final_columns[index];
336
+ if (col.type) {
337
+ switch (col.type) {
338
+ case 'combo':
339
+ case 'select':
340
+ //@ts-ignore
341
+ col.editor = SelectEditor;
342
+ break;
343
+ case 'number':
344
+ case 'numberic':
345
+ //@ts-ignore
346
+ col.editor = NumericEditor;
347
+ break;
348
+ case 'truefalse':
349
+ case 'bool':
350
+ //@ts-ignore
351
+ col.editor = BoolEditor;
352
+ break;
353
+ }
354
+ if (col.editable != false)
355
+ col.editable = true;
356
+ delete col.type;
357
+ }
358
+ //@ts-ignore
359
+ if (!col.editorProps)
360
+ col.editorProps = {};
361
+ if (col.items) {
362
+ //@ts-ignore
363
+ if (!col.editorProps.idProperty)
364
+ col.editorProps.idProperty = 'id';
365
+ //@ts-ignore
366
+ if (!col.editorProps.collapseOnSelect)
367
+ col.editorProps.collapseOnSelect = true;
368
+ //@ts-ignore
369
+ col.editorProps.dataSource = to_datasource(col.items, col.editorProps.idProperty);
370
+ delete col.items;
371
+ }
372
+ }
373
+ /* -------------------------------------------------------------------------- */
374
+ if (Array.isArray(customColumns)) {
375
+ for (let index = 0; index < customColumns.length; index++) {
376
+ const r = customColumns[index];
377
+ const rn = get_col_name(r);
378
+ const mcol = final_columns.find((s) => get_col_name(s) === rn);
379
+ if (mcol) {
380
+ //@ts-ignore
381
+ mcol.render = r.render;
382
+ }
383
+ }
384
+ }
385
+ /* ------------------------------ apply renames ----------------------------- */
386
+ for (let index = 0; index < final_columns.length; index++) {
387
+ const col = final_columns[index];
388
+ if (!col.name) {
389
+ col.name = get_col_name(col);
390
+ delete col.field;
391
+ delete col.key;
392
+ delete col.columnName;
393
+ if (col.flex && !col.defaultFlex)
394
+ col.defaultFlex = col.flex;
395
+ }
396
+ }
397
+ /* -------------------------------------------------------------------------- */
398
+ //console.log(final_columns);
399
+ return final_columns;
400
+ }
401
+ function to_datasource(arr, idProperty, valueAsID = true) {
402
+ const res = [];
403
+ let index = 0;
404
+ if (Array.isArray(arr)) {
405
+ arr.map((s) => {
406
+ let elem = {};
407
+ if (valueAsID)
408
+ elem[idProperty] = String(s).toLowerCase();
409
+ else
410
+ elem[idProperty] = '_cid_' + index;
411
+ if (typeof s === 'object')
412
+ elem = { ...elem, ...s };
413
+ else
414
+ elem['label'] = s;
415
+ res.push(elem);
416
+ index++;
417
+ });
418
+ }
419
+ return res;
420
+ }
442
421
  /**
443
422
  * Gets name of a column from its column object
444
423
  * @param obj
445
424
  * @returns
446
- */
447
- function get_col_name(obj) {
448
- if (!obj) return '';
449
- if (typeof obj == 'string') return obj;
450
- if (typeof obj != 'object') return '';
451
- return obj.name || obj.field || obj.key || obj.columnName || '';
452
- }
425
+ */
426
+ export function get_col_name(obj) {
427
+ if (!obj)
428
+ return '';
429
+ if (typeof obj == 'string')
430
+ return obj;
431
+ if (typeof obj != 'object')
432
+ return '';
433
+ return obj.name || obj.field || obj.key || obj.columnName || '';
434
+ }
453
435
  /**
454
436
  * Gets header of a column
455
437
  * @param obj
456
438
  * @returns
457
- */
458
- function get_col_header(obj) {
459
- if (!obj) return '';
460
- if (typeof obj == 'string') return obj;
461
- if (typeof obj != 'object') return '';
462
- return obj.header || obj.title || get_col_name(obj);
463
- }
439
+ */
440
+ export function get_col_header(obj) {
441
+ if (!obj)
442
+ return '';
443
+ if (typeof obj == 'string')
444
+ return obj;
445
+ if (typeof obj != 'object')
446
+ return '';
447
+ return obj.header || obj.title || get_col_name(obj);
448
+ }
464
449
  /**
465
450
  * Gets name of a column from its column object
466
451
  * @param obj
467
452
  * @returns
468
- */
469
- function get_row_id(obj) {
470
- if (!obj) return '-1';
471
- if (typeof obj == 'string') return obj;
472
- if (typeof obj != 'object') return '-1';
473
- return obj.__table_id || obj.id || '-1';
474
- }
453
+ */
454
+ function get_row_id(obj) {
455
+ if (!obj)
456
+ return '-1';
457
+ if (typeof obj == 'string')
458
+ return obj;
459
+ if (typeof obj != 'object')
460
+ return '-1';
461
+ return obj.__table_id || obj.id || '-1';
462
+ }
475
463
  /**
476
464
  * Gets default activation key for grid plugin
477
465
  * @returns
478
- */
479
- function get_default_key() {
480
- return 'ExpiryDate=2999-01-01,Z=1040577552';
481
- }
482
- //check if two cache objects are the same
483
- function validateCache(current, old, looseCheck = false) {
484
- if (!old || !(0, _object.object_equals)(current, old)) return false;
485
- if (looseCheck) return true;
486
- if (current.ds != old.ds) return false;
487
- return true;
488
- }
489
- /* -------------------------------------------------------------------------- */
490
- var _default = AzGrid;
491
- exports.default = _default;
466
+ */
467
+ function get_default_key() {
468
+ return 'ExpiryDate=2999-01-01,Z=1040577552';
469
+ }
470
+ //check if two cache objects are the same
471
+ function validateCache(current, old, looseCheck = false) {
472
+ if (!old || !object_equals(current, old))
473
+ return false;
474
+ if (looseCheck)
475
+ return true;
476
+ if (current.ds != old.ds)
477
+ return false;
478
+ return true;
479
+ }
480
+ /* -------------------------------------------------------------------------- */
481
+ export default AzGrid;