clr-angular-static-fix
Version:
1. Install Clarity Icons package through npm:
155 lines (127 loc) • 4.89 kB
text/typescript
/*
* Copyright (c) 2016-2018 VMware, Inc. All Rights Reserved.
* This software is released under MIT license.
* The full license information can be found in LICENSE in the root directory of this project.
*/
import { Component, EventEmitter, forwardRef, Input, Output } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
/**
* Private counter to generate unique IDs for the checkboxes, to bind the labels to them.
*/
let latestId = 0;
/**
* @deprecated ClrCheckbox will be renamed to ClrCheckboxDeprecated in 0.12, and will be replaced with a new
* implementation in 0.13, so if you import it you will need to update your references.
*/
export class ClrCheckboxDeprecated implements ControlValueAccessor {
// If our host has an ID attribute, we use this instead of our index.
_id: string = (latestId++).toString();
public get id() {
return `clr-checkbox-${this._id}`;
}
// If host provides an clrAriaLabeledBy input, we apply it to the checkbox
public clrAriaLabeledBy: string = null;
// If our host has a name attribute, we apply it to the checkbox.
public name: string = null;
// If the host is disabled we apply it to the checkbox
public disabled: boolean = false;
// Support for inline checkboxes, adds the necessary class to the host
public inline = false;
private _checked = false;
public get checked() {
return this._checked;
}
public set checked(value: boolean) {
if (value !== this._checked) {
if (this._indeterminate) {
this.setIndeterminate(false);
}
this.setChecked(value);
}
}
private _indeterminate: boolean = false;
public get indeterminate() {
return this._indeterminate;
}
public set indeterminate(value: boolean) {
if (this._indeterminate !== value) {
if (this._checked) {
this.setChecked(false);
}
this.setIndeterminate(value);
}
}
public indeterminateChange: EventEmitter<boolean> = new EventEmitter<boolean>(false);
private setIndeterminate(value: boolean) {
this._indeterminate = value;
this.indeterminateChange.emit(this._indeterminate);
}
private setChecked(value: boolean) {
this._checked = value;
this.change.emit(this._checked);
}
public change = new EventEmitter<boolean>(false);
public toggle() {
this.checked = !this.checked;
this.onChangeCallback(this.checked);
}
writeValue(value: any): void {
if (value === null) {
value = false;
}
if (value !== this.checked) {
this.checked = value;
}
}
/*
* These callbacks will be given to us through the ControlValueAccessor interface,
* and we need to call them when the user interacts with the checkbox.
*/
private onChangeCallback = (_: any) => {};
registerOnChange(onChange: any): void {
this.onChangeCallback = onChange;
}
private onTouchedCallback = () => {};
registerOnTouched(onTouched: any): void {
this.onTouchedCallback = onTouched;
}
public touch() {
this.onTouchedCallback();
}
checkIndeterminateState(): void {
if (!this.disabled) {
this.toggle();
}
}
}