2 * <eg-combobox [allowFreeText]="true" [entries]="comboboxEntryList"/>
3 * <!-- see also <eg-combobox-entry> -->
6 import {Component, OnInit, Input, Output, ViewChild,
7 Directive, ViewChildren, QueryList, AfterViewInit,
8 OnChanges, SimpleChanges,
9 TemplateRef, EventEmitter, ElementRef, forwardRef} from '@angular/core';
10 import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
11 import {Observable, of, Subject} from 'rxjs';
12 import {map, tap, reduce, mergeMap, mapTo, debounceTime, distinctUntilChanged, merge, filter} from 'rxjs/operators';
13 import {NgbTypeahead, NgbTypeaheadSelectItemEvent} from '@ng-bootstrap/ng-bootstrap';
14 import {StoreService} from '@eg/core/store.service';
15 import {IdlService, IdlObject} from '@eg/core/idl.service';
16 import {PcrudService} from '@eg/core/pcrud.service';
17 import {OrgService} from '@eg/core/org.service';
19 export interface ComboboxEntry {
21 // If no label is provided, the 'id' value is used.
24 userdata?: any; // opaque external value; ignored by this component.
30 selector: 'ng-template[egIdlClass]'
32 export class IdlClassTemplateDirective {
33 @Input() egIdlClass: string;
34 constructor(public template: TemplateRef<any>) {}
38 selector: 'eg-combobox',
39 templateUrl: './combobox.component.html',
41 .icons {margin-left:-18px}
42 .material-icons {font-size: 16px;font-weight:bold}
45 provide: NG_VALUE_ACCESSOR,
46 useExisting: forwardRef(() => ComboboxComponent),
50 export class ComboboxComponent
51 implements ControlValueAccessor, OnInit, AfterViewInit, OnChanges {
55 selected: ComboboxEntry;
56 click$: Subject<string>;
57 entrylist: ComboboxEntry[];
59 @ViewChild('instance', {static: false}) instance: NgbTypeahead;
60 @ViewChild('defaultDisplayTemplate', {static: true}) defaultDisplayTemplate: TemplateRef<any>;
61 @ViewChildren(IdlClassTemplateDirective) idlClassTemplates: QueryList<IdlClassTemplateDirective>;
63 @Input() domId = 'eg-combobox-' + ComboboxComponent.domIdAuto++;
65 // Applies a name attribute to the input.
67 @Input() name: string;
69 // Placeholder text for selector input
70 @Input() placeholder = '';
72 @Input() persistKey: string; // TODO
74 @Input() allowFreeText = false;
76 @Input() inputSize: number = null;
78 // If true, applies form-control-sm CSS
79 @Input() smallFormControl = false;
81 // If true, the typeahead only matches values that start with
82 // the value typed as opposed to a 'contains' match.
83 @Input() startsWith = false;
85 // Add a 'required' attribute to the input
87 @Input() set required(r: boolean) {
90 // and a 'mandatory' synonym, as an issue
91 // has been observed in at least Firefox 88.0.1
92 // where the left border indicating whether a required
93 // value has been set or not is displayed in the
94 // container of the combobox, not just the dropdown
95 @Input() set mandatory(r: boolean) {
99 // Array of entry identifiers to disable in the selector
100 @Input() disableEntries: any[] = [];
104 @Input() set disabled(d: boolean) {
108 // Entry ID of the default entry to select (optional)
109 // onChange() is NOT fired when applying the default value,
110 // unless startIdFiresOnChange is set to true.
111 @Input() startId: any = null;
112 @Input() idlClass: string;
113 @Input() idlBaseQuery: any = null;
114 @Input() startIdFiresOnChange: boolean;
116 // This will be appended to the async data retrieval query
117 // when fetching objects by idlClass.
118 @Input() idlQueryAnd: {[field: string]: any};
120 @Input() idlQuerySort: {[cls: string]: string};
122 // Display the selected value as text instead of within
124 @Input() readOnly = false;
126 // Allow the selected entry ID to be passed via the template
127 // This does NOT not emit onChange events.
128 @Input() set selectedId(id: any) {
129 if (id === undefined) { return; }
131 // clear on explicit null
133 this.selected = null;
137 if (this.entrylist.length) {
138 this.selected = this.entrylist.filter(e => e.id === id)[0];
141 if (!this.selected) {
142 // It's possible the selected ID lives in a set of entries
143 // that are yet to be provided.
146 this.pcrud.retrieve(this.idlClass, id)
150 label: this.getFmRecordLabel(rec),
152 disabled : this.disableEntries.includes(id)
154 this.selected = this.entrylist.filter(e => e.id === id)[0];
160 get selectedId(): any {
161 return this.selected ? this.selected.id : null;
164 @Input() idlField: string;
165 @Input() idlIncludeLibraryInLabel: string;
166 @Input() asyncDataSource: (term: string) => Observable<ComboboxEntry>;
168 // If true, an async data search is allowed to fetch all
169 // values when given an empty term. This should be used only
170 // if the maximum number of entries returned by the data source
171 // is known to be no more than a couple hundred.
172 @Input() asyncSupportsEmptyTermClick: boolean;
174 // Useful for efficiently preventing duplicate async entries
175 asyncIds: {[idx: string]: boolean};
177 // True if a default selection has been made.
178 defaultSelectionApplied: boolean;
180 @Input() set entries(el: ComboboxEntry[]) {
183 if (this.entrylistMatches(el)) {
184 // Avoid reprocessing data we already have.
190 // new set of entries essentially means a new instance. reset.
191 this.defaultSelectionApplied = false;
192 this.applySelection();
194 // It's possible to provide an entrylist at load time, but
195 // fetch all future data via async data source. Track the
196 // values we already have so async lookup won't add them again.
197 // A new entry list wipes out any existing async values.
199 el.forEach(entry => this.asyncIds['' + entry.id] = true);
203 // When provided use this as the display template for each entry.
204 @Input() displayTemplate: TemplateRef<any>;
206 // Emitted when the value is changed via UI.
207 // When the UI value is cleared, null is emitted.
208 @Output() onChange: EventEmitter<ComboboxEntry>;
210 // Useful for massaging the match string prior to comparison
211 // and display. Default version trims leading/trailing spaces.
212 formatDisplayString: (e: ComboboxEntry) => string;
214 idlDisplayTemplateMap: { [key: string]: TemplateRef<any> } = {};
215 getFmRecordLabel: (fm: IdlObject) => string;
217 // Stub functions required by ControlValueAccessor
218 propagateChange = (_: any) => {};
219 propagateTouch = () => {};
222 private elm: ElementRef,
223 private store: StoreService,
224 private idl: IdlService,
225 private pcrud: PcrudService,
226 private org: OrgService,
230 this.click$ = new Subject<string>();
231 this.onChange = new EventEmitter<ComboboxEntry>();
232 this.defaultSelectionApplied = false;
234 this.formatDisplayString = (result: ComboboxEntry) => {
235 const display = result.label || result.id;
236 return (display + '').trim();
239 this.getFmRecordLabel = (fm: IdlObject) => {
240 // FIXME: it would be cleaner if we could somehow use
241 // the per-IDL-class ng-templates directly
242 switch (this.idlClass) {
244 return fm.course_number() + ': ' + fm.name();
247 return fm.code() + ' (' + fm.year() + ')' +
248 ' (' + this.getOrgShortname(fm.org()) + ')';
251 return fm.name() + ' (' + this.getOrgShortname(fm.owning_lib()) + ')';
254 const field = this.idlField;
255 if (this.idlIncludeLibraryInLabel) {
256 return fm[field]() + ' (' + this.getOrgShortname(fm[this.idlIncludeLibraryInLabel]()) + ')';
266 const classDef = this.idl.classes[this.idlClass];
267 const pkeyField = classDef.pkey;
270 throw new Error(`IDL class ${this.idlClass} has no pkey field`);
273 if (!this.idlField) {
274 this.idlField = this.idl.getClassSelector(this.idlClass);
277 this.asyncDataSource = term => {
278 const field = this.idlField;
280 if (this.idlBaseQuery) {
281 args = this.idlBaseQuery;
283 const extra_args = { order_by : {} };
284 if (this.startsWith) {
285 args[field] = {'ilike': `${term}%`};
287 args[field] = {'ilike': `%${term}%`}; // could -or search on label
289 if (this.idlQueryAnd) {
290 Object.assign(args, this.idlQueryAnd);
292 if (this.idlQuerySort) {
293 extra_args['order_by'] = this.idlQuerySort;
295 extra_args['order_by'][this.idlClass] = field;
297 extra_args['limit'] = 100;
298 if (this.idlIncludeLibraryInLabel) {
299 extra_args['flesh'] = 1;
300 const flesh_fields: Object = {};
301 flesh_fields[this.idlClass] = [ this.idlIncludeLibraryInLabel ];
302 extra_args['flesh_fields'] = flesh_fields;
303 return this.pcrud.search(this.idlClass, args, extra_args).pipe(map(data => {
305 id: data[pkeyField](),
306 label: this.getFmRecordLabel(data),
311 return this.pcrud.search(this.idlClass, args, extra_args).pipe(map(data => {
312 return {id: data[pkeyField](), label: this.getFmRecordLabel(data), fm: data};
320 this.idlDisplayTemplateMap = this.idlClassTemplates.reduce((acc, cur) => {
321 acc[cur.egIdlClass] = cur.template;
326 ngOnChanges(changes: SimpleChanges) {
327 let firstTime = true;
328 Object.keys(changes).forEach(key => {
329 if (!changes[key].firstChange) {
334 if ('selectedId' in changes) {
335 if (!changes.selectedId.currentValue) {
337 // In allowFreeText mode, selectedId will be null even
338 // though a freetext value may be present in the combobox.
339 if (this.allowFreeText) {
340 if (this.selected && !this.selected.freetext) {
341 this.selected = null;
344 this.selected = null;
348 if ('idlClass' in changes) {
349 if (!('idlField' in changes)) {
350 // let ngOnInit reset it to the
351 // selector of the new IDL class
352 this.idlField = null;
355 this.entrylist.length = 0;
356 this.selected = null;
359 if ('idlQueryAnd' in changes) {
361 this.entrylist.length = 0;
362 this.selected = null;
369 this.click$.next($event.target.value);
372 getResultTemplate(): TemplateRef<any> {
373 if (this.displayTemplate) {
374 return this.displayTemplate;
376 if (this.idlClass in this.idlDisplayTemplateMap) {
377 return this.idlDisplayTemplateMap[this.idlClass];
379 return this.defaultDisplayTemplate;
382 getOrgShortname(ou: any) {
383 if (typeof ou === 'object') {
384 return ou.shortname();
386 return this.org.get(ou).shortname();
391 // Give the input a chance to focus then fire the click
392 // handler to force open the typeahead
393 this.elm.nativeElement.getElementsByTagName('input')[0].focus();
394 setTimeout(() => this.click$.next(''));
397 // Returns true if the 2 entries are equivalent.
398 entriesMatch(e1: ComboboxEntry, e2: ComboboxEntry): boolean {
402 e1.label === e2.label &&
403 e1.freetext === e2.freetext
407 // Returns true if the 2 lists are equivalent.
408 entrylistMatches(el: ComboboxEntry[]): boolean {
409 if (el.length === 0 && this.entrylist.length === 0) {
410 // Empty arrays are only equivalent if they are the same array,
411 // since the caller may provide an array that starts empty, but
412 // is later populated.
413 return el === this.entrylist;
415 if (el.length !== this.entrylist.length) {
418 for (let i = 0; i < el.length; i++) {
419 const mine = this.entrylist[i];
420 if (!mine || !this.entriesMatch(mine, el[i])) {
427 // Apply a default selection where needed
430 if (this.entrylist && !this.defaultSelectionApplied) {
433 this.entrylist.filter(e => e.id === this.startId)[0];
436 this.selected = entry;
437 this.defaultSelectionApplied = true;
438 if (this.startIdFiresOnChange) {
439 this.selectorChanged(
440 {item: this.selected, preventDefault: () => true});
446 // Called by combobox-entry.component
447 addEntry(entry: ComboboxEntry) {
448 this.entrylist.push(entry);
449 this.applySelection();
452 // Manually set the selected value by ID.
453 // This does NOT fire the onChange handler.
454 // DEPRECATED: use this.selectedId = abc or [selectedId]="abc" instead.
455 applyEntryId(entryId: any) {
456 this.selected = this.entrylist.filter(e => e.id === entryId)[0];
459 addAsyncEntry(entry: ComboboxEntry) {
460 if (!entry) { return; }
461 // Avoid duplicate async entries
462 if (!this.asyncIds['' + entry.id]) {
463 this.asyncIds['' + entry.id] = true;
464 this.addEntry(entry);
468 hasEntry(entryId: any): boolean {
469 return this.entrylist.filter(e => e.id === entryId)[0] !== undefined;
473 // When the selected value is a string it means we have either
474 // no value (user cleared the input) or a free-text value.
476 if (typeof this.selected === 'string') {
478 if (this.allowFreeText && this.selected !== '') {
479 const freeText = this.entrylist.filter(e => e.id === null)[0];
483 // If we already had a free text entry, just replace
484 // the label with the new value
485 freeText.label = this.selected;
486 this.selected = freeText;
490 // Free text entered which does not match a known entry
491 // translate it into a dummy ComboboxEntry
494 label: this.selected,
501 this.selected = null;
504 // Manually fire the onchange since NgbTypeahead fails
505 // to fire the onchange when the value is cleared.
506 this.selectorChanged(
507 {item: this.selected, preventDefault: () => true});
509 this.propagateTouch();
512 // Fired by the typeahead to inform us of a change.
513 selectorChanged(selEvent: NgbTypeaheadSelectItemEvent) {
514 this.onChange.emit(selEvent.item);
515 this.propagateChange(selEvent.item);
518 // Adds matching async entries to the entry list
519 // and propagates the search term for pipelining.
520 addAsyncEntries(term: string): Observable<string> {
522 if (!term || !this.asyncDataSource) {
526 let searchTerm = term;
527 if (term === '_CLICK_') {
528 if (this.asyncSupportsEmptyTermClick) {
529 // Search for "all", but retain and propage the _CLICK_
530 // term so the filter knows to open the selector
533 // Skip the final filter map and display nothing.
538 return new Observable(observer => {
539 this.asyncDataSource(searchTerm).subscribe(
540 (entry: ComboboxEntry) => this.addAsyncEntry(entry),
550 // NgbTypeahead doesn't offer a way to style the dropdown
551 // button directly, so we have to reach up and style it ourselves.
552 applyDisableStyle() {
553 this.disableEntries.forEach(id => {
554 const node = document.getElementById(`${this.domId}-${id}`);
556 const button = node.parentNode as HTMLElement;
557 button.classList.add('disabled');
562 filter = (text$: Observable<string>): Observable<ComboboxEntry[]> => {
565 distinctUntilChanged(),
567 // Merge click actions in with the stream of text entry
569 // Inject a specifier indicating the source of the
570 // action is a user click instead of a text entry.
571 // This tells the filter to show all values in sync mode.
572 this.click$.pipe(filter(() =>
573 !this.instance.isPopupOpen()
574 )).pipe(mapTo('_CLICK_'))
577 // mergeMap coalesces an observable into our stream.
578 mergeMap(term => this.addAsyncEntries(term)),
579 map((term: string) => {
581 // Display no values when the input is empty and no
582 // click action occurred.
583 if (term === '') { return []; }
585 // If we make it this far, _CLICK_ means show everything.
586 if (term === '_CLICK_') { term = ''; }
588 // Give the typeahead a chance to open before applying
589 // the disabled entry styling.
590 setTimeout(() => this.applyDisableStyle());
592 // Filter entrylist whose labels substring-match the
594 return this.entrylist.filter(entry => {
595 const label = String(entry.label) || String(entry.id);
596 if (!label) { return false; }
598 if (this.startsWith) {
599 return label.toLowerCase().startsWith(term.toLowerCase());
601 return label.toLowerCase().indexOf(term.toLowerCase()) > -1;
608 writeValue(value: ComboboxEntry) {
609 if (value !== undefined && value !== null) {
610 this.startId = value.id;
611 this.applySelection();
615 registerOnChange(fn) {
616 this.propagateChange = fn;
619 registerOnTouched(fn) {
620 this.propagateTouch = fn;