From 6487c9ca676b00977f231f5c72d9e68521a75f91 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Wed, 26 Feb 2020 11:42:53 -0500 Subject: [PATCH] LP#1850547: eg-combobox: teach it to accommodate idlClass changes 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 Signed-off-by: Tiffany Little Signed-off-by: Bill Erickson --- .../src/app/share/combobox/combobox.component.html | 2 +- .../src/app/share/combobox/combobox.component.ts | 37 ++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.html b/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.html index 4f5ae6e66f..d2280811e3 100644 --- a/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.html +++ b/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.html @@ -21,7 +21,7 @@ [required]="isRequired" [(ngModel)]="selected" [ngbTypeahead]="filter" - [resultTemplate]=" displayTemplate || idlDisplayTemplateMap[idlClass] || defaultDisplayTemplate" + [resultTemplate]="getResultTemplate()" [inputFormatter]="formatDisplayString" (click)="onClick($event)" (blur)="onBlur()" diff --git a/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts b/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts index 91a5154acc..60ed2f7ad9 100644 --- a/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts +++ b/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts @@ -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; entrylist: ComboboxEntry[]; @ViewChild('instance', { static: true }) instance: NgbTypeahead; - @ViewChild('defaultDisplayTemplate', { static: true}) t: TemplateRef; + @ViewChild('defaultDisplayTemplate', { static: true}) defaultDisplayTemplate: TemplateRef; @ViewChildren(IdlClassTemplateDirective) idlClassTemplates: QueryList; // 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 { + 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(); -- 2.11.0