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';
12 selector: 'eg-catalog-record-notes',
13 templateUrl: 'notes.component.html'
15 export class NotesComponent implements OnInit {
18 gridDataSource: GridDataSource;
20 @ViewChild('notesGrid', { static: true }) notesGrid: GridComponent;
21 @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
25 createNew: () => void;
26 deleteSelected: (rows: IdlObject[]) => void;
27 permissions: {[name: string]: boolean};
29 @Input() set recordId(id: number) {
31 // Only force new data collection when recordId()
32 // is invoked after ngInit() has already run.
34 this.notesGrid.reload();
38 get recordId(): number {
43 private idl: IdlService,
44 private org: OrgService,
45 private pcrud: PcrudService,
46 private perm: PermService
48 this.permissions = {};
49 this.gridDataSource = new GridDataSource();
56 this.perm.hasWorkPermHere([
60 ]).then(perms => this.permissions = perms);
62 this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
63 const orderBy: any = {};
65 if (sort.length) { // Sort provided by grid.
66 orderBy.bren = sort[0].name + ' ' + sort[0].dir;
68 orderBy.bren = 'edit_date';
76 flesh_fields: {bren: ['creator', 'editor']}
79 return this.pcrud.search('bren',
80 {record: this.recId, deleted: 'f'}, searchOps);
83 this.notesGrid.onRowActivate.subscribe(
84 (note: IdlObject) => {
85 this.editDialog.mode = 'update';
86 this.editDialog.recordId = note.id();
87 this.editDialog.open()
88 .subscribe(ok => this.notesGrid.reload());
92 this.createNew = () => {
94 const note = this.idl.create('bren');
95 note.record(this.recordId);
96 this.editDialog.record = note;
98 this.editDialog.mode = 'create';
99 this.editDialog.open().subscribe(ok => this.notesGrid.reload());
102 this.deleteSelected = (notes: IdlObject[]) => {
103 notes.forEach(note => note.isdeleted(true));
104 this.pcrud.autoApply(notes).subscribe(
107 () => this.notesGrid.reload()