react-graph-grid 0.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.
package/src/GridGR.jsx ADDED
@@ -0,0 +1,310 @@
1
+ import { useState, useEffect } from 'react';
2
+ import { GridClass } from './Grid';
3
+ import { GraphClass } from './Graph';
4
+ // ==================================================================================================================================================================
5
+ export function GridGR(props) {
6
+ let grid = null;
7
+
8
+ const [gridState, setState] = useState({ grid: grid, ind: 0 });
9
+
10
+ grid = gridState.grid;
11
+ let needGetRows = false;
12
+ if (!grid || grid.uid !== props.uid && props.uid != null) {
13
+ grid = null;
14
+ if (props.findGrid) {
15
+ grid = props.findGrid(props);
16
+ }
17
+ grid = grid || new GridGRClass(props);
18
+ needGetRows = !props.noAutoRefresh && !props.parentGrids;
19
+ }
20
+
21
+ if (props.init) {
22
+ props.init(grid);
23
+ }
24
+
25
+ grid.refreshState = function () {
26
+ setState({ grid: grid, ind: grid.stateind++ });
27
+ }
28
+
29
+ grid._waitingRows = needGetRows && (grid.rows.length <= 0 || grid.columns.length <= 0);
30
+
31
+ useEffect(() => {
32
+ grid.setupEvents(grid);
33
+
34
+ if (grid._waitingRows) {
35
+
36
+ grid.getRows({ filters: grid.collectFilters(), grid: grid }).then(
37
+ rows => {
38
+ grid.rows = rows;
39
+ grid.afterGetRows();
40
+ grid.refreshState();
41
+ }
42
+ ).finally(() => {
43
+ grid._waitingRows = false;
44
+ grid.refreshState();
45
+ });
46
+ }
47
+ else if (grid.columns.length <= 0 && grid.getColumns) {
48
+ grid.prepareColumns().then(() => grid.refreshState());
49
+ }
50
+
51
+ return () => {
52
+ grid.clearEvents();
53
+ }
54
+ }, [grid])
55
+
56
+ return (grid.render());
57
+ }
58
+ // -------------------------------------------------------------------------------------------------------------------------------------------------------------
59
+ export class GridGRClass extends GridClass {
60
+
61
+ constructor(props) {
62
+ super(props);
63
+
64
+ const grid = this;
65
+
66
+ if (props.entity) {
67
+ grid.entity = props.entity;
68
+ }
69
+
70
+ if (props.getDefaultLinkContent) {
71
+ grid.getDefaultLinkContent = props.getDefaultLinkContent;
72
+ }
73
+
74
+ if (!props.graph && (props.parentGrids || props.uid)) {
75
+ grid.graphUid = props.graphUid || "defaultGraphUID";
76
+
77
+ grid.parentGrids = props.parentGrids;
78
+
79
+ window._graphDict = window._graphDict || {};
80
+
81
+ window._graphDict[grid.graphUid] = window._graphDict[grid.graphUid] || new GraphClass();
82
+ const graph = window._graphDict[grid.graphUid];
83
+
84
+ while (graph.nodesDict[GridClass._seq]) {
85
+ GridClass._seq++;
86
+ }
87
+ grid.id = GridClass._seq++;
88
+
89
+ grid.graph = graph;
90
+ graph.uid = grid.graphUid;
91
+
92
+ grid.parents = [];
93
+ grid.children = [];
94
+
95
+ if (props.parentGrids) {
96
+ grid.graph.needCheckIntegrity = true;
97
+ }
98
+
99
+ grid.uid = props.uid != null ? props.uid : grid.id;
100
+ graph.nodeCount++;
101
+ graph.nodesDict[grid.uid] = grid;
102
+
103
+ }
104
+ else {
105
+ grid.uid = props.uid != null ? props.uid : grid.id;
106
+ }
107
+ }
108
+ // -------------------------------------------------------------------------------------------------------------------------------------------------------------
109
+ log(message, pref) {
110
+ const grid = this;
111
+ pref = pref || `grid#${grid.uid ? grid.id + '(' + grid.uid + ')' : grid.id} ${grid.title || ''}`;
112
+ super.log(`${pref}: ` + message, ' ');
113
+ }
114
+ // -------------------------------------------------------------------------------------------------------------------------------------------------------------
115
+ clearEvents() {
116
+ const grid = this;
117
+
118
+ super.clearEvents();
119
+
120
+ if (window._graphDict && grid.graphUid) {
121
+ grid.log(' delete graph')
122
+ delete window._graphDict[grid.graphUid];
123
+ }
124
+ }
125
+ // -------------------------------------------------------------------------------------------------------------------------------------------------------------
126
+ connectToParents(noDetectCycles) {
127
+ const grid = this;
128
+ const graph = grid.graph;
129
+
130
+ grid.connectedToParents = true;
131
+ graph.waveCache = {};
132
+ if (!grid.parentGrids) return;
133
+
134
+ const parentUids = ',' + grid.parentGrids + ',';
135
+ for (let uid in graph.nodesDict) {
136
+ if (uid === grid.uid) continue;
137
+
138
+ let parentGrid = graph.nodesDict[uid];
139
+ if (parentUids.indexOf(parentGrid.uid) <= 0) continue;
140
+
141
+ const link = { content: grid.getDefaultLinkContent(), parent: parentGrid, child: grid };
142
+
143
+ const lkey = grid.id + '_' + parentGrid.id;
144
+ graph.linksDict[lkey] = link;
145
+ grid.parents.push(parentGrid.uid);
146
+ parentGrid.children.push(grid.uid);
147
+ }
148
+
149
+ if (!noDetectCycles) {
150
+ graph.markCycles();
151
+ }
152
+ }
153
+ // -------------------------------------------------------------------------------------------------------------------------------------------------------------
154
+ getDefaultLinkContent() {
155
+ const grid = this;
156
+ return {
157
+ applyLink: function (link) {
158
+ if (!link.parent || !link.parent.rows) return '';
159
+
160
+ if (link.parent.getConnectContent) {
161
+ return link.parent.getConnectContent({ child: grid });
162
+ }
163
+
164
+ return link.parent.selectedValue();
165
+ }
166
+ };
167
+ }
168
+ // -------------------------------------------------------------------------------------------------------------------------------------------------------------
169
+ getEntity() {
170
+ const grid = this;
171
+ return grid.entity || 'grid_' + (grid.uid || grid.id);
172
+ }
173
+ // -------------------------------------------------------------------------------------------------------------------------------------------------------------
174
+ onSelectedRowChanged(e) {
175
+ super.onSelectedRowChanged(e);
176
+
177
+ const grid = this;
178
+ const graph = grid.graph;
179
+ if (graph && (!graph._isMakingWave || e.source == 'rowClick')) {
180
+
181
+ if (!grid.connectedToParents) {
182
+ grid.connectToParents();
183
+ }
184
+
185
+ if (graph.needCheckIntegrity) {
186
+ grid.checkGraphIntegrity();
187
+ }
188
+
189
+ if (graph.checkNeedTriggerWave && !graph.checkNeedTriggerWave(grid)) return;
190
+
191
+ graph.triggerWave({ nodes: [grid], withStartNodes: false });
192
+ }
193
+ }
194
+ // -------------------------------------------------------------------------------------------------------------------------------------------------------------
195
+ checkGraphIntegrity() {
196
+ const grid = this;
197
+ const graph = grid.graph;
198
+ graph.needCheckIntegrity = false;
199
+
200
+ for (let uid in graph.nodesDict) {
201
+ if (uid === grid.id) continue;
202
+
203
+ let node = graph.nodesDict[uid];
204
+ if (!node.connectedToParents) {
205
+ node.connectToParents();
206
+ }
207
+ }
208
+
209
+ let link;
210
+ for (let lkey in graph.linksDict) {
211
+ link = graph.linksDict[lkey];
212
+ if (!link || !graph.nodesDict[link.parent.uid] || !graph.nodesDict[link.child.uid]) {
213
+ delete graph.linksDict[lkey];
214
+ }
215
+ }
216
+ }
217
+ // -------------------------------------------------------------------------------------------------------------------------------------------------------------
218
+ collectFilters() {
219
+ const grid = this;
220
+ const filters = [];
221
+
222
+ if (grid.parentGrids && !grid.connectedToParents) {
223
+ return ["1=2"];
224
+ }
225
+
226
+ if (!grid.parents || grid.parents.length <= 0) return filters;
227
+
228
+ for (let uid of grid.parents) {
229
+ let link = grid.graph.linksDict[grid.id + '_' + grid.graph.nodesDict[uid].id];
230
+ if (!link.content) continue;
231
+
232
+ if (link.content.applyLink) {
233
+ let filter = link.content.applyLink(link);
234
+ if (filter == null || filter === '') continue;
235
+
236
+ let fo = { type: 'graphLink', filter: filter };
237
+ filters.push(fo);
238
+ }
239
+ }
240
+
241
+ return filters;
242
+ }
243
+ // -------------------------------------------------------------------------------------------------------------------------------------------------------------
244
+ skipOnWaveVisit() {
245
+ return false;
246
+ }
247
+ // -------------------------------------------------------------------------------------------------------------------------------------------------------------
248
+ visitByWave(e) {
249
+ const grid = this;
250
+
251
+ return new Promise(function (resolve) {
252
+ if (grid.skipOnWaveVisit(e)) {
253
+ resolve(e);
254
+ return;
255
+ }
256
+
257
+ grid._waitingRows = true;
258
+ grid.getRows({ filters: grid.collectFilters(), grid: grid }).then(
259
+ rows => {
260
+ grid.rows = rows;
261
+ grid.afterGetRows(e);
262
+ resolve(e);
263
+ grid.refreshState();
264
+ }
265
+ ).finally(() => {
266
+ grid._waitingRows = false;
267
+ grid.refreshState();
268
+ });
269
+ });
270
+ }
271
+ // -------------------------------------------------------------------------------------------------------------------------------------------------------------
272
+ //afterGetRows(e) {
273
+ // super.afterGetRows(e);
274
+
275
+ // const grid = this;
276
+
277
+ // if (grid.graph) {
278
+ // grid.graph.visitNodesByWave(e);
279
+ // }
280
+ //}
281
+ // -------------------------------------------------------------------------------------------------------------------------------------------------------------
282
+ visitByWaveAlt(e) {
283
+ const grid = this;
284
+
285
+ return new Promise(function (resolve) {
286
+ if (grid.skipOnWaveVisit(e)) {
287
+ resolve(e);
288
+ return;
289
+ }
290
+
291
+ grid.selectedRowIndex = 0;
292
+
293
+ grid._waitingRows = true;
294
+ grid.refreshState();
295
+
296
+ grid.getRows({ filters: grid.collectFilters(), grid: grid }).then(
297
+ rows => {
298
+ grid.rows = rows;
299
+ grid.afterGetRows(e);
300
+ resolve(e);
301
+ grid.refreshState();
302
+ }
303
+ ).finally(() => {
304
+ grid._waitingRows = false;
305
+ grid.refreshState();
306
+ });
307
+ });
308
+ }
309
+ // -------------------------------------------------------------------------------------------------------------------------------------------------------------
310
+ }