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 {ServerStoreService} from '@eg/core/server-store.service';
8 import {OrgService} from '@eg/core/org.service';
9 import {IdlObject} from '@eg/core/idl.service';
10 import {PermService} from '@eg/core/perm.service';
11 import {NgbTypeahead, NgbTypeaheadSelectItemEvent} from '@ng-bootstrap/ng-bootstrap';
15 * The following precedence is used when applying a load-time value
17 * 1. initialOrg / initialOrgId
18 * 2. Value from server setting specificed with persistKey (fires onload).
19 * 3. Value from fallbackOrg / fallbackOrgId (fires onload).
20 * 4. Default applyed when applyDefault is set (fires onload).
22 * Users can detect when the component has completed its load-time
23 * machinations by subscribing to the componentLoaded Output which
24 * fires exactly once when loading is completed.
27 // Use a unicode char for spacing instead of ASCII=32 so the browser
28 // won't collapse the nested display entries down to a single space.
29 const PAD_SPACE = ' '; // U+2007
31 interface OrgDisplay {
38 selector: 'eg-org-select',
39 templateUrl: './org-select.component.html'
41 export class OrgSelectComponent implements OnInit {
44 click$ = new Subject<string>();
45 valueFromSetting: number = null;
46 sortedOrgs: IdlObject[] = [];
48 // Disable the entire input
49 @Input() disabled: boolean;
51 @ViewChild('instance', { static: false }) instance: NgbTypeahead;
53 // Placeholder text for selector input
54 @Input() placeholder = '';
56 // ID to display in the DOM for this selector
59 // Org unit field displayed in the selector
60 @Input() displayField = 'shortname';
62 // if no initialOrg is provided, none could be found via persist
63 // setting, and no fallbackoOrg is provided, apply a sane default.
64 // First tries workstation org unit, then user home org unit.
65 // An onChange event WILL be generated when a default is applied.
66 @Input() applyDefault = false;
68 @Input() readOnly = false;
70 // List of org unit IDs to exclude from the selector
71 hidden: number[] = [];
72 @Input() set hideOrgs(ids: number[]) {
73 if (ids) { this.hidden = ids; }
76 // List of org unit IDs to disable in the selector
77 _disabledOrgs: number[] = [];
78 @Input() set disableOrgs(ids: number[]) {
79 if (ids) { this._disabledOrgs = ids; }
82 // Apply an org unit value at load time.
83 // These will NOT result in an onChange event.
84 @Input() initialOrg: IdlObject;
85 @Input() initialOrgId: number;
87 // Value is persisted via server setting with this key.
88 // Key is prepended with 'eg.orgselect.'
89 @Input() persistKey: string;
91 // If no initialOrg is provided and no value could be found
92 // from a persist setting, fall back to one of these values.
93 // These WILL result in an onChange event
94 @Input() fallbackOrg: IdlObject;
95 @Input() fallbackOrgId: number;
97 // Modify the selected org unit via data binding.
98 // This WILL NOT result in an onChange event firing.
99 @Input() set applyOrg(org: IdlObject) {
100 this.selected = org ? this.formatForDisplay(org) : null;
103 // Modify the selected org unit by ID via data binding.
104 // This WILL NOT result in an onChange event firing.
105 @Input() set applyOrgId(id: number) {
106 this.selected = id ? this.formatForDisplay(this.org.get(id)) : null;
109 // Limit org unit display to those where the logged in user
110 // has the following permissions.
111 permLimitOrgs: number[];
112 @Input() set limitPerms(perms: string[]) {
113 this.applyPermLimitOrgs(perms);
116 // Emitted when the org unit value is changed via the selector.
117 // Does not fire on initialOrg
118 @Output() onChange = new EventEmitter<IdlObject>();
120 // Emitted once when the component is done fetching settings
121 // and applying its initial value. For apps that use the value
122 // of this selector to load data, this event can be used to reliably
123 // detect when the selector is done with all of its automated
124 // underground shuffling and landed on a value.
125 @Output() componentLoaded: EventEmitter<void> = new EventEmitter<void>();
127 // convenience method to get an IdlObject representing the current
128 // selected org unit. One way of invoking this is via a template
129 // reference variable.
130 selectedOrg(): IdlObject {
131 if (this.selected == null) {
134 return this.org.get(this.selected.id);
138 private auth: AuthService,
139 private store: StoreService,
140 private serverStore: ServerStoreService,
141 private org: OrgService,
142 private perm: PermService
147 // Sort the tree and reabsorb to propagate the sorted nodes to
148 // the org.list() used by this component. Maintain our own
149 // copy of the org list in case the org service is sorted in a
150 // different manner by other parts of the code.
151 this.org.sortTree(this.displayField);
152 this.org.absorbTree();
153 this.sortedOrgs = this.org.list();
155 if (this.initialOrg || this.initialOrgId) {
156 this.selected = this.formatForDisplay(
157 this.initialOrg || this.org.get(this.initialOrgId)
164 const promise = this.persistKey ?
165 this.getFromSetting() : Promise.resolve(null);
167 promise.then((startupOrgId: number) => {
171 if (this.fallbackOrgId) {
172 startupOrgId = this.fallbackOrgId;
174 } else if (this.fallbackOrg) {
175 startupOrgId = this.org.get(this.fallbackOrg).id();
177 } else if (this.applyDefault && this.auth.user()) {
178 startupOrgId = this.auth.user().ws_ou();
184 startupOrg = this.org.get(startupOrgId);
185 this.selected = this.formatForDisplay(startupOrg);
188 this.markAsLoaded(startupOrg);
192 getFromSetting(): Promise<number> {
194 const key = `eg.orgselect.${this.persistKey}`;
196 return this.serverStore.getItem(key).then(
197 value => this.valueFromSetting = value
201 // Indicate all load-time shuffling has completed.
202 markAsLoaded(onChangeOrg?: IdlObject) {
203 setTimeout(() => { // Avoid emitting mid-digest
204 this.componentLoaded.emit();
205 this.componentLoaded.complete();
206 if (onChangeOrg) { this.onChange.emit(onChangeOrg); }
211 applyPermLimitOrgs(perms: string[]) {
217 // handle lazy clients that pass null perm names
218 perms = perms.filter(p => p !== null && p !== undefined);
220 if (perms.length === 0) {
224 // NOTE: If permLimitOrgs is useful in a non-staff context
225 // we need to change this to support non-staff perm checks.
226 this.perm.hasWorkPermAt(perms, true).then(permMap => {
228 // safari-friendly version of Array.flat()
229 Object.values(permMap).reduce((acc, val) => acc.concat(val), []);
233 // Format for display in the selector drop-down and input.
234 formatForDisplay(org: IdlObject): OrgDisplay {
235 let label = org[this.displayField]();
236 if (!this.readOnly) {
237 label = PAD_SPACE.repeat(org.ou_type().depth()) + label;
246 // Fired by the typeahead to inform us of a change.
247 // TODO: this does not fire when the value is cleared :( -- implement
248 // change detection on this.selected to look specifically for NULL.
249 orgChanged(selEvent: NgbTypeaheadSelectItemEvent) {
250 // console.debug('org unit change occurred ' + selEvent.item);
251 this.onChange.emit(this.org.get(selEvent.item.id));
253 if (this.persistKey && this.valueFromSetting !== selEvent.item.id) {
254 // persistKey is active. Update the persisted value when changed.
256 const key = `eg.orgselect.${this.persistKey}`;
257 this.valueFromSetting = selEvent.item.id;
258 this.serverStore.setItem(key, this.valueFromSetting);
262 // Remove the tree-padding spaces when matching.
263 formatter = (result: OrgDisplay) => result ? result.label.trim() : '';
265 // reset the state of the component
267 this.selected = null;
270 filter = (text$: Observable<string>): Observable<OrgDisplay[]> => {
273 distinctUntilChanged(),
275 // Inject a specifier indicating the source of the
276 // action is a user click
277 this.click$.pipe(filter(() => !this.instance.isPopupOpen()))
278 .pipe(mapTo('_CLICK_'))
282 let orgs = this.sortedOrgs.filter(org =>
283 this.hidden.filter(id => org.id() === id).length === 0
286 if (this.permLimitOrgs) {
287 // Avoid showing org units where the user does
288 // not have the requested permission.
289 orgs = orgs.filter(org =>
290 this.permLimitOrgs.includes(org.id()));
293 if (term !== '_CLICK_') {
294 // For search-driven events, limit to the matching
296 orgs = orgs.filter(org => {
297 return term === '' || // show all
298 org[this.displayField]()
299 .toLowerCase().indexOf(term.toLowerCase()) > -1;
304 return orgs.map(org => this.formatForDisplay(org));