jenesius-vue-form
Advanced tools
Comparing version 0.0.5 to 0.0.6
/*! | ||
* jenesius-vue-form v0.0.5 | ||
* jenesius-vue-form v0.0.6 | ||
* (c) 2022 Jenesius | ||
@@ -17,43 +17,2 @@ * @license MIT | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */ | ||
/* global Reflect, Promise */ | ||
var extendStatics = function(d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
function __extends(d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
} | ||
function __spreadArray(to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
} | ||
/** | ||
@@ -63,4 +22,3 @@ * @description Мержит два объекта, полностью не перезаписываю значения. | ||
* */ | ||
function mergeObjects(formValues, newValues, path) { | ||
if (path === void 0) { path = []; } | ||
function mergeObjects(formValues, newValues, path = []) { | ||
function set(formValues, v, path) { | ||
@@ -71,3 +29,3 @@ if (path.length === 0) { | ||
} | ||
var currentKey = path[0]; | ||
const currentKey = path[0]; | ||
if (path.length === 1) { | ||
@@ -79,3 +37,3 @@ formValues[currentKey] = v; | ||
formValues[currentKey] = {}; | ||
var currentValue = formValues[currentKey]; | ||
const currentValue = formValues[currentKey]; | ||
if (typeof currentValue !== 'object') { | ||
@@ -91,4 +49,4 @@ console.warn('Текущее значение не является объектом, продолжать рекурсивный спуск по объеут нельзя.'); | ||
else { | ||
for (var key in newValues) { | ||
mergeObjects(formValues, newValues[key], __spreadArray(__spreadArray([], path, true), [key], false)); | ||
for (const key in newValues) { | ||
mergeObjects(formValues, newValues[key], [...path, key]); | ||
} | ||
@@ -121,3 +79,3 @@ } | ||
// Поиск точки. Мульти имени | ||
var _index = name.indexOf('.'); | ||
const _index = name.indexOf('.'); | ||
// Если точка найдена. обрубаем первую часть и ищем рекурсивно далее | ||
@@ -130,3 +88,3 @@ if (_index > -1) { | ||
* */ | ||
var subName = name.substring(0, _index); // address (first name) | ||
const subName = name.substring(0, _index); // address (first name) | ||
if (subName in obj) | ||
@@ -139,64 +97,59 @@ return getPropFromObject(obj[subName], name.substr(_index + 1)); | ||
var FormErrors = /** @class */ (function (_super) { | ||
__extends(FormErrors, _super); | ||
function FormErrors(message, details) { | ||
var _this = _super.call(this) || this; | ||
_this.message = message; | ||
_this.details = details; | ||
return _this; | ||
class FormErrors extends Error { | ||
constructor(message, details) { | ||
super(); | ||
this.message = message; | ||
this.details = details; | ||
} | ||
FormErrors.RepeatDependingWithSameName = function (name, element) { | ||
return new FormErrors("The element with the name ".concat(name, " has already been subscribed to the form."), element); | ||
}; | ||
return FormErrors; | ||
}(Error)); | ||
static RepeatDependingWithSameName(name, element) { | ||
return new FormErrors(`The element with the name ${name} has already been subscribed to the form.`, element); | ||
} | ||
} | ||
var Form = /** @class */ (function (_super) { | ||
__extends(Form, _super); | ||
function Form(params) { | ||
var _this = _super.call(this) || this; | ||
class Form extends EventEmitter__default["default"] { | ||
constructor(params) { | ||
super(); | ||
/** | ||
* @description ComputedRef, true - when one of form's elements is changed. | ||
* */ | ||
_this.changed = vue.computed(function () { return true; }); | ||
this.changed = vue.computed(() => true); | ||
/** | ||
* @description Store all depend elements. | ||
* */ | ||
_this.dependElements = vue.reactive([]); | ||
_this.values = {}; | ||
_this.disabled = false; | ||
this.dependElements = vue.reactive([]); | ||
this.values = {}; | ||
this.disabled = false; | ||
/** | ||
* Methods and props for hide/show fields | ||
* */ | ||
_this.hidden = false; | ||
_this.hiddenFields = vue.reactive([]); | ||
this.hidden = false; | ||
this.hiddenFields = vue.reactive([]); | ||
if (params.name) | ||
_this.name = params.name; | ||
this.name = params.name; | ||
if (params.composition) { | ||
var parentForm = vue.inject(Form.PROVIDE_NAME); | ||
parentForm.depend(_this); | ||
const parentForm = vue.inject(Form.PROVIDE_NAME); | ||
parentForm.depend(this); | ||
} | ||
// Connection plugins to form. | ||
if (params.plugins) { | ||
params.plugins.forEach(function (p) { return p(_this); }); | ||
params.plugins.forEach(p => p(this)); | ||
} | ||
_this.reinitialization(); | ||
return _this; | ||
this.reinitialization(); | ||
} | ||
Form.getParentForm = function () { | ||
static getParentForm() { | ||
return vue.inject(Form.PROVIDE_NAME); | ||
}; | ||
Form.prototype.reinitialization = function () { | ||
} | ||
reinitialization() { | ||
vue.provide(Form.PROVIDE_NAME, this); | ||
}; | ||
Form.prototype.findDependence = function (name) { | ||
return this.dependElements.find(function (element) { return element.name === name; }); | ||
}; | ||
} | ||
findDependence(name) { | ||
return this.dependElements.find(element => element.name === name); | ||
} | ||
/** | ||
* @description Подписывает элемент на форму | ||
* */ | ||
Form.prototype.depend = function (element) { | ||
depend(element) { | ||
// Если элемент с таким именем уже был подписан на форму | ||
if (element.name && | ||
this.dependElements.find(function (depend) { return depend.name === element.name; })) | ||
this.dependElements.find(depend => depend.name === element.name)) | ||
throw FormErrors.RepeatDependingWithSameName(element.name, element); | ||
@@ -206,3 +159,3 @@ this.dependElements.push(vue.markRaw(element)); | ||
if (element.name) { | ||
var value = getPropFromObject(this.values, element.name); | ||
const value = getPropFromObject(this.values, element.name); | ||
// Нового значения у нас не найдено. | ||
@@ -230,5 +183,5 @@ if (value === undefined) | ||
*/ | ||
}; | ||
Form.prototype.restoreDependence = function (name) { | ||
var dep = this.findDependence(name); | ||
} | ||
restoreDependence(name) { | ||
const dep = this.findDependence(name); | ||
if (!dep) | ||
@@ -239,3 +192,3 @@ return undefined; | ||
return dep; | ||
}; | ||
} | ||
/** | ||
@@ -248,4 +201,4 @@ * Функция проходит по всем зависимым элементам и проверяет правила валидации | ||
* */ | ||
Form.prototype.validate = function () { | ||
return this.dependElements.reduce(function (acc, elem) { | ||
validate() { | ||
return this.dependElements.reduce((acc, elem) => { | ||
if (elem.validate) | ||
@@ -255,4 +208,4 @@ acc = elem.validate() && acc; | ||
}, true); | ||
}; | ||
Form.prototype.setValueItem = function (elementController, values) { | ||
} | ||
setValueItem(elementController, values) { | ||
if (elementController.setValue) { | ||
@@ -267,20 +220,19 @@ elementController.setValue(values); | ||
} | ||
}; | ||
Form.prototype.setValues = function (values) { | ||
var _this = this; | ||
} | ||
setValues(values) { | ||
mergeObjects(this.values, values); | ||
this.dependElements.forEach(function (controller) { | ||
var name = controller.name; // name of fields or form | ||
this.dependElements.forEach(controller => { | ||
const name = controller.name; // name of fields or form | ||
if (name) { | ||
var value = getPropFromObject(values, name); | ||
const value = getPropFromObject(values, name); | ||
// Нового значения у нас не найдено. | ||
if (value === undefined) | ||
return; | ||
_this.setValueItem(controller, value); | ||
this.setValueItem(controller, value); | ||
} | ||
}); | ||
}; | ||
Form.prototype.getValues = function () { | ||
var output = mergeObjects({}, this.values); | ||
this.dependElements.forEach(function (elem) { | ||
} | ||
getValues() { | ||
const output = mergeObjects({}, this.values); | ||
this.dependElements.forEach(elem => { | ||
if (elem.getValue) | ||
@@ -292,7 +244,7 @@ output[elem.name] = elem.getValue(); | ||
return output; | ||
}; | ||
} | ||
/** | ||
* @description Блокировка элементов формы | ||
* */ | ||
Form.prototype.disable = function (names) { | ||
disable(names) { | ||
if (!names) { | ||
@@ -305,4 +257,4 @@ this.setDisabled(true); | ||
this.disableDepends(names); | ||
}; | ||
Form.prototype.enable = function (names) { | ||
} | ||
enable(names) { | ||
if (!names) { | ||
@@ -315,10 +267,10 @@ this.setDisabled(false); | ||
this.enableDepends(names); | ||
}; | ||
Form.prototype.setDisabled = function (v) { | ||
} | ||
setDisabled(v) { | ||
this.disabled = v; | ||
v ? this.disableDepends() : this.enableDepends(); | ||
this.emit(Form.EVENT_DISABLED_UPDATE, v); | ||
}; | ||
Form.prototype.disableDepends = function (names) { | ||
this.dependElements.filter(function (v) { | ||
} | ||
disableDepends(names) { | ||
this.dependElements.filter(v => { | ||
if (!names) | ||
@@ -329,3 +281,3 @@ return true; | ||
return false; | ||
}).forEach(function (elem) { | ||
}).forEach(elem => { | ||
var _a; | ||
@@ -337,5 +289,5 @@ if (elem.disabled) | ||
}); | ||
}; | ||
Form.prototype.enableDepends = function (names) { | ||
this.dependElements.filter(function (v) { return !names || names.includes(v.name); }).forEach(function (elem) { | ||
} | ||
enableDepends(names) { | ||
this.dependElements.filter(v => !names || names.includes(v.name)).forEach(elem => { | ||
var _a; | ||
@@ -346,15 +298,15 @@ if (!elem.disabled) | ||
}); | ||
}; | ||
Form.prototype.setChanges = function (values) { }; | ||
Form.prototype.getChanges = function () { }; | ||
Form.prototype.hideFields = function (names) { | ||
} | ||
setChanges(values) { } | ||
getChanges() { } | ||
hideFields(names) { | ||
if (typeof names === "string") | ||
names = [names]; | ||
this.setHiddenFields(__spreadArray(__spreadArray([], this.hiddenFields, true), names, true)); | ||
}; | ||
Form.prototype.showFields = function (names) { | ||
this.setHiddenFields([...this.hiddenFields, ...names]); | ||
} | ||
showFields(names) { | ||
if (!names) | ||
return this.setHiddenFields([]); | ||
var newArray = []; | ||
this.hiddenFields.forEach(function (name) { | ||
const newArray = []; | ||
this.hiddenFields.forEach(name => { | ||
if (names.includes(name)) | ||
@@ -365,16 +317,16 @@ return; | ||
this.setHiddenFields(newArray); | ||
}; | ||
Form.prototype.isHidden = function (name) { | ||
} | ||
isHidden(name) { | ||
return this.hiddenFields.includes(name); | ||
}; | ||
Form.prototype.hide = function () { | ||
} | ||
hide() { | ||
this.setHidden(true); | ||
}; | ||
Form.prototype.show = function () { | ||
} | ||
show() { | ||
this.setHidden(false); | ||
}; | ||
Form.prototype.setHidden = function (v) { | ||
} | ||
setHidden(v) { | ||
this.hidden = v; | ||
this.emit(Form.EVENT_HIDE_UPDATE, v); | ||
}; | ||
} | ||
/** | ||
@@ -384,13 +336,11 @@ * @description Function get new array of hidden fields. All fields, that now | ||
* */ | ||
Form.prototype.setHiddenFields = function (names) { | ||
var _a; | ||
var _this = this; | ||
setHiddenFields(names) { | ||
// Скрываем новые | ||
names.forEach(function (name) { | ||
names.forEach(name => { | ||
var _a; | ||
var d = _this.findDependence(name); | ||
const d = this.findDependence(name); | ||
if (!d) | ||
return; | ||
// Если он уже содержит этот инпут | ||
if (_this.hiddenFields.includes(name)) | ||
if (this.hiddenFields.includes(name)) | ||
return; | ||
@@ -400,5 +350,5 @@ (_a = d.hide) === null || _a === void 0 ? void 0 : _a.call(d); | ||
// Если в скрытых поля присутсвует поле, которые нужно расскрыть | ||
this.hiddenFields.forEach(function (name) { | ||
this.hiddenFields.forEach(name => { | ||
var _a; | ||
var d = _this.findDependence(name); | ||
const d = this.findDependence(name); | ||
if (!d) | ||
@@ -412,14 +362,13 @@ return; | ||
this.hiddenFields.splice(0, this.hiddenFields.length); | ||
(_a = this.hiddenFields).push.apply(_a, names); | ||
}; | ||
Form.PROVIDE_NAME = 'form-controller'; | ||
Form.EVENT_CHANGE_NAME = 'change'; | ||
Form.EVENT_DISABLED_UPDATE = 'update-disabled'; | ||
Form.EVENT_HIDE_UPDATE = 'update-hidden-fields'; | ||
Form.EVENT_DEPEND = 'on-depend'; | ||
return Form; | ||
}(EventEmitter__default["default"])); | ||
this.hiddenFields.push(...names); | ||
} | ||
} | ||
Form.PROVIDE_NAME = 'form-controller'; | ||
Form.EVENT_CHANGE_NAME = 'change'; | ||
Form.EVENT_DISABLED_UPDATE = 'update-disabled'; | ||
Form.EVENT_HIDE_UPDATE = 'update-hidden-fields'; | ||
Form.EVENT_DEPEND = 'on-depend'; | ||
function Input(params) { | ||
var obj = vue.reactive({ | ||
const obj = vue.reactive({ | ||
value: undefined, | ||
@@ -439,6 +388,6 @@ name: params.name, | ||
}, | ||
hide: function () { | ||
hide() { | ||
this.hidden = true; | ||
}, | ||
show: function () { | ||
show() { | ||
this.hidden = false; | ||
@@ -465,9 +414,7 @@ }, | ||
validate: function () { | ||
var _a; | ||
var _this = this; | ||
if (!params.validation) | ||
return true; | ||
var newErrors = []; | ||
params.validation.forEach(function (rule) { | ||
var ruleResult = rule(_this.value); | ||
const newErrors = []; | ||
params.validation.forEach(rule => { | ||
const ruleResult = rule(this.value); | ||
if (ruleResult === true) | ||
@@ -477,3 +424,3 @@ return; | ||
}); | ||
(_a = this.errors).splice.apply(_a, __spreadArray([0, this.errors.length], newErrors, false)); | ||
this.errors.splice(0, this.errors.length, ...newErrors); | ||
return this.errors.length === 0; | ||
@@ -486,3 +433,3 @@ } | ||
function useFormState(form) { | ||
var state = vue.reactive({ | ||
const state = vue.reactive({ | ||
disabled: form.disabled, | ||
@@ -493,6 +440,6 @@ hidden: form.hidden | ||
throw new Error('eee'); | ||
form.on(Form.EVENT_DISABLED_UPDATE, function () { | ||
form.on(Form.EVENT_DISABLED_UPDATE, () => { | ||
state.disabled = form.disabled; | ||
}); | ||
form.on(Form.EVENT_HIDE_UPDATE, function () { | ||
form.on(Form.EVENT_HIDE_UPDATE, () => { | ||
state.hidden = form.hidden; | ||
@@ -504,5 +451,5 @@ }); | ||
function buildDepend(name, init) { | ||
var parentForm = vue.inject(Form.PROVIDE_NAME); | ||
return parentForm.restoreDependence(name) || (function () { | ||
var d = init(); | ||
const parentForm = vue.inject(Form.PROVIDE_NAME); | ||
return parentForm.restoreDependence(name) || (() => { | ||
const d = init(); | ||
parentForm.depend(d); | ||
@@ -509,0 +456,0 @@ return d; |
{ | ||
"name": "jenesius-vue-form", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "email": "lokargenia@gmail.com", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24101
580