UNPKG

sk-react

Version:
452 lines (405 loc) 13.2 kB
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } import { Proxy0, SK } from 'sk-js'; import Validator from './Validator'; var Model = /*#__PURE__*/ function () { /** * freeObject = { * m:{"xxx":"most form"}, * v:{"yyy":"most table/tree"}, * c:{"zzz":"button state"} * } * * @param freeObject plain object * @param validator */ function Model() { var freeObject = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var validator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Validator(); _classCallCheck(this, Model); this.errors = {}; this.idListeners = {}; this.monitors = {}; this.regListeners = {}; this.freeObject = freeObject; this.validator = validator; this.addAllValidatorMonitor(); } /** * {a:{b:true,c:false}} => a.b * @param prefix * @param modelIds * @param object */ _createClass(Model, [{ key: "addIdChangedListener", /** * * @param id * @param listener please bind context */ value: function addIdChangedListener(id, listener) { this.addIdListener(id, Model.EvtType.Changed, listener); } /** * * @param id * @param listener please bind context */ }, { key: "addIdErroredListener", value: function addIdErroredListener(id, listener) { this.addIdListener(id, Model.EvtType.Errored, listener); } /** * * @param id * @param type * @param listener please bind context */ }, { key: "addIdListener", value: function addIdListener(id, type, listener) { if (id) { if (!this.idListeners[type]) { this.idListeners[type] = {}; } if (!this.idListeners[type][id]) { this.idListeners[type][id] = []; } if (this.idListeners[type][id].indexOf(listener) < 0) { this.idListeners[type][id].push(listener); } } } /** * * @param reg * @param listener please bind context */ }, { key: "addRegChangedListener", value: function addRegChangedListener(reg, listener) { this.addRegListener(reg, Model.EvtType.Changed, listener); } /** * * @param reg * @param listener please bind context */ }, { key: "addRegErroredListener", value: function addRegErroredListener(reg, listener) { this.addRegListener(reg, Model.EvtType.Errored, listener); } /** * * @param reg * @param type * @param listener please bind context */ }, { key: "addRegListener", value: function addRegListener(reg, type, listener) { if (reg) { if (!this.regListeners[type]) { this.regListeners[type] = {}; } if (!this.regListeners[type][reg]) { this.regListeners[type][reg] = []; } if (this.regListeners[type][reg].indexOf(listener) < 0) { this.regListeners[type][reg].push(listener); } } } }, { key: "fireChangedEvent", value: function fireChangedEvent(id, old, current) { this.fireEvent({ model: this, id: id, old: old, current: current, type: Model.EvtType.Changed }); } }, { key: "fireErroredEvent", value: function fireErroredEvent(id, old, current) { this.fireEvent({ model: this, id: id, old: old, current: current, type: Model.EvtType.Errored }); } }, { key: "fireEvent", value: function fireEvent(evt) { var matchedListeners = []; var idListeners = this.idListeners[evt.type] ? this.idListeners[evt.type] : {}; matchedListeners.push.apply(matchedListeners, _toConsumableArray(idListeners[evt.id] ? idListeners[evt.id] : [])); var regListeners = this.regListeners[evt.type] ? this.regListeners[evt.type] : {}; Object.keys(regListeners).forEach(function (reg) { matchedListeners.push.apply(matchedListeners, _toConsumableArray(evt.id.match(reg) ? regListeners[reg] : [])); }); matchedListeners.forEach(function (listener) { listener(evt); }); return this; } }, { key: "getAllErrors", value: function getAllErrors() { return this.errors; } }, { key: "getErrors", value: function getErrors(id) { return SK.s4o(this.errors[id]); } }, { key: "setErrors", value: function setErrors() { var errors = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; this.errors = errors; return this; } }, { key: "getFreeObject", value: function getFreeObject() { return this.freeObject; } }, { key: "setFreeObject", value: function setFreeObject() { var freeObject = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; this.freeObject = freeObject; return this; } }, { key: "getValidator", value: function getValidator() { return this.validator; } }, { key: "hasErrors", value: function hasErrors() { var _this = this; var rtn = false; Object.keys(this.errors).forEach(function (modelId) { rtn = rtn || !Proxy0._.isEmpty(_this.errors[modelId]); }); return rtn; } }, { key: "rmvIdChangedListener", value: function rmvIdChangedListener(id, listener) { this.rmvIdListener(id, Model.EvtType.Changed, listener); } }, { key: "rmvIdErroredListener", value: function rmvIdErroredListener(id, listener) { this.rmvIdListener(id, Model.EvtType.Errored, listener); } }, { key: "rmvIdListener", value: function rmvIdListener(id, type, listener) { if (id) { if (!this.idListeners[type]) { this.idListeners[type] = {}; } if (!this.idListeners[type][id]) { this.idListeners[type][id] = []; } if (this.idListeners[type][id].indexOf(listener) >= 0) { this.idListeners[type][id].skRmv(listener); } } } }, { key: "rmvRegChangedListener", value: function rmvRegChangedListener(reg, listener) { this.rmvRegListener(reg, Model.EvtType.Changed, listener); } }, { key: "rmvRegErroredListener", value: function rmvRegErroredListener(reg, listener) { this.rmvRegListener(reg, Model.EvtType.Errored, listener); } }, { key: "rmvRegListener", value: function rmvRegListener(reg, type, listener) { if (reg) { if (!this.regListeners[type]) { this.regListeners[type] = {}; } if (!this.regListeners[type][reg]) { this.regListeners[type][reg] = []; } if (this.regListeners[type][reg].indexOf(listener) >= 0) { this.regListeners[type][reg].skRmv(listener); } } } }, { key: "skVal", value: function skVal(id, value) { var oldValue = this.freeObject.skVal(id); if (arguments.length > 1) { if (oldValue !== value) { this.freeObject.skVal(id, value); this.fireChangedEvent(id, oldValue, value); } return this; } else { return oldValue; } } //validator begin }, { key: "addAllValidatorMonitor", value: function addAllValidatorMonitor() { var _this2 = this; var tmpModelIds = this.getValidator().getModelIds(); Object.keys(tmpModelIds).forEach(function (key) { _this2.addValidatorMonitor(key, tmpModelIds[key]); }); } }, { key: "addValidatorMonitor", value: function addValidatorMonitor(modelId, config) { var _this3 = this; if (!this.monitors[modelId]) { this.errors[modelId] = {}; this.monitors[modelId] = this.validate.bind(modelId); this.addIdChangedListener(modelId, this.monitors[modelId]); } Object.keys(config).filter(function (key) { return key === Validator.PROP_DEPS; }).forEach(function (key1) { Model.parseSao(config[key1]).forEach(function (key2) { if (Proxy0._.isRegExp(key2)) { _this3.addRegChangedListener(key2, _this3.monitors[modelId]); } else { _this3.addIdChangedListener(key2, _this3.monitors[modelId]); } }); }); } }, { key: "execValidate", value: function execValidate(ruleKey, modelId, ruleFunc, model, setting) { var tmpRtn = ruleFunc(model, model.skVal(modelId), setting); if (!this.errors[modelId]) { this.errors[modelId] = {}; } if (!Proxy0._.isBoolean(tmpRtn)) { //true or message this.errors[modelId][ruleKey] = tmpRtn; } else { delete this.errors[modelId][ruleKey]; } this.fireErroredEvent(modelId, model.skVal(modelId), model.skVal(modelId)); } }, { key: "rmvValidatorMonitor", value: function rmvValidatorMonitor(modelId, config) { var _this4 = this; Object.keys(config).filter(function (key) { return key === Validator.PROP_DEPS; }).forEach(function (key1) { Model.parseSao(config[key1]).forEach(function (key2) { if (Proxy0._.isRegExp(key2)) { _this4.rmvRegChangedListener(key2, _this4.monitors[modelId]); } else { _this4.rmvIdChangedListener(key2, _this4.monitors[modelId]); } }); }); } }, { key: "validate", value: function validate(evt) { var tmpModelId = this; var tmpModel = evt.model; var tmpConfig = evt.model.getValidator().getModelIds()[tmpModelId]; if (tmpConfig && Proxy0._.isObject(tmpConfig)) { Object.keys(tmpConfig).filter(function (key) { return key !== Validator.PROP_DEPS; }).forEach(function (key) { if (key === Validator.PROP_FUNC) { tmpModel.execValidate(key, tmpModelId, tmpConfig[key], tmpModel, tmpConfig[key]); } else if (!Proxy0._.isFunction(tmpConfig[key].exec) || tmpConfig[key].exec(tmpModel, tmpModelId, key)) { //rule.exec var tmpRule = tmpModel.getValidator().getRules()[key]; if (Proxy0._.isFunction(tmpRule)) { tmpModel.execValidate(key, tmpModelId, tmpRule, tmpModel, tmpConfig[key]); } } }); } } }, { key: "validateAll", value: function validateAll() { var _this5 = this; var configs = this.getValidator().getModelIds(); Object.keys(configs).forEach(function (modelId) { _this5.validate.call(modelId, { model: _this5 }); }); } //validator end }], [{ key: "object2ModelIds", value: function object2ModelIds(prefix) { var modelIds = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; var object = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; Object.keys(object).forEach(function (key) { var tmpKey = prefix + (String(prefix).skBlank() ? SK.STR_EMPTY : SK.CHAR_DOT) + key; var tmpVal = object[key]; if (Proxy0._.isPlainObject(tmpVal)) { Model.object2ModelIds(tmpKey, modelIds, tmpVal); } else if (SK.s4b(tmpVal) && tmpVal) { modelIds.push(tmpKey); } }); } /** * @param sao is string[reg], array[string] or object * @returns {Array} */ }, { key: "parseSao", value: function parseSao(sao) { var rtn = []; if (sao) { if (Proxy0._.isPlainObject(sao)) { Model.object2ModelIds(SK.STR_EMPTY, rtn, sao); } else if (Proxy0._.isArray(sao)) { rtn = rtn.concat(sao); } else if (Proxy0._.isString(sao)) { rtn.push(sao); } } return rtn; } }]); return Model; }(); Model.EvtType = { Changed: 'Changed', Errored: 'Errored' }; Model.PROP_SK = 'skModel'; Model.PROP_SYS = 'skSysModel'; export { Model as default };