]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/record/parts.component.ts
LP1806087 Angular catalog Ang7 & lint repairs
[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') partsGrid: GridComponent;
22     @ViewChild('editDialog') editDialog: FmRecordEditorComponent;
23     @ViewChild('mergeDialog') 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     constructor(
42         private idl: IdlService,
43         private org: OrgService,
44         private pcrud: PcrudService,
45         private perm: PermService
46     ) {
47         this.permissions = {};
48         this.gridDataSource = new GridDataSource();
49     }
50
51     ngOnInit() {
52         this.initDone = true;
53
54         // Load edit perms
55         this.perm.hasWorkPermHere([
56             'CREATE_MONOGRAPH_PART',
57             'UPDATE_MONOGRAPH_PART',
58             'DELETE_MONOGRAPH_PART'
59         ]).then(perms => this.permissions = perms);
60
61         this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
62             const orderBy: any = {};
63
64             if (sort.length) { // Sort provided by grid.
65                 orderBy.bmp = sort[0].name + ' ' + sort[0].dir;
66             } else {
67                 orderBy.bmp = 'label';
68             }
69
70             const searchOps = {
71                 offset: pager.offset,
72                 limit: pager.limit,
73                 order_by: orderBy
74             };
75
76             return this.pcrud.search('bmp',
77                 {record: this.recId, deleted: 'f'}, searchOps);
78         };
79
80         this.partsGrid.onRowActivate.subscribe(
81             (part: IdlObject) => {
82                 this.editDialog.mode = 'update';
83                 this.editDialog.recId = part.id();
84                 this.editDialog.open().then(
85                     ok => this.partsGrid.reload(),
86                     err => {}
87                 );
88             }
89         );
90
91         this.createNew = () => {
92
93             const part = this.idl.create('bmp');
94             part.record(this.recId);
95             this.editDialog.record = part;
96
97             this.editDialog.mode = 'create';
98             this.editDialog.open().then(
99                 ok => this.partsGrid.reload(),
100                 err => {}
101             );
102         };
103
104         this.deleteSelected = (parts: IdlObject[]) => {
105             parts.forEach(part => part.isdeleted(true));
106             this.pcrud.autoApply(parts).subscribe(
107                 val => console.debug('deleted: ' + val),
108                 err => {},
109                 ()  => this.partsGrid.reload()
110             );
111         };
112
113         this.mergeSelected = (parts: IdlObject[]) => {
114             if (parts.length < 2) { return; }
115             this.mergeDialog.parts = parts;
116             this.mergeDialog.open().then(
117                 ok => this.partsGrid.reload(),
118                 err => console.debug('Dialog dismissed')
119             );
120         };
121     }
122 }
123