@onehat/data 1.18.11 → 1.18.13

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.11",
3
+ "version": "1.18.13",
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
@@ -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
  }
@@ -1012,6 +1012,7 @@ export default class Repository extends EventEmitter {
1012
1012
  entity,
1013
1013
  ...this.entities.slice(ix)
1014
1014
  ];
1015
+ this.assembleTreeNodes();
1015
1016
  } else {
1016
1017
  this.entities.unshift(entity); // Add to *beginning* of entities array, so the phantom record will appear at the beginning of the current page
1017
1018
  }
@@ -93,7 +93,7 @@ class Parsers {
93
93
  result = moment(value, format);
94
94
  } catch(err) {}
95
95
 
96
- if ((!result || !result.isValid() && chrono)) {
96
+ if ((!result?.isValid() && typeof chrono !== "undefined")) {
97
97
  // try using chrono
98
98
  const parsed = chrono.parse(value);
99
99
  if (parsed && parsed[0] && parsed[0].date) {