@onehat/data 1.18.8 → 1.18.9
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.
|
@@ -1139,7 +1139,7 @@ describe('Repository Base', function() {
|
|
|
1139
1139
|
parent_id: null,
|
|
1140
1140
|
depth: 0,
|
|
1141
1141
|
hasChildren: true,
|
|
1142
|
-
|
|
1142
|
+
areChildrenLoaded: true,
|
|
1143
1143
|
},
|
|
1144
1144
|
{
|
|
1145
1145
|
id: 2,
|
|
@@ -1147,21 +1147,21 @@ describe('Repository Base', function() {
|
|
|
1147
1147
|
parent_id: 1,
|
|
1148
1148
|
depth: 1,
|
|
1149
1149
|
hasChildren: true,
|
|
1150
|
-
|
|
1150
|
+
areChildrenLoaded: true,
|
|
1151
1151
|
},
|
|
1152
1152
|
{
|
|
1153
1153
|
id: 3,
|
|
1154
1154
|
display: 'Child 2',
|
|
1155
1155
|
parent_id: 1,
|
|
1156
1156
|
depth: 1,
|
|
1157
|
-
|
|
1157
|
+
areChildrenLoaded: true,
|
|
1158
1158
|
},
|
|
1159
1159
|
{
|
|
1160
1160
|
id: 4,
|
|
1161
1161
|
display: 'Grandchild',
|
|
1162
1162
|
parent_id: 2,
|
|
1163
1163
|
depth: 2,
|
|
1164
|
-
|
|
1164
|
+
areChildrenLoaded: true,
|
|
1165
1165
|
},
|
|
1166
1166
|
],
|
|
1167
1167
|
creatRepository = async () => {
|
package/package.json
CHANGED
package/src/Entity/Entity.js
CHANGED
|
@@ -121,21 +121,24 @@ class Entity extends EventEmitter {
|
|
|
121
121
|
|
|
122
122
|
/**
|
|
123
123
|
* @member {TreeNode} parent - The parent TreeNode for this TreeNode
|
|
124
|
+
* @public
|
|
124
125
|
* For trees only
|
|
125
126
|
*/
|
|
126
127
|
this.parent = null;
|
|
127
128
|
|
|
128
129
|
/**
|
|
129
130
|
* @member {array} children - Contains any children of this TreeNode
|
|
131
|
+
* @public
|
|
130
132
|
* For trees only
|
|
131
133
|
*/
|
|
132
134
|
this.children = this._originalData.children && !_.isEmpty(this._originalData.children) ? this._originalData.children : [];
|
|
133
135
|
|
|
134
136
|
/**
|
|
135
|
-
* @member {boolean}
|
|
137
|
+
* @member {boolean} areChildrenLoaded - Whether child TreeNodes have loaded for this TreeNode
|
|
138
|
+
* @public
|
|
136
139
|
* For trees only
|
|
137
140
|
*/
|
|
138
|
-
this.
|
|
141
|
+
this.areChildrenLoaded = this._originalData.areChildrenLoaded || false;
|
|
139
142
|
|
|
140
143
|
/**
|
|
141
144
|
* @member {boolean} isPersisted - Whether this object has been persisted in a storage medium
|
|
@@ -1639,7 +1642,7 @@ class Entity extends EventEmitter {
|
|
|
1639
1642
|
if (this.isDestroyed) {
|
|
1640
1643
|
throw Error('this.getChildren is no longer valid. TreeNode has been destroyed.');
|
|
1641
1644
|
}
|
|
1642
|
-
if (!this.
|
|
1645
|
+
if (!this.areChildrenLoaded) {
|
|
1643
1646
|
await this.loadChildren();
|
|
1644
1647
|
}
|
|
1645
1648
|
return this.children;
|
|
@@ -1667,12 +1670,13 @@ class Entity extends EventEmitter {
|
|
|
1667
1670
|
if (this.isDestroyed) {
|
|
1668
1671
|
throw Error('this.loadChildren is no longer valid. TreeNode has been destroyed.');
|
|
1669
1672
|
}
|
|
1670
|
-
if (!this.repository?.
|
|
1671
|
-
throw Error('repository.
|
|
1673
|
+
if (!this.repository?.getChildNodes) {
|
|
1674
|
+
throw Error('repository.getChildNodes is not defined.');
|
|
1672
1675
|
}
|
|
1673
1676
|
|
|
1674
|
-
|
|
1675
|
-
this.
|
|
1677
|
+
const children = await this.repository.getChildNodes(this, depth);
|
|
1678
|
+
this.areChildrenLoaded = true;
|
|
1679
|
+
return children;
|
|
1676
1680
|
}
|
|
1677
1681
|
|
|
1678
1682
|
/**
|
|
@@ -106,7 +106,7 @@ class OneBuildRepository extends AjaxRepository {
|
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
const headers = _.merge({
|
|
109
|
-
'Content-Type': 'application/json',
|
|
109
|
+
// 'Content-Type': 'application/json', // Stops axios from using 'application/x-www-form-urlencoded'
|
|
110
110
|
Accept: 'application/json',
|
|
111
111
|
}, this.headers);
|
|
112
112
|
|
|
@@ -178,7 +178,11 @@ class OneBuildRepository extends AjaxRepository {
|
|
|
178
178
|
});
|
|
179
179
|
|
|
180
180
|
if (this.isLoaded && this.isAutoLoad && !this.eventsPaused) {
|
|
181
|
-
|
|
181
|
+
if (this.isTree) {
|
|
182
|
+
return this.getRootNodes(1);
|
|
183
|
+
} else {
|
|
184
|
+
return this.reload();
|
|
185
|
+
}
|
|
182
186
|
}
|
|
183
187
|
}
|
|
184
188
|
|
|
@@ -199,9 +203,15 @@ class OneBuildRepository extends AjaxRepository {
|
|
|
199
203
|
|
|
200
204
|
if (!this.eventsPaused) {
|
|
201
205
|
if (this.isLoaded && this.isAutoLoad) {
|
|
202
|
-
|
|
203
|
-
this.
|
|
204
|
-
|
|
206
|
+
if (this.isTree) {
|
|
207
|
+
return this.getRootNodes(1).then(() => {
|
|
208
|
+
this.emit('changeSorters');
|
|
209
|
+
});
|
|
210
|
+
} else {
|
|
211
|
+
return this.reload().then(() => {
|
|
212
|
+
this.emit('changeSorters');
|
|
213
|
+
});
|
|
214
|
+
}
|
|
205
215
|
} else {
|
|
206
216
|
this.emit('changeSorters');
|
|
207
217
|
}
|
|
@@ -426,7 +436,7 @@ class OneBuildRepository extends AjaxRepository {
|
|
|
426
436
|
/**
|
|
427
437
|
* Gets the root nodes of this tree.
|
|
428
438
|
*/
|
|
429
|
-
getRootNodes =
|
|
439
|
+
getRootNodes = (depth) => {
|
|
430
440
|
this.ensureTree();
|
|
431
441
|
if (this.isDestroyed) {
|
|
432
442
|
this.throwError('this.setRootNode is no longer valid. Repository has been destroyed.');
|
|
@@ -436,26 +446,12 @@ class OneBuildRepository extends AjaxRepository {
|
|
|
436
446
|
this.throwError('Offline');
|
|
437
447
|
return;
|
|
438
448
|
}
|
|
449
|
+
|
|
439
450
|
this.emit('beforeLoad'); // TODO: canceling beforeLoad will cancel the load operation
|
|
440
451
|
this.markLoading();
|
|
441
452
|
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
url: this.name + '/getRootNodes',
|
|
445
|
-
data: qs.stringify({
|
|
446
|
-
getChildren,
|
|
447
|
-
depth,
|
|
448
|
-
conditions: getChildParams ? getChildParams() : null
|
|
449
|
-
}),
|
|
450
|
-
method: 'POST',
|
|
451
|
-
baseURL: this.api.baseURL,
|
|
452
|
-
};
|
|
453
|
-
|
|
454
|
-
if (this.debugMode) {
|
|
455
|
-
console.log('getRootNodes', data);
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
return this.axios(data)
|
|
453
|
+
const data = _.merge({ depth }, this._baseParams, this._params);
|
|
454
|
+
return this._send('POST', this.name + '/getNodes', data)
|
|
459
455
|
.then((result) => {
|
|
460
456
|
if (this.debugMode) {
|
|
461
457
|
console.log('Response for getRootNodes', result);
|
|
@@ -474,6 +470,11 @@ class OneBuildRepository extends AjaxRepository {
|
|
|
474
470
|
message
|
|
475
471
|
} = this._processServerResponse(result);
|
|
476
472
|
|
|
473
|
+
if (!success) {
|
|
474
|
+
this.throwError(message);
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
|
|
477
478
|
this._destroyEntities();
|
|
478
479
|
|
|
479
480
|
// Set the current entities
|
|
@@ -491,10 +492,10 @@ class OneBuildRepository extends AjaxRepository {
|
|
|
491
492
|
|
|
492
493
|
this.areRootNodesLoaded = true;
|
|
493
494
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
this.emit('changeData', this.entities);
|
|
495
|
+
|
|
496
|
+
// Don't emit events for root nodes...
|
|
497
497
|
this.emit('load', this);
|
|
498
|
+
// this.emit('changeData', this.entities);
|
|
498
499
|
|
|
499
500
|
return this.getBy((entity) => {
|
|
500
501
|
return entity.isRoot;
|
|
@@ -506,75 +507,125 @@ class OneBuildRepository extends AjaxRepository {
|
|
|
506
507
|
}
|
|
507
508
|
|
|
508
509
|
/**
|
|
509
|
-
*
|
|
510
|
-
* This basically takes the search query and returns whatever the server sends
|
|
510
|
+
* Loads the children of the supplied treeNode
|
|
511
511
|
*/
|
|
512
|
-
|
|
512
|
+
getChildNodes = (treeNode, depth = 1) => {
|
|
513
513
|
this.ensureTree();
|
|
514
514
|
if (this.isDestroyed) {
|
|
515
|
-
this.throwError('this.
|
|
515
|
+
this.throwError('this.getChildNodes is no longer valid. Repository has been destroyed.');
|
|
516
516
|
return;
|
|
517
517
|
}
|
|
518
518
|
if (!this.isOnline) {
|
|
519
519
|
this.throwError('Offline');
|
|
520
520
|
return;
|
|
521
521
|
}
|
|
522
|
-
|
|
523
|
-
const data = {
|
|
524
|
-
url: this.name + '/searchTree',
|
|
525
|
-
data: qs.stringify({
|
|
526
|
-
q,
|
|
527
|
-
}),
|
|
528
|
-
method: 'POST',
|
|
529
|
-
baseURL: this.api.baseURL,
|
|
530
|
-
};
|
|
531
522
|
|
|
532
|
-
|
|
533
|
-
|
|
523
|
+
// If children already exist, remove them from the repository
|
|
524
|
+
// This way, we can reload just a portion of the tree
|
|
525
|
+
if (!_.isEmpty(treeNode.children)) {
|
|
526
|
+
const children = treeNode.children;
|
|
527
|
+
treeNode.children = [];
|
|
528
|
+
|
|
529
|
+
_.each(children, (child) => {
|
|
530
|
+
this.removeNode(child);
|
|
531
|
+
});
|
|
534
532
|
}
|
|
533
|
+
|
|
534
|
+
this.markLoading();
|
|
535
535
|
|
|
536
|
-
|
|
536
|
+
const data = _.merge({ depth, parentId: treeNode.id, }, this._baseParams, this._params);
|
|
537
|
+
return this._send('POST', this.name + '/getNodes', data)
|
|
537
538
|
.then((result) => {
|
|
538
539
|
if (this.debugMode) {
|
|
539
|
-
console.log('
|
|
540
|
+
console.log('Response for getChildNodes', result);
|
|
540
541
|
}
|
|
541
542
|
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
543
|
+
if (this.isDestroyed) {
|
|
544
|
+
// If this repository gets destroyed before it has a chance
|
|
545
|
+
// to process the Ajax request, just ignore the response.
|
|
545
546
|
return;
|
|
546
547
|
}
|
|
547
548
|
|
|
548
|
-
|
|
549
|
+
const {
|
|
550
|
+
root,
|
|
551
|
+
success,
|
|
552
|
+
total,
|
|
553
|
+
message
|
|
554
|
+
} = this._processServerResponse(result);
|
|
555
|
+
|
|
556
|
+
if (!success) {
|
|
557
|
+
this.throwError(message);
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// Set the current entities
|
|
562
|
+
const children = _.map(root, (data) => {
|
|
563
|
+
const entity = Repository._createEntity(this.schema, data, this, true);
|
|
564
|
+
this._relayEntityEvents(entity);
|
|
565
|
+
return entity;
|
|
566
|
+
});
|
|
567
|
+
|
|
568
|
+
this.entities = this.entities.concat(children);
|
|
569
|
+
|
|
570
|
+
this.assembleTreeNodes();
|
|
571
|
+
|
|
572
|
+
this._setPaginationVars();
|
|
573
|
+
|
|
574
|
+
// this.emit('changeData', this.entities);
|
|
575
|
+
this.emit('load', this);
|
|
576
|
+
|
|
577
|
+
return children;
|
|
578
|
+
})
|
|
579
|
+
.finally(() => {
|
|
580
|
+
this.markLoading(false);
|
|
549
581
|
});
|
|
550
582
|
}
|
|
583
|
+
|
|
551
584
|
/**
|
|
552
|
-
*
|
|
585
|
+
* Searches all nodes for the supplied text.
|
|
586
|
+
* This basically takes the search query and returns whatever the server sends
|
|
553
587
|
*/
|
|
554
|
-
|
|
588
|
+
searchNodes = (q) => {
|
|
555
589
|
this.ensureTree();
|
|
556
590
|
if (this.isDestroyed) {
|
|
557
|
-
this.throwError('this.
|
|
591
|
+
this.throwError('this.searchNodes is no longer valid. Repository has been destroyed.');
|
|
558
592
|
return;
|
|
559
593
|
}
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
// This way, we can reload just a portion of the tree
|
|
564
|
-
if (!_.isEmpty(treeNode.children)) {
|
|
565
|
-
const children = treeNode.children;
|
|
566
|
-
treeNode.children = [];
|
|
567
|
-
|
|
568
|
-
_.each(children, (child) => {
|
|
569
|
-
this.removeNode(child);
|
|
570
|
-
});
|
|
594
|
+
if (!this.isOnline) {
|
|
595
|
+
this.throwError('Offline');
|
|
596
|
+
return;
|
|
571
597
|
}
|
|
572
598
|
|
|
599
|
+
const data = _.merge({ q, }, this._baseParams, this._params);
|
|
600
|
+
return this._send('POST', this.name + '/searchNodes', data)
|
|
601
|
+
.then((result) => {
|
|
602
|
+
if (this.debugMode) {
|
|
603
|
+
console.log('Response for searchNodes', result);
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
if (this.isDestroyed) {
|
|
607
|
+
// If this repository gets destroyed before it has a chance
|
|
608
|
+
// to process the Ajax request, just ignore the response.
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
573
611
|
|
|
574
|
-
|
|
612
|
+
const {
|
|
613
|
+
root,
|
|
614
|
+
success,
|
|
615
|
+
total,
|
|
616
|
+
message
|
|
617
|
+
} = this._processServerResponse(result);
|
|
575
618
|
|
|
619
|
+
if (!success) {
|
|
620
|
+
this.throwError(message);
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
576
623
|
|
|
577
|
-
|
|
624
|
+
return root;
|
|
625
|
+
})
|
|
626
|
+
.finally(() => {
|
|
627
|
+
this.markLoading(false);
|
|
628
|
+
});
|
|
578
629
|
}
|
|
579
630
|
|
|
580
631
|
/**
|
|
@@ -288,10 +288,10 @@ export default class Repository extends EventEmitter {
|
|
|
288
288
|
});
|
|
289
289
|
|
|
290
290
|
// Auto load & sort
|
|
291
|
-
if (this.isAutoLoad) {
|
|
291
|
+
if (this.isAutoLoad && !this.isTree) {
|
|
292
292
|
await this.load();
|
|
293
293
|
}
|
|
294
|
-
if (!this.isSorted && this.isAutoSort && !this.isRemoteSort) { // load may have sorted, in which case this will be skipped.
|
|
294
|
+
if (!this.isSorted && this.isAutoSort && !this.isRemoteSort && !this.isTree) { // load may have sorted, in which case this will be skipped.
|
|
295
295
|
await this.sort();
|
|
296
296
|
}
|
|
297
297
|
|