]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/acq/edi_attr_set/edi-attr-set-edit-dialog.component.ts
LP1959048: manual ng lint fixes
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / acq / edi_attr_set / edi-attr-set-edit-dialog.component.ts
1 import {Component, Input, OnInit} from '@angular/core';
2 import {DialogComponent} from '@eg/share/dialog/dialog.component';
3 import {IdlService, IdlObject} from '@eg/core/idl.service';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
6
7 @Component({
8   selector: 'eg-edi-attr-set-edit-dialog',
9   templateUrl: './edi-attr-set-edit-dialog.component.html'
10 })
11
12 export class EdiAttrSetEditDialogComponent
13   extends DialogComponent implements OnInit {
14
15     @Input() mode = 'create';
16     @Input() attrSetId: number;
17     @Input() cloneSource: number;
18     attrSet: IdlObject;
19     attrInputs: any = [];
20     clonedLabel = '';
21
22     constructor(
23         private idl: IdlService,
24         private pcrud: PcrudService,
25         private modal: NgbModal
26     ) {
27         super(modal);
28     }
29
30     ngOnInit() {
31         this.onOpen$.subscribe(() => this._initRecord());
32     }
33
34     private _initRecord() {
35         this.attrSet = null;
36         this.attrInputs = [];
37         this.clonedLabel = '';
38         if (this.mode === 'update') {
39             this.pcrud.retrieve('aeas', this.attrSetId, {
40                 flesh: 1,
41                 flesh_fields: { aeas: ['attr_maps'] }
42             }).subscribe(res => {
43                 this.attrSet = res;
44                 this._generateAttrInputs();
45             });
46         } else if (this.mode === 'clone') {
47             this.pcrud.retrieve('aeas', this.cloneSource, {
48                 flesh: 1,
49                 flesh_fields: { aeas: ['attr_maps'] }
50             }).subscribe(res => {
51                 this.clonedLabel = res.label();
52                 this.attrSet = this.idl.create('aeas');
53                 this.attrSet.attr_maps([]);
54                 res.attr_maps().forEach((m) => {
55                     const newMap = this.idl.create('aeasm');
56                     newMap.attr(m.attr());
57                     this.attrSet.attr_maps().push(newMap);
58                 });
59                 this._generateAttrInputs();
60             });
61         } else if (this.mode === 'create') {
62             this.attrSet = this.idl.create('aeas');
63             this.attrSet.attr_maps([]);
64             this._generateAttrInputs();
65         }
66     }
67
68     _generateAttrInputs() {
69         const hasAttr: {[key: string]: boolean} = {};
70         const hasAttrId: {[key: string]: number} = {};
71         this.attrSet.attr_maps().forEach((m) => {
72             hasAttr[m.attr()] = true;
73             hasAttrId[m.attr()] = m.id();
74         });
75         this.pcrud.retrieveAll('aea', {order_by: {aea: 'key'}}).subscribe(attr => {
76             const inp = {
77                 key: attr.key(),
78                 label: attr.label(),
79                 id: null,
80                 selected: false
81             };
82             if (attr.key() in hasAttr) {
83                 inp.selected = true;
84                 inp.id = hasAttrId[attr.key()];
85             }
86             this.attrInputs.push(inp);
87         });
88     }
89
90     save() {
91         if (this.attrSet.id() === undefined || this.attrSet.id() === null) {
92             this.attrSet.isnew(true);
93         } else {
94             this.attrSet.ischanged(true);
95         }
96         this.pcrud.autoApply([this.attrSet]).subscribe(res => {
97             const setId = this.mode === 'update' ? res : res.id();
98             const updates: IdlObject[] = [];
99             if (this.mode === 'create' || this.mode === 'clone') {
100                 this.attrInputs.forEach((inp) => {
101                     if (inp.selected) {
102                         const aesm = this.idl.create('aeasm');
103                         aesm.attr(inp.key);
104                         aesm.attr_set(setId);
105                         aesm.isnew(true);
106                         updates.push(aesm);
107                     }
108                 });
109             } else {
110                 // updating an existing set
111                 this.attrInputs.forEach((inp) => {
112                     if (inp.id) {
113                         if (!inp.selected) {
114                             // used to be wanted, but no longer
115                             const aesm = this.idl.create('aeasm');
116                             aesm.id(inp.id);
117                             aesm.isdeleted(true);
118                             updates.push(aesm);
119                         }
120                     } else if (inp.selected) {
121                         // no ID, must be newly checked
122                         const aesm = this.idl.create('aeasm');
123                         aesm.attr(inp.key);
124                         aesm.attr_set(setId);
125                         aesm.isnew(true);
126                         updates.push(aesm);
127                     }
128                 });
129             }
130             this.pcrud.autoApply(updates).subscribe(
131                 ret => this.close(true),
132                 err => this.close(err),
133                 () => this.close(true)
134             );
135         }, err => this.close(false));
136     }
137
138 }