1 import {Component, OnInit, Input, ViewChild} from '@angular/core';
2 import {IdlService, IdlObject} from '@eg/core/idl.service';
3 import {PcrudService} from '@eg/core/pcrud.service';
4 import {Pager} from '@eg/share/util/pager';
5 import {OrgService} from '@eg/core/org.service';
6 import {PermService} from '@eg/core/perm.service';
7 import {GridDataSource} from '@eg/share/grid/grid';
8 import {GridComponent} from '@eg/share/grid/grid.component';
9 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
10 import {PartMergeDialogComponent} from './part-merge-dialog.component';
13 selector: 'eg-catalog-record-parts',
14 templateUrl: 'parts.component.html'
16 export class PartsComponent implements OnInit {
19 gridDataSource: GridDataSource;
21 @ViewChild('partsGrid', { static: true }) partsGrid: GridComponent;
22 @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
23 @ViewChild('mergeDialog', { static: true }) mergeDialog: PartMergeDialogComponent;
27 createNew: () => void;
28 deleteSelected: (rows: IdlObject[]) => void;
29 mergeSelected: (rows: IdlObject[]) => void;
30 permissions: {[name: string]: boolean};
32 @Input() set recordId(id: number) {
34 // Only force new data collection when recordId()
35 // is invoked after ngInit() has already run.
37 this.partsGrid.reload();
41 get recordId(): number {
46 private idl: IdlService,
47 private org: OrgService,
48 private pcrud: PcrudService,
49 private perm: PermService
51 this.permissions = {};
52 this.gridDataSource = new GridDataSource();
59 this.perm.hasWorkPermHere([
60 'CREATE_MONOGRAPH_PART',
61 'UPDATE_MONOGRAPH_PART',
62 'DELETE_MONOGRAPH_PART'
63 ]).then(perms => this.permissions = perms);
65 this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
66 const orderBy: any = {};
68 if (sort.length) { // Sort provided by grid.
69 orderBy.bmp = sort[0].name + ' ' + sort[0].dir;
71 orderBy.bmp = 'label';
80 return this.pcrud.search('bmp',
81 {record: this.recId, deleted: 'f'}, searchOps);
84 this.partsGrid.onRowActivate.subscribe(
85 (part: IdlObject) => {
86 this.editDialog.mode = 'update';
87 this.editDialog.recordId = part.id();
88 this.editDialog.open()
89 .subscribe(ok => this.partsGrid.reload());
93 this.createNew = () => {
95 const part = this.idl.create('bmp');
96 part.record(this.recordId);
97 this.editDialog.record = part;
99 this.editDialog.mode = 'create';
100 this.editDialog.open().subscribe(ok => this.partsGrid.reload());
103 this.deleteSelected = (parts: IdlObject[]) => {
104 parts.forEach(part => part.isdeleted(true));
105 this.pcrud.autoApply(parts).subscribe(
106 val => console.debug('deleted: ' + val),
108 () => this.partsGrid.reload()
112 this.mergeSelected = (parts: IdlObject[]) => {
113 if (parts.length < 2) { return; }
114 this.mergeDialog.parts = parts;
115 this.mergeDialog.open().subscribe(ok => this.partsGrid.reload());