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 {
35 // Limit copy locations to those owned at or above org units where
36 // the user has work permissions for the provided permission code.
37 @Input() permFilter: string;
39 // Limit copy locations to those owned at or above this org unit.
40 private _contextOrgId: number;
41 @Input() set contextOrgId(value: number) {
42 this._contextOrgId = value;
46 @Input() orgUnitLabelField = 'shortname';
48 // Emits an acpl object or null on combobox value change
49 @Output() valueChange: EventEmitter<IdlObject>;
51 @Input() required: boolean;
53 @Input() domId = 'eg-item-location-select-' +
54 ItemLocationSelectComponent.domIdAuto++;
56 @ViewChild('comboBox', {static: false}) comboBox: ComboboxComponent;
57 @ViewChild('unsetString', {static: false}) unsetString: StringComponent;
59 startId: number = null;
60 filterOrgs: number[] = [];
61 cache: {[id: number]: IdlObject} = {};
63 initDone = false; // true after first data load
64 propagateChange = (id: number) => {};
65 propagateTouch = () => {};
68 private org: OrgService,
69 private auth: AuthService,
70 private perm: PermService,
71 private pcrud: PcrudService
73 this.valueChange = new EventEmitter<IdlObject>();
78 .then(_ => this.getLocations())
79 .then(_ => this.initDone = true);
84 // Format the display of locations to include the org unit
85 this.comboBox.formatDisplayString = (result: ComboboxEntry) => {
86 let display = result.label || result.id;
87 display = (display + '').trim();
88 if (result.userdata) {
90 this.orgName(result.userdata.owning_lib()) + ')';
96 getLocations(): Promise<any> {
98 if (this.filterOrgs.length === 0) {
99 this.comboBox.entries = [];
100 return Promise.resolve();
103 const search: any = {deleted: 'f'};
106 // Guarantee we have the load-time copy location, which
107 // may not be included in the org-scoped set of locations
108 // we fetch by default.
111 {owning_lib: this.filterOrgs}
114 search.owning_lib = this.filterOrgs;
117 const entries: ComboboxEntry[] = [];
119 if (!this.required) {
120 entries.push({id: null, label: this.unsetString.text});
123 return this.pcrud.search('acpl', search, {order_by: {acpl: 'name'}}
125 this.cache[loc.id()] = loc;
126 entries.push({id: loc.id(), label: loc.name(), userdata: loc});
127 })).toPromise().then(_ => {
128 this.comboBox.entries = entries;
132 registerOnChange(fn) {
133 this.propagateChange = fn;
136 registerOnTouched(fn) {
137 this.propagateTouch = fn;
140 cboxChanged(entry: ComboboxEntry) {
141 const id = entry ? entry.id : null;
142 this.propagateChange(id);
143 this.valueChange.emit(id ? this.cache[id] : null);
146 writeValue(id: number) {
148 this.getOneLocation(id).then(_ => this.comboBox.selectedId = id);
154 getOneLocation(id: number) {
155 if (!id || this.cache[id]) { return Promise.resolve(); }
157 return this.pcrud.retrieve('acpl', id).toPromise()
159 this.cache[loc.id()] = loc;
160 this.comboBox.addAsyncEntry(
161 {id: loc.id(), label: loc.name(), userdata: loc});
165 setFilterOrgs(): Promise<number[]> {
166 const org = this._contextOrgId || this.auth.user().ws_ou();
167 const contextOrgIds = this.org.ancestors(org, true);
169 if (!this.permFilter) {
170 return Promise.resolve(this.filterOrgs = contextOrgIds);
173 return this.perm.hasWorkPermAt([this.permFilter], true)
175 // Always limit the org units to /at most/ those within
176 // scope of the context org ID.
178 const permOrgIds = values[this.permFilter];
179 const trimmedOrgIds = [];
180 permOrgIds.forEach(orgId => {
181 if (contextOrgIds.includes(orgId)) {
182 trimmedOrgIds.push(orgId);
186 return this.filterOrgs = trimmedOrgIds;
190 orgName(orgId: number): string {
191 return this.org.get(orgId)[this.orgUnitLabelField]();