]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/item-location-select/item-location-select.component.ts
082aa80ccfc8c7979652e302780e6b4e99995da4
[Evergreen.git] / Open-ILS / src / eg2 / src / app / share / item-location-select / item-location-select.component.ts
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';
11
12 /**
13  * Item (Copy) Location Selector.
14  *
15  * <eg-item-location-select [(ngModel)]="myAcplId"
16     [contextOrgId]="anOrgId" permFilter="ADMIN_STUFF">
17  * </eg-item-location-select>
18  */
19
20 @Component({
21   selector: 'eg-item-location-select',
22   templateUrl: './item-location-select.component.html',
23   providers: [{
24       provide: NG_VALUE_ACCESSOR,
25       useExisting: forwardRef(() => ItemLocationSelectComponent),
26       multi: true
27   }]
28 })
29 export class ItemLocationSelectComponent implements OnInit, ControlValueAccessor {
30
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;
34
35     // Limit copy locations to those owned at or above this org unit.
36     @Input() contextOrgId: number;
37
38     @Input() orgUnitLabelField = 'shortname';
39
40     // Emits an acpl object or null on combobox value change
41     @Output() valueChange: EventEmitter<IdlObject>;
42
43     @ViewChild('comboBox', {static: false}) comboBox: ComboboxComponent;
44
45     startId: number = null;
46     filterOrgs: number[];
47     cache: {[id: number]: IdlObject} = {};
48
49     propagateChange = (id: number) => {};
50     propagateTouch = () => {};
51
52     constructor(
53         private org: OrgService,
54         private auth: AuthService,
55         private perm: PermService,
56         private pcrud: PcrudService
57     ) {
58         this.valueChange = new EventEmitter<IdlObject>();
59     }
60
61     ngOnInit() {
62         this.setFilterOrgs().then(_ => this.getLocations());
63     }
64
65     getLocations(): Promise<any> {
66         const entries: ComboboxEntry[] = [];
67         const search = {owning_lib: this.filterOrgs, deleted: 'f'};
68
69         return this.pcrud.search('acpl', search, {order_by: {acpl: 'name'}}
70         ).pipe(map(loc => {
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;
75         });
76     }
77
78     registerOnChange(fn) {
79         this.propagateChange = fn;
80     }
81
82     registerOnTouched(fn) {
83         this.propagateTouch = fn;
84     }
85
86     cboxChanged(entry: ComboboxEntry) {
87         const id = entry ? entry.id : null;
88         this.propagateChange(id);
89         this.valueChange.emit(id ? this.cache[id] : null);
90     }
91
92     writeValue(id: number) {
93         if (this.comboBox) { // May not yet be initialized
94             this.comboBox.selectedId = id;
95         } else if (id) {
96             this.startId = id;
97         }
98     }
99
100     setFilterOrgs(): Promise<number[]> {
101         if (this.permFilter) {
102             return this.perm.hasWorkPermAt([this.permFilter], true)
103                 .then(values => this.filterOrgs = values[this.permFilter]);
104         }
105
106         const org = this.contextOrgId || this.auth.user().ws_ou();
107         this.filterOrgs = this.org.ancestors(this.contextOrgId, true);
108
109         return Promise.resolve(this.filterOrgs);
110     }
111
112     orgName(orgId: number): string {
113         return this.org.get(orgId)[this.orgUnitLabelField]();
114     }
115 }
116
117
118