]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/record/notes.component.ts
LP#1929242: (follow-up) additional tweaks
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / record / notes.component.ts
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
11 @Component({
12   selector: 'eg-catalog-record-notes',
13   templateUrl: 'notes.component.html'
14 })
15 export class NotesComponent implements OnInit {
16
17     recId: number;
18     gridDataSource: GridDataSource;
19     initDone: boolean;
20     @ViewChild('notesGrid', { static: true }) notesGrid: GridComponent;
21     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
22
23     canCreate: boolean;
24     canDelete: boolean;
25     createNew: () => void;
26     deleteSelected: (rows: IdlObject[]) => void;
27     permissions: {[name: string]: boolean};
28
29     @Input() set recordId(id: number) {
30         this.recId = id;
31         // Only force new data collection when recordId()
32         // is invoked after ngInit() has already run.
33         if (this.initDone) {
34             this.notesGrid.reload();
35         }
36     }
37
38     get recordId(): number {
39         return this.recId;
40     }
41
42     constructor(
43         private idl: IdlService,
44         private org: OrgService,
45         private pcrud: PcrudService,
46         private perm: PermService
47     ) {
48         this.permissions = {};
49         this.gridDataSource = new GridDataSource();
50     }
51
52     ngOnInit() {
53         this.initDone = true;
54
55         // Load edit perms
56         this.perm.hasWorkPermHere([
57             'CREATE_RECORD_NOTE',
58             'UPDATE_RECORD_NOTE',
59             'DELETE_RECORD_NOTE'
60         ]).then(perms => this.permissions = perms);
61
62         this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
63             const orderBy: any = {};
64
65             if (sort.length) { // Sort provided by grid.
66                 orderBy.bren = sort[0].name + ' ' + sort[0].dir;
67             } else {
68                 orderBy.bren = 'edit_date';
69             }
70
71             const searchOps = {
72                 offset: pager.offset,
73                 limit: pager.limit,
74                 order_by: orderBy,
75                 flesh: 2,
76                 flesh_fields: {bren: ['creator', 'editor']}
77             };
78
79             return this.pcrud.search('bren',
80                 {record: this.recId, deleted: 'f'}, searchOps);
81         };
82
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());
89             }
90         );
91
92         this.createNew = () => {
93
94             const note = this.idl.create('bren');
95             note.record(this.recordId);
96             this.editDialog.record = note;
97
98             this.editDialog.mode = 'create';
99             this.editDialog.open().subscribe(ok => this.notesGrid.reload());
100         };
101
102         this.deleteSelected = (notes: IdlObject[]) => {
103             notes.forEach(note => note.isdeleted(true));
104             this.pcrud.autoApply(notes).subscribe(
105                 val => {},
106                 err => {},
107                 ()  => this.notesGrid.reload()
108             );
109         };
110     }
111 }
112