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';
14 templateUrl: './circ-matrix-matchpoint.component.html'
16 export class CircMatrixMatchpointComponent implements OnInit {
18 gridDataSource: GridDataSource;
20 dataSource: GridDataSource = new GridDataSource();
21 showLinkLimitSets = false;
22 usedSetLimitList = {};
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;
41 @Input() idlClass = 'ccmm';
42 // Default sort field, used when no grid sorting is applied.
43 @Input() sortField: string;
45 @Input() dialogSize: 'sm' | 'lg' = 'lg';
48 private pcrud: PcrudService,
49 private toast: ToastService
51 this.gridDataSource = new GridDataSource();
56 this.dataSource.getRows = (pager: Pager, sort: any[]) => {
57 const orderBy: any = {};
59 // Sort specified from grid
60 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
61 } else if (this.sortField) {
63 orderBy[this.idlClass] = this.sortField;
71 return this.pcrud.retrieveAll('ccmm', searchOps, {fleshSelectors: true});
75 clearLinkedCircLimitSets() {
76 this.limitSetsComponent.usedSetLimitList = {};
77 this.limitSetsComponent.linkedSetList = [];
78 this.linkedLimitSets = [];
82 this.matchpointDialog.closeEditor();
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()));
93 editOneThing(fields.shift());
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(
104 this.successString.current()
105 .then(str => this.toast.success(str));
110 this.updateFailedString.current()
111 .then(str => this.toast.danger(str));
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(
129 this.createString.current()
130 .then(str => this.toast.success(str));
131 this.limitSetsComponent.showLinkLimitSets = false;
135 if (!rejection.dismissed) {
136 this.createErrString.current()
137 .then(str => this.toast.danger(str));
139 this.limitSetsComponent.showLinkLimitSets = false;
145 this.linkedLimitSets = sets;
149 * Runs through the different CRUD operations, specified by the object that is passed into each.
152 configureLimitSets(matchpoint) {
153 const linkedSets = this.linkedLimitSets;
154 Object.keys(linkedSets).forEach((key) => {
155 const ls = linkedSets[key];
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);
169 this.clearLinkedCircLimitSets();
170 this.pcrud.retrieveAll('ccmlsm').subscribe((res) => {
172 * If the limit set's matchpoint equals the matchpoint given
173 * by the user, then add that to the set limit list
175 this.limitSetsComponent.usedSetLimitList[res.limit_set()] = res.id();
176 if (res.matchpoint() === id) {
177 this.limitSetsComponent.createFilledLimitSetObject(res);
181 * Retrives all limit set names
183 this.pcrud.retrieveAll('ccls').subscribe((res) => {
184 this.limitSetsComponent.limitSetNames[res.id()] = res.name();
188 createLimitSets(limitSet, matchpoint) {
189 if (typeof matchpoint === 'number' || typeof matchpoint === 'string') {
190 limitSet.matchpoint(matchpoint);
192 limitSet.matchpoint(matchpoint.id());
197 updateLimitSets(limitSet) {
198 this.pcrud.update(limitSet).subscribe(() => {});
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 => {