LP#1863387: multi-select now allows filtering shelving locations by owner
[evergreen-equinox.git] / Open-ILS / src / eg2 / src / app / share / multi-select / multi-select.component.ts
1 /**
2  * <eg-multi-select idlClass="acpl" linkedLibraryLabel="owning_lib" idlKey="id">
3  * </eg-multi-select>
4  */
5 import {Component, OnInit, Input, Output, ViewChild, EventEmitter, ElementRef} from '@angular/core';
6 import {map} from 'rxjs/operators';
7 import {Observable, of, Subject} from 'rxjs';
8 import {StoreService} from '@eg/core/store.service';
9 import {PcrudService} from '@eg/core/pcrud.service';
10 import {OrgService} from '@eg/core/org.service';
11 import {ComboboxComponent, ComboboxEntry} from '@eg/share/combobox/combobox.component';
12 import {ItemLocationSelectComponent} from '@eg/share/item-location-select/item-location-select.component';
13
14 @Component({
15   selector: 'eg-multi-select',
16   templateUrl: './multi-select.component.html',
17   styles: [`
18     .icons {margin-left:-18px}
19     .material-icons {font-size: 16px;font-weight:bold}
20   `]
21 })
22 export class MultiSelectComponent implements OnInit {
23
24     selected: ComboboxEntry;
25     entrylist: ComboboxEntry[];
26
27     @Input() idlClass: string;
28     @Input() idlBaseQuery: any = null;
29     @Input() idlKey: string;
30     @Input() linkedLibraryLabel: string;
31     @Input() startValue: string;
32
33     @Output() onChange: EventEmitter<string>;
34
35     acplContextOrgId: number;
36     acplIncludeDescendants: boolean;
37
38     constructor(
39       private store: StoreService,
40       private pcrud: PcrudService,
41       private org: OrgService,
42     ) {
43         this.entrylist = [];
44         this.onChange = new EventEmitter<string>();
45     }
46
47     valueSelected(entry: ComboboxEntry) {
48         if (entry) {
49             this.selected = entry;
50         } else {
51             this.selected = null;
52         }
53     }
54
55     getOrgShortname(ou: any) {
56         if (typeof ou === 'object') {
57             return ou.shortname();
58         } else {
59             return this.org.get(ou).shortname();
60         }
61     }
62
63     addSelectedValue() {
64         // special case to format the label
65         if (this.idlClass === 'acpl' && this.selected.userdata) {
66             this.selected.label =
67                 this.selected.userdata.name() + ' (' +
68                 this.getOrgShortname(this.selected.userdata.owning_lib()) + ')';
69         }
70         this.entrylist.push(this.selected);
71         this.onChange.emit(this.compileCurrentValue());
72     }
73     removeValue(entry: ComboboxEntry) {
74         this.entrylist = this.entrylist.filter(ent => ent.id !== entry.id);
75         this.onChange.emit(this.compileCurrentValue());
76     }
77
78     compileCurrentValue(): string {
79         const valstr = this.entrylist.map(entry => entry.id).join(',');
80         return '{' + valstr + '}';
81     }
82
83     ngOnInit() {
84         if (!this.idlKey) {
85             this.idlKey = 'id';
86         }
87
88         if (this.startValue && this.startValue !== '{}') {
89             let valstr = this.startValue;
90             valstr = valstr.replace(/^{/, '');
91             valstr = valstr.replace(/}$/, '');
92             const ids = valstr.split(',');
93             const searchHash = {};
94             searchHash[this.idlKey] = ids;
95             const extra_args = {};
96             if (this.linkedLibraryLabel) {
97                 const flesh_fields: Object = {};
98                 flesh_fields[this.idlClass] = [ this.linkedLibraryLabel ];
99                 extra_args['flesh'] = 1;
100                 extra_args['flesh_fields'] = flesh_fields;
101                 this.pcrud.search(this.idlClass, searchHash, extra_args).pipe(map(data => {
102                     this.entrylist.push({
103                         'id' : data.id(),
104                         'label' : data.name() + ' (' + data[this.linkedLibraryLabel]().shortname() + ')'
105                     });
106                 })).toPromise();
107             } else {
108                 this.pcrud.search(this.idlClass, searchHash, extra_args).pipe(map(data => {
109                     this.entrylist.push({ 'id' : data.id(), 'label' : data.name() });
110                 })).toPromise();
111             }
112         }
113     }
114
115 }
116
117