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