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();
42 private idl: IdlService,
43 private org: OrgService,
44 private pcrud: PcrudService,
45 private perm: PermService
47 this.permissions = {};
48 this.gridDataSource = new GridDataSource();
55 this.perm.hasWorkPermHere([
56 'CREATE_MONOGRAPH_PART',
57 'UPDATE_MONOGRAPH_PART',
58 'DELETE_MONOGRAPH_PART'
59 ]).then(perms => this.permissions = perms);
61 this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
62 const orderBy: any = {};
64 if (sort.length) { // Sort provided by grid.
65 orderBy.bmp = sort[0].name + ' ' + sort[0].dir;
67 orderBy.bmp = 'label';
76 return this.pcrud.search('bmp',
77 {record: this.recId, deleted: 'f'}, searchOps);
80 this.partsGrid.onRowActivate.subscribe(
81 (part: IdlObject) => {
82 this.editDialog.mode = 'update';
83 this.editDialog.recordId = part.id();
84 this.editDialog.open()
85 .subscribe(ok => this.partsGrid.reload());
89 this.createNew = () => {
91 const part = this.idl.create('bmp');
92 part.record(this.recordId);
93 this.editDialog.record = part;
95 this.editDialog.mode = 'create';
96 this.editDialog.open().subscribe(ok => this.partsGrid.reload());
99 this.deleteSelected = (parts: IdlObject[]) => {
100 parts.forEach(part => part.isdeleted(true));
101 this.pcrud.autoApply(parts).subscribe(
102 val => console.debug('deleted: ' + val),
104 () => this.partsGrid.reload()
108 this.mergeSelected = (parts: IdlObject[]) => {
109 if (parts.length < 2) { return; }
110 this.mergeDialog.parts = parts;
111 this.mergeDialog.open().subscribe(ok => this.partsGrid.reload());