LP#1850547: eg-combobox: teach it to accommodate idlClass changes
authorGalen Charlton <gmc@equinoxinitiative.org>
Wed, 26 Feb 2020 16:42:53 +0000 (11:42 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 3 Sep 2020 15:51:20 +0000 (11:51 -0400)
This can happen in dynamically constructed search forms such
as the acquisitions search form.

Sponsored-by: Evergreen Community Development Initiative
Sponsored-by: Georgia Public Library Service
Sponsored-by: Indiana State Library
Sponsored-by: C/W MARS

Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Signed-off-by: Tiffany Little <tlittle@georgialibraries.org>
Signed-off-by: Bill Erickson <berickxx@gmail.com>

Open-ILS/src/eg2/src/app/share/combobox/combobox.component.html
Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts

index 4f5ae6e..d228081 100644 (file)
@@ -21,7 +21,7 @@
     [required]="isRequired"
     [(ngModel)]="selected" 
     [ngbTypeahead]="filter"
-    [resultTemplate]=" displayTemplate || idlDisplayTemplateMap[idlClass] || defaultDisplayTemplate"
+    [resultTemplate]="getResultTemplate()"
     [inputFormatter]="formatDisplayString"
     (click)="onClick($event)"
     (blur)="onBlur()"
index 91a5154..60ed2f7 100644 (file)
@@ -5,6 +5,7 @@
  */
 import {Component, OnInit, Input, Output, ViewChild,
     Directive, ViewChildren, QueryList, AfterViewInit,
+    OnChanges, SimpleChanges,
     TemplateRef, EventEmitter, ElementRef, forwardRef} from '@angular/core';
 import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
 import {Observable, of, Subject} from 'rxjs';
@@ -45,14 +46,14 @@ export class IdlClassTemplateDirective {
     multi: true
   }]
 })
-export class ComboboxComponent implements ControlValueAccessor, OnInit, AfterViewInit {
+export class ComboboxComponent implements ControlValueAccessor, OnInit, AfterViewInit, OnChanges {
 
     selected: ComboboxEntry;
     click$: Subject<string>;
     entrylist: ComboboxEntry[];
 
     @ViewChild('instance', { static: true }) instance: NgbTypeahead;
-    @ViewChild('defaultDisplayTemplate', { static: true}) t: TemplateRef<any>;
+    @ViewChild('defaultDisplayTemplate', { static: true}) defaultDisplayTemplate: TemplateRef<any>;
     @ViewChildren(IdlClassTemplateDirective) idlClassTemplates: QueryList<IdlClassTemplateDirective>;
 
     // Applies a name attribute to the input.
@@ -265,10 +266,42 @@ export class ComboboxComponent implements ControlValueAccessor, OnInit, AfterVie
         }, {});
     }
 
+    ngOnChanges(changes: SimpleChanges) {
+        let firstTime = true;
+        Object.keys(changes).forEach(key => {
+            if (!changes[key].firstChange) {
+                firstTime = false;
+            }
+        });
+        if (!firstTime) {
+            if ('idlClass' in changes) {
+                if (!('idlField' in changes)) {
+                    // let ngOnInit reset it to the
+                    // selector of the new IDL class
+                    this.idlField = null;
+                }
+                this.asyncIds = {};
+                this.entrylist.length = 0;
+                this.selected = null;
+            }
+            this.ngOnInit();
+        }
+    }
+
     onClick($event) {
         this.click$.next($event.target.value);
     }
 
+    getResultTemplate(): TemplateRef<any> {
+        if (this.displayTemplate) {
+            return this.displayTemplate;
+        }
+        if (this.idlClass in this.idlDisplayTemplateMap) {
+            return this.idlDisplayTemplateMap[this.idlClass];
+        }
+        return this.defaultDisplayTemplate;
+    }
+
     getOrgShortname(ou: any) {
         if (typeof ou === 'object') {
             return ou.shortname();