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()
100 this.grid.onRowActivate.subscribe(
101 (idlThing: IdlObject) => this.showEditDialog(idlThing)
106 cspReadonlyOverride = (field: string, csp: IdlObject): boolean => {
107 if (csp.id() >= 100 || csp.id() === undefined) {
113 cspGridCellClassCallback = (row: any, col: GridColumn): string => {
114 if (col.name === 'id' && row.a[0] < 100) {
115 return 'text-danger';
120 showEditDialog(standingPenalty: IdlObject): Promise<any> {
121 this.editDialog.mode = 'update';
122 this.editDialog.recordId = standingPenalty['id']();
123 return new Promise((resolve, reject) => {
124 this.editDialog.open({size: this.dialogSize}).subscribe(
126 this.successString.current()
127 .then(str => this.toast.success(str));
132 this.updateFailedString.current()
133 .then(str => this.toast.danger(str));
140 editSelected(standingPenaltyFields: IdlObject[]) {
141 // Edit each IDL thing one at a time
142 const editOneThing = (standingPenalty: IdlObject) => {
143 if (!standingPenalty) { return; }
145 this.showEditDialog(standingPenalty).then(
146 () => editOneThing(standingPenaltyFields.shift()));
149 editOneThing(standingPenaltyFields.shift());
153 this.editDialog.mode = 'create';
154 // We reuse the same editor for all actions. Be sure
155 // create action does not try to modify an existing record.
156 this.editDialog.recordId = null;
157 this.editDialog.record = null;
158 this.editDialog.open({size: this.dialogSize}).subscribe(
160 this.createString.current()
161 .then(str => this.toast.success(str));
165 if (!rejection.dismissed) {
166 this.createErrString.current()
167 .then(str => this.toast.danger(str));