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();
20 @ViewChild('partsGrid') partsGrid: GridComponent;
21 @ViewChild('editDialog') editDialog: FmRecordEditorComponent;
22 @ViewChild('grid') grid: GridComponent;
23 @ViewChild('successString') successString: StringComponent;
24 @ViewChild('createString') createString: StringComponent;
25 @ViewChild('createErrString') createErrString: StringComponent;
26 @ViewChild('updateFailedString') updateFailedString: StringComponent;
27 @ViewChild('cspFlairTooltip') private cspFlairTooltip: StringComponent;
29 cspRowFlairCallback: (row: any) => GridRowFlairEntry;
33 deleteSelected: (rows: IdlObject[]) => void;
35 permissions: {[name: string]: boolean};
37 // Default sort field, used when no grid sorting is applied.
38 @Input() sortField: string;
40 @Input() idlClass: string = "csp";
41 // Size of create/edito dialog. Uses large by default.
42 @Input() dialogSize: 'sm' | 'lg' = 'lg';
43 // Optional comma-separated list of read-only fields
44 // @Input() readonlyFields: string;
46 @Input() set recordId(id: number) {
48 // Only force new data collection when recordId()
49 // is invoked after ngInit() has already run.
51 this.partsGrid.reload();
56 private pcrud: PcrudService,
57 private toast: ToastService
59 this.gridDataSource = new GridDataSource();
64 this.cspSource.getRows = (pager: Pager, sort: any[]) => {
65 const orderBy: any = {};
67 // Sort specified from grid
68 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
69 } else if (this.sortField) {
71 orderBy[this.idlClass] = this.sortField;
79 return this.pcrud.retrieveAll('csp', searchOps, {fleshSelectors: true});
82 this.cspRowFlairCallback = (row: any): GridRowFlairEntry => {
83 const flair = {icon: null, title: null};
85 flair.icon = 'not_interested';
86 flair.title = this.cspFlairTooltip.text;
92 cspReadonlyOverride = (field: string, copy: IdlObject): boolean => {
93 if (copy.id() >= 100) {
99 cspGridCellClassCallback = (row: any, col: GridColumn): string => {
100 if (col.name === "id" && row.a[0] < 100) {
101 return "text-danger";
106 showEditDialog(standingPenalty: IdlObject): Promise<any> {
107 this.editDialog.mode = 'update';
108 this.editDialog.recId = standingPenalty["id"]();
109 return new Promise((resolve, reject) => {
110 this.editDialog.open({size: this.dialogSize}).subscribe(
112 this.successString.current()
113 .then(str => this.toast.success(str));
118 this.updateFailedString.current()
119 .then(str => this.toast.danger(str));
126 editSelected(standingPenaltyFields: IdlObject[]) {
127 // Edit each IDL thing one at a time
128 const editOneThing = (standingPenalty: IdlObject) => {
129 if (!standingPenalty) { return; }
131 this.showEditDialog(standingPenalty).then(
132 () => editOneThing(standingPenaltyFields.shift()));
135 editOneThing(standingPenaltyFields.shift());
139 this.editDialog.mode = 'create';
140 // We reuse the same editor for all actions. Be sure
141 // create action does not try to modify an existing record.
142 this.editDialog.recId = null;
143 this.editDialog.record = null;
144 this.editDialog.open({size: this.dialogSize}).subscribe(
146 this.createString.current()
147 .then(str => this.toast.success(str));
151 if (!rejection.dismissed) {
152 this.createErrString.current()
153 .then(str => this.toast.danger(str));