@pepperi/components
Version:
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 10.0.1.
92 lines (76 loc) • 3.27 kB
text/typescript
import { Component, OnInit, OnChanges, Input, Output, EventEmitter, ChangeDetectionStrategy, OnDestroy, Renderer2, ElementRef } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { TranslateService } from '@ngx-translate/core';
import { SessionService, CustomizationService, LAYOUT_TYPE } from '@pepperi/lib';
export class PepperiCheckboxComponent implements OnChanges, OnInit, OnDestroy {
key: string = '';
value: string = '';
label: string = '';
type: string = 'checkbox'; // || 'booleanText'
required: boolean = false;
disabled: boolean = false;
readonly: boolean = false;
xAlignment: string = '0';
rowSpan: number = 1;
additionalValue: any;
controlType = 'checkbox';
form: FormGroup = null;
isActive: boolean = false;
showTitle: boolean = true;
layoutType: LAYOUT_TYPE = LAYOUT_TYPE.PepperiForm;
valueChanged: EventEmitter<any> = new EventEmitter<any>();
LAYOUT_TYPE = LAYOUT_TYPE;
standAlone = false;
additionalValueObject: any;
public jsonLib = JSON;
constructor(
private renderer: Renderer2,
private customizationService: CustomizationService,
public sessionService: SessionService,
public translate: TranslateService,
public hostElement: ElementRef
) { }
ngOnInit() {
if (this.form === null) {
this.standAlone = true;
this.form = this.customizationService.getDefaultFromGroup(this.key, this.value, this.required, this.readonly, this.disabled, 0, '', true);
this.renderer.addClass(this.hostElement.nativeElement, CustomizationService.STAND_ALONE_FIELD_CLASS_NAME);
}
if (this.type === 'booleanText') {
try {
if (typeof this.additionalValue === 'string') {
this.additionalValueObject = JSON.parse(this.additionalValue);
} else {
this.additionalValueObject = this.additionalValue;
}
} catch {
this.additionalValueObject = { CheckedText: this.translate.instant('True'), UncheckedText: this.translate.instant('False') };
}
}
}
ngOnChanges(changes: any) { }
ngOnDestroy() {
if (this.valueChanged) {
this.valueChanged.unsubscribe();
}
}
onMaterialChange(e: any) {
this.changeValue(e.checked);
}
toggleChecked(event: any) {
if (!this.disabled) {
const isChecked: boolean = this.value === 'true' || this.value === '1' ? true : false;
this.changeValue(!isChecked);
}
}
changeValue(value: any) {
this.customizationService.updateFormFieldValue(this.form, this.key, value);
this.valueChanged.emit({ apiName: this.key, value: value });
}
}