1 import {Component, OnInit, Input, Output, ViewChild, EventEmitter, forwardRef} from '@angular/core';
2 import {ControlValueAccessor, FormGroup, FormControl, NG_VALUE_ACCESSOR} from '@angular/forms';
3 import {Observable} from 'rxjs';
4 import {map} from 'rxjs/operators';
5 import {IdlObject} from '@eg/core/idl.service';
6 import {OrgService} from '@eg/core/org.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {PermService} from '@eg/core/perm.service';
9 import {PcrudService} from '@eg/core/pcrud.service';
10 import {ComboboxComponent, ComboboxEntry} from '@eg/share/combobox/combobox.component';
13 * Item (Copy) Location Selector.
15 * <eg-item-location-select [(ngModel)]="myAcplId"
16 [contextOrgId]="anOrgId" permFilter="ADMIN_STUFF">
17 * </eg-item-location-select>
21 selector: 'eg-item-location-select',
22 templateUrl: './item-location-select.component.html',
24 provide: NG_VALUE_ACCESSOR,
25 useExisting: forwardRef(() => ItemLocationSelectComponent),
29 export class ItemLocationSelectComponent implements OnInit, ControlValueAccessor {
31 // Limit copy locations to those owned at or above org units where
32 // the user has work permissions for the provided permission code.
33 @Input() permFilter: string;
35 // Limit copy locations to those owned at or above this org unit.
36 @Input() contextOrgId: number;
38 @Input() orgUnitLabelField = 'shortname';
40 // Emits an acpl object or null on combobox value change
41 @Output() valueChange: EventEmitter<IdlObject>;
43 @ViewChild('comboBox', {static: false}) comboBox: ComboboxComponent;
45 startId: number = null;
47 cache: {[id: number]: IdlObject} = {};
49 propagateChange = (id: number) => {};
50 propagateTouch = () => {};
53 private org: OrgService,
54 private auth: AuthService,
55 private perm: PermService,
56 private pcrud: PcrudService
58 this.valueChange = new EventEmitter<IdlObject>();
62 this.setFilterOrgs().then(_ => this.getLocations());
65 getLocations(): Promise<any> {
66 const entries: ComboboxEntry[] = [];
67 const search = {owning_lib: this.filterOrgs, deleted: 'f'};
69 return this.pcrud.search('acpl', search, {order_by: {acpl: 'name'}}
71 this.cache[loc.id()] = loc;
72 entries.push({id: loc.id(), label: loc.name(), userdata: loc});
73 })).toPromise().then(_ => {
74 this.comboBox.entries = entries;
78 registerOnChange(fn) {
79 this.propagateChange = fn;
82 registerOnTouched(fn) {
83 this.propagateTouch = fn;
86 cboxChanged(entry: ComboboxEntry) {
87 const id = entry ? entry.id : null;
88 this.propagateChange(id);
89 this.valueChange.emit(id ? this.cache[id] : null);
92 writeValue(id: number) {
93 if (this.comboBox) { // May not yet be initialized
94 this.comboBox.selectedId = id;
100 setFilterOrgs(): Promise<number[]> {
101 if (this.permFilter) {
102 return this.perm.hasWorkPermAt([this.permFilter], true)
103 .then(values => this.filterOrgs = values[this.permFilter]);
106 const org = this.contextOrgId || this.auth.user().ws_ou();
107 this.filterOrgs = this.org.ancestors(this.contextOrgId, true);
109 return Promise.resolve(this.filterOrgs);
112 orgName(orgId: number): string {
113 return this.org.get(orgId)[this.orgUnitLabelField]();