mirror of https://github.com/Squidex/squidex.git
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.
56 lines
1.4 KiB
56 lines
1.4 KiB
/*
|
|
* Squidex Headless CMS
|
|
*
|
|
* @license
|
|
* Copyright (c) Sebastian Stehle. All rights reserved
|
|
*/
|
|
|
|
import { Component, forwardRef } from '@angular/core';
|
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
|
|
|
import { Types } from './../utils/types';
|
|
|
|
export const SQX_TOGGLE_CONTROL_VALUE_ACCESSOR: any = {
|
|
provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => ToggleComponent), multi: true
|
|
};
|
|
|
|
@Component({
|
|
selector: 'sqx-toggle',
|
|
styleUrls: ['./toggle.component.scss'],
|
|
templateUrl: './toggle.component.html',
|
|
providers: [SQX_TOGGLE_CONTROL_VALUE_ACCESSOR]
|
|
})
|
|
export class ToggleComponent implements ControlValueAccessor {
|
|
private callChange = (v: any) => { /* NOOP */ };
|
|
private callTouched = () => { /* NOOP */ };
|
|
|
|
public isChecked: boolean | null = null;
|
|
public isDisabled = false;
|
|
|
|
public writeValue(value: boolean | null | undefined) {
|
|
this.isChecked = Types.isBoolean(value) ? value || null : null;
|
|
}
|
|
|
|
public setDisabledState(isDisabled: boolean): void {
|
|
this.isDisabled = isDisabled;
|
|
}
|
|
|
|
public registerOnChange(fn: any) {
|
|
this.callChange = fn;
|
|
}
|
|
|
|
public registerOnTouched(fn: any) {
|
|
this.callTouched = fn;
|
|
}
|
|
|
|
public changeState() {
|
|
if (this.isDisabled) {
|
|
return;
|
|
}
|
|
|
|
this.isChecked = !(this.isChecked === true);
|
|
|
|
this.callChange(this.isChecked);
|
|
this.callTouched();
|
|
}
|
|
}
|