1 import {Component, OnInit, AfterViewInit, Input, Output, ViewChild,
2 EventEmitter, forwardRef} from '@angular/core';
3 import {ControlValueAccessor, FormGroup, FormControl, NG_VALUE_ACCESSOR} from '@angular/forms';
4 import {Observable} from 'rxjs';
5 import {map} from 'rxjs/operators';
6 import {IdlObject} from '@eg/core/idl.service';
7 import {OrgService} from '@eg/core/org.service';
8 import {AuthService} from '@eg/core/auth.service';
9 import {PermService} from '@eg/core/perm.service';
10 import {PcrudService} from '@eg/core/pcrud.service';
11 import {ComboboxComponent, ComboboxEntry} from '@eg/share/combobox/combobox.component';
12 import {StringComponent} from '@eg/share/string/string.component';
15 * Item (Copy) Location Selector.
17 * <eg-item-location-select [(ngModel)]="myAcplId"
18 [contextOrgId]="anOrgId" permFilter="ADMIN_STUFF">
19 * </eg-item-location-select>
23 selector: 'eg-item-location-select',
24 templateUrl: './item-location-select.component.html',
26 provide: NG_VALUE_ACCESSOR,
27 useExisting: forwardRef(() => ItemLocationSelectComponent),
31 export class ItemLocationSelectComponent
32 implements OnInit, AfterViewInit, ControlValueAccessor {
34 // Limit copy locations to those owned at or above org units where
35 // the user has work permissions for the provided permission code.
36 @Input() permFilter: string;
38 // Limit copy locations to those owned at or above this org unit.
39 @Input() contextOrgId: number;
41 @Input() orgUnitLabelField = 'shortname';
43 // Emits an acpl object or null on combobox value change
44 @Output() valueChange: EventEmitter<IdlObject>;
46 @Input() required: boolean;
48 @ViewChild('comboBox', {static: false}) comboBox: ComboboxComponent;
49 @ViewChild('unsetString', {static: false}) unsetString: StringComponent;
51 startId: number = null;
52 filterOrgs: number[] = [];
53 cache: {[id: number]: IdlObject} = {};
55 initDone = false; // true after first data load
56 propagateChange = (id: number) => {};
57 propagateTouch = () => {};
60 private org: OrgService,
61 private auth: AuthService,
62 private perm: PermService,
63 private pcrud: PcrudService
65 this.valueChange = new EventEmitter<IdlObject>();
70 .then(_ => this.getLocations())
71 .then(_ => this.initDone = true);
76 // Format the display of locations to include the org unit
77 this.comboBox.formatDisplayString = (result: ComboboxEntry) => {
78 let display = result.label || result.id;
79 display = (display + '').trim();
80 if (result.userdata) {
82 this.orgName(result.userdata.owning_lib()) + ')';
88 getLocations(): Promise<any> {
90 if (this.filterOrgs.length === 0) {
91 this.comboBox.entries = [];
92 return Promise.resolve();
95 const search: any = {deleted: 'f'};
98 // Guarantee we have the load-time copy location, which
99 // may not be included in the org-scoped set of locations
100 // we fetch by default.
103 {owning_lib: this.filterOrgs}
106 search.owning_lib = this.filterOrgs;
109 const entries: ComboboxEntry[] = [];
111 if (!this.required) {
112 entries.push({id: null, label: this.unsetString.text});
115 return this.pcrud.search('acpl', search, {order_by: {acpl: 'name'}}
117 this.cache[loc.id()] = loc;
118 entries.push({id: loc.id(), label: loc.name(), userdata: loc});
119 })).toPromise().then(_ => {
120 this.comboBox.entries = entries;
124 registerOnChange(fn) {
125 this.propagateChange = fn;
128 registerOnTouched(fn) {
129 this.propagateTouch = fn;
132 cboxChanged(entry: ComboboxEntry) {
133 const id = entry ? entry.id : null;
134 this.propagateChange(id);
135 this.valueChange.emit(id ? this.cache[id] : null);
138 writeValue(id: number) {
140 this.getOneLocation(id).then(_ => this.comboBox.selectedId = id);
146 getOneLocation(id: number) {
147 if (!id || this.cache[id]) { return Promise.resolve(); }
149 return this.pcrud.retrieve('acpl', id).toPromise()
151 this.cache[loc.id()] = loc;
152 this.comboBox.entries.push(
153 {id: loc.id(), label: loc.name(), userdata: loc});
157 setFilterOrgs(): Promise<number[]> {
158 if (this.permFilter) {
159 return this.perm.hasWorkPermAt([this.permFilter], true)
160 .then(values => this.filterOrgs = values[this.permFilter]);
163 const org = this.contextOrgId || this.auth.user().ws_ou();
164 this.filterOrgs = this.org.ancestors(this.contextOrgId, true);
166 return Promise.resolve(this.filterOrgs);
169 orgName(orgId: number): string {
170 return this.org.get(orgId)[this.orgUnitLabelField]();