1 /** TODO PORT ME TO <eg-combobox> */
2 import {Component, OnInit, Input, Output, ViewChild, EventEmitter} from '@angular/core';
3 import {Observable, Subject} from 'rxjs';
4 import {map, mapTo, debounceTime, distinctUntilChanged, merge, filter} from 'rxjs/operators';
5 import {AuthService} from '@eg/core/auth.service';
6 import {StoreService} from '@eg/core/store.service';
7 import {OrgService} from '@eg/core/org.service';
8 import {IdlObject} from '@eg/core/idl.service';
9 import {PermService} from '@eg/core/perm.service';
10 import {NgbTypeahead, NgbTypeaheadSelectItemEvent} from '@ng-bootstrap/ng-bootstrap';
12 // Use a unicode char for spacing instead of ASCII=32 so the browser
13 // won't collapse the nested display entries down to a single space.
14 const PAD_SPACE = ' '; // U+2007
16 interface OrgDisplay {
23 selector: 'eg-org-select',
24 templateUrl: './org-select.component.html'
26 export class OrgSelectComponent implements OnInit {
29 hidden: number[] = [];
30 click$ = new Subject<string>();
33 // Disable the entire input
34 @Input() disabled: boolean;
36 @ViewChild('instance') instance: NgbTypeahead;
38 // Placeholder text for selector input
39 @Input() placeholder = '';
40 @Input() stickySetting: string;
42 // ID to display in the DOM for this selector
45 // Org unit field displayed in the selector
46 @Input() displayField = 'shortname';
48 // Apply a default org unit value when none is set.
49 // First tries workstation org unit, then user home org unit.
50 // An onChange event WILL be generated when a default is applied.
51 @Input() applyDefault = false;
53 @Input() readOnly = false;
55 // List of org unit IDs to exclude from the selector
56 @Input() set hideOrgs(ids: number[]) {
57 if (ids) { this.hidden = ids; }
60 // List of org unit IDs to disable in the selector
61 _disabledOrgs: number[] = [];
62 @Input() set disableOrgs(ids: number[]) {
63 if (ids) { this._disabledOrgs = ids; }
66 // Apply an org unit value at load time.
67 // This will NOT result in an onChange event.
68 @Input() set initialOrg(org: IdlObject) {
69 if (org) { this.startOrg = org; }
72 // Apply an org unit value by ID at load time.
73 // This will NOT result in an onChange event.
74 @Input() set initialOrgId(id: number) {
75 if (id) { this.startOrg = this.org.get(id); }
78 // Modify the selected org unit via data binding.
79 // This WILL result in an onChange event firing.
80 @Input() set applyOrg(org: IdlObject) {
82 this.selected = this.formatForDisplay(org);
86 permLimitOrgs: number[];
87 @Input() set limitPerms(perms: string[]) {
88 this.applyPermLimitOrgs(perms);
91 // Modify the selected org unit by ID via data binding.
92 // This WILL result in an onChange event firing.
93 @Input() set applyOrgId(id: number) {
95 this.selected = this.formatForDisplay(this.org.get(id));
99 // Emitted when the org unit value is changed via the selector.
100 // Does not fire on initialOrg
101 @Output() onChange = new EventEmitter<IdlObject>();
103 // convenience method to get an IdlObject representing the current
104 // selected org unit. One way of invoking this is via a template
105 // reference variable.
106 selectedOrg(): IdlObject {
107 if (this.selected == null) {
110 return this.org.get(this.selected.id);
114 private auth: AuthService,
115 private store: StoreService,
116 private org: OrgService,
117 private perm: PermService
122 // Apply a default org unit if desired and possible.
123 if (!this.startOrg && this.applyDefault && this.auth.user()) {
124 // note: ws_ou defaults to home_ou on the server
125 // when when no workstation is used
126 this.startOrg = this.org.get(this.auth.user().ws_ou());
127 this.selected = this.formatForDisplay(
128 this.org.get(this.auth.user().ws_ou())
131 // avoid notifying mid-digest
132 setTimeout(() => this.onChange.emit(this.startOrg), 0);
136 this.selected = this.formatForDisplay(this.startOrg);
141 applyPermLimitOrgs(perms: string[]) {
147 // handle lazy clients that pass null perm names
148 perms = perms.filter(p => p !== null && p !== undefined);
150 if (perms.length === 0) {
154 // NOTE: If permLimitOrgs is useful in a non-staff context
155 // we need to change this to support non-staff perm checks.
156 this.perm.hasWorkPermAt(perms, true).then(permMap => {
158 // safari-friendly version of Array.flat()
159 Object.values(permMap).reduce((acc, val) => acc.concat(val), []);
163 // Format for display in the selector drop-down and input.
164 formatForDisplay(org: IdlObject): OrgDisplay {
165 let label = org[this.displayField]();
166 if (!this.readOnly) {
167 label = PAD_SPACE.repeat(org.ou_type().depth()) + label;
176 // Fired by the typeahead to inform us of a change.
177 // TODO: this does not fire when the value is cleared :( -- implement
178 // change detection on this.selected to look specifically for NULL.
179 orgChanged(selEvent: NgbTypeaheadSelectItemEvent) {
180 // console.debug('org unit change occurred ' + selEvent.item);
181 this.onChange.emit(this.org.get(selEvent.item.id));
184 // Remove the tree-padding spaces when matching.
185 formatter = (result: OrgDisplay) => result.label.trim();
187 // reset the state of the component
189 this.selected = null;
192 filter = (text$: Observable<string>): Observable<OrgDisplay[]> => {
195 distinctUntilChanged(),
197 // Inject a specifier indicating the source of the
198 // action is a user click
199 this.click$.pipe(filter(() => !this.instance.isPopupOpen()))
200 .pipe(mapTo('_CLICK_'))
204 let orgs = this.org.list().filter(org =>
205 this.hidden.filter(id => org.id() === id).length === 0
208 if (this.permLimitOrgs) {
209 // Avoid showing org units where the user does
210 // not have the requested permission.
211 orgs = orgs.filter(org =>
212 this.permLimitOrgs.includes(org.id()));
215 if (term !== '_CLICK_') {
216 // For search-driven events, limit to the matching
218 orgs = orgs.filter(org => {
219 return term === '' || // show all
220 org[this.displayField]()
221 .toLowerCase().indexOf(term.toLowerCase()) > -1;
226 return orgs.map(org => this.formatForDisplay(org));