1 import {Pager} from '@eg/share/util/pager';
2 import {Component, OnInit, Input, ViewChild} 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 {PcrudService} from '@eg/core/pcrud.service';
7 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
8 import {StringComponent} from '@eg/share/string/string.component';
9 import {ToastService} from '@eg/share/toast/toast.service';
12 templateUrl: './standing-penalty.component.html'
15 export class StandingPenaltyComponent implements OnInit {
17 gridDataSource: GridDataSource;
19 cspSource: GridDataSource = new GridDataSource();
21 @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
22 @ViewChild('grid', { static: true }) grid: GridComponent;
23 @ViewChild('successString', { static: true }) successString: StringComponent;
24 @ViewChild('createString', { static: false }) createString: StringComponent;
25 @ViewChild('createErrString', { static: false }) createErrString: StringComponent;
26 @ViewChild('updateFailedString', { static: false }) updateFailedString: StringComponent;
27 @ViewChild('deleteFailedString', { static: true }) deleteFailedString: StringComponent;
28 @ViewChild('deleteSuccessString', { static: true }) deleteSuccessString: StringComponent;
29 @ViewChild('cspFlairTooltip', { static: true }) private cspFlairTooltip: StringComponent;
31 cspRowFlairCallback: (row: any) => GridRowFlairEntry;
35 deleteSelected: (rows: IdlObject[]) => void;
37 permissions: {[name: string]: boolean};
39 // Default sort field, used when no grid sorting is applied.
40 @Input() sortField: string;
42 @Input() idlClass = 'csp';
43 // Size of create/edito dialog. Uses large by default.
44 @Input() dialogSize: 'sm' | 'lg' = 'lg';
45 // Optional comma-separated list of read-only fields
46 // @Input() readonlyFields: string;
49 private pcrud: PcrudService,
50 private toast: ToastService
52 this.gridDataSource = new GridDataSource();
57 this.cspSource.getRows = (pager: Pager, sort: any[]) => {
58 const orderBy: any = {};
60 // Sort specified from grid
61 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
62 } else if (this.sortField) {
64 orderBy[this.idlClass] = this.sortField;
72 return this.pcrud.retrieveAll('csp', searchOps, {fleshSelectors: true});
75 this.cspRowFlairCallback = (row: any): GridRowFlairEntry => {
76 const flair = {icon: null, title: null};
78 flair.icon = 'not_interested';
79 flair.title = this.cspFlairTooltip.text;
84 this.deleteSelected = (idlThings: IdlObject[]) => {
85 idlThings.forEach(idlThing => idlThing.isdeleted(true));
86 this.pcrud.autoApply(idlThings).subscribe(
88 console.debug('deleted: ' + val);
89 this.deleteSuccessString.current()
90 .then(str => this.toast.success(str));
93 this.deleteFailedString.current()
94 .then(str => this.toast.danger(str));
96 () => this.grid.reload()
102 cspReadonlyOverride = (field: string, csp: IdlObject): boolean => {
103 if (csp.id() >= 100 || csp.id() === undefined) {
109 cspGridCellClassCallback = (row: any, col: GridColumn): string => {
110 if (col.name === 'id' && row.a[0] < 100) {
111 return 'text-danger';
116 showEditDialog(standingPenalty: IdlObject): Promise<any> {
117 this.editDialog.mode = 'update';
118 this.editDialog.recordId = standingPenalty['id']();
119 return new Promise((resolve, reject) => {
120 this.editDialog.open({size: this.dialogSize}).subscribe(
122 this.successString.current()
123 .then(str => this.toast.success(str));
128 this.updateFailedString.current()
129 .then(str => this.toast.danger(str));
136 editSelected(standingPenaltyFields: IdlObject[]) {
137 // Edit each IDL thing one at a time
138 const editOneThing = (standingPenalty: IdlObject) => {
139 if (!standingPenalty) { return; }
141 this.showEditDialog(standingPenalty).then(
142 () => editOneThing(standingPenaltyFields.shift()));
145 editOneThing(standingPenaltyFields.shift());
149 this.editDialog.mode = 'create';
150 // We reuse the same editor for all actions. Be sure
151 // create action does not try to modify an existing record.
152 this.editDialog.recordId = null;
153 this.editDialog.record = null;
154 this.editDialog.open({size: this.dialogSize}).subscribe(
156 this.createString.current()
157 .then(str => this.toast.success(str));
161 if (!rejection.dismissed) {
162 this.createErrString.current()
163 .then(str => this.toast.danger(str));