1820cc8e4e415cf295139d5bd7f6dcc5f7144545
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / server / org-unit-type.component.ts
1 import {Component, Input, ViewChild, OnInit} from '@angular/core';
2 import {Tree, TreeNode} from '@eg/share/tree/tree';
3 import {IdlService, IdlObject} from '@eg/core/idl.service';
4 import {OrgService} from '@eg/core/org.service';
5 import {AuthService} from '@eg/core/auth.service';
6 import {PcrudService} from '@eg/core/pcrud.service';
7 import {ToastService} from '@eg/share/toast/toast.service';
8 import {StringComponent} from '@eg/share/string/string.component';
9 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
10 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
11
12 @Component({
13     templateUrl: './org-unit-type.component.html'
14 })
15
16 export class OrgUnitTypeComponent implements OnInit {
17
18     tree: Tree;
19     selected: TreeNode;
20     @ViewChild('editDialog') editDialog: FmRecordEditorComponent;
21     @ViewChild('editString') editString: StringComponent;
22     @ViewChild('createString') createString: StringComponent;
23     @ViewChild('errorString') errorString: StringComponent;
24     @ViewChild('delConfirm') delConfirm: ConfirmDialogComponent;
25
26     constructor(
27         private idl: IdlService,
28         private org: OrgService,
29         private auth: AuthService,
30         private pcrud: PcrudService,
31         private toast: ToastService
32     ) {}
33
34
35     ngOnInit() {
36         this.loadAoutTree();
37     }
38
39     loadAoutTree() {
40         this.pcrud.search('aout', {depth: 0},
41             {flesh: -1, flesh_fields: {aout: ['children']}},
42             {anonymous: true}
43         ).subscribe(aoutTree => this.ingestAoutTree(aoutTree));
44     }
45
46     // Translate the org unt type tree into a structure EgTree can use.
47     ingestAoutTree(aoutTree) {
48
49         const handleNode = (aoutNode: IdlObject): TreeNode => {
50             if (!aoutNode) { return; }
51
52             const treeNode = new TreeNode({
53                 id: aoutNode.id(),
54                 label: aoutNode.name(),
55                 callerData: aoutNode
56             });
57
58             aoutNode.children().forEach(childNode =>
59                 treeNode.children.push(handleNode(childNode))
60             );
61
62             return treeNode;
63         };
64
65         const rootNode = handleNode(aoutTree);
66         this.tree = new Tree(rootNode);
67     }
68
69     nodeClicked($event: any) {
70         this.selected = $event;
71     }
72
73     postUpdate(message: StringComponent) {
74         // Modifying org unit types means refetching the org unit
75         // data normally fetched on page load, since it includes
76         // org unit type data.
77         this.org.fetchOrgs().then(
78             ok => {
79                 message.current().then(str => this.toast.success(str));
80             }
81         );
82     }
83
84     edit() {
85         this.editDialog.mode = 'update';
86         this.editDialog.setRecord(this.selected.callerData);
87
88         this.editDialog.open().then(
89             success => {
90                 this.postUpdate(this.editString);
91             },
92             rejected => {
93                 if (rejected && rejected.dismissed) {
94                     return;
95                 }
96                 this.errorString.current()
97                     .then(str => this.toast.danger(str));
98             }
99         );
100     }
101
102     remove() {
103         this.delConfirm.open().then(
104             ok => {
105                 this.pcrud.remove(this.selected.callerData)
106                 .subscribe(
107                     ok2 => {},
108                     err => {
109                         this.errorString.current()
110                           .then(str => this.toast.danger(str));
111                     },
112                     ()  => {
113                         // Avoid updating until we know the entire
114                         // pcrud action/transaction completed.
115                         this.tree.removeNode(this.selected);
116                         this.selected = null;
117                         this.postUpdate(this.editString);
118                     }
119                 );
120             },
121             notConfirmed => {}
122         );
123     }
124
125     addChild() {
126         const parentTreeNode = this.selected;
127         const parentType = parentTreeNode.callerData;
128
129         const newType = this.idl.create('aout');
130         newType.parent(parentType.id());
131         newType.depth(Number(parentType.depth()) + 1);
132
133         this.editDialog.setRecord(newType);
134         this.editDialog.mode = 'create';
135
136         this.editDialog.open().then(
137             result => { // aout object
138
139                 // Add our new node to the tree
140                 const newNode = new TreeNode({
141                     id: result.id(),
142                     label: result.name(),
143                     callerData: result
144                 });
145                 parentTreeNode.children.push(newNode);
146                 this.postUpdate(this.createString);
147             },
148
149             rejected => {
150                 if (rejected && rejected.dismissed) {
151                     return;
152                 }
153                 this.errorString.current()
154                     .then(str => this.toast.danger(str));
155             }
156         );
157     }
158 }
159