@syncfusion/ej2-ng-base
Version:
A common package of Essential JS 2 base Angular libraries, methods and class definitions
85 lines (84 loc) • 3.03 kB
JavaScript
import { getValue, setValue, isNullOrUndefined } from '@syncfusion/ej2-base';
/**
* Angular Form Base Module
*/
var FormBase = /** @class */ (function () {
function FormBase() {
}
FormBase.prototype.propagateChange = function (_) { return; };
FormBase.prototype.propagateTouch = function () { return; };
FormBase.prototype.localChange = function (e) {
var value = (e.checked === undefined ? e.value : e.checked);
if (value !== this.ngrValue && this.propagateChange !== undefined && value !== undefined) {
// Update angular from our control
this.propagateChange(value);
}
};
FormBase.prototype.registerOnChange = function (registerFunction) {
this.propagateChange = registerFunction;
};
FormBase.prototype.registerOnTouched = function (registerFunction) {
this.propagateTouch = registerFunction;
};
FormBase.prototype.twoWaySetter = function (newVal, prop) {
var oldVal = getValue(prop, this.properties);
var ele = this.inputElement || this.element;
if (oldVal === newVal &&
(ele.value === undefined || ele.value === '')) {
return;
}
this.saveChanges(prop, newVal, oldVal);
setValue(prop, (isNullOrUndefined(newVal) ? null : newVal), this.properties);
getValue(prop + 'Change', this).emit(newVal);
};
FormBase.prototype.ngAfterViewInit = function () {
var _this = this;
// Used setTimeout for template binding
// Refer Link: https://github.com/angular/angular/issues/6005
setTimeout(function () {
/* istanbul ignore else */
if (typeof window !== 'undefined') {
_this.appendTo(_this.element);
var ele = _this.inputElement || _this.element;
ele.addEventListener('focus', _this.ngOnFocus.bind(_this));
ele.addEventListener('blur', _this.ngOnBlur.bind(_this));
}
});
};
FormBase.prototype.setDisabledState = function (disabled) {
this.enabled = !disabled;
};
FormBase.prototype.writeValue = function (value) {
//update control value from angular
if (this.checked === undefined) {
this.value = value;
}
else {
if (typeof value === 'boolean') {
this.checked = value;
}
else {
this.checked = value === this.value;
}
}
if (value === null) {
return;
}
this.ngrValue = value;
};
FormBase.prototype.ngOnFocus = function (e) {
/* istanbul ignore else */
if (this.skipFromEvent !== true) {
this.focus.emit(e);
}
};
FormBase.prototype.ngOnBlur = function (e) {
this.propagateTouch();
/* istanbul ignore else */
if (this.skipFromEvent !== true) {
this.blur.emit(e);
}
};
return FormBase;
}());
export { FormBase };