@onehat/data 1.18.10 → 1.18.12

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.18.10",
3
+ "version": "1.18.12",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -1808,6 +1808,21 @@ class Entity extends EventEmitter {
1808
1808
  return parentIds.reverse().join('/');
1809
1809
  }
1810
1810
 
1811
+ /**
1812
+ * Moves this TreeNode to another parentId.
1813
+ */
1814
+ moveTreeNode = (newParentId) => {
1815
+ this.ensureTree();
1816
+ if (this.isDestroyed) {
1817
+ throw Error('this.moveTreeNode is no longer valid. TreeNode has been destroyed.');
1818
+ }
1819
+ if (!this.repository?.moveTreeNode) {
1820
+ throw Error('repository.moveTreeNode is not defined.');
1821
+ }
1822
+
1823
+ return this.repository.moveTreeNode(this, newParentId);
1824
+ }
1825
+
1811
1826
  /**
1812
1827
  * Helper to make sure this Repository is a tree
1813
1828
  * @private
@@ -787,7 +787,13 @@ class AjaxRepository extends Repository {
787
787
  const
788
788
  method = this.methods.delete,
789
789
  url = this.api.delete,
790
- data = { id: entity.id, };
790
+ data = {
791
+ id: entity.id,
792
+ };
793
+
794
+ if (this.isTree && this.moveSubtreeUp) {
795
+ data.moveSubtreeUp = this.moveSubtreeUp;
796
+ }
791
797
 
792
798
  return this._send(method, url, data)
793
799
  .then(result => {
@@ -506,6 +506,81 @@ class OneBuildRepository extends AjaxRepository {
506
506
  });
507
507
  }
508
508
 
509
+ /**
510
+ * Loads (or reloads) the supplied treeNode
511
+ */
512
+ loadNode = (treeNode, depth = 1) => {
513
+ this.ensureTree();
514
+ if (this.isDestroyed) {
515
+ this.throwError('this.loadNode is no longer valid. Repository has been destroyed.');
516
+ return;
517
+ }
518
+ if (!this.isOnline) {
519
+ this.throwError('Offline');
520
+ return;
521
+ }
522
+
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.removeEntity(child);
531
+ });
532
+ }
533
+
534
+ this.markLoading();
535
+
536
+ const data = _.merge({ depth, nodeId: treeNode.id, }, this._baseParams, this._params);
537
+ return this._send('POST', this.name + '/getNodes', data)
538
+ .then((result) => {
539
+ if (this.debugMode) {
540
+ console.log('Response for loadNode', result);
541
+ }
542
+
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.
546
+ return;
547
+ }
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);
581
+ });
582
+ }
583
+
509
584
  /**
510
585
  * Loads (or reloads) the children of the supplied treeNode
511
586
  */
@@ -527,7 +602,7 @@ class OneBuildRepository extends AjaxRepository {
527
602
  treeNode.children = [];
528
603
 
529
604
  _.each(children, (child) => {
530
- this.removeNode(child);
605
+ this.removeEntity(child);
531
606
  });
532
607
  }
533
608
 
@@ -581,6 +656,17 @@ class OneBuildRepository extends AjaxRepository {
581
656
  });
582
657
  }
583
658
 
659
+ /**
660
+ * Override the AjaxRepository to we can reload a treeNode if needed
661
+ */
662
+ reloadEntity = (entity, callback = null) => {
663
+ if (!entity.isTree) {
664
+ return super.reloadEntity(entity, callback);
665
+ }
666
+
667
+ return this.loadNode(entity, 1);
668
+ }
669
+
584
670
  /**
585
671
  * Searches all nodes for the supplied text.
586
672
  * This basically takes the search query and returns whatever the server sends
@@ -637,18 +723,67 @@ class OneBuildRepository extends AjaxRepository {
637
723
 
638
724
  /**
639
725
  * Moves the supplied treeNode to a new position on the tree
726
+ * @returns id of common ancestor node
640
727
  */
641
- moveTreeNode = async (treeNode, newParentId) => {
728
+ moveTreeNode = (treeNode, newParentId) => {
642
729
  this.ensureTree();
643
730
  if (this.isDestroyed) {
644
731
  this.throwError('this.moveTreeNode is no longer valid. Repository has been destroyed.');
645
732
  return;
646
733
  }
734
+ if (!this.isOnline) {
735
+ this.throwError('Offline');
736
+ return;
737
+ }
738
+
739
+ const oldParentId = treeNode.parent?.id;
740
+
741
+ const data = _.merge({ nodeId: treeNode.id, parentId: newParentId, }, this._baseParams, this._params);
742
+ return this._send('POST', this.name + '/moveNode', data)
743
+ .then((result) => {
744
+ if (this.debugMode) {
745
+ console.log('Response for searchNodes', result);
746
+ }
647
747
 
648
- // TODO: move node here
748
+ if (this.isDestroyed) {
749
+ // If this repository gets destroyed before it has a chance
750
+ // to process the Ajax request, just ignore the response.
751
+ return;
752
+ }
649
753
 
754
+ const {
755
+ root: {
756
+ commonAncestorId,
757
+ oldParent,
758
+ newParent,
759
+ node,
760
+ },
761
+ success,
762
+ total,
763
+ message
764
+ } = this._processServerResponse(result);
650
765
 
651
-
766
+ if (!success) {
767
+ this.throwError(message);
768
+ return;
769
+ }
770
+
771
+ // move it from oldParent.children to newParent.children
772
+ const
773
+ oldParentRecord = this.getById(oldParentId),
774
+ newParentRecord = this.getById(newParentId);
775
+
776
+ oldParentRecord?.loadOriginalData(oldParent);
777
+ newParentRecord.loadOriginalData(newParent);
778
+ treeNode.loadOriginalData(node);
779
+
780
+ this.assembleTreeNodes();
781
+
782
+ return commonAncestorId;
783
+ })
784
+ .finally(() => {
785
+ this.markLoading(false);
786
+ });
652
787
  }
653
788
 
654
789
  }
@@ -204,6 +204,11 @@ export default class Repository extends EventEmitter {
204
204
  */
205
205
  this.isTree = schema.model.isTree || false;
206
206
 
207
+ /**
208
+ * @member {boolean} moveSubtreeUp - Whether to move the subtree up on the next delete operation (trees only)
209
+ */
210
+ this.moveSubtreeUp = false;
211
+
207
212
  /**
208
213
  * @member {boolean} isLoaded - State: whether or not entities have been loaded at least once
209
214
  */
@@ -1007,6 +1012,7 @@ export default class Repository extends EventEmitter {
1007
1012
  entity,
1008
1013
  ...this.entities.slice(ix)
1009
1014
  ];
1015
+ this.assembleTreeNodes();
1010
1016
  } else {
1011
1017
  this.entities.unshift(entity); // Add to *beginning* of entities array, so the phantom record will appear at the beginning of the current page
1012
1018
  }
@@ -1791,9 +1797,10 @@ export default class Repository extends EventEmitter {
1791
1797
  * Marks entities for deletion from storage medium.
1792
1798
  * Actual deletion takes place in save(), unless isPhantom
1793
1799
  * @param {object|array} entities - one or more entities to delete
1800
+ * @param {bool} moveSubtreeUp - whether or not to move the subtree up (if this is a tree)
1794
1801
  * @fires delete
1795
1802
  */
1796
- delete = async (entities) => {
1803
+ delete = async (entities, moveSubtreeUp = false) => {
1797
1804
  if (this.isDestroyed) {
1798
1805
  this.throwError('this.delete is no longer valid. Repository has been destroyed.');
1799
1806
  return;
@@ -1819,6 +1826,10 @@ export default class Repository extends EventEmitter {
1819
1826
  }
1820
1827
  });
1821
1828
 
1829
+ if (this.isTree) {
1830
+ this.moveSubtreeUp = moveSubtreeUp;
1831
+ }
1832
+
1822
1833
  this.emit('delete', entities);
1823
1834
 
1824
1835
  if (this.isAutoSave) {