b2409c196c536057fecbfda8922cae704dff7f51
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / cat / vandelay / match-set-quality.component.ts
1 import {Component, OnInit, ViewChild, AfterViewInit, Input} from '@angular/core';
2 import {Observable} from 'rxjs/Observable';
3 import 'rxjs/add/observable/of';
4 import {IdlObject, IdlService} from '@eg/core/idl.service';
5 import {PcrudService} from '@eg/core/pcrud.service';
6 import {NetService} from '@eg/core/net.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {OrgService} from '@eg/core/org.service';
9 import {GridComponent} from '@eg/share/grid/grid.component';
10 import {GridDataSource} from '@eg/share/grid/grid';
11 import {Pager} from '@eg/share/util/pager';
12 import {MatchSetNewPointComponent} from './match-set-new-point.component';
13
14 @Component({
15   selector: 'eg-match-set-quality',
16   templateUrl: 'match-set-quality.component.html'
17 })
18 export class MatchSetQualityComponent implements OnInit {
19
20     // Match set arrives from parent async.
21     matchSet_: IdlObject;
22     @Input() set matchSet(ms: IdlObject) {
23         this.matchSet_ = ms;
24         if (ms) { 
25             this.matchSetType = ms.mtype(); 
26             if (this.grid) {
27                 this.grid.reload();
28             }
29         }
30     }
31
32     newPointType: string;
33     matchSetType: string;
34     dataSource: GridDataSource;
35     @ViewChild('newPoint') newPoint: MatchSetNewPointComponent;
36     @ViewChild('grid') grid: GridComponent;
37     deleteSelected: (rows: IdlObject[]) => void;
38
39     constructor(
40         private idl: IdlService,
41         private pcrud: PcrudService,
42         private net: NetService,
43         private auth: AuthService,
44         private org: OrgService
45     ) {
46
47         this.dataSource = new GridDataSource();
48         this.dataSource.getRows = (pager: Pager, sort: any[]) => {
49
50             if (!this.matchSet_) {
51                 return Observable.of();
52             }
53
54             const orderBy: any = {};
55             if (sort.length) {
56                 orderBy.vmsq = sort[0].name + ' ' + sort[0].dir;
57             }
58
59             const searchOps = {
60                 offset: pager.offset,
61                 limit: pager.limit,
62                 order_by: orderBy
63             };
64
65             const search = {match_set: this.matchSet_.id()};
66             return this.pcrud.search('vmsq', search, searchOps);
67         }
68
69         this.deleteSelected = (rows: any[]) => {
70             this.pcrud.remove(rows).subscribe(
71                 ok  => console.log('deleted ', ok),
72                 err => console.error(err),
73                 ()  => this.grid.reload()
74             );
75         };
76     }
77
78     ngOnInit() {}
79
80     addQuality() {
81         const quality = this.idl.create('vmsq');  
82         const values = this.newPoint.values;
83
84         quality.match_set(this.matchSet_.id());
85         quality.quality(values.matchScore);
86         quality.value(values.value);
87
88         if (values.recordAttr) {
89             quality.svf(values.recordAttr);
90         } else {
91             quality.tag(values.marcTag);
92             quality.subfield(values.marcSf);
93         }
94
95         this.pcrud.create(quality).subscribe(
96             ok  => console.debug('created ', ok),
97             err => console.error(err),
98             ()  => {
99                 this.newPointType = null;
100                 this.grid.reload();
101             }
102         );
103     }
104 }
105