Browse Source

UI: Add entity autocomplete output EntityChange

pull/4602/head
Vladyslav_Prykhodko 5 years ago
parent
commit
476035e993
  1. 29
      ui-ngx/src/app/shared/components/entity/entity-autocomplete.component.ts

29
ui-ngx/src/app/shared/components/entity/entity-autocomplete.component.ts

@ -14,7 +14,17 @@
/// limitations under the License. /// limitations under the License.
/// ///
import { AfterViewInit, Component, ElementRef, forwardRef, Input, OnInit, ViewChild } from '@angular/core'; import {
AfterViewInit,
Component,
ElementRef,
EventEmitter,
forwardRef,
Input,
OnInit,
Output,
ViewChild
} from '@angular/core';
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms'; import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { map, mergeMap, share, tap } from 'rxjs/operators'; import { map, mergeMap, share, tap } from 'rxjs/operators';
@ -28,6 +38,7 @@ import { EntityService } from '@core/http/entity.service';
import { coerceBooleanProperty } from '@angular/cdk/coercion'; import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { getCurrentAuthUser } from '@core/auth/auth.selectors'; import { getCurrentAuthUser } from '@core/auth/auth.selectors';
import { Authority } from '@shared/models/authority.enum'; import { Authority } from '@shared/models/authority.enum';
import { isEqual } from '@core/utils';
@Component({ @Component({
selector: 'tb-entity-autocomplete', selector: 'tb-entity-autocomplete',
@ -43,7 +54,7 @@ export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit
selectEntityFormGroup: FormGroup; selectEntityFormGroup: FormGroup;
modelValue: string | null; modelValue: string | EntityId | null;
entityTypeValue: EntityType | AliasEntityType; entityTypeValue: EntityType | AliasEntityType;
@ -95,6 +106,9 @@ export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit
@Input() @Input()
disabled: boolean; disabled: boolean;
@Output()
entityChanged = new EventEmitter<BaseData<EntityId>>();
@ViewChild('entityInput', {static: true}) entityInput: ElementRef; @ViewChild('entityInput', {static: true}) entityInput: ElementRef;
entityText: string; entityText: string;
@ -135,7 +149,7 @@ export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit
} else { } else {
modelValue = value.id.id; modelValue = value.id.id;
} }
this.updateView(modelValue); this.updateView(modelValue, value);
if (value === null) { if (value === null) {
this.clear(); this.clear();
} }
@ -265,10 +279,12 @@ export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit
(entity) => { (entity) => {
this.modelValue = entity.id.id; this.modelValue = entity.id.id;
this.selectEntityFormGroup.get('entity').patchValue(entity, {emitEvent: false}); this.selectEntityFormGroup.get('entity').patchValue(entity, {emitEvent: false});
this.entityChanged.emit(entity);
}, },
() => { () => {
this.modelValue = null; this.modelValue = null;
this.selectEntityFormGroup.get('entity').patchValue('', {emitEvent: false}); this.selectEntityFormGroup.get('entity').patchValue('', {emitEvent: false});
this.entityChanged.emit(null);
if (value !== null) { if (value !== null) {
this.propagateChange(this.modelValue); this.propagateChange(this.modelValue);
} }
@ -280,10 +296,12 @@ export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit
(entity) => { (entity) => {
this.modelValue = entity.id.id; this.modelValue = entity.id.id;
this.selectEntityFormGroup.get('entity').patchValue(entity, {emitEvent: false}); this.selectEntityFormGroup.get('entity').patchValue(entity, {emitEvent: false});
this.entityChanged.emit(entity);
}, },
() => { () => {
this.modelValue = null; this.modelValue = null;
this.selectEntityFormGroup.get('entity').patchValue('', {emitEvent: false}); this.selectEntityFormGroup.get('entity').patchValue('', {emitEvent: false});
this.entityChanged.emit(null);
if (value !== null) { if (value !== null) {
this.propagateChange(this.modelValue); this.propagateChange(this.modelValue);
} }
@ -311,10 +329,11 @@ export class EntityAutocompleteComponent implements ControlValueAccessor, OnInit
this.selectEntityFormGroup.get('entity').patchValue('', {emitEvent: false}); this.selectEntityFormGroup.get('entity').patchValue('', {emitEvent: false});
} }
updateView(value: string | null) { updateView(value: string | EntityId | null, entity: BaseData<EntityId> | null) {
if (this.modelValue !== value) { if (!isEqual(this.modelValue, value)) {
this.modelValue = value; this.modelValue = value;
this.propagateChange(this.modelValue); this.propagateChange(this.modelValue);
this.entityChanged.emit(entity);
} }
} }

Loading…
Cancel
Save