3c6b060debbc47f459a8a0790366c7ce5ee11919
[working/Evergreen.git] /
1 import {Pager} from '@eg/share/util/pager';
2 import {Component, OnInit, Input, ViewChild, ElementRef} from '@angular/core';
3 import {GridComponent} from '@eg/share/grid/grid.component';
4 import {GridDataSource, GridColumn, GridRowFlairEntry} from '@eg/share/grid/grid';
5 import {IdlObject} from '@eg/core/idl.service';
6 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
7 import {LinkedCircLimitSetsComponent} from './linked-circ-limit-sets.component';
8 import {StringComponent} from '@eg/share/string/string.component';
9 import {PcrudService} from '@eg/core/pcrud.service';
10 import {ToastService} from '@eg/share/toast/toast.service';
11
12 @Component({
13     templateUrl: './circ-matrix-matchpoint.component.html'
14 })
15
16 export class CircMatrixMatchpointComponent implements OnInit {
17     recId: number;
18     gridDataSource: GridDataSource;
19     initDone = false;
20     dataSource: GridDataSource = new GridDataSource();
21     showLinkLimitSets = false;
22     usedSetLimitList = [];
23     linkedLimitSets = [];
24     limitSetNames = {};
25
26     
27     @ViewChild('limitSets', { static: false }) limitSets: ElementRef;
28     @ViewChild('circLimitSets', { static: true }) limitSetsComponent: LinkedCircLimitSetsComponent;
29     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
30     @ViewChild('grid', { static: true }) grid: GridComponent;
31     @ViewChild('successString', { static: true }) successString: StringComponent;
32     @ViewChild('createString', { static: false }) createString: StringComponent;
33     @ViewChild('createErrString', { static: false }) createErrString: StringComponent;
34     @ViewChild('updateFailedString', { static: false }) updateFailedString: StringComponent;
35
36     @Input() idlClass = 'ccmm';
37     // Default sort field, used when no grid sorting is applied.
38     @Input() sortField: string;
39
40     @Input() dialogSize: 'sm' | 'lg' = 'lg';
41
42     
43     constructor(
44         private pcrud: PcrudService,
45         private toast: ToastService
46     ) {
47         this.gridDataSource = new GridDataSource();
48     }
49
50     ngOnInit() {
51         this.initDone = true;
52         this.dataSource.getRows = (pager: Pager, sort: any[]) => {
53             const orderBy: any = {};
54             if (sort.length) {
55                 // Sort specified from grid
56                 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
57             } else if (this.sortField) {
58                 // Default sort field
59                 orderBy[this.idlClass] = this.sortField;
60             }
61
62             const searchOps = {
63                 offset: pager.offset,
64                 limit: pager.limit,
65                 order_by: orderBy
66             };
67             return this.pcrud.retrieveAll('ccmm', searchOps, {fleshSelectors: true});
68         }
69     }
70
71
72     clearLinkedCircLimitSets() {
73         this.limitSetsComponent.usedSetLimitList = [];
74         this.limitSetsComponent.linkedSetList = [];
75         this.linkedLimitSets = [];
76     }
77
78     showEditDialog(field: IdlObject): Promise<any> {
79         this.limitSetsComponent.showLinkLimitSets = true;
80         this.getLimitSets(field.id());
81         this.editDialog.mode = 'update';
82         this.editDialog.recordId = field['id']();
83         return new Promise((resolve, reject) => {
84             this.editDialog.open({size: this.dialogSize}).subscribe(
85                 result => {
86                     this.successString.current()
87                         .then(str => this.toast.success(str));
88                     this.grid.reload();
89                     resolve(result);
90                 },
91                 error => {
92                     this.updateFailedString.current()
93                         .then(str => this.toast.danger(str));
94                     reject(error);
95                 }
96             );
97             const modalBody = document.getElementsByClassName("modal-body");
98             modalBody[modalBody.length-1].appendChild(this.limitSets.nativeElement)
99         })
100     }
101
102     editSelected(fields: IdlObject[]) {
103         // Edit each IDL thing one at a time
104         const editOneThing = (field: IdlObject) => {
105             if (!field) { return; }
106             this.showEditDialog(field).then(
107                 () => editOneThing(fields.shift()));
108         };
109         editOneThing(fields.shift());
110     }
111
112     createNew() {
113         this.getLimitSets(null);
114         this.limitSetsComponent.showLinkLimitSets = true;
115         this.editDialog.mode = 'create';
116         // We reuse the same editor for all actions.  Be sure
117         // create action does not try to modify an existing record.
118         this.editDialog.recordId = null;
119         this.editDialog.record = null;
120         this.editDialog.open({size: this.dialogSize}).subscribe(
121             ok => {
122                 this.createString.current()
123                     .then(str => this.toast.success(str));
124                 this.limitSetsComponent.showLinkLimitSets = false;
125                 this.grid.reload();
126             },
127             rejection => {
128                 if (!rejection.dismissed) {
129                     this.createErrString.current()
130                         .then(str => this.toast.danger(str));
131                 }
132                 this.limitSetsComponent.showLinkLimitSets = false;
133             }
134         );
135         const modalBody = document.getElementsByClassName("modal-body");
136         modalBody[modalBody.length-1].appendChild(this.limitSets.nativeElement)
137     }
138
139     setLimitSets(sets) {
140         this.linkedLimitSets = sets;
141     }
142
143     /**
144      * Runs through the different CRUD operations, specified by the object that is passed into each.
145      * @param matchpoint 
146      */
147     configureLimitSets(matchpoint) {
148         const linkedSets = this.linkedLimitSets;
149         Object.keys(linkedSets).forEach((key) =>{
150             let ls = linkedSets[key]
151             if(ls.created) {
152                 this.deleteLimitSets(ls).then(()=>{
153                     if (ls.isNew && !ls.isDeleted) {
154                         this.pcrud.create(this.createLimitSets(ls.linkedLimitSet,matchpoint)).subscribe(() =>{})
155                     } else if(!ls.isNew && !ls.isDeleted) {
156                         this.updateLimitSets(ls.linkedLimitSet);
157                     }
158                 })
159             }
160         })
161     }
162
163     getLimitSets(id) {
164         this.pcrud.retrieveAll("ccmlsm").subscribe((res) =>{
165             /**
166              * If the limit set's matchpoint equals the matchpoint given
167              * by the user, then add that to the set limit list
168              */
169             this.limitSetsComponent.usedSetLimitList.push(res.limit_set());
170             if (res.matchpoint() == id) {
171                 this.limitSetsComponent.createFilledLimitSetObject(res)
172             }
173         })
174         /**
175          * Retrives all limit set names
176          */
177         this.pcrud.retrieveAll("ccls").subscribe(res =>{
178             this.limitSetsComponent.limitSetNames[res.id()] = res.name();
179         })
180     }
181
182     createLimitSets(limitSet,matchpoint) {
183         if(typeof matchpoint == "number" || typeof matchpoint == "string") {
184             limitSet.matchpoint(matchpoint)
185         } else {
186             limitSet.matchpoint(matchpoint.id())
187         }
188         return limitSet
189     }
190
191     updateLimitSets(limitSet) {
192         this.pcrud.update(limitSet).subscribe(() =>{})
193     }
194
195     deleteLimitSets(limitSet) {
196         return new Promise((resolve, reject) =>{
197             if (limitSet.isDeleted) {
198                 if(limitSet.linkedLimitSet.id()) {
199                     this.pcrud.remove(limitSet.linkedLimitSet).subscribe(res =>{
200                         resolve();
201                     })
202                 } else {
203                     resolve();
204                 }
205             } else {
206                 resolve();
207             }
208         })
209     }
210 }
211