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 @Input() contextOrgId: number;
42 @Input() orgUnitLabelField = 'shortname';
44 // Emits an acpl object or null on combobox value change
45 @Output() valueChange: EventEmitter<IdlObject>;
47 @Input() required: boolean;
49 @Input() domId = 'eg-item-location-select-' +
50 ItemLocationSelectComponent.domIdAuto++;
52 @ViewChild('comboBox', {static: false}) comboBox: ComboboxComponent;
53 @ViewChild('unsetString', {static: false}) unsetString: StringComponent;
55 startId: number = null;
56 filterOrgs: number[] = [];
57 cache: {[id: number]: IdlObject} = {};
59 initDone = false; // true after first data load
60 propagateChange = (id: number) => {};
61 propagateTouch = () => {};
64 private org: OrgService,
65 private auth: AuthService,
66 private perm: PermService,
67 private pcrud: PcrudService
69 this.valueChange = new EventEmitter<IdlObject>();
74 .then(_ => this.getLocations())
75 .then(_ => this.initDone = true);
80 // Format the display of locations to include the org unit
81 this.comboBox.formatDisplayString = (result: ComboboxEntry) => {
82 let display = result.label || result.id;
83 display = (display + '').trim();
84 if (result.userdata) {
86 this.orgName(result.userdata.owning_lib()) + ')';
92 getLocations(): Promise<any> {
94 if (this.filterOrgs.length === 0) {
95 this.comboBox.entries = [];
96 return Promise.resolve();
99 const search: any = {deleted: 'f'};
102 // Guarantee we have the load-time copy location, which
103 // may not be included in the org-scoped set of locations
104 // we fetch by default.
107 {owning_lib: this.filterOrgs}
110 search.owning_lib = this.filterOrgs;
113 const entries: ComboboxEntry[] = [];
115 if (!this.required) {
116 entries.push({id: null, label: this.unsetString.text});
119 return this.pcrud.search('acpl', search, {order_by: {acpl: 'name'}}
121 this.cache[loc.id()] = loc;
122 entries.push({id: loc.id(), label: loc.name(), userdata: loc});
123 })).toPromise().then(_ => {
124 this.comboBox.entries = entries;
128 registerOnChange(fn) {
129 this.propagateChange = fn;
132 registerOnTouched(fn) {
133 this.propagateTouch = fn;
136 cboxChanged(entry: ComboboxEntry) {
137 const id = entry ? entry.id : null;
138 this.propagateChange(id);
139 this.valueChange.emit(id ? this.cache[id] : null);
142 writeValue(id: number) {
144 this.getOneLocation(id).then(_ => this.comboBox.selectedId = id);
150 getOneLocation(id: number) {
151 if (!id || this.cache[id]) { return Promise.resolve(); }
153 return this.pcrud.retrieve('acpl', id).toPromise()
155 this.cache[loc.id()] = loc;
156 this.comboBox.addAsyncEntry(
157 {id: loc.id(), label: loc.name(), userdata: loc});
161 setFilterOrgs(): Promise<number[]> {
162 const org = this.contextOrgId || this.auth.user().ws_ou();
163 const contextOrgIds = this.org.ancestors(org, true);
165 if (!this.permFilter) {
166 return Promise.resolve(this.filterOrgs = contextOrgIds);
169 return this.perm.hasWorkPermAt([this.permFilter], true)
171 // Always limit the org units to /at most/ those within
172 // scope of the context org ID.
174 const permOrgIds = values[this.permFilter];
175 const trimmedOrgIds = [];
176 permOrgIds.forEach(orgId => {
177 if (contextOrgIds.includes(orgId)) {
178 trimmedOrgIds.push(orgId);
182 return this.filterOrgs = trimmedOrgIds;
186 orgName(orgId: number): string {
187 return this.org.get(orgId)[this.orgUnitLabelField]();