]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/cat/vandelay/match-set-list.component.ts
c58735fc3a3fefec28caac52a0c32b1c1f41ba62
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / cat / vandelay / match-set-list.component.ts
1 import {Component, AfterViewInit, ViewChild} from '@angular/core';
2 import {Router} from '@angular/router';
3 import {Pager} from '@eg/share/util/pager';
4 import {IdlObject} from '@eg/core/idl.service';
5 import {PcrudService} from '@eg/core/pcrud.service';
6 import {OrgService} from '@eg/core/org.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {GridComponent} from '@eg/share/grid/grid.component';
9 import {GridDataSource, GridColumn} from '@eg/share/grid/grid';
10 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
11
12 @Component({
13   templateUrl: 'match-set-list.component.html'
14 })
15 export class MatchSetListComponent implements AfterViewInit {
16
17     contextOrg: IdlObject;
18     gridSource: GridDataSource;
19     deleteSelected: (rows: IdlObject[]) => void;
20     createNew: () => void;
21     @ViewChild('grid', { static: true }) grid: GridComponent;
22     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
23
24     cellPrintValues: (row: any, cell: GridColumn) => string;
25
26     constructor(
27         private router: Router,
28         private pcrud: PcrudService,
29         private auth: AuthService,
30         private org: OrgService) {
31
32         this.gridSource = new GridDataSource();
33         this.contextOrg = this.org.get(this.auth.user().ws_ou());
34
35         this.gridSource.getRows = (pager: Pager) => {
36             const orgs = this.org.ancestors(this.contextOrg, true);
37             return this.pcrud.search('vms', {owner: orgs}, {
38                 order_by: {vms: ['name']},
39                 limit: pager.limit,
40                 offset: pager.offset
41             });
42         };
43
44         // Text-ify function for cells that use display templates.
45         this.cellPrintValues = (row: any, cell: GridColumn): string => {
46             switch (cell.name) {
47                 case 'name':
48                     return row.name();
49             }
50         };
51
52         this.createNew = () => {
53             this.editDialog.mode = 'create';
54             this.editDialog.open({size: 'lg'})
55                 .subscribe(() => this.grid.reload());
56         };
57
58         this.deleteSelected = (matchSets: IdlObject[]) => {
59             matchSets.forEach(matchSet => matchSet.isdeleted(true));
60             this.pcrud.autoApply(matchSets).subscribe(
61                 val => console.debug('deleted: ' + val),
62                 err => {},
63                 ()  => this.grid.reload()
64             );
65         };
66     }
67
68     ngAfterViewInit() {
69         this.grid.onRowActivate.subscribe(
70             (matchSet: IdlObject) => {
71                 this.editDialog.mode = 'update';
72                 this.editDialog.recordId = matchSet.id();
73                 this.editDialog.open({size: 'lg'})
74                     .subscribe(() => this.grid.reload());
75             }
76         );
77     }
78
79     orgOnChange(org: IdlObject) {
80         this.contextOrg = org;
81         this.grid.reload();
82     }
83 }
84