cloning
authorJason Etheridge <phasefx@gmail.com>
Mon, 15 May 2023 03:19:50 +0000 (23:19 -0400)
committerJason Etheridge <phasefx@gmail.com>
Thu, 25 May 2023 16:45:32 +0000 (12:45 -0400)
Signed-off-by: Jason Etheridge <phasefx@gmail.com>
Open-ILS/src/eg2/src/app/share/tree/tree.ts
Open-ILS/src/eg2/src/app/staff/admin/server/custom-org-unit-trees.component.ts

index fd76d82..2eedee9 100644 (file)
@@ -39,6 +39,18 @@ export class TreeNode {
     toggleExpand() {
         this.expanded = !this.expanded;
     }
+
+    clone(): TreeNode {
+        const clonedNode = new TreeNode({
+            id: this.id,
+            label: this.label,
+            expanded: this.expanded,
+            callerData: this.callerData, // NOTE: shallow copy
+        });
+
+        clonedNode.children = this.children.map(child => child.clone());
+        return clonedNode;
+    }
 }
 
 export class Tree {
@@ -176,5 +188,9 @@ export class Tree {
         });
     }
 
+    clone(): Tree {
+        const clonedTree = new Tree(this.rootNode.clone());
+        return clonedTree;
+    }
 }
 
index 95d5958..10fa01d 100644 (file)
@@ -37,10 +37,14 @@ export class CustomOrgUnitTreesComponent implements OnInit {
     ) {}
 
 
-    ngOnInit() {
-        this.loadAouTree(this.org.root().id());
-        this.loadCustomTree();
-               console.log('component this', this);
+    async ngOnInit() {
+        try {
+            await this.loadAouTree(this.org.root().id());
+            await this.loadCustomTree('opac');
+            console.log('component this', this);
+        } catch(E) {
+            console.error('caught during ngOnInit',E);
+        }
     }
 
     async loadAouTree(selectNodeId?: number): Promise<any> {
@@ -51,7 +55,7 @@ export class CustomOrgUnitTreesComponent implements OnInit {
                 {flesh : -1, flesh_fields : {aou : flesh}}, {authoritative: true}
             ));
 
-            this.ingestAouTree(tree);
+            this.ingestAouTree(tree); // sets this.tree as a side-effect
             if (!selectNodeId) { selectNodeId = this.org.root().id(); }
 
             const node = this.tree.findNode(selectNodeId);
@@ -64,12 +68,12 @@ export class CustomOrgUnitTreesComponent implements OnInit {
         }
     }
 
-       async loadCustomTree(): Promise<any> {
+       async loadCustomTree(purpose: string): Promise<any> {
                const flesh = ['children', 'org_unit'];
 
                let tree_type: IdlObject;
                tree_type = await firstValueFrom(
-                       this.pcrud.search('aouct', { purpose: 'opac' })
+                       this.pcrud.search('aouct', { purpose: purpose })
                                .pipe(
                                        take(1),
                                        defaultIfEmpty(undefined),
@@ -88,21 +92,24 @@ export class CustomOrgUnitTreesComponent implements OnInit {
                }
 
                let tree: IdlObject;
-               tree = await firstValueFrom(
-                       this.pcrud.search('aouctn', {tree: tree_id, parent_node: null},
-                       {flesh: -1, flesh_fields: {aouctn: flesh}}, {authoritative: true})
-                               .pipe(
-                                       take(1),
-                                       defaultIfEmpty(undefined),
-                                       catchError(err => {
-                                               console.warn('phasefx: caught from pcrud (aouctn): 2', err);
-                                               return of(undefined);
-                                       })
-                               )
-               );
-
-               this.ingestCustomTree(tree);
-               return this.custom_tree;
+        if (tree_id) {
+            tree = await firstValueFrom(
+                this.pcrud.search('aouctn', {tree: tree_id, parent_node: null},
+                {flesh: -1, flesh_fields: {aouctn: flesh}}, {authoritative: true})
+                    .pipe(
+                        take(1),
+                        defaultIfEmpty(undefined),
+                        catchError(err => {
+                            console.warn('phasefx: caught from pcrud (aouctn): 2', err);
+                            return of(undefined);
+                        })
+                    )
+            );
+            this.ingestCustomTree(tree); // sets this.custom_tree as a side-effect
+        } else {
+            this.custom_tree = this.tree.clone(); // need to remember to create the aouct if needed upon saving
+        }
+        return this.custom_tree;
        }
 
     // Translate the org unt type tree into a structure EgTree can use.