@dooboostore/dom-render
Version:
html view template engine
203 lines • 6.22 kB
JavaScript
// export interface Valid<T = any, E = Element> {
// valid(value?: T, target?: E, event?: Event): boolean;
// }
export class Validator {
constructor(_value, target, event, autoValid = true, autoValidAction = true) {
this._value = _value;
this.setTarget(target);
this.setEvent(event);
this.setAutoValid(autoValid);
this.setAutoValidAction(autoValidAction);
}
getValidAction() {
return this._validAction;
}
setValidAction(value) {
this._validAction = value;
return this;
}
getAutoValid() {
return this._autoValid;
}
setAutoValid(autoValid) {
this._autoValid = autoValid;
return this;
}
getAutoValidAction() {
return this._autoValidAction;
}
setAutoValidAction(autoValid) {
this._autoValidAction = autoValid;
return this;
}
getEvent() {
return this._event;
}
setEvent(event) {
if (event) {
this._event = this.domRenderFinal(event);
}
return this;
}
getTarget() {
return this._target;
}
targetFocus() {
var _a, _b;
(_b = (_a = this._target) === null || _a === void 0 ? void 0 : _a.focus) === null || _b === void 0 ? void 0 : _b.call(_a);
}
targetReset() {
var _a, _b;
(_b = (_a = this._target) === null || _a === void 0 ? void 0 : _a.reset) === null || _b === void 0 ? void 0 : _b.call(_a);
}
targetDispatchEvent(event) {
var _a;
return (_a = this._target) === null || _a === void 0 ? void 0 : _a.dispatchEvent(event);
}
setTarget(target) {
if (target) {
this._target = this.domRenderFinal(target);
}
return this;
}
domRenderFinal(obj) {
obj._DomRender_isFinal = true;
return obj;
}
get value() {
var _a;
if (this._value === undefined || this._value === null) {
this._value = (_a = this.getTarget()) === null || _a === void 0 ? void 0 : _a.value;
}
return this._value;
}
set value(value) {
// console.log('---?set?', value, this)
this._value = value;
this.tickValue(value);
}
tickValue(value) {
this.changeValue(value);
const target = this.getTarget();
if (target && (target === null || target === void 0 ? void 0 : target.value) !== undefined && (target === null || target === void 0 ? void 0 : target.value) !== null) {
try {
target.value = this._value;
}
catch (e) {
console.error('set value function is blocked ');
}
}
if (this.getAutoValidAction()) {
this.validAction();
}
else if (this.getAutoValid()) {
this.valid();
}
}
set(value, target, event) {
this.setTarget(target);
this.setEvent(event);
this.value = value;
}
changeValue(value) {
}
get checked() {
var _a, _b;
return (_b = (_a = this.getTarget()) === null || _a === void 0 ? void 0 : _a.checked) !== null && _b !== void 0 ? _b : false;
}
set checked(checked) {
const target = this.getTarget();
if (target) {
target.checked = checked;
}
}
get selectedIndex() {
var _a, _b;
return (_b = (_a = this.getTarget()) === null || _a === void 0 ? void 0 : _a.selectedIndex) !== null && _b !== void 0 ? _b : -1;
}
set selectedIndex(selectedIndex) {
const target = this.getTarget();
if (target) {
target.selectedIndex = selectedIndex;
}
}
querySelector(selector) {
var _a;
return (_a = this.getTarget()) === null || _a === void 0 ? void 0 : _a.querySelector(selector);
}
querySelectorALL(selector) {
var _a;
return (_a = this.getTarget()) === null || _a === void 0 ? void 0 : _a.querySelectorAll(selector);
}
validAction() {
var _a;
const valid = this.valid();
(_a = this.getValidAction()) === null || _a === void 0 ? void 0 : _a(valid, this.value, this.getTarget(), this.getEvent());
return valid;
}
inValid() {
return !this.valid();
}
;
allValid() {
return this.valid() && this.childInValid();
}
allValidAction() {
return this.validAction() && this.childInValidAction();
}
allInValid() {
return !this.allValid();
}
allInValidAction() {
return !this.allValidAction();
}
childValid() {
return !this.childInValid();
}
childValue() {
const data = {};
this.childValidators().filter(([k, v]) => {
data[k] = v.value;
});
return data;
}
childValidAction() {
return !this.childInValidAction();
}
childInValid() {
const inValid = this.childValidators().filter(([k, v]) => !v.valid());
return inValid.length > 0;
}
childInValidValidator() {
const inValid = this.childValidators().filter(([k, v]) => !v.valid());
return inValid;
}
childInValidAction() {
const inValid = this.childValidators().filter(([k, v]) => !v.validAction());
return inValid.length > 0;
}
childValidator(name) {
var _a;
return (_a = Object.entries(this).find(([k, v]) => (k === name && (v instanceof Validator)))) === null || _a === void 0 ? void 0 : _a[1];
}
childValidators() {
return Object.entries(this).filter(([k, v]) => (v instanceof Validator));
}
childValidValidator() {
return this.childValidators().filter(it => it[1].valid());
}
syncValue() {
var _a;
this.value = (_a = this.getTarget()) === null || _a === void 0 ? void 0 : _a.value;
}
allSyncValue() {
this.childValidators().forEach(([k, e]) => {
e.syncValue();
});
}
get length() {
var _a, _b;
return (_b = (_a = this.value) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
}
}
//# sourceMappingURL=Validator.js.map