ngx-bootstrap
Version:
Native Angular Bootstrap Components
134 lines • 5.75 kB
JavaScript
import { ChangeDetectorRef, Directive, ElementRef, forwardRef, Host, Renderer2 } from '@angular/core';
import { NG_VALIDATORS, NG_VALUE_ACCESSOR } from '@angular/forms';
import { parseDate } from '../chronos/create/local';
import { formatDate } from '../chronos/format';
import { getLocale } from '../chronos/locale/locales';
import { isAfter, isBefore } from '../chronos/utils/date-compare';
import { isDate, isDateValid } from '../chronos/utils/type-checks';
import { BsDatepickerDirective } from './bs-datepicker.component';
import { BsLocaleService } from './bs-locale.service';
var BS_DATEPICKER_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
// tslint:disable-next-line
useExisting: forwardRef(function () { return BsDatepickerInputDirective; }),
multi: true
};
var BS_DATEPICKER_VALIDATOR = {
provide: NG_VALIDATORS,
useExisting: forwardRef(function () { return BsDatepickerInputDirective; }),
multi: true
};
var BsDatepickerInputDirective = /** @class */ (function () {
function BsDatepickerInputDirective(_picker, _localeService, _renderer, _elRef, changeDetection) {
var _this = this;
this._picker = _picker;
this._localeService = _localeService;
this._renderer = _renderer;
this._elRef = _elRef;
this.changeDetection = changeDetection;
this._onChange = Function.prototype;
this._onTouched = Function.prototype;
this._validatorChange = Function.prototype;
// update input value on datepicker value update
this._picker.bsValueChange.subscribe(function (value) {
_this._setInputValue(value);
if (_this._value !== value) {
_this._value = value;
_this._onChange(value);
_this._onTouched();
}
_this.changeDetection.markForCheck();
});
// update input value on locale change
this._localeService.localeChange.subscribe(function () {
_this._setInputValue(_this._value);
});
}
BsDatepickerInputDirective.prototype._setInputValue = function (value) {
var initialDate = !value ? ''
: formatDate(value, this._picker._config.dateInputFormat, this._localeService.currentLocale);
this._renderer.setProperty(this._elRef.nativeElement, 'value', initialDate);
};
BsDatepickerInputDirective.prototype.onChange = function (event) {
this.writeValue(event.target.value);
this._onChange(this._value);
this._onTouched();
};
BsDatepickerInputDirective.prototype.validate = function (c) {
var _value = c.value;
if (_value === null || _value === undefined || _value === '') {
return null;
}
if (isDate(_value)) {
var _isDateValid = isDateValid(_value);
if (!_isDateValid) {
return { bsDate: { invalid: _value } };
}
if (this._picker && this._picker.minDate && isBefore(_value, this._picker.minDate, 'date')) {
return { bsDate: { minDate: this._picker.minDate } };
}
if (this._picker && this._picker.maxDate && isAfter(_value, this._picker.maxDate, 'date')) {
return { bsDate: { maxDate: this._picker.maxDate } };
}
}
};
BsDatepickerInputDirective.prototype.registerOnValidatorChange = function (fn) {
this._validatorChange = fn;
};
BsDatepickerInputDirective.prototype.writeValue = function (value) {
if (!value) {
this._value = null;
}
else {
var _localeKey = this._localeService.currentLocale;
var _locale = getLocale(_localeKey);
if (!_locale) {
throw new Error("Locale \"" + _localeKey + "\" is not defined, please add it with \"defineLocale(...)\"");
}
this._value = parseDate(value, this._picker._config.dateInputFormat, this._localeService.currentLocale);
}
this._picker.bsValue = this._value;
};
BsDatepickerInputDirective.prototype.setDisabledState = function (isDisabled) {
this._picker.isDisabled = isDisabled;
if (isDisabled) {
this._renderer.setAttribute(this._elRef.nativeElement, 'disabled', 'disabled');
return;
}
this._renderer.removeAttribute(this._elRef.nativeElement, 'disabled');
};
BsDatepickerInputDirective.prototype.registerOnChange = function (fn) {
this._onChange = fn;
};
BsDatepickerInputDirective.prototype.registerOnTouched = function (fn) {
this._onTouched = fn;
};
BsDatepickerInputDirective.prototype.onBlur = function () {
this._onTouched();
};
BsDatepickerInputDirective.prototype.hide = function () {
this._picker.hide();
};
BsDatepickerInputDirective.decorators = [
{ type: Directive, args: [{
selector: "input[bsDatepicker]",
host: {
'(change)': 'onChange($event)',
'(keyup.esc)': 'hide()',
'(blur)': 'onBlur()'
},
providers: [BS_DATEPICKER_VALUE_ACCESSOR, BS_DATEPICKER_VALIDATOR]
},] },
];
/** @nocollapse */
BsDatepickerInputDirective.ctorParameters = function () { return [
{ type: BsDatepickerDirective, decorators: [{ type: Host },] },
{ type: BsLocaleService, },
{ type: Renderer2, },
{ type: ElementRef, },
{ type: ChangeDetectorRef, },
]; };
return BsDatepickerInputDirective;
}());
export { BsDatepickerInputDirective };
//# sourceMappingURL=bs-datepicker-input.directive.js.map