2 * <eg-multi-select idlClass="acpl" linkedLibraryLabel="owning_lib" idlKey="id">
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';
15 selector: 'eg-multi-select',
16 templateUrl: './multi-select.component.html',
18 .icons {margin-left:-18px}
19 .material-icons {font-size: 16px;font-weight:bold}
22 export class MultiSelectComponent implements OnInit {
24 selected: ComboboxEntry;
25 entrylist: ComboboxEntry[];
27 @Input() idlClass: string;
28 @Input() idlBaseQuery: any = null;
29 @Input() idlKey: string;
30 @Input() linkedLibraryLabel: string;
31 @Input() startValue: string;
33 @Output() onChange: EventEmitter<string>;
35 acplContextOrgId: number;
36 acplIncludeDescendants: boolean;
39 private store: StoreService,
40 private pcrud: PcrudService,
41 private org: OrgService,
44 this.onChange = new EventEmitter<string>();
47 valueSelected(entry: ComboboxEntry) {
49 this.selected = entry;
55 getOrgShortname(ou: any) {
56 if (typeof ou === 'object') {
57 return ou.shortname();
59 return this.org.get(ou).shortname();
64 // special case to format the label
65 if (this.idlClass === 'acpl' && this.selected.userdata) {
67 this.selected.userdata.name() + ' (' +
68 this.getOrgShortname(this.selected.userdata.owning_lib()) + ')';
70 this.entrylist.push(this.selected);
71 this.onChange.emit(this.compileCurrentValue());
73 removeValue(entry: ComboboxEntry) {
74 this.entrylist = this.entrylist.filter(ent => ent.id !== entry.id);
75 this.onChange.emit(this.compileCurrentValue());
78 compileCurrentValue(): string {
79 const valstr = this.entrylist.map(entry => entry.id).join(',');
80 return '{' + valstr + '}';
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({
104 'label' : data.name() + ' (' + data[this.linkedLibraryLabel]().shortname() + ')'
108 this.pcrud.search(this.idlClass, searchHash, extra_args).pipe(map(data => {
109 this.entrylist.push({ 'id' : data.id(), 'label' : data.name() });