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