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',
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
) {}
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'];
tree = null;
}
- console.warn('custom tree', tree);
this.ingestCustomTree(tree);
return this.custom_tree;
}
}
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);
+ }
}
}