@zeedhi/teknisa-components-common 3.0.0 → 3.0.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.
Files changed (104) hide show
  1. package/.package.json +5 -2
  2. package/dist/teknisa-components-common.js +3722 -32
  3. package/dist/teknisa-components-common.min.js +3722 -32
  4. package/dist/types/components/index.d.ts +5 -0
  5. package/dist/types/components/tek-datasource/index.d.ts +3 -0
  6. package/dist/types/components/tek-datasource/interfaces.d.ts +16 -0
  7. package/dist/types/components/tek-datasource/tek-memory-datasource.d.ts +93 -0
  8. package/dist/types/components/tek-datasource/tek-rest-datasource.d.ts +95 -0
  9. package/dist/types/components/tek-grid/columns-searcher.d.ts +5 -0
  10. package/dist/types/components/tek-grid/dynamic-filter-datasource-factory.d.ts +6 -0
  11. package/dist/types/components/tek-grid/filter-helper.d.ts +7 -0
  12. package/dist/types/components/tek-grid/grid-filter-button.d.ts +29 -0
  13. package/dist/types/components/tek-grid/grouped-data-manager.d.ts +82 -0
  14. package/dist/types/components/tek-grid/grouped-data-selector.d.ts +7 -0
  15. package/dist/types/components/tek-grid/grouped-view-navigator.d.ts +14 -0
  16. package/dist/types/components/tek-grid/index.d.ts +18 -0
  17. package/dist/types/components/tek-grid/interfaces.d.ts +259 -0
  18. package/dist/types/components/tek-grid/keymap-grouped.d.ts +6 -0
  19. package/dist/types/components/tek-grid/layout-options.d.ts +39 -0
  20. package/dist/types/components/tek-grid/tek-grid-column.d.ts +42 -0
  21. package/dist/types/components/tek-grid/tek-grid-columns-button/tek-grid-columns-button-controller.d.ts +8 -0
  22. package/dist/types/components/tek-grid/tek-grid-columns-button/tek-grid-columns-button.d.ts +13 -0
  23. package/dist/types/components/tek-grid/tek-grid-controller.d.ts +31 -0
  24. package/dist/types/components/tek-grid/tek-grid-events.d.ts +31 -0
  25. package/dist/types/components/tek-grid/tek-grid-toolbar-provider/export-options/button-option.d.ts +17 -0
  26. package/dist/types/components/tek-grid/tek-grid-toolbar-provider/export-options/index.d.ts +3 -0
  27. package/dist/types/components/tek-grid/tek-grid-toolbar-provider/export-options/interfaces.d.ts +5 -0
  28. package/dist/types/components/tek-grid/tek-grid-toolbar-provider/export-options/multi-option.d.ts +12 -0
  29. package/dist/types/components/tek-grid/tek-grid-toolbar-provider/index.d.ts +2 -0
  30. package/dist/types/components/tek-grid/tek-grid-toolbar-provider/tek-grid-toolbar-provider.d.ts +22 -0
  31. package/dist/types/components/tek-grid/tek-grid.d.ts +216 -0
  32. package/dist/types/components/tek-user-info/TekUserInfoController.d.ts +22 -0
  33. package/dist/types/components/tek-user-info/interfaces.d.ts +27 -0
  34. package/dist/types/components/tek-user-info/tek-user-info-list.d.ts +32 -0
  35. package/dist/types/components/tek-user-info/tek-user-info.d.ts +37 -0
  36. package/dist/types/error/tek-grid-delete-rows-error.d.ts +7 -0
  37. package/dist/types/error/teknisa-common-error.d.ts +6 -0
  38. package/dist/types/index.d.ts +1 -0
  39. package/dist/types/utils/config/config.d.ts +7 -0
  40. package/dist/types/utils/index.d.ts +3 -0
  41. package/dist/types/utils/is-filled-object/is-filled-object.d.ts +2 -0
  42. package/dist/types/utils/is-nil.d.ts +1 -0
  43. package/package.json +2 -2
  44. package/src/components/index.ts +5 -12
  45. package/src/components/tek-datasource/index.ts +3 -0
  46. package/src/components/tek-datasource/interfaces.ts +36 -0
  47. package/src/components/tek-datasource/tek-memory-datasource.ts +314 -0
  48. package/src/components/tek-datasource/tek-rest-datasource.ts +224 -0
  49. package/src/components/tek-grid/columns-searcher.ts +22 -0
  50. package/src/components/tek-grid/dynamic-filter-datasource-factory.ts +20 -0
  51. package/src/components/tek-grid/filter-helper.ts +20 -0
  52. package/src/components/tek-grid/grid-filter-button.ts +419 -0
  53. package/src/components/tek-grid/grouped-data-manager.ts +448 -0
  54. package/src/components/tek-grid/grouped-data-selector.ts +40 -0
  55. package/src/components/tek-grid/grouped-view-navigator.ts +84 -0
  56. package/src/components/tek-grid/index.ts +18 -0
  57. package/src/components/tek-grid/interfaces.ts +329 -0
  58. package/src/components/tek-grid/keymap-grouped.ts +20 -0
  59. package/src/components/tek-grid/layout-options.ts +248 -0
  60. package/src/components/tek-grid/tek-grid-column.ts +193 -0
  61. package/src/components/tek-grid/tek-grid-columns-button/tek-grid-columns-button-controller.ts +28 -0
  62. package/src/components/tek-grid/tek-grid-columns-button/tek-grid-columns-button.ts +38 -0
  63. package/src/components/tek-grid/tek-grid-controller.ts +140 -0
  64. package/src/components/tek-grid/tek-grid-events.ts +105 -0
  65. package/src/components/tek-grid/tek-grid-toolbar-provider/export-options/button-option.ts +26 -0
  66. package/src/components/tek-grid/tek-grid-toolbar-provider/export-options/index.ts +3 -0
  67. package/src/components/tek-grid/tek-grid-toolbar-provider/export-options/interfaces.ts +6 -0
  68. package/src/components/tek-grid/tek-grid-toolbar-provider/export-options/multi-option.ts +85 -0
  69. package/src/components/tek-grid/tek-grid-toolbar-provider/index.ts +2 -0
  70. package/src/components/tek-grid/tek-grid-toolbar-provider/tek-grid-toolbar-provider.ts +365 -0
  71. package/src/components/tek-grid/tek-grid.ts +1118 -0
  72. package/src/components/tek-user-info/TekUserInfoController.ts +87 -0
  73. package/src/components/tek-user-info/interfaces.ts +21 -0
  74. package/src/components/tek-user-info/tek-user-info-list.ts +64 -0
  75. package/src/components/tek-user-info/tek-user-info.ts +337 -0
  76. package/src/error/tek-grid-delete-rows-error.ts +15 -0
  77. package/src/error/teknisa-common-error.ts +8 -0
  78. package/src/index.ts +1 -0
  79. package/src/utils/config/config.ts +8 -0
  80. package/src/utils/index.ts +3 -0
  81. package/src/utils/is-filled-object/is-filled-object.ts +5 -0
  82. package/src/utils/is-nil.ts +3 -0
  83. package/tests/unit/components/tek-grid/button-option.spec.ts +49 -0
  84. package/tests/unit/components/tek-grid/columns-searcher.spec.ts +112 -0
  85. package/tests/unit/components/tek-grid/dynamic-filter-datasource-factory.spec.ts +90 -0
  86. package/tests/unit/components/tek-grid/filter-helper.spec.ts +34 -130
  87. package/tests/unit/components/tek-grid/grid-filter-button.spec.ts +110 -241
  88. package/tests/unit/components/tek-grid/grouped-data-manager.spec.ts +593 -0
  89. package/tests/unit/components/tek-grid/grouped-data-selector.spec.ts +136 -0
  90. package/tests/unit/components/tek-grid/grouped-view-navigator.spec.ts +244 -0
  91. package/tests/unit/components/tek-grid/keymap-grouped.spec.ts +20 -0
  92. package/tests/unit/components/tek-grid/{layout_options.spec.ts → layout-options.spec.ts} +77 -35
  93. package/tests/unit/components/tek-grid/multi-option.spec.ts +139 -0
  94. package/tests/unit/components/tek-grid/{grid-column.spec.ts → tek-grid-column.spec.ts} +48 -6
  95. package/tests/unit/components/tek-grid/{grid-columns-button.spec.ts → tek-grid-columns-button.spec.ts} +42 -9
  96. package/tests/unit/components/tek-grid/tek-grid-controller.spec.ts +253 -0
  97. package/tests/unit/components/tek-grid/tek-grid-events.spec.ts +186 -0
  98. package/tests/unit/components/tek-grid/tek-grid-toolbar-provider.spec.ts +34 -0
  99. package/tests/unit/components/tek-grid/tek-grid.spec.ts +895 -0
  100. package/tests/unit/components/tek-grid/tek-memory-datasource.spec.ts +482 -0
  101. package/tests/unit/components/tek-grid/tek-rest-datasource.spec.ts +422 -0
  102. package/dist/types/error/delete-rows-error.d.ts +0 -6
  103. package/src/error/delete-rows-error.ts +0 -11
  104. package/tests/unit/components/tek-grid/grid.spec.ts +0 -2701
@@ -0,0 +1,3 @@
1
+ export * from './interfaces';
2
+ export * from './tek-memory-datasource';
3
+ export * from './tek-rest-datasource';
@@ -0,0 +1,36 @@
1
+ import { IDictionary, IMemoryDatasource, IRestDatasource } from '@zeedhi/core';
2
+
3
+ export interface ITekMemoryDatasource extends IMemoryDatasource {
4
+ dynamicFilter?: IDictionary<any>;
5
+ searchJoin?: IDictionary<Array<string | number>>;
6
+ }
7
+
8
+ export interface ITekRestDatasource extends IRestDatasource {
9
+ dynamicFilter?: IDictionary<any>;
10
+ searchJoin?: IDictionary<Array<string | number>>;
11
+ }
12
+
13
+ export interface IDynamicFilterItem {
14
+ relation: string;
15
+ operation: string;
16
+ value: any;
17
+ }
18
+
19
+ export const DynamicFilterOperations: IDictionary<boolean> = {
20
+ CONTAINS: true,
21
+ NOT_CONTAINS: true,
22
+ EQUALS: true,
23
+ NOT_EQUALS: true,
24
+ GREATER_THAN: true,
25
+ LESS_THAN: true,
26
+ GREATER_THAN_EQUALS: true,
27
+ LESS_THAN_EQUALS: true,
28
+ IN: true,
29
+ NOT_IN: true,
30
+ BETWEEN: true,
31
+ };
32
+
33
+ export const DynamicFilterRelations: IDictionary<boolean> = {
34
+ AND: true,
35
+ OR: true,
36
+ };
@@ -0,0 +1,314 @@
1
+ import { DatasourceFactory, IDatasource, IDictionary, MemoryDatasource, URL, Utils } from '@zeedhi/core';
2
+ import {
3
+ DynamicFilterOperations,
4
+ DynamicFilterRelations,
5
+ IDynamicFilterItem,
6
+ ITekMemoryDatasource,
7
+ } from './interfaces';
8
+
9
+ export class TekMemoryDatasource extends MemoryDatasource implements ITekMemoryDatasource {
10
+ /** Dynamic filter data */
11
+ public dynamicFilter!: IDictionary<IDynamicFilterItem[]>;
12
+
13
+ /** Search Join data */
14
+ public searchJoin!: IDictionary<Array<string | number>>;
15
+
16
+ /**
17
+ * Dynamic Filter Operations
18
+ */
19
+ public dynamicFilterOperations = DynamicFilterOperations;
20
+
21
+ /**
22
+ * Dynamic Filter Relations
23
+ */
24
+ public dynamicFilterRelations = DynamicFilterRelations;
25
+
26
+ /**
27
+ * Dynamic Filter applied flag
28
+ */
29
+ protected dynamicFilterApplied = '';
30
+
31
+ /**
32
+ * Create new datasource
33
+ * @param props Datasource properties
34
+ */
35
+ constructor(props: ITekMemoryDatasource) {
36
+ super(props);
37
+
38
+ if (!this.watchUrl) {
39
+ this.dynamicFilter = this.getInitValue('dynamicFilter', props.dynamicFilter, {});
40
+ this.searchJoin = this.getInitValue('searchJoin', props.searchJoin, {});
41
+ }
42
+
43
+ this.createAccessors();
44
+ this.createObjAccessors(this.dynamicFilter, 'dynamicFilter');
45
+ this.createObjAccessors(this.searchJoin, 'searchJoin');
46
+ }
47
+
48
+ protected updateReservedKeys() {
49
+ this.reservedKeys.dynamic_filter = true;
50
+ this.reservedKeys.search_join = true;
51
+ }
52
+
53
+ protected updateInternalProperties(datasource: IDatasource = {}) {
54
+ if (!this.watchUrl) return;
55
+ this.updateReservedKeys();
56
+ super.updateInternalProperties(datasource);
57
+ const queryString = URL.getParsedQueryStringFromUrl();
58
+ this.dynamicFilter = this.getEncodedParam(queryString.dynamic_filter, datasource.dynamicFilter);
59
+ this.searchJoin = this.getEncodedParam(queryString.search_join, datasource.searchJoin);
60
+ }
61
+
62
+ protected getEncodedParam(urlParam: string, datasourceParam: IDictionary<any> = {}): IDictionary<any> {
63
+ return urlParam ? JSON.parse(atob(urlParam)) : datasourceParam;
64
+ }
65
+
66
+ protected getQueryStringValues(): IDictionary<any> {
67
+ const values = super.getQueryStringValues();
68
+ if (this.dynamicFilter && Object.keys(this.dynamicFilter).length) {
69
+ values.dynamic_filter = btoa(JSON.stringify(this.dynamicFilter));
70
+ }
71
+ if (this.searchJoin && Object.keys(this.searchJoin).length) {
72
+ values.search_join = btoa(JSON.stringify(this.searchJoin));
73
+ }
74
+ return values;
75
+ }
76
+
77
+ protected getUrlQueryString() {
78
+ const superQueryString = super.getUrlQueryString();
79
+ const query = URL.getParsedQueryStringFromUrl();
80
+
81
+ let dynamicFilterQuerystring = '';
82
+ if (query.dynamic_filter) {
83
+ dynamicFilterQuerystring = `&${URL.getFormattedQueryString({
84
+ dynamic_filter: query.dynamic_filter,
85
+ })}`;
86
+ }
87
+
88
+ let searchJoinQuerystring = '';
89
+ if (query.search_join) {
90
+ searchJoinQuerystring = `&${URL.getFormattedQueryString({
91
+ search_join: query.search_join,
92
+ })}`;
93
+ }
94
+ return superQueryString + dynamicFilterQuerystring + searchJoinQuerystring;
95
+ }
96
+
97
+ /**
98
+ * Adds a new dynamic filter position or replace if exists
99
+ * @param column Dynamic Filter column name
100
+ * @param value Dynamic Filter value
101
+ * @returns Promise with data collection
102
+ */
103
+ public addDynamicFilter(column: string, value: any) {
104
+ if (this.isValidDynamicFilterValue(column, value)) {
105
+ this.dynamicFilter[column] = value;
106
+ return this.updateDynamicFilter();
107
+ }
108
+ return this.removeDynamicFilter(column);
109
+ }
110
+
111
+ /**
112
+ * Removes a dynamic filter position
113
+ * @param column Dynamic Filter column name
114
+ * @returns Promise with data collection
115
+ */
116
+ public removeDynamicFilter(column: string) {
117
+ delete this.dynamicFilter[column];
118
+ return this.updateDynamicFilter();
119
+ }
120
+
121
+ /**
122
+ * Sets new dynamic filter value
123
+ * @param filter Dynamic Filter value
124
+ * @returns Promise with data collection
125
+ */
126
+ public setDynamicFilter(filter: IDictionary<any>) {
127
+ this.dynamicFilter = {};
128
+ Object.keys(filter).forEach((column: string) => {
129
+ if (this.isValidDynamicFilterValue(column, filter[column])) {
130
+ this.dynamicFilter[column] = filter[column];
131
+ } else {
132
+ delete this.dynamicFilter[column];
133
+ }
134
+ });
135
+ return this.updateDynamicFilter();
136
+ }
137
+
138
+ /**
139
+ * Clears Dynamic filter value
140
+ * @returns Promise with data collection
141
+ */
142
+ public clearDynamicFilter() {
143
+ this.dynamicFilter = {};
144
+ return this.updateDynamicFilter();
145
+ }
146
+
147
+ /**
148
+ * Resets page and selected rows and tries to update the url
149
+ * @returns Promise with data collection
150
+ */
151
+ public async updateDynamicFilter() {
152
+ this.page = this.firstPage;
153
+ this.selectedRows = [];
154
+ this.visibleSelectedRows = [];
155
+ if (this.watchUrl) {
156
+ if (this.dynamicFilter && Object.keys(this.dynamicFilter).length) {
157
+ URL.updateQueryString({
158
+ dynamic_filter: btoa(JSON.stringify(this.dynamicFilter)),
159
+ });
160
+ } else {
161
+ URL.updateQueryString({
162
+ dynamic_filter: undefined,
163
+ });
164
+ }
165
+ }
166
+ return this.get();
167
+ }
168
+
169
+ /**
170
+ * Checks if a filter value is valid
171
+ * @param value Filter value
172
+ * @returns Is valid filter value
173
+ */
174
+ protected isValidDynamicFilterValue(column: string, value?: IDictionary<any>[]) {
175
+ return (
176
+ !this.reservedKeys[column] &&
177
+ value &&
178
+ value.length > 0 &&
179
+ value.every(
180
+ (filterValue) =>
181
+ this.dynamicFilterOperations[filterValue.operation] &&
182
+ this.dynamicFilterRelations[filterValue.relation] &&
183
+ filterValue.value !== '' &&
184
+ filterValue.value !== null,
185
+ )
186
+ );
187
+ }
188
+
189
+ public clone() {
190
+ return {
191
+ ...super.clone(),
192
+ dynamicFilter: this.dynamicFilter,
193
+ searchJoin: this.searchJoin,
194
+ type: 'tek-memory',
195
+ };
196
+ }
197
+
198
+ /**
199
+ * Updates filtered data
200
+ */
201
+ protected updateFilteredData() {
202
+ // first apply filters (simple and dynamic)
203
+ this.filteredData = Object.keys(this.filter).length
204
+ ? this.allData.filter((row) => this.getRowByFilter(row))
205
+ : Array.from(this.allData);
206
+
207
+ if (this.dynamicFilter && Object.keys(this.dynamicFilter).length) {
208
+ this.filteredData = this.filteredData.filter((row) => this.getRowByDynamicFilter(row));
209
+ }
210
+
211
+ const searchWithoutSearchJoin = (row: IDictionary<any>) => {
212
+ const searchRow = { ...row };
213
+ if (this.searchJoin) {
214
+ // do not search on columns with searchJoin
215
+ Object.keys(this.searchJoin).forEach((key) => delete searchRow[key]);
216
+ }
217
+ return this.getRowBySearch(searchRow);
218
+ };
219
+
220
+ // only after do the search
221
+ const searchData = this.search ? this.filteredData.filter(searchWithoutSearchJoin) : this.filteredData;
222
+
223
+ let searchIds = searchData.map((row) => row[this.uniqueKey]);
224
+
225
+ if (this.searchJoin && Object.keys(this.searchJoin).length) {
226
+ const searchJoinData = this.filteredData.filter((row) => this.getRowBySearchJoin(row));
227
+
228
+ // get the ids from search and searchJoin
229
+ searchIds = searchIds.concat(searchJoinData.map((row) => row[this.uniqueKey])).sort();
230
+ }
231
+
232
+ // filter filteredData using searchIds
233
+ this.filteredData = this.allData.filter((row) => searchIds.indexOf(row[this.uniqueKey]) !== -1);
234
+ }
235
+
236
+ protected getRowByDynamicFilter(row: IDictionary<any>) {
237
+ let filtered: any;
238
+ try {
239
+ Object.keys(this.dynamicFilter).forEach((key) => {
240
+ const filterItems = this.dynamicFilter[key];
241
+ filterItems.forEach((item) => {
242
+ if (filtered === false && item.relation === 'AND') return;
243
+ if (filtered === true && item.relation === 'OR') return;
244
+
245
+ const columnValue = Utils.normalize(row[key].toString());
246
+ let value: string | string[] = '';
247
+ if (Array.isArray(item.value)) {
248
+ value = item.value.map((val: string) => Utils.normalize(val.toString()));
249
+
250
+ switch (item.operation) {
251
+ case 'IN':
252
+ filtered = value.includes(columnValue);
253
+ break;
254
+ case 'NOT_IN':
255
+ filtered = !value.includes(columnValue);
256
+ break;
257
+ case 'BETWEEN':
258
+ filtered =
259
+ (Number(columnValue) || columnValue) >= (Number(value[0]) || value[0]) &&
260
+ (Number(columnValue) || columnValue) <= (Number(value[1]) || value[1]);
261
+ break;
262
+ default:
263
+ break;
264
+ }
265
+ } else {
266
+ value = Utils.normalize(item.value.toString());
267
+
268
+ switch (item.operation) {
269
+ case 'CONTAINS':
270
+ filtered = columnValue.indexOf(value) !== -1;
271
+ break;
272
+ case 'NOT_CONTAINS':
273
+ filtered = columnValue.indexOf(value) === -1;
274
+ break;
275
+ case 'EQUALS':
276
+ filtered = columnValue === value;
277
+ break;
278
+ case 'NOT_EQUALS':
279
+ filtered = columnValue !== value;
280
+ break;
281
+ case 'GREATER_THAN':
282
+ filtered = (Number(columnValue) || columnValue) > (Number(value) || value);
283
+ break;
284
+ case 'LESS_THAN':
285
+ filtered = (Number(columnValue) || columnValue) < (Number(value) || value);
286
+ break;
287
+ case 'GREATER_THAN_EQUALS':
288
+ filtered = (Number(columnValue) || columnValue) >= (Number(value) || value);
289
+ break;
290
+ case 'LESS_THAN_EQUALS':
291
+ filtered = (Number(columnValue) || columnValue) <= (Number(value) || value);
292
+ break;
293
+ default:
294
+ break;
295
+ }
296
+ }
297
+ });
298
+ });
299
+ } catch {
300
+ // do nothing
301
+ }
302
+ return filtered;
303
+ }
304
+
305
+ protected getRowBySearchJoin(row: IDictionary<any>) {
306
+ return Object.keys(this.searchJoin).some((key) => this.searchJoin[key].includes(row[key]));
307
+ }
308
+
309
+ public hasFilter(columnName: string): boolean {
310
+ return this.dynamicFilter[columnName] && this.dynamicFilter[columnName].length > 0;
311
+ }
312
+ }
313
+
314
+ DatasourceFactory.register('tek-memory', TekMemoryDatasource);
@@ -0,0 +1,224 @@
1
+ import { DatasourceFactory, IDatasource, IDictionary, RestDatasource, URL } from '@zeedhi/core';
2
+ import { DynamicFilterOperations, DynamicFilterRelations, IDynamicFilterItem, ITekRestDatasource } from './interfaces';
3
+
4
+ export class TekRestDatasource extends RestDatasource implements ITekRestDatasource {
5
+ /** Dynamic filter data */
6
+ public dynamicFilter!: IDictionary<IDynamicFilterItem[]>;
7
+
8
+ /** Search Join data */
9
+ public searchJoin!: IDictionary<Array<string | number>>;
10
+
11
+ /**
12
+ * Dynamic Filter Operations
13
+ */
14
+ public dynamicFilterOperations = DynamicFilterOperations;
15
+
16
+ /**
17
+ * Dynamic Filter Relations
18
+ */
19
+ public dynamicFilterRelations = DynamicFilterRelations;
20
+
21
+ /**
22
+ * Dynamic Filter applied flag
23
+ */
24
+ protected dynamicFilterApplied = '';
25
+
26
+ /**
27
+ * Create new datasource
28
+ * @param props Datasource properties
29
+ */
30
+ constructor(props: ITekRestDatasource) {
31
+ super({ ...props, lazyLoad: true });
32
+
33
+ if (!this.watchUrl) {
34
+ this.dynamicFilter = this.getInitValue('dynamicFilter', props.dynamicFilter, {});
35
+ this.searchJoin = this.getInitValue('searchJoin', props.searchJoin, {});
36
+ }
37
+ this.lazyLoad = this.getInitValue('lazyLoad', props.lazyLoad, this.lazyLoad);
38
+
39
+ this.createAccessors();
40
+ this.createObjAccessors(this.dynamicFilter, 'dynamicFilter');
41
+ this.createObjAccessors(this.searchJoin, 'searchJoin');
42
+ }
43
+
44
+ protected updateReservedKeys() {
45
+ this.reservedKeys.dynamic_filter = true;
46
+ this.reservedKeys.search_join = true;
47
+ }
48
+
49
+ protected updateInternalProperties(datasource: IDatasource = {}) {
50
+ if (!this.watchUrl) return;
51
+ this.updateReservedKeys();
52
+ super.updateInternalProperties(datasource);
53
+ const queryString = URL.getParsedQueryStringFromUrl();
54
+ this.dynamicFilter = this.getEncodedParam(queryString.dynamic_filter, datasource.dynamicFilter);
55
+ this.searchJoin = this.getEncodedParam(queryString.search_join, datasource.searchJoin);
56
+ }
57
+
58
+ protected getEncodedParam(urlParam: string, datasourceParam: IDictionary<any> = {}): IDictionary<any> {
59
+ return urlParam ? JSON.parse(atob(urlParam)) : datasourceParam;
60
+ }
61
+
62
+ protected getQueryStringValues(): IDictionary<any> {
63
+ const values = super.getQueryStringValues();
64
+ if (this.dynamicFilter && Object.keys(this.dynamicFilter).length) {
65
+ values.dynamic_filter = btoa(JSON.stringify(this.dynamicFilter));
66
+ }
67
+ if (this.searchJoin && Object.keys(this.searchJoin).length) {
68
+ values.search_join = btoa(JSON.stringify(this.searchJoin));
69
+ }
70
+ return values;
71
+ }
72
+
73
+ protected getUrlQueryString() {
74
+ const superQueryString = super.getUrlQueryString();
75
+ const query = URL.getParsedQueryStringFromUrl();
76
+
77
+ let dynamicFilterQuerystring = '';
78
+ if (query.dynamic_filter) {
79
+ dynamicFilterQuerystring = `&${URL.getFormattedQueryString({
80
+ dynamic_filter: query.dynamic_filter,
81
+ })}`;
82
+ }
83
+
84
+ let searchJoinQuerystring = '';
85
+ if (query.search_join) {
86
+ searchJoinQuerystring = `&${URL.getFormattedQueryString({
87
+ search_join: query.search_join,
88
+ })}`;
89
+ }
90
+
91
+ return superQueryString + dynamicFilterQuerystring + searchJoinQuerystring;
92
+ }
93
+
94
+ /**
95
+ * Adds a new dynamic filter position or replace if exists
96
+ * @param column Dynamic Filter column name
97
+ * @param value Dynamic Filter value
98
+ * @returns Promise with data collection
99
+ */
100
+ public addDynamicFilter(column: string, value: any) {
101
+ if (this.isValidDynamicFilterValue(column, value)) {
102
+ this.dynamicFilter[column] = value;
103
+ return this.updateDynamicFilter();
104
+ }
105
+ return this.removeDynamicFilter(column);
106
+ }
107
+
108
+ /**
109
+ * Removes a dynamic filter position
110
+ * @param column Dynamic Filter column name
111
+ * @returns Promise with data collection
112
+ */
113
+ public removeDynamicFilter(column: string) {
114
+ delete this.dynamicFilter[column];
115
+ return this.updateDynamicFilter();
116
+ }
117
+
118
+ /**
119
+ * Sets new dynamic filter value
120
+ * @param filter Dynamic Filter value
121
+ * @returns Promise with data collection
122
+ */
123
+ public setDynamicFilter(filter: IDictionary<any>) {
124
+ this.dynamicFilter = {};
125
+ Object.keys(filter).forEach((column: string) => {
126
+ if (this.isValidDynamicFilterValue(column, filter[column])) {
127
+ this.dynamicFilter[column] = filter[column];
128
+ } else {
129
+ delete this.dynamicFilter[column];
130
+ }
131
+ });
132
+ return this.updateDynamicFilter();
133
+ }
134
+
135
+ /**
136
+ * Clears Dynamic filter value
137
+ * @returns Promise with data collection
138
+ */
139
+ public clearDynamicFilter() {
140
+ this.dynamicFilter = {};
141
+ return this.updateDynamicFilter();
142
+ }
143
+
144
+ /**
145
+ * Resets page and selected rows and tries to update the url
146
+ * @returns Promise with data collection
147
+ */
148
+ public async updateDynamicFilter() {
149
+ this.page = this.firstPage;
150
+ this.selectedRows = [];
151
+ this.visibleSelectedRows = [];
152
+ if (this.watchUrl) {
153
+ if (this.dynamicFilter && Object.keys(this.dynamicFilter).length) {
154
+ URL.updateQueryString({
155
+ dynamic_filter: btoa(JSON.stringify(this.dynamicFilter)),
156
+ });
157
+ } else {
158
+ URL.updateQueryString({
159
+ dynamic_filter: undefined,
160
+ });
161
+ }
162
+ }
163
+ return this.get();
164
+ }
165
+
166
+ /**
167
+ * Checks if a filter value is valid
168
+ * @param value Filter value
169
+ * @returns Is valid filter value
170
+ */
171
+ protected isValidDynamicFilterValue(column: string, value?: IDictionary<any>[]) {
172
+ return (
173
+ !this.reservedKeys[column] &&
174
+ value &&
175
+ value.length > 0 &&
176
+ value.every(
177
+ (filterValue) =>
178
+ this.dynamicFilterOperations[filterValue.operation] &&
179
+ this.dynamicFilterRelations[filterValue.relation] &&
180
+ filterValue.value !== '' &&
181
+ filterValue.value !== null,
182
+ )
183
+ );
184
+ }
185
+
186
+ /**
187
+ * Retrieves request params
188
+ */
189
+ protected getRequestParams(): any {
190
+ const requestParams = super.getRequestParams();
191
+ const isNotEmptyObj = (obj?: IDictionary) => !!(obj && Object.keys(obj).length);
192
+ const isValid =
193
+ this.dynamicFilter &&
194
+ Object.keys(this.dynamicFilter).every((column) => {
195
+ const value = this.dynamicFilter[column];
196
+
197
+ return value && value.length > 0 && this.isValidDynamicFilterValue(column, value);
198
+ });
199
+
200
+ if (isNotEmptyObj(this.dynamicFilter) && isValid) {
201
+ requestParams.dynamic_filter = btoa(JSON.stringify(this.dynamicFilter));
202
+ }
203
+
204
+ if (isNotEmptyObj(this.searchJoin)) {
205
+ requestParams.search_join = btoa(JSON.stringify(this.searchJoin));
206
+ }
207
+ return requestParams;
208
+ }
209
+
210
+ public hasFilter(columnName: string): boolean {
211
+ return this.dynamicFilter[columnName] && this.dynamicFilter[columnName].length > 0;
212
+ }
213
+
214
+ public clone() {
215
+ return {
216
+ ...super.clone(),
217
+ dynamicFilter: this.dynamicFilter,
218
+ searchJoin: this.searchJoin,
219
+ type: 'tek-rest',
220
+ };
221
+ }
222
+ }
223
+
224
+ DatasourceFactory.register('tek-rest', TekRestDatasource);
@@ -0,0 +1,22 @@
1
+ import { IDictionary } from '@zeedhi/core';
2
+ import { IColumnsSearcher, ITekGridColumn } from './interfaces';
3
+
4
+ export class ColumnsSearcher implements IColumnsSearcher {
5
+ async searchColumn(columns: ITekGridColumn[], search: string): Promise<IDictionary<(string | number)[]>> {
6
+ const lookupColumns = columns.filter((column) => !!column.componentProps?.datasource && !column.skipLookupSearch);
7
+ const searchJoin: IDictionary<Array<string | number>> = {};
8
+
9
+ if (search) {
10
+ const promises = lookupColumns.map(async (column) => {
11
+ const searchData = await column.memorySearch(search);
12
+
13
+ const lookupId = column.componentProps!.datasource.uniqueKey;
14
+ searchJoin[column.name] = searchData.map((row: IDictionary) => row[lookupId]);
15
+ });
16
+
17
+ await Promise.all(promises);
18
+ }
19
+
20
+ return searchJoin;
21
+ }
22
+ }
@@ -0,0 +1,20 @@
1
+ import { Datasource, DatasourceFactory, IDictionary } from '@zeedhi/core';
2
+ import { IDynamicFilterDatasourceFactory } from './interfaces';
3
+
4
+ export class DynamicFilterDatasourceFactory implements IDynamicFilterDatasourceFactory {
5
+ factory(options: IDictionary<boolean>): Datasource {
6
+ return DatasourceFactory.factory({
7
+ data: this.getFilterOptionsData(options),
8
+ loadAll: true,
9
+ translate: ['text'],
10
+ });
11
+ }
12
+
13
+ private getFilterOptionsData(options: IDictionary<boolean>): IDictionary<any>[] {
14
+ const optionsKeys = Object.keys(options);
15
+ return optionsKeys.map((item) => ({
16
+ text: `TEKGRID_${item}`,
17
+ value: item,
18
+ }));
19
+ }
20
+ }
@@ -0,0 +1,20 @@
1
+ import { DateHelper } from '@zeedhi/core';
2
+ import { TekGridColumn } from './tek-grid-column';
3
+
4
+ export class TekFilterHelper {
5
+ public static getLabel(name: string) {
6
+ return DateHelper.getLabel(name);
7
+ }
8
+
9
+ public static getValue(name: string, column: TekGridColumn) {
10
+ return DateHelper.getValue(name, column.componentProps.dateFormat);
11
+ }
12
+
13
+ public static register(name: string, label: string, fn: () => Date | [Date, Date]) {
14
+ DateHelper.register(name, label, fn);
15
+ }
16
+
17
+ public static unregister(name: string) {
18
+ DateHelper.unregister(name);
19
+ }
20
+ }