@pepperi/components
Version:
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 10.0.1.
156 lines (131 loc) • 5.23 kB
text/typescript
import { Component, OnInit, OnChanges, Input, Output, EventEmitter, ViewChild, ElementRef,
ChangeDetectionStrategy, OnDestroy, Renderer2 } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { MatSelect } from '@angular/material/select';
import { SessionService, LAYOUT_TYPE, CustomizationService } from '@pepperi/lib';
export class PepperiSelectComponent implements OnChanges, OnInit, OnDestroy {
key = '';
value = '';
formattedValue = '';
label = '';
type = 'select';
required = false;
disabled = false;
readonly = false;
xAlignment = '0';
rowSpan = 1;
options: any = null;
controlType = 'select';
// @Input() field: PepperiFieldBase;
form: FormGroup = null;
layoutType: LAYOUT_TYPE = LAYOUT_TYPE.PepperiForm;
parentField: any = false;
isActive = false;
showTitle = true;
emptyOption = true;
valueChanged: EventEmitter<any> = new EventEmitter<any>();
formValidationChanged: EventEmitter<boolean> = new EventEmitter<boolean>();
select: MatSelect;
LAYOUT_TYPE = LAYOUT_TYPE;
standAlone = false;
isInEditMode = false;
// isFocus: boolean = false;
isMulti = false;
selectedValuesModel: string[];
selectedValueModel: string;
fieldFormattedValue = '';
constructor(public sessionService: SessionService, private customizationService: CustomizationService,
private renderer: Renderer2, public _eref: ElementRef) { }
private addOptionsIfNeeded() {
if (this.isMulti) {
// Go gor all selected and add to options if not exist
for (let i = 0; i < this.selectedValuesModel.length; i++) {
let valueNotExist = false;
if (!this.options.find((opt) => opt.Key === this.selectedValuesModel[i])) {
valueNotExist = true;
}
// Add it to options.
if (valueNotExist) {
this.options.push({ Key: this.selectedValuesModel[i], Value: this.selectedValuesModel[i] });
}
}
} else {
if (this.value && this.value !== '' && !this.options.find((opt) => opt.Key === this.value)) {
this.options.push({ Key: this.value, Value: this.formattedValue });
}
}
}
ngOnInit() {
if (this.form === null) {
this.standAlone = true;
this.form = this.customizationService.getDefaultFromGroup(this.key, this.value, this.required, this.readonly, this.disabled);
this.renderer.addClass(this._eref.nativeElement, CustomizationService.STAND_ALONE_FIELD_CLASS_NAME);
}
}
ngOnChanges(changes: any) {
this.isMulti = this.type === 'multi';
if (this.isMulti) {
this.selectedValuesModel = this.value.length > 0 ? this.value.split(';') : [];
} else {
this.selectedValueModel = this.value;
}
this.fieldFormattedValue = typeof this.value === 'string' ? this.value.replace(new RegExp(';', 'g'), ', ') : '';
this.addOptionsIfNeeded();
}
ngOnDestroy() {
if (this.valueChanged) {
this.valueChanged.unsubscribe();
}
if (this.formValidationChanged) {
this.formValidationChanged.unsubscribe();
}
}
selectionChange(event: any) {
// this.isFocus = false;
if (!this.isMulti) {
this.changeValue(this.selectedValueModel);
}
}
openedChange(event: any) {
// Only on close.
if (!event) {
if (this.isMulti) {
// this.isFocus = false;
this.changeValue(this.selectedValuesModel.join(';'));
}
if (this.isInEditMode) {
this.isInEditMode = false;
}
}
}
changeValue(value: any) {
this.formattedValue = value;
this.customizationService.updateFormFieldValue(this.form, this.key, value);
if (this.required) {
const fieldControl = this.form.controls[this.key];
if (value) {
fieldControl.setErrors(null);
} else {
fieldControl.setErrors({
serverError: 'Required',
});
}
this.formValidationChanged.emit(this.form.valid);
}
this.valueChanged.emit({ apiName: this.key, value });
}
cardTemplateClicked(event: any) {
const self = this;
this.isInEditMode = true;
setTimeout(() => {
self.select.open();
}, 0);
}
}