ui
authorJason Etheridge <jason@EquinoxOLI.org>
Mon, 1 May 2023 09:46:37 +0000 (05:46 -0400)
committerJason Etheridge <phasefx@gmail.com>
Thu, 25 May 2023 16:45:32 +0000 (12:45 -0400)
Signed-off-by: Jason Etheridge <jason@EquinoxOLI.org>
Open-ILS/src/eg2/src/app/staff/admin/server/custom-org-unit-trees.component.html
Open-ILS/src/eg2/src/app/staff/admin/server/custom-org-unit-trees.component.ts

index 9dc9ff6..2b7b12a 100644 (file)
@@ -29,6 +29,7 @@
         disableRootSelector="true"
         [tree]="tree"
         (nodeClicked)="nodeClicked($event)">
+        <button [disabled]="copyNodesDisabled" (click)="copyNodes()" i18n>Copy Selected Nodes</button>
       </eg-tree>
     </div>
   </div>
@@ -38,7 +39,7 @@
       <eg-tree
         showTogglers="true"
         showSelectors="true"
-        disableRootSelector="true"
+        disableRootSelector="false"
         [tree]="custom_tree"
         (nodeClicked)="custom_nodeClicked($event)">
       </eg-tree>
index 86d2433..1924e0e 100644 (file)
@@ -1,15 +1,14 @@
 import {Component, ViewChild, OnInit} from '@angular/core';
 import { firstValueFrom } from 'rxjs';
 import {Tree, TreeNode} from '@eg/share/tree/tree';
-import {IdlService, IdlObject} from '@eg/core/idl.service';
+//import {IdlService} from '@eg/core/idl.service';
+import {IdlObject} from '@eg/core/idl.service';
 import {OrgService} from '@eg/core/org.service';
-import {AuthService} from '@eg/core/auth.service';
 import {PcrudService} from '@eg/core/pcrud.service';
-import {ToastService} from '@eg/share/toast/toast.service';
+//import {ToastService} from '@eg/share/toast/toast.service';
 import {StringComponent} from '@eg/share/string/string.component';
 import {StringService} from '@eg/share/string/string.service';
 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
-import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
 
 @Component({
     templateUrl: './custom-org-unit-trees.component.html',
@@ -23,18 +22,18 @@ export class CustomOrgUnitTreesComponent implements OnInit {
     selected: TreeNode;
     custom_selected: TreeNode;
     orgUnitTab: string;
+    copyNodesDisabled = true;
 
     @ViewChild('editString', { static: true }) editString: StringComponent;
     @ViewChild('errorString', { static: true }) errorString: StringComponent;
     @ViewChild('delConfirm', { static: true }) delConfirm: ConfirmDialogComponent;
 
     constructor(
-        private idl: IdlService,
+        //private idl: IdlService,
         private org: OrgService,
-        private auth: AuthService,
         private pcrud: PcrudService,
         private strings: StringService,
-        private toast: ToastService
+        //private toast: ToastService
     ) {}
 
 
@@ -43,23 +42,6 @@ export class CustomOrgUnitTreesComponent implements OnInit {
         this.loadCustomTree();
     }
 
-    orgSaved(orgId: number | IdlObject) {
-        let id: number;
-
-        if (orgId) { // new org created, focus it.
-            id = typeof orgId === 'object' ? orgId.id() : orgId;
-        } else if (this.currentOrg()) {
-            id = this.currentOrg().id();
-        }
-
-        this.loadAouTree(id).then(_ => this.postUpdate(this.editString));
-    }
-
-    orgDeleted() {
-        this.loadAouTree();
-    }
-
-
     async loadAouTree(selectNodeId?: number): Promise<any> {
         const flesh = ['children', 'ou_type', 'hours_of_operation'];
 
@@ -112,7 +94,6 @@ export class CustomOrgUnitTreesComponent implements OnInit {
             tree = null;
         }
         
-        console.warn('custom tree', tree);
         this.ingestCustomTree(tree);
         return this.custom_tree;
     }
@@ -185,100 +166,54 @@ export class CustomOrgUnitTreesComponent implements OnInit {
     }
 
     nodeClicked($event: any) {
-        this.selected = $event;
+        //this.selected = $event;
+        console.log($event);
+        this.copyNodesDisabled = !this.isCopyNodesAllowed();
     }
 
     custom_nodeClicked($event: any) {
-        this.custom_selected = $event;
-    }
-    
-    currentOrg(): IdlObject {
-        return this.selected ? this.selected.callerData.orgUnit : null;
-    }
-
-    orgHasChildren(): boolean {
-        const org = this.currentOrg();
-        return (org && org.children().length > 0);
-    }
-
-    postUpdate(message: StringComponent) {
-        // Modifying org unit types means refetching the org unit
-        // data normally fetched on page load, since it includes
-        // org unit type data.
-        this.org.fetchOrgs().then(() =>
-            message.current().then(str => this.toast.success(str)));
-    }
-
-    remove() {
-        this.delConfirm.open().subscribe(confirmed => {
-            if (!confirmed) { return; }
-
-            const org = this.selected.callerData.orgUnit;
-
-            this.pcrud.remove(org).subscribe(
-                ok2 => {},
-                err => {
-                    this.errorString.current()
-                      .then(str => this.toast.danger(str));
-                },
-                ()  => {
-                    // Avoid updating until we know the entire
-                    // pcrud action/transaction completed.
-                    // After removal, select the parent org if available
-                    // otherwise the root org.
-                    const orgId = org.parent_ou() ?
-                        org.parent_ou() : this.org.root().id();
-                    this.loadAouTree(orgId).then(_ =>
-                        this.postUpdate(this.editString));
-                }
-            );
-        });
-    }
-
-    orgTypeOptions(): ComboboxEntry[] {
-        let ouType = this.currentOrg().ou_type();
-
-        if (typeof ouType === 'number') {
-            // May not be fleshed for new org units
-            ouType = this.org.typeMap()[ouType];
-        }
-        const curDepth = ouType.depth();
-
-        return this.org.typeList()
-            .filter(type_ => type_.depth() === curDepth)
-            .map(type_ => ({id: type_.id(), label: type_.name()}));
-    }
-
-    orgChildTypes(): IdlObject[] {
-        let ouType = this.currentOrg().ou_type();
-
-        if (typeof ouType === 'number') {
-            // May not be fleshed for new org units
-            ouType = this.org.typeMap()[ouType];
-        }
-
-        const depth = ouType.depth();
-        return this.org.typeList()
-            .filter(type_ => type_.depth() === depth + 1);
-    }
-
-    addChild() {
-        const parentTreeNode = this.selected;
-        const parentOrg = this.currentOrg();
-        const newType = this.orgChildTypes()[0];
-
-        const org = this.idl.create('aou');
-        org.isnew(true);
-        org.parent_ou(parentOrg.id());
-        org.ou_type(newType.id());
-        org.children([]);
-
-        // Create a dummy, detached org node to keep the UI happy.
-        this.selected = new TreeNode({
-            id: org.id(),
-            label: org.name(),
-            callerData: {orgUnit: org}
-        });
+        //this.custom_selected = $event;
+        console.log($event);
+        this.copyNodesDisabled = !this.isCopyNodesAllowed();
+    }
+
+    isCopyNodesAllowed(): boolean {
+      const sourceNodes = this.tree.selectedNodes();
+      const targetNodes = this.custom_tree.selectedNodes();
+      if (sourceNodes.length === 0) {
+          console.log('isCopyNodesAllowed', false);
+          return false;
+      }
+      if (targetNodes.length !== 1) {
+          console.log('isCopyNodesAllowed', false);
+          return false;
+      }
+      for (let sourceNode of sourceNodes) {
+          if (this.custom_tree.findNodesByFieldAndValue('label', sourceNode.label).length > 0) {
+              console.log('isCopyNodesAllowed', false);
+              return false;
+          }
+          if (sourceNode === this.tree.rootNode) {
+              console.log('isCopyNodesAllowed', false);
+              return false;
+          }
+      }
+      console.log('isCopyNodesAllowed', true);
+      return true;
+    }
+
+    copyNodes() {
+      const sourceNodes = this.tree.selectedNodes();
+      const targetNode = this.custom_tree.selectedNode();
+      if (!this.isCopyNodesAllowed()) {
+          return;
+      }
+      for (let sourceNode of sourceNodes) {
+        // Create a deep copy of sourceNode to prevent reference issues
+        let newNode = JSON.parse(JSON.stringify(sourceNode));
+        newNode.children = [];
+        targetNode.children.push(newNode);
+      }
     }
 
 }