@azrico/reactgrid 1.2.5 → 1.2.7

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.
@@ -0,0 +1,288 @@
1
+ import { ColumnDefinition, LooseColumnDefinition } from '../views/DataGrid';
2
+
3
+ import BoolEditor from '@inovua/reactdatagrid-community/BoolEditor';
4
+ import NumericEditor from '@inovua/reactdatagrid-community/NumericEditor';
5
+ import SelectEditor from '@inovua/reactdatagrid-community/SelectEditor';
6
+ import { fix_parent_child_relations } from './func_child';
7
+ import { object_equals } from '@azrico/object';
8
+
9
+ export type MakeWriteable<Type> = {
10
+ -readonly [Key in keyof Type]: Type[Key];
11
+ };
12
+ export type ModifyFields<T, R> = Omit<T, keyof R> & R;
13
+
14
+ const defaultColumnOptions: ColumnDefinition = {
15
+ draggable: false,
16
+ lockable: false,
17
+ editable: false,
18
+ };
19
+
20
+ export async function ApplyDataSource(datasource, props) {
21
+ let result_data: any = undefined;
22
+
23
+ let isUsingCache = false;
24
+
25
+ const cacheInfo = { ds: datasource, skip: props.skip, limit: props.limit };
26
+ //check if we can used cached data
27
+ //if the datasource is changed since last render, we should invalidate cache
28
+ if (!validateCache(props.last_cache_info.current, cacheInfo, props.looseCacheCheck)) {
29
+ props.cached_data.current = undefined;
30
+ props.last_cache_info.current = cacheInfo;
31
+ }
32
+
33
+ if (props.cached_data.current) {
34
+ //cache is valid, use the cache
35
+ isUsingCache = true;
36
+ result_data = props.cached_data.current;
37
+ } else {
38
+ //cache is invalid, use the datasource again
39
+
40
+ isUsingCache = false;
41
+ if (typeof datasource === 'function') {
42
+ result_data = await datasource(props);
43
+ } else result_data = datasource;
44
+ }
45
+
46
+ //making sure the datasource is a valid object :
47
+ if (!result_data) result_data = [];
48
+ if (Array.isArray(result_data) || typeof result_data !== 'object')
49
+ result_data = { data: result_data };
50
+
51
+ if (!Array.isArray(result_data.data)) result_data.data = [result_data.data];
52
+
53
+ //no need to fix parent/child when using cache (it should be already fixed)
54
+ if (!isUsingCache && props.allowChildRows)
55
+ if (!result_data.count)
56
+ //result_data.data = fix_parent_child_relations(result_data.data);
57
+
58
+ result_data.count = (result_data.data as Array<any>).length;
59
+
60
+ //update cache
61
+ props.cached_data.current = result_data;
62
+ return result_data;
63
+ }
64
+ /**
65
+ * merges together all different props for columns
66
+ * to build a single final column data array
67
+ * @param column_data
68
+ * @returns
69
+ */
70
+ export function ApplyColumnData(column_data: any): ColumnDefinition[] {
71
+ const final_columns: MakeWriteable<LooseColumnDefinition>[] = [];
72
+ if (!column_data) column_data = {};
73
+ if (Array.isArray(column_data)) column_data = { columns: column_data };
74
+ let mcols = column_data.columns || [];
75
+ let mcolExts = column_data.columnExtensions || [];
76
+ let rightColumns = column_data.rightColumns || [];
77
+ let leftColumns = column_data.leftColumns || [];
78
+ let editableColumns = column_data.editableColumns || [];
79
+ let customColumns = column_data.customColumns || [];
80
+
81
+ if (!Array.isArray(mcols)) return [];
82
+ /* ------------------------------- fix columns ------------------------------ */
83
+
84
+ mcols
85
+ .map((r) => {
86
+ if (r == null) return null;
87
+ if (typeof r === 'string') return { field: r, header: r };
88
+ if (typeof r === 'object') {
89
+ let final_cl: MakeWriteable<ColumnDefinition> = {
90
+ name: get_col_name(r),
91
+ header: get_col_header(r),
92
+ width: r.width || undefined,
93
+ };
94
+ final_cl = {
95
+ ...defaultColumnOptions,
96
+ ...r,
97
+ ...final_cl,
98
+ };
99
+ return final_cl;
100
+ }
101
+ return null;
102
+ })
103
+ .forEach((r) => {
104
+ if (r != null) final_columns.push(r);
105
+ });
106
+
107
+ /* ---------------------------- apply extensions ---------------------------- */
108
+ if (mcolExts && Array.isArray(mcolExts))
109
+ for (let index = 0; index < mcolExts.length; index++) {
110
+ const ext = mcolExts[index];
111
+ const ext_cl = get_col_name(ext);
112
+ if (!ext_cl) {
113
+ console.warn('ColumnExtension must have a name');
114
+ continue;
115
+ }
116
+ const mcol = final_columns.find((s) => get_col_name(s) === ext_cl);
117
+ if (mcol) {
118
+ for (const key in ext) {
119
+ if (!mcol[key] && key != 'name') mcol[key] = ext[key];
120
+ }
121
+ }
122
+ // if (ext.widthGrow) mcol.widthGrow = ext.widthGrow;
123
+ }
124
+
125
+ /* ------------------------------- frozen cols ------------------------------ */
126
+ if (Array.isArray(leftColumns)) {
127
+ for (let index = 0; index < leftColumns.length; index++) {
128
+ const r = get_col_name(leftColumns[index]);
129
+ const mcol = final_columns.find((s) => get_col_name(s) === r);
130
+ if (mcol) {
131
+ mcol.locked = 'start';
132
+ }
133
+ }
134
+ }
135
+ if (Array.isArray(rightColumns)) {
136
+ for (let index = 0; index < rightColumns.length; index++) {
137
+ const r = get_col_name(rightColumns[index]);
138
+ const mcol = final_columns.find((s) => get_col_name(s) === r);
139
+ if (mcol) {
140
+ mcol.locked = 'end';
141
+ }
142
+ }
143
+ }
144
+ /* -------------------------------- ediatable ------------------------------- */
145
+ if (Array.isArray(editableColumns)) {
146
+ for (let index = 0; index < editableColumns.length; index++) {
147
+ const r = get_col_name(editableColumns[index]);
148
+ const mcol = final_columns.find((s) => get_col_name(s) === r);
149
+ if (mcol) {
150
+ mcol.editable = true;
151
+ }
152
+ }
153
+ }
154
+ for (let index = 0; index < final_columns.length; index++) {
155
+ const col = final_columns[index];
156
+ if (col.type) {
157
+ switch (col.type) {
158
+ case 'combo':
159
+ case 'select':
160
+ //@ts-ignore
161
+ col.editor = SelectEditor;
162
+ break;
163
+ case 'number':
164
+ case 'numberic':
165
+ //@ts-ignore
166
+ col.editor = NumericEditor;
167
+ break;
168
+ case 'truefalse':
169
+ case 'bool':
170
+ //@ts-ignore
171
+ col.editor = BoolEditor;
172
+ break;
173
+ }
174
+ if (col.editable != false) col.editable = true;
175
+ delete col.type;
176
+ }
177
+ //@ts-ignore
178
+ if (!col.editorProps) col.editorProps = {};
179
+ if (col.items) {
180
+ //@ts-ignore
181
+ if (!col.editorProps.idProperty) col.editorProps.idProperty = 'id';
182
+ //@ts-ignore
183
+ if (!col.editorProps.collapseOnSelect) col.editorProps.collapseOnSelect = true;
184
+ //@ts-ignore
185
+ col.editorProps.dataSource = to_datasource(col.items, col.editorProps.idProperty);
186
+
187
+ delete col.items;
188
+ }
189
+ }
190
+
191
+ /* -------------------------------------------------------------------------- */
192
+ if (Array.isArray(customColumns)) {
193
+ for (let index = 0; index < customColumns.length; index++) {
194
+ const r = customColumns[index];
195
+ const rn = get_col_name(r);
196
+ const mcol = final_columns.find((s) => get_col_name(s) === rn);
197
+ if (mcol) {
198
+ //@ts-ignore
199
+ mcol.render = r.render;
200
+ }
201
+ }
202
+ }
203
+
204
+ /* ------------------------------ apply renames ----------------------------- */
205
+ for (let index = 0; index < final_columns.length; index++) {
206
+ const col = final_columns[index];
207
+ if (!col.name) {
208
+ col.name = get_col_name(col);
209
+ delete col.field;
210
+ delete col.key;
211
+ delete col.columnName;
212
+
213
+ if (col.flex && !col.defaultFlex) col.defaultFlex = col.flex;
214
+ }
215
+ }
216
+ /* -------------------------------------------------------------------------- */
217
+ //console.log(final_columns);
218
+ return final_columns;
219
+ }
220
+
221
+ export function to_datasource(arr: any[], idProperty: string, valueAsID = true) {
222
+ const res: any[] = [];
223
+ let index = 0;
224
+ if (Array.isArray(arr)) {
225
+ arr.map((s: any) => {
226
+ let elem = {};
227
+ if (valueAsID) elem[idProperty] = String(s).toLowerCase();
228
+ else elem[idProperty] = '_cid_' + index;
229
+ if (typeof s === 'object') elem = { ...elem, ...s };
230
+ else elem['label'] = s;
231
+ res.push(elem);
232
+ index++;
233
+ });
234
+ }
235
+
236
+ return res;
237
+ }
238
+ /**
239
+ * Gets name of a column from its column object
240
+ * @param obj
241
+ * @returns
242
+ */
243
+ export function get_col_name(obj: any): string {
244
+ if (!obj) return '';
245
+ if (typeof obj == 'string') return obj;
246
+ if (typeof obj != 'object') return '';
247
+ return obj.name || obj.field || obj.key || obj.columnName || '';
248
+ }
249
+ /**
250
+ * Gets header of a column
251
+ * @param obj
252
+ * @returns
253
+ */
254
+ export function get_col_header(obj: any): string {
255
+ if (!obj) return '';
256
+ if (typeof obj == 'string') return obj;
257
+ if (typeof obj != 'object') return '';
258
+ return obj.header || obj.title || get_col_name(obj);
259
+ }
260
+ /**
261
+ * Gets name of a column from its column object
262
+ * @param obj
263
+ * @returns
264
+ */
265
+ export function get_row_id(obj: any): any {
266
+ if (!obj) return '-1';
267
+ if (typeof obj == 'string') return obj;
268
+ if (typeof obj != 'object') return '-1';
269
+ return obj.__table_id || obj.id || '-1';
270
+ }
271
+
272
+ /**
273
+ * Gets default activation key for grid plugin
274
+ * @returns
275
+ */
276
+ export function get_default_key() {
277
+ return 'ExpiryDate=2999-01-01,Z=1040577552';
278
+ }
279
+
280
+ //check if two cache objects are the same
281
+ export function validateCache(current: any, old: any, looseCheck = false) {
282
+ if (!old || !object_equals(current, old)) return false;
283
+
284
+ if (looseCheck) return true;
285
+
286
+ if (current.ds != old.ds) return false;
287
+ return true;
288
+ }
package/src/index.tsx CHANGED
@@ -1,9 +1,9 @@
1
1
  export * from './views/DataGrid';
2
2
 
3
3
  import AzGrid from './views/DataGrid';
4
- // import ExampleGrid from './example';
5
- // import React from 'react';
6
- // import ReactDOM from 'react-dom/client';
4
+ import ExampleGrid from './example';
5
+ import React from 'react';
6
+ import ReactDOM from 'react-dom/client';
7
7
 
8
8
  /* -------------------------------------------------------------------------- */
9
9