Forward-port 3.3.4 upgrade script
[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') grid: GridComponent;
22     @ViewChild('editDialog') editDialog: FmRecordEditorComponent;
23
24     constructor(
25         private router: Router,
26         private pcrud: PcrudService,
27         private auth: AuthService,
28         private org: OrgService) {
29
30         this.gridSource = new GridDataSource();
31         this.contextOrg = this.org.get(this.auth.user().ws_ou());
32
33         this.gridSource.getRows = (pager: Pager) => {
34             const orgs = this.org.ancestors(this.contextOrg, true);
35             return this.pcrud.search('vms', {owner: orgs}, {
36                 order_by: {vms: ['name']},
37                 limit: pager.limit,
38                 offset: pager.offset
39             });
40         };
41
42         this.createNew = () => {
43             this.editDialog.mode = 'create';
44             this.editDialog.open({size: 'lg'})
45                 .subscribe(() => this.grid.reload());
46         };
47
48         this.deleteSelected = (matchSets: IdlObject[]) => {
49             matchSets.forEach(matchSet => matchSet.isdeleted(true));
50             this.pcrud.autoApply(matchSets).subscribe(
51                 val => console.debug('deleted: ' + val),
52                 err => {},
53                 ()  => this.grid.reload()
54             );
55         };
56     }
57
58     ngAfterViewInit() {
59         this.grid.onRowActivate.subscribe(
60             (matchSet: IdlObject) => {
61                 this.editDialog.mode = 'update';
62                 this.editDialog.recordId = matchSet.id();
63                 this.editDialog.open({size: 'lg'})
64                     .subscribe(() => this.grid.reload());
65             }
66         );
67     }
68
69     orgOnChange(org: IdlObject) {
70         this.contextOrg = org;
71         this.grid.reload();
72     }
73 }
74