ngx-bootstrap
Version:
Native Angular Bootstrap Components
384 lines (378 loc) • 9.74 kB
JavaScript
import { forwardRef, Directive, Input, HostBinding, HostListener, ChangeDetectorRef, ElementRef, Optional, Renderer2, NgModule } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
// TODO: config: activeClass - Class to apply to the checked buttons
/** @type {?} */
const CHECKBOX_CONTROL_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
/* tslint:disable-next-line: no-use-before-declare */
useExisting: forwardRef((/**
* @return {?}
*/
() => ButtonCheckboxDirective)),
multi: true
};
/**
* Add checkbox functionality to any element
*/
class ButtonCheckboxDirective {
constructor() {
/**
* Truthy value, will be set to ngModel
*/
this.btnCheckboxTrue = true;
/**
* Falsy value, will be set to ngModel
*/
this.btnCheckboxFalse = false;
this.state = false;
this.onChange = Function.prototype;
this.onTouched = Function.prototype;
}
// view -> model
/**
* @return {?}
*/
onClick() {
if (this.isDisabled) {
return;
}
this.toggle(!this.state);
this.onChange(this.value);
}
/**
* @return {?}
*/
ngOnInit() {
this.toggle(this.trueValue === this.value);
}
/**
* @protected
* @return {?}
*/
get trueValue() {
return typeof this.btnCheckboxTrue !== 'undefined'
? this.btnCheckboxTrue
: true;
}
/**
* @protected
* @return {?}
*/
get falseValue() {
return typeof this.btnCheckboxFalse !== 'undefined'
? this.btnCheckboxFalse
: false;
}
/**
* @param {?} state
* @return {?}
*/
toggle(state) {
this.state = state;
this.value = this.state ? this.trueValue : this.falseValue;
}
// ControlValueAccessor
// model -> view
/**
* @param {?} value
* @return {?}
*/
writeValue(value) {
this.state = this.trueValue === value;
this.value = value ? this.trueValue : this.falseValue;
}
/**
* @param {?} isDisabled
* @return {?}
*/
setDisabledState(isDisabled) {
this.isDisabled = isDisabled;
}
/**
* @param {?} fn
* @return {?}
*/
registerOnChange(fn) {
this.onChange = fn;
}
/**
* @param {?} fn
* @return {?}
*/
registerOnTouched(fn) {
this.onTouched = fn;
}
}
ButtonCheckboxDirective.decorators = [
{ type: Directive, args: [{
selector: '[btnCheckbox]',
providers: [CHECKBOX_CONTROL_VALUE_ACCESSOR]
},] }
];
ButtonCheckboxDirective.propDecorators = {
btnCheckboxTrue: [{ type: Input }],
btnCheckboxFalse: [{ type: Input }],
state: [{ type: HostBinding, args: ['class.active',] }, { type: HostBinding, args: ['attr.aria-pressed',] }],
onClick: [{ type: HostListener, args: ['click',] }]
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
const RADIO_CONTROL_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
/* tslint:disable-next-line: no-use-before-declare */
useExisting: forwardRef((/**
* @return {?}
*/
() => ButtonRadioGroupDirective)),
multi: true
};
/**
* A group of radio buttons.
* A value of a selected button is bound to a variable specified via ngModel.
*/
class ButtonRadioGroupDirective {
/**
* @param {?} cdr
*/
constructor(cdr) {
this.cdr = cdr;
this.onChange = Function.prototype;
this.onTouched = Function.prototype;
}
/**
* @return {?}
*/
get value() {
return this._value;
}
/**
* @param {?} value
* @return {?}
*/
set value(value) {
this._value = value;
}
/**
* @param {?} value
* @return {?}
*/
writeValue(value) {
this._value = value;
this.cdr.markForCheck();
}
/**
* @param {?} fn
* @return {?}
*/
registerOnChange(fn) {
this.onChange = fn;
}
/**
* @param {?} fn
* @return {?}
*/
registerOnTouched(fn) {
this.onTouched = fn;
}
}
ButtonRadioGroupDirective.decorators = [
{ type: Directive, args: [{
selector: '[btnRadioGroup]',
providers: [RADIO_CONTROL_VALUE_ACCESSOR]
},] }
];
/** @nocollapse */
ButtonRadioGroupDirective.ctorParameters = () => [
{ type: ChangeDetectorRef }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
const RADIO_CONTROL_VALUE_ACCESSOR$1 = {
provide: NG_VALUE_ACCESSOR,
/* tslint:disable-next-line: no-use-before-declare */
useExisting: forwardRef((/**
* @return {?}
*/
() => ButtonRadioDirective)),
multi: true
};
/**
* Create radio buttons or groups of buttons.
* A value of a selected button is bound to a variable specified via ngModel.
*/
class ButtonRadioDirective {
/**
* @param {?} el
* @param {?} cdr
* @param {?} group
* @param {?} renderer
*/
constructor(el, cdr, group, renderer) {
this.el = el;
this.cdr = cdr;
this.group = group;
this.renderer = renderer;
this.onChange = Function.prototype;
this.onTouched = Function.prototype;
}
/**
* Current value of radio component or group
* @return {?}
*/
get value() {
return this.group ? this.group.value : this._value;
}
/**
* @param {?} value
* @return {?}
*/
set value(value) {
if (this.group) {
this.group.value = value;
return;
}
this._value = value;
}
/**
* If `true` — radio button is disabled
* @return {?}
*/
get disabled() {
return this._disabled;
}
/**
* @param {?} disabled
* @return {?}
*/
set disabled(disabled) {
this._disabled = disabled;
this.setDisabledState(disabled);
}
/**
* @return {?}
*/
get isActive() {
return this.btnRadio === this.value;
}
/**
* @return {?}
*/
onClick() {
if (this.el.nativeElement.attributes.disabled || !this.uncheckable && this.btnRadio === this.value) {
return;
}
this.value = this.uncheckable && this.btnRadio === this.value ? undefined : this.btnRadio;
this._onChange(this.value);
}
/**
* @return {?}
*/
ngOnInit() {
this.uncheckable = typeof this.uncheckable !== 'undefined';
}
/**
* @return {?}
*/
onBlur() {
this.onTouched();
}
/**
* @param {?} value
* @return {?}
*/
_onChange(value) {
if (this.group) {
this.group.onTouched();
this.group.onChange(value);
return;
}
this.onTouched();
this.onChange(value);
}
// ControlValueAccessor
// model -> view
/**
* @param {?} value
* @return {?}
*/
writeValue(value) {
this.value = value;
this.cdr.markForCheck();
}
/**
* @param {?} fn
* @return {?}
*/
registerOnChange(fn) {
this.onChange = fn;
}
/**
* @param {?} fn
* @return {?}
*/
registerOnTouched(fn) {
this.onTouched = fn;
}
/**
* @param {?} disabled
* @return {?}
*/
setDisabledState(disabled) {
if (disabled) {
this.renderer.setAttribute(this.el.nativeElement, 'disabled', 'disabled');
return;
}
this.renderer.removeAttribute(this.el.nativeElement, 'disabled');
}
}
ButtonRadioDirective.decorators = [
{ type: Directive, args: [{
selector: '[btnRadio]',
providers: [RADIO_CONTROL_VALUE_ACCESSOR$1]
},] }
];
/** @nocollapse */
ButtonRadioDirective.ctorParameters = () => [
{ type: ElementRef },
{ type: ChangeDetectorRef },
{ type: ButtonRadioGroupDirective, decorators: [{ type: Optional }] },
{ type: Renderer2 }
];
ButtonRadioDirective.propDecorators = {
btnRadio: [{ type: Input }],
uncheckable: [{ type: Input }],
value: [{ type: Input }],
disabled: [{ type: Input }],
isActive: [{ type: HostBinding, args: ['class.active',] }, { type: HostBinding, args: ['attr.aria-pressed',] }],
onClick: [{ type: HostListener, args: ['click',] }]
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class ButtonsModule {
/**
* @return {?}
*/
static forRoot() {
return { ngModule: ButtonsModule, providers: [] };
}
}
ButtonsModule.decorators = [
{ type: NgModule, args: [{
declarations: [ButtonCheckboxDirective, ButtonRadioDirective, ButtonRadioGroupDirective],
exports: [ButtonCheckboxDirective, ButtonRadioDirective, ButtonRadioGroupDirective]
},] }
];
export { ButtonCheckboxDirective, ButtonRadioDirective, ButtonRadioGroupDirective, ButtonsModule, CHECKBOX_CONTROL_VALUE_ACCESSOR as ɵa, RADIO_CONTROL_VALUE_ACCESSOR as ɵb, RADIO_CONTROL_VALUE_ACCESSOR$1 as ɵc };
//# sourceMappingURL=ngx-bootstrap-buttons.js.map