Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

62 lines
1.8 KiB

/*
* Squidex Headless CMS
*
* @license
* Copyright (c) Sebastian Stehle. All rights reserved
*/
import { Directive, forwardRef, ElementRef, Renderer } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
const NOOP = () => { /* NOOP */ };
export const SQX_LOWERCASE_INPUT_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => LowerCaseInputDirective), multi: true
};
@Directive({
selector: '[sqxLowerCaseInput]',
providers: [SQX_LOWERCASE_INPUT_VALUE_ACCESSOR],
host: {
'(input)': 'onChange($event.target.value)', '(blur)': 'onTouched()'
}
})
export class LowerCaseInputDirective implements ControlValueAccessor {
private changeCallback: (value: any) => void = NOOP;
private touchedCallback: () => void = NOOP;
constructor(
private readonly renderer: Renderer,
private readonly elementRef: ElementRef
) {
}
public writeValue(value: any) {
const normalizedValue = (value == null ? '' : value.toString()).toLowerCase();
this.renderer.setElementProperty(this.elementRef.nativeElement, 'value', normalizedValue);
}
public setDisabledState(isDisabled: boolean): void {
this.renderer.setElementProperty(this.elementRef.nativeElement, 'disabled', isDisabled);
}
public registerOnChange(fn: any) {
this.changeCallback = fn;
}
public registerOnTouched(fn: any) {
this.touchedCallback = fn;
}
public onChange(value: any) {
const normalizedValue = (value == null ? '' : value.toString()).toLowerCase();
this.renderer.setElementProperty(this.elementRef.nativeElement, 'value', normalizedValue);
this.changeCallback(normalizedValue);
}
public onTouched() {
this.touchedCallback();
}
}