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';
13 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 = [];
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;
36 @Input() idlClass = 'ccmm';
37 // Default sort field, used when no grid sorting is applied.
38 @Input() sortField: string;
40 @Input() dialogSize: 'sm' | 'lg' = 'lg';
44 private pcrud: PcrudService,
45 private toast: ToastService
47 this.gridDataSource = new GridDataSource();
52 this.dataSource.getRows = (pager: Pager, sort: any[]) => {
53 const orderBy: any = {};
55 // Sort specified from grid
56 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
57 } else if (this.sortField) {
59 orderBy[this.idlClass] = this.sortField;
67 return this.pcrud.retrieveAll('ccmm', searchOps, {fleshSelectors: true});
72 clearLinkedCircLimitSets() {
73 this.limitSetsComponent.usedSetLimitList = [];
74 this.limitSetsComponent.linkedSetList = [];
75 this.linkedLimitSets = [];
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(
86 this.successString.current()
87 .then(str => this.toast.success(str));
92 this.updateFailedString.current()
93 .then(str => this.toast.danger(str));
97 const modalBody = document.getElementsByClassName("modal-body");
98 modalBody[modalBody.length-1].appendChild(this.limitSets.nativeElement)
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()));
109 editOneThing(fields.shift());
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(
122 this.createString.current()
123 .then(str => this.toast.success(str));
124 this.limitSetsComponent.showLinkLimitSets = false;
128 if (!rejection.dismissed) {
129 this.createErrString.current()
130 .then(str => this.toast.danger(str));
132 this.limitSetsComponent.showLinkLimitSets = false;
135 const modalBody = document.getElementsByClassName("modal-body");
136 modalBody[modalBody.length-1].appendChild(this.limitSets.nativeElement)
140 this.linkedLimitSets = sets;
144 * Runs through the different CRUD operations, specified by the object that is passed into each.
147 configureLimitSets(matchpoint) {
148 const linkedSets = this.linkedLimitSets;
149 Object.keys(linkedSets).forEach((key) =>{
150 let ls = linkedSets[key]
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);
164 this.pcrud.retrieveAll("ccmlsm").subscribe((res) =>{
166 * If the limit set's matchpoint equals the matchpoint given
167 * by the user, then add that to the set limit list
169 this.limitSetsComponent.usedSetLimitList.push(res.limit_set());
170 if (res.matchpoint() == id) {
171 this.limitSetsComponent.createFilledLimitSetObject(res)
175 * Retrives all limit set names
177 this.pcrud.retrieveAll("ccls").subscribe(res =>{
178 this.limitSetsComponent.limitSetNames[res.id()] = res.name();
182 createLimitSets(limitSet,matchpoint) {
183 if(typeof matchpoint == "number" || typeof matchpoint == "string") {
184 limitSet.matchpoint(matchpoint)
186 limitSet.matchpoint(matchpoint.id())
191 updateLimitSets(limitSet) {
192 this.pcrud.update(limitSet).subscribe(() =>{})
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 =>{