simple-form-manager-v2
Version:
Simple form Manager V2
319 lines • 12.3 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var CFormManager = /** @class */ (function () {
function CFormManager(formValidationSchema) {
var _this = this;
if (formValidationSchema === void 0) { formValidationSchema = {}; }
this.setFieldValidationStatus = function (fieldName, validator, value) {
var fieldValidators = _this.scheme[fieldName];
if (fieldValidators) {
Object.keys(fieldValidators).forEach(function (v) {
if (v === validator || !validator) {
if (fieldValidators[v].isActive) {
fieldValidators[v].valid = (value === undefined) ? !fieldValidators[v].valid : value;
_this.validateField(fieldName, true);
}
}
});
}
};
this.isRunning = false;
this.iTickSpeed = 0;
this.isValid = false;
this.isDirty = false;
this.isTouched = false;
this.scheme = formValidationSchema;
this.fieldNameArray = this.buildFieldNameArray(formValidationSchema);
this.validateValidator();
this.fields = this.initializeFields(this.fieldNameArray);
}
Object.defineProperty(CFormManager.prototype, "fieldScheme", {
get: function () {
return this.scheme;
},
enumerable: false,
configurable: true
});
Object.defineProperty(CFormManager.prototype, "form", {
get: function () {
return {
dirty: this.isDirty,
valid: this.isValid,
touched: this.isTouched
};
},
enumerable: false,
configurable: true
});
Object.defineProperty(CFormManager.prototype, "data", {
get: function () {
var _this = this;
var fields = {};
this.fieldNameArray.forEach(function (fieldName) {
var _a;
fields = __assign(__assign({}, fields), (_a = {}, _a[fieldName] = _this.fields[fieldName].value, _a));
});
return fields;
},
enumerable: false,
configurable: true
});
Object.defineProperty(CFormManager.prototype, "running", {
get: function () {
return this.isRunning;
},
enumerable: false,
configurable: true
});
Object.defineProperty(CFormManager.prototype, "formSubmittable", {
get: function () {
return (this.form.dirty && this.form.valid);
},
enumerable: false,
configurable: true
});
Object.defineProperty(CFormManager.prototype, "all", {
get: function () {
return {
form: this.form,
running: this.running,
tickSpeed: this.iTickSpeed,
scheme: this.scheme,
fieldDetails: this.fields,
data: this.data
};
},
enumerable: false,
configurable: true
});
CFormManager.prototype.showFieldError = function (fieldName) {
var field = this.fields[fieldName];
return !field.valid && field.touched;
};
CFormManager.prototype.onBlur = function (fieldName) {
var _this = this;
setTimeout(function () {
_this.fields[fieldName].touched = true;
}, 50);
};
CFormManager.prototype.onUpdateValue = function (fieldName, value) {
this.fields[fieldName].value = value;
};
CFormManager.prototype.onUpdateObjectValue = function (fieldName, value) {
this.fields[fieldName].objectValue = value;
};
CFormManager.prototype.setTouched = function (fieldName, value) {
if (value === void 0) { value = true; }
this.fields[fieldName].touched = true;
};
CFormManager.prototype.setValue = function (fieldName, value) {
this.fields[fieldName].value = value;
};
CFormManager.prototype.setObjectValue = function (fieldName, value) {
this.fields[fieldName].objectValue = value;
};
CFormManager.prototype.setValues = function (values) {
var _this = this;
this.fieldNameArray.forEach(function (fieldName) {
if (fieldName in values) {
// @ts-ignore
_this.fields[fieldName].value = values[fieldName];
}
});
};
CFormManager.prototype.start = function (tickSpeed, preserve) {
var _this = this;
if (tickSpeed === void 0) { tickSpeed = 25; }
if (preserve === void 0) { preserve = false; }
this.iTickSpeed = tickSpeed;
this.isRunning = true;
this.fieldNameArray.forEach(function (fieldName) {
if (!preserve) {
_this.fields[fieldName].originalValue = _this.fields[fieldName].value;
}
});
this.valuePoll = setInterval(function () {
_this.fieldNameArray.forEach(function (fieldName) {
var field = _this.fields[fieldName];
field.dirty = (field.originalValue !== field.value);
if (!field.manualOverride) {
_this.validateField(fieldName, false);
}
});
_this.updateFormStatus();
}, tickSpeed);
};
CFormManager.prototype.stop = function () {
this.isRunning = false;
this.iTickSpeed = 0;
clearInterval(this.valuePoll);
};
CFormManager.prototype.setFieldStatus = function (fieldName, manualOverride, isValid, errorMsg) {
this.validateField(fieldName, false);
var field = this.fields[fieldName];
var isSame = (field.isValid === isValid);
field.manualOverride = manualOverride;
if (!isSame && field.valid) {
field.valid = isValid;
field.errorMessage = errorMsg;
this.updateFormStatus();
}
};
CFormManager.prototype.toggleValidationNode = function (fieldName, validator, value) {
if (validator === void 0) { validator = undefined; }
if (value === void 0) { value = undefined; }
var fieldValidators = this.scheme[fieldName];
if (fieldValidators) {
Object.keys(fieldValidators).forEach(function (v) {
if (v === validator || !validator) {
fieldValidators[v].isActive = (value === undefined) ? !fieldValidators[v].isActive : value;
}
});
}
this.validateField(fieldName, true);
};
CFormManager.prototype.restoreForm = function () {
var _this = this;
var speed = this.iTickSpeed;
var running = this.running;
if (running) {
this.stop();
}
this.fieldNameArray.forEach(function (fieldName) {
_this.fields[fieldName].dirty = false;
_this.fields[fieldName].touched = false;
_this.fields[fieldName].value = _this.fields[fieldName].originalValue;
});
this.updateFormStatus();
if (running) {
this.start(speed);
}
};
CFormManager.prototype.resetForm = function () {
var _this = this;
var speed = this.iTickSpeed;
var running = this.running;
if (running) {
this.stop();
}
this.fieldNameArray.forEach(function (fieldName) {
_this.fields[fieldName].dirty = false;
_this.fields[fieldName].touched = false;
_this.fields[fieldName].originalValue = _this.fields[fieldName].value;
});
this.updateFormStatus();
if (running) {
this.start(speed);
}
};
CFormManager.prototype.resetField = function (fieldName) {
this.fields[fieldName].dirty = false;
this.fields[fieldName].touched = false;
this.fields[fieldName].originalValue = this.fields[fieldName].value;
};
CFormManager.prototype.setValidator = function (fieldName, validator, validatorFunc) {
var field = this.scheme[fieldName];
if (field) {
field[validator].validator = validatorFunc;
}
};
// ********************
// Private Functions
// ********************
CFormManager.prototype.updateFormStatus = function () {
var _this = this;
var isValid = true;
var isDirty = false;
var isTouched = false;
this.fieldNameArray.forEach(function (fieldName) {
if (!_this.fields[fieldName].valid) {
isValid = false;
}
if (_this.fields[fieldName].dirty) {
isDirty = true;
}
if (_this.fields[fieldName].touched) {
isTouched = true;
}
});
this.isValid = isValid;
this.isDirty = isDirty;
this.isTouched = isTouched;
};
CFormManager.prototype.validateField = function (fieldName, updateFormStatus) {
var _this = this;
if (updateFormStatus === void 0) { updateFormStatus = true; }
var fieldValidators = this.scheme[fieldName];
var isValid = true;
var errorMessage = '';
if (fieldValidators) {
Object.keys(fieldValidators).forEach(function (e) {
if (fieldValidators[e].isActive) {
var result = (fieldValidators[e].validator === null) ? fieldValidators[e].valid : fieldValidators[e].validator(_this.fields[fieldName].value);
if (!result) {
isValid = false;
if (fieldValidators[e].errorMessage) {
errorMessage = fieldValidators[e].errorMessage;
}
}
}
});
}
this.fields[fieldName].valid = isValid;
this.fields[fieldName].errorMessage = errorMessage;
if (updateFormStatus) {
this.updateFormStatus();
}
};
CFormManager.prototype.buildFieldNameArray = function (formValidationSchema) {
if (formValidationSchema === void 0) { formValidationSchema = {}; }
var fieldArray = [];
Object.keys(formValidationSchema).forEach(function (fieldName) {
fieldArray.push(fieldName);
});
return fieldArray;
};
CFormManager.prototype.validateValidator = function () {
var schema = this.scheme;
this.fieldNameArray.forEach(function (fieldName) {
var fieldValidators = schema[fieldName];
if (fieldValidators) {
Object.keys(fieldValidators).forEach(function (e) {
var validatorProperties = fieldValidators[e];
if (validatorProperties.isActive === undefined) {
validatorProperties.isActive = true;
}
});
}
});
};
CFormManager.prototype.initializeFields = function (fieldArray) {
var fields = {};
fieldArray.forEach(function (fieldName) {
var _a;
fields = __assign(__assign({}, fields), (_a = {}, _a[fieldName] = {
manualOverride: false,
dirty: false,
touched: false,
value: null,
objectValue: null,
originalValue: null,
valid: true,
errorMessage: ''
}, _a));
});
return fields;
};
return CFormManager;
}());
export default CFormManager;
//# sourceMappingURL=CFormManager.js.map