UNPKG

ngx-bootstrap

Version:
730 lines (722 loc) 18.9 kB
import { forwardRef, Directive, Input, HostBinding, HostListener, ElementRef, ChangeDetectorRef, Renderer2, Optional, Inject, ContentChildren, 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',] }] }; if (false) { /** * Truthy value, will be set to ngModel * @type {?} */ ButtonCheckboxDirective.prototype.btnCheckboxTrue; /** * Falsy value, will be set to ngModel * @type {?} */ ButtonCheckboxDirective.prototype.btnCheckboxFalse; /** @type {?} */ ButtonCheckboxDirective.prototype.state; /** * @type {?} * @protected */ ButtonCheckboxDirective.prototype.value; /** * @type {?} * @protected */ ButtonCheckboxDirective.prototype.isDisabled; /** * @type {?} * @protected */ ButtonCheckboxDirective.prototype.onChange; /** * @type {?} * @protected */ ButtonCheckboxDirective.prototype.onTouched; } /** * @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 {?} */ () => 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 {?} renderer * @param {?} group */ constructor(el, cdr, renderer, group) { this.el = el; this.cdr = cdr; this.renderer = renderer; this.group = group; this.onChange = Function.prototype; this.onTouched = Function.prototype; this.role = 'radio'; this._hasFocus = false; } /** * 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.setDisabledState(disabled); } /** * @return {?} */ get controlOrGroupDisabled() { return this.disabled || (this.group && this.group.disabled) ? true : undefined; } /** * @return {?} */ get hasDisabledClass() { // Although the radio is disabled the active radio should still stand out. // The disabled class will prevent this so don't add it on the active radio return this.controlOrGroupDisabled && !this.isActive; } /** * @return {?} */ get isActive() { return this.btnRadio === this.value; } /** * @return {?} */ get tabindex() { if (this.controlOrGroupDisabled) { // Disabled radio buttons should not receive focus return undefined; } else if (this.isActive || this.group == null) { return 0; } else { return -1; } } /** * @return {?} */ get hasFocus() { return this._hasFocus; } /** * @return {?} */ toggleIfAllowed() { if (!this.canToggle()) { return; } this.value = this.uncheckable && this.btnRadio === this.value ? undefined : this.btnRadio; this._onChange(this.value); } /** * @param {?} event * @return {?} */ onSpacePressed(event) { this.toggleIfAllowed(); event.preventDefault(); } /** * @return {?} */ focus() { this.el.nativeElement.focus(); } /** * @return {?} */ onFocus() { this._hasFocus = true; } /** * @return {?} */ onBlur() { this._hasFocus = false; this.onTouched(); } /** * @return {?} */ canToggle() { return !this.controlOrGroupDisabled && (this.uncheckable || this.btnRadio !== this.value); } /** * @return {?} */ ngOnInit() { this.uncheckable = typeof this.uncheckable !== 'undefined'; } /** * @param {?} value * @return {?} */ _onChange(value) { if (this.group) { this.group.value = 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) { this._disabled = 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] },] } ]; /** @nocollapse */ ButtonRadioDirective.ctorParameters = () => [ { type: ElementRef }, { type: ChangeDetectorRef }, { type: Renderer2 }, { type: ButtonRadioGroupDirective, decorators: [{ type: Optional }, { type: Inject, args: [forwardRef((/** * @return {?} */ () => ButtonRadioGroupDirective)),] }] } ]; ButtonRadioDirective.propDecorators = { btnRadio: [{ type: Input }], uncheckable: [{ type: Input }], value: [{ type: Input }], disabled: [{ type: Input }], controlOrGroupDisabled: [{ type: HostBinding, args: ['attr.aria-disabled',] }], hasDisabledClass: [{ type: HostBinding, args: ['class.disabled',] }], isActive: [{ type: HostBinding, args: ['class.active',] }, { type: HostBinding, args: ['attr.aria-checked',] }], role: [{ type: HostBinding, args: ['attr.role',] }], tabindex: [{ type: HostBinding, args: ['attr.tabindex',] }], toggleIfAllowed: [{ type: HostListener, args: ['click',] }], onSpacePressed: [{ type: HostListener, args: ['keydown.space', ['$event'],] }], onFocus: [{ type: HostListener, args: ['focus',] }], onBlur: [{ type: HostListener, args: ['blur',] }] }; if (false) { /** @type {?} */ ButtonRadioDirective.prototype.onChange; /** @type {?} */ ButtonRadioDirective.prototype.onTouched; /** * Radio button value, will be set to `ngModel` * @type {?} */ ButtonRadioDirective.prototype.btnRadio; /** * If `true` — radio button can be unchecked * @type {?} */ ButtonRadioDirective.prototype.uncheckable; /** @type {?} */ ButtonRadioDirective.prototype.role; /** * @type {?} * @private */ ButtonRadioDirective.prototype._value; /** * @type {?} * @private */ ButtonRadioDirective.prototype._disabled; /** * @type {?} * @private */ ButtonRadioDirective.prototype._hasFocus; /** * @type {?} * @private */ ButtonRadioDirective.prototype.el; /** * @type {?} * @private */ ButtonRadioDirective.prototype.cdr; /** * @type {?} * @private */ ButtonRadioDirective.prototype.renderer; /** * @type {?} * @private */ ButtonRadioDirective.prototype.group; } /** * @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 {?} */ () => 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; this.role = 'radiogroup'; } /** * @return {?} */ get value() { return this._value; } /** * @param {?} value * @return {?} */ set value(value) { this._value = value; this.onChange(value); } /** * @return {?} */ get tabindex() { if (this._disabled) { return null; } else { return 0; } } /** * @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 (this.radioButtons) { this._disabled = disabled; this.radioButtons.forEach((/** * @param {?} buttons * @return {?} */ buttons => { buttons.setDisabledState(disabled); })); this.cdr.markForCheck(); } } /** * @return {?} */ onFocus() { if (this._disabled) { return; } /** @type {?} */ const activeRadio = this.getActiveOrFocusedRadio(); if (activeRadio) { activeRadio.focus(); } else { /** @type {?} */ const firstEnabled = this.radioButtons.find((/** * @param {?} r * @return {?} */ r => !r.disabled)); if (firstEnabled) { firstEnabled.focus(); } } } /** * @return {?} */ onBlur() { if (this.onTouched) { this.onTouched(); } } /** * @param {?} event * @return {?} */ selectNext(event) { this.selectInDirection('next'); event.preventDefault(); } /** * @param {?} event * @return {?} */ selectPrevious(event) { this.selectInDirection('previous'); event.preventDefault(); } /** * @return {?} */ get disabled() { return this._disabled; } /** * @private * @param {?} direction * @return {?} */ selectInDirection(direction) { if (this._disabled) { return; } /** * @param {?} currentIndex * @param {?} buttonRadioDirectives * @return {?} */ function nextIndex(currentIndex, buttonRadioDirectives) { /** @type {?} */ const step = direction === 'next' ? 1 : -1; /** @type {?} */ let calcIndex = (currentIndex + step) % buttonRadioDirectives.length; if (calcIndex < 0) { calcIndex = buttonRadioDirectives.length - 1; } return calcIndex; } /** @type {?} */ const activeRadio = this.getActiveOrFocusedRadio(); if (activeRadio) { /** @type {?} */ const buttonRadioDirectives = this.radioButtons.toArray(); /** @type {?} */ const currentActiveIndex = buttonRadioDirectives.indexOf(activeRadio); for (let i = nextIndex(currentActiveIndex, buttonRadioDirectives); i !== currentActiveIndex; i = nextIndex(i, buttonRadioDirectives)) { if (buttonRadioDirectives[i].canToggle()) { buttonRadioDirectives[i].toggleIfAllowed(); buttonRadioDirectives[i].focus(); break; } } } } /** * @private * @return {?} */ getActiveOrFocusedRadio() { return this.radioButtons.find((/** * @param {?} button * @return {?} */ button => button.isActive)) || this.radioButtons.find((/** * @param {?} button * @return {?} */ button => button.hasFocus)); } } ButtonRadioGroupDirective.decorators = [ { type: Directive, args: [{ selector: '[btnRadioGroup]', providers: [RADIO_CONTROL_VALUE_ACCESSOR$1] },] } ]; /** @nocollapse */ ButtonRadioGroupDirective.ctorParameters = () => [ { type: ChangeDetectorRef } ]; ButtonRadioGroupDirective.propDecorators = { role: [{ type: HostBinding, args: ['attr.role',] }], radioButtons: [{ type: ContentChildren, args: [forwardRef((/** * @return {?} */ () => ButtonRadioDirective)),] }], tabindex: [{ type: HostBinding, args: ['attr.tabindex',] }], onFocus: [{ type: HostListener, args: ['focus',] }], onBlur: [{ type: HostListener, args: ['blur',] }], selectNext: [{ type: HostListener, args: ['keydown.ArrowRight', ['$event'],] }, { type: HostListener, args: ['keydown.ArrowDown', ['$event'],] }], selectPrevious: [{ type: HostListener, args: ['keydown.ArrowLeft', ['$event'],] }, { type: HostListener, args: ['keydown.ArrowUp', ['$event'],] }] }; if (false) { /** @type {?} */ ButtonRadioGroupDirective.prototype.onChange; /** @type {?} */ ButtonRadioGroupDirective.prototype.onTouched; /** @type {?} */ ButtonRadioGroupDirective.prototype.role; /** @type {?} */ ButtonRadioGroupDirective.prototype.radioButtons; /** * @type {?} * @private */ ButtonRadioGroupDirective.prototype._value; /** * @type {?} * @private */ ButtonRadioGroupDirective.prototype._disabled; /** * @type {?} * @private */ ButtonRadioGroupDirective.prototype.cdr; } /** * @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] },] } ]; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { ButtonCheckboxDirective, ButtonRadioDirective, ButtonRadioGroupDirective, ButtonsModule, CHECKBOX_CONTROL_VALUE_ACCESSOR as ɵa, RADIO_CONTROL_VALUE_ACCESSOR$1 as ɵb, RADIO_CONTROL_VALUE_ACCESSOR as ɵc }; //# sourceMappingURL=ngx-bootstrap-buttons.js.map