1
0
mirror of synced 2024-11-25 14:56:03 +03:00
vue-formulario/dist/formulario.umd.js
2020-10-21 10:58:40 +03:00

17 lines
33 KiB
JavaScript

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("is-url"),require("nanoid/non-secure"),require("is-plain-object"),require("vue"),require("vue-property-decorator")):"function"==typeof define&&define.amd?define(["is-url","nanoid/non-secure","is-plain-object","vue","vue-property-decorator"],t):(e=e||self).Formulario=t(e.isUrl,e.nanoid,e.isPlainObject,e.Vue,e.vuePropertyDecorator)}(this,(function(e,t,r,i,o){"use strict";e=e&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e,t=t&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t,r=r&&Object.prototype.hasOwnProperty.call(r,"default")?r.default:r,i=i&&Object.prototype.hasOwnProperty.call(i,"default")?i.default:i;const s=(e,t)=>({classification:e,component:"FormularioInput"+(t||e[0].toUpperCase()+e.substr(1))});var a=Object.assign(Object.assign({},["text","email","number","color","date","hidden","month","password","search","tel","time","url","week","datetime-local"].reduce((e,t)=>Object.assign(Object.assign({},e),{[t]:s("text")}),{})),{range:s("slider"),textarea:s("textarea","TextArea"),checkbox:s("box"),radio:s("box"),submit:s("button"),button:s("button"),select:s("select"),file:s("file"),image:s("file"),group:s("group")});class n{constructor(e,t={},r={}){this.input=e,this.fileList=e.files,this.files=[],this.options=Object.assign({mimes:{}},r),this.results=!1,this.context=t,Array.isArray(this.fileList)?this.rehydrateFileList(this.fileList):this.addFileList(this.fileList)}rehydrateFileList(e){const t=e.reduce((e,t)=>{const r=t[this.options?this.options.fileUrlKey:"url"],i=!(!r||-1===r.lastIndexOf("."))&&r.substr(r.lastIndexOf(".")+1),o=this.options.mimes[i]||!1;return e.push(Object.assign({},t,r?{name:r.substr(r.lastIndexOf("/")+1||0),type:t.type?t.type:o,previewData:r}:{})),e},[]);this.results=e,this.addFileList(t)}addFileList(e){for(let r=0;r<e.length;r++){const i=e[r],o=t();this.files.push({progress:!1,error:!1,complete:!1,justFinished:!1,name:i.name||"file-upload",file:i,uuid:o,path:!1,removeFile:()=>this.removeFile(o),previewData:i.previewData||!1})}}hasUploader(){return!!this.context.uploader}uploaderIsAxios(){return this.hasUploader&&"function"==typeof this.context.uploader.request&&"function"==typeof this.context.uploader.get&&"function"==typeof this.context.uploader.delete&&"function"==typeof this.context.uploader.post}getUploader(...e){if(this.uploaderIsAxios()){const t=new FormData;if(t.append(this.context.name||"file",e[0]),!1===this.context.uploadUrl)throw new Error("No uploadURL specified: https://vueformulate.com/guide/inputs/file/#props");return this.context.uploader.post(this.context.uploadUrl,t,{headers:{"Content-Type":"multipart/form-data"},onUploadProgress:t=>{e[1](Math.round(100*t.loaded/t.total))}}).then(e=>e.data).catch(e[2])}return this.context.uploader(...e)}upload(){return this.results?Promise.resolve(this.results):new Promise((e,t)=>{if(!this.hasUploader)return t(new Error("No uploader has been defined"));Promise.all(this.files.map(e=>e.path?Promise.resolve(e.path):this.getUploader(e.file,t=>{e.progress=t,t>=100&&(e.complete||(e.justFinished=!0,setTimeout(()=>{e.justFinished=!1},this.options.uploadJustCompleteDuration)),e.complete=!0)},t=>{e.progress=0,e.error=t,e.complete=!0},this.options))).then(t=>{this.results=t,e(t)}).catch(e=>{throw new Error(e)})})}removeFile(e){if(this.files=this.files.filter(t=>t.uuid!==e),this.context.performValidation(),window&&this.fileList instanceof FileList){const e=new DataTransfer;this.files.map(({file:t})=>e.items.add(t)),this.fileList=e.files,this.input=e}}loadPreviews(){this.files.map(e=>{if(!e.previewData&&window&&window.FileReader&&/^image\//.test(e.file.type)){const t=new FileReader;t.onload=t=>Object.assign(e,{previewData:t.target.result}),t.readAsDataURL(e.file)}})}getFileList(){return this.fileList}getFiles(){return this.files}toString(){const e=this.files.length?this.files.length+" files":"empty";return this.results?JSON.stringify(this.results,null," "):`FileUpload(${e})`}}function l(e,t){if(e===t)return!0;if(!e||!t)return!1;const r=Object.keys(e);if(Object.keys(t).length!==r.length)return!1;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(0===r.length)return e===t;for(let i=0;i<r.length;i++){const o=r[i];if(e[o]!==t[o])return!1}return!0}function h(e){return"string"==typeof e?e.replace(/([_][a-z0-9])/gi,t=>0!==e.indexOf(t)&&"_"!==e[e.indexOf(t)-1]?t.toUpperCase().replace("_",""):t):e}function u(e){return"string"==typeof e&&/^[\^]/.test(e.charAt(0))?[h(e.substr(1)),e.charAt(0)]:[h(e),null]}function d(e){return e?"string"==typeof e?[e]:Array.isArray(e)?e:"object"==typeof e?Object.values(e):[]:[]}function p(e,t){return"string"==typeof e?p(e.split("|"),t):Array.isArray(e)?e.map(e=>function(e,t){if("function"==typeof e)return[e,[]];if(Array.isArray(e)&&e.length){e=e.slice();const[r,i]=u(e.shift());if("string"==typeof r&&Object.prototype.hasOwnProperty.call(t,r))return[t[r],e,r,i];if("function"==typeof r)return[r,e,r,i]}if("string"==typeof e){const r=e.split(":"),[i,o]=u(r.shift());if(Object.prototype.hasOwnProperty.call(t,i))return[t[i],r.length?r.join(":").split(","):[],i,o];throw new Error("Unknown validation rule "+e)}return!1}(e,t)).filter(e=>!!e):[]}function c(e){switch(typeof e){case"symbol":case"number":case"string":case"boolean":case"undefined":return!0;default:return null===e}}function f(e,t){return Object.prototype.hasOwnProperty.call(e,t)}function m(e,t){const r=t.split(".");let i=e;for(const e in r){const t=r[e].match(/(.+)\[(\d+)\]$/);if(void 0===i)return null;if(t){if(i=i[t[1]],void 0===i)return null;i=i[t[2]]}else i=i[r[e]]}return i}var g={accepted:({value:e})=>Promise.resolve(["yes","on","1",1,!0,"true"].includes(e)),after({value:e},t=!1){const r=!1!==t?Date.parse(t):Date.now(),i=e instanceof Date?e.getTime():Date.parse(e);return Promise.resolve(!isNaN(i)&&i>r)},alpha({value:e},t="default"){const r={default:/^[a-zA-ZÀ-ÖØ-öø-ÿ]+$/,latin:/^[a-zA-Z]+$/},i=f(r,t)?t:"default";return Promise.resolve(r[i].test(e))},alphanumeric({value:e},t="default"){const r={default:/^[a-zA-Z0-9À-ÖØ-öø-ÿ]+$/,latin:/^[a-zA-Z0-9]+$/},i=f(r,t)?t:"default";return Promise.resolve(r[i].test(e))},before({value:e},t=!1){const r=!1!==t?Date.parse(t):Date.now(),i=e instanceof Date?e.getTime():Date.parse(e);return Promise.resolve(!isNaN(i)&&i<r)},between:({value:e},t=0,r=10,i)=>Promise.resolve(null!==t&&null!==r&&!isNaN(t)&&!isNaN(r)&&(!isNaN(Number(e))&&"length"!==i||"value"===i?(e=Number(e),t=Number(t),r=Number(r),e>t&&e<r):("string"==typeof e||"length"===i)&&(e=isNaN(Number(e))?e:e.toString()).length>t&&e.length<r)),confirm:({value:e,getFormValues:t,name:r},i)=>Promise.resolve((()=>{const o=t();let s=i;return s||(s=/_confirm$/.test(r)?r.substr(0,r.length-8):r+"_confirm"),o[s]===e})()),date:({value:e},t=!1)=>Promise.resolve(t?function(e){const t=`^${r=e,r.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}$`;var r;const i={MM:"(0[1-9]|1[012])",M:"([1-9]|1[012])",DD:"([012][1-9]|3[01])",D:"([012]?[1-9]|3[01])",YYYY:"\\d{4}",YY:"\\d{2}"};return new RegExp(Object.keys(i).reduce((e,t)=>e.replace(t,i[t]),t))}(t).test(e):!isNaN(Date.parse(e))),email({value:e}){if(!e)return Promise.resolve(!0);return Promise.resolve(/^(([^<>()\[\].,;:\s@"]+(\.[^<>()\[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})$/i.test(e))},endsWith:({value:e},...t)=>e?"string"==typeof e?Promise.resolve(0===t.length||t.some(t=>e.endsWith(t))):Promise.resolve(!1):Promise.resolve(!0),in:({value:e},...t)=>Promise.resolve(t.some(t=>"object"==typeof t?l(t,e):t===e)),matches:({value:e},...t)=>Promise.resolve(!!t.find(t=>("string"==typeof t&&"/"===t.substr(0,1)&&"/"===t.substr(-1)&&(t=new RegExp(t.substr(1,t.length-2))),t instanceof RegExp?t.test(e):t===e))),mime({value:e},...t){if(e instanceof n){const r=e.getFiles(),i=e=>t.includes(e.type),o=r.reduce((e,{file:t})=>e&&i(t),!0);return Promise.resolve(o)}return Promise.resolve(!0)},min:({value:e},t=1,r)=>Promise.resolve(Array.isArray(e)?(t=isNaN(t)?t:Number(t),e.length>=t):!isNaN(e)&&"length"!==r||"value"===r?(e=isNaN(e)?e:Number(e))>=t:("string"==typeof e||"length"===r)&&(e=isNaN(e)?e:e.toString()).length>=t),max:({value:e},t=10,r)=>Promise.resolve(Array.isArray(e)?(t=isNaN(Number(t))?t:Number(t),e.length<=t):!isNaN(e)&&"length"!==r||"value"===r?(e=isNaN(e)?e:Number(e))<=t:("string"==typeof e||"length"===r)&&(e=isNaN(e)?e:e.toString()).length<=t),not:({value:e},...t)=>Promise.resolve(!t.some(t=>"object"==typeof t?l(t,e):t===e)),number:({value:e})=>Promise.resolve(!isNaN(Number(e))),required:({value:e},t=!0)=>Promise.resolve(!(t&&!["no","false"].includes(t))||(Array.isArray(e)?!!e.length:e instanceof n?e.getFiles().length>0:"string"==typeof e?!!e:"object"!=typeof e||!!e&&!!Object.keys(e).length)),startsWith:({value:e},...t)=>e?"string"==typeof e?Promise.resolve(0===t.length||t.some(t=>e.startsWith(t))):Promise.resolve(!1):Promise.resolve(!0),url:({value:t})=>Promise.resolve(e(t)),bail:()=>Promise.resolve(!0)},y={csv:"text/csv",gif:"image/gif",jpeg:"image/jpeg",jpg:"image/jpeg",png:"image/png",pdf:"application/pdf",svg:"image/svg+xml"};const v={default:(e,t)=>e.$t("validation.default",t),accepted:(e,t)=>e.$t("validation.accepted",t),after:(e,t)=>Array.isArray(t.args)&&t.args.length?e.$t("validation.after.compare",t):e.$t("validation.after.default",t),alpha:(e,t)=>e.$t("validation.alpha",t),alphanumeric:(e,t)=>e.$t("validation.alphanumeric",t),before:(e,t)=>Array.isArray(t.args)&&t.args.length?e.$t("validation.before.compare",t):e.$t("validation.before.default",t),between(e,t){const r=!(!Array.isArray(t.args)||!t.args[2])&&t.args[2];return!isNaN(t.value)&&"length"!==r||"value"===r?e.$t("validation.between.force",t):e.$t("validation.between.default",t)},confirm:(e,t)=>e.$t("validation.confirm",t),date:(e,t)=>Array.isArray(t.args)&&t.args.length?e.$t("validation.date.format",t):e.$t("validation.date.default",t),email:(e,t)=>e.$t("validation.email.default",t),endsWith:(e,t)=>e.$t("validation.endsWith.default",t),in:function(e,t){return"string"==typeof t.value&&t.value?e.$t("validation.in.string",t):e.$t("validation.in.default",t)},matches:(e,t)=>e.$t("validation.matches.default",t),max(e,t){const r=t.args[0];if(Array.isArray(t.value))return e.$tc("validation.max.array",r,t);const i=!(!Array.isArray(t.args)||!t.args[1])&&t.args[1];return!isNaN(t.value)&&"length"!==i||"value"===i?e.$tc("validation.max.force",r,t):e.$tc("validation.max.default",r,t)},mime:(e,t)=>t.args[0]?e.$t("validation.mime.default",t):e.$t("validation.mime.no_formats_allowed",t),min(e,t){const r=t.args[0];if(Array.isArray(t.value))return e.$tc("validation.min.array",r,t);const i=!(!Array.isArray(t.args)||!t.args[1])&&t.args[1];return!isNaN(t.value)&&"length"!==i||"value"===i?e.$tc("validation.min.force",r,t):e.$tc("validation.min.default",r,t)},not:(e,t)=>e.$t("validation.not.default",t),number:(e,t)=>e.$t("validation.number.default",t),required:(e,t)=>e.$t("validation.required.default",t),startsWith:(e,t)=>e.$t("validation.startsWith.default",t),url:(e,t)=>e.$t("validation.url.default",t)};function b(e){e.extend({validationMessages:v})}function E(e,t,r,i){return new Promise(r=>{const o=(i.fauxUploaderDuration||2e3)*(.5+Math.random()),s=performance.now(),a=()=>setTimeout(()=>{const i=performance.now()-s,n=Math.min(100,Math.round(i/o*100));if(t(n),n>=100)return r({url:"http://via.placeholder.com/350x150.png",name:e.name});a()},20);a()})}
/*! *****************************************************************************
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.
***************************************************************************** */
function x(e,t){var r={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(r[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o<i.length;o++)t.indexOf(i[o])<0&&Object.prototype.propertyIsEnumerable.call(e,i[o])&&(r[i[o]]=e[i[o]])}return r}function V(e,t,r,i){var o,s=arguments.length,a=s<3?t:null===i?i=Object.getOwnPropertyDescriptor(t,r):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,r,i);else for(var n=e.length-1;n>=0;n--)(o=e[n])&&(a=(s<3?o(a):s>3?o(t,r,a):o(t,r))||a);return s>3&&a&&Object.defineProperty(t,r,a),a}class O{constructor(e){this.registry=new Map,this.ctx=e}add(e,t){this.registry.set(e,t)}remove(e){this.registry.delete(e);const t=this.ctx.proxy,r=e,i=(t[r],x(t,["symbol"==typeof r?r:r+""]));this.ctx.proxy=i}has(e){return this.registry.has(e)}hasNested(e){for(const t of this.registry.keys())if(t===e||t.includes(e+"."))return!0;return!1}get(e){return this.registry.get(e)}getNested(e){const t=new Map;for(const r of this.registry.keys())(r===e||r.includes(e+"."))&&t.set(r,this.registry.get(r));return t}map(e){const t={};return this.registry.forEach((r,i)=>Object.assign(t,{[i]:e(r,i)})),t}forEach(e){this.registry.forEach((t,r)=>{e(t,r)})}keys(){return Array.from(this.registry.keys())}register(e,t){if(this.registry.has(e))return;this.registry.set(e,t);const r=f(t.$options.propsData,"formularioValue"),i=f(t.$options.propsData,"value");!r&&this.ctx.hasInitialValue&&void 0!==m(this.ctx.initialValues,e)?t.context.model=m(this.ctx.initialValues,e):!r&&!i||l(t.proxy,m(this.ctx.initialValues,e))||this.ctx.setFieldValue(e,t.proxy),this.ctx.childrenShouldShowErrors&&(t.formShouldShowErrors=!0)}reduce(e,t){return this.registry.forEach((r,i)=>{t=e(t,r,i)}),t}}class P{constructor(e){this.form=e}hasValidationErrors(){return this.form.hasValidationErrors()}values(){return new Promise((e,t)=>{const r=this.form,i=[],o=function e(t){if("object"!=typeof t)return t;const r=Array.isArray(t)?[]:{};for(const i in t)Object.prototype.hasOwnProperty.call(t,i)&&(c(t[i])||t[i]instanceof n?r[i]=t[i]:r[i]=e(t[i]));return r}(r.proxy);for(const e in o)Object.prototype.hasOwnProperty.call(o,e)&&"object"==typeof r.proxy[e]&&r.proxy[e]instanceof n&&i.push(r.proxy[e].upload().then(t=>Object.assign(o,{[e]:t})));Promise.all(i).then(()=>e(o)).catch(e=>t(e))})}}let w=class extends i{constructor(){super(...arguments),this.formularioRegister=this.register,this.formularioDeregister=this.deregister,this.formularioSetter=this.setFieldValue,this.getFormValues=()=>this.proxy,this.path="",this.proxy={},this.registry=new O(this),this.childrenShouldShowErrors=!1,this.formShouldShowErrors=!1,this.errorObservers=[],this.namedErrors=[],this.namedFieldErrors={}}formularioFieldValidation(e){this.$emit("validation",e)}removeErrorObserver(e){this.errorObservers=this.errorObservers.filter(t=>t.callback!==e)}get mergedFormErrors(){return this.formErrors.concat(this.namedErrors)}get mergedFieldErrors(){const e={};if(this.errors)for(const t in this.errors)e[t]=d(this.errors[t]);for(const t in this.namedFieldErrors)e[t]=d(this.namedFieldErrors[t]);return e}get hasFormErrorObservers(){return this.errorObservers.some(e=>"form"===e.type)}get hasInitialValue(){return this.formularioValue&&"object"==typeof this.formularioValue||this.values&&"object"==typeof this.values||this.isGrouping&&"object"==typeof this.context.model[this.index]}get isVmodeled(){return!!(f(this.$options.propsData,"formularioValue")&&this._events&&Array.isArray(this._events.input)&&this._events.input.length)}get initialValues(){return f(this.$options.propsData,"formularioValue")&&"object"==typeof this.formularioValue?Object.assign({},this.formularioValue):f(this.$options.propsData,"values")&&"object"==typeof this.values?Object.assign({},this.values):this.isGrouping&&"object"==typeof this.context.model[this.index]?this.context.model[this.index]:{}}onFormularioValueChanged(e){this.isVmodeled&&e&&"object"==typeof e&&this.setValues(e)}onMergedFormErrorsChanged(e){this.errorObservers.filter(e=>"form"===e.type).forEach(t=>t.callback(e))}onMergedFieldErrorsChanged(e){this.errorObservers.filter(e=>"input"===e.type).forEach(t=>t.callback(e[t.field]||[]))}created(){this.$formulario.register(this),this.applyInitialValues()}destroyed(){this.$formulario.deregister(this)}register(e,t){this.registry.register(e,t)}deregister(e){this.registry.remove(e)}applyErrors({formErrors:e,inputErrors:t}){this.namedErrors=e,this.namedFieldErrors=t}addErrorObserver(e){this.errorObservers.find(t=>e.callback===t.callback)||(this.errorObservers.push(e),"form"===e.type?e.callback(this.mergedFormErrors):f(this.mergedFieldErrors,e.field)&&e.callback(this.mergedFieldErrors[e.field]))}registerErrorComponent(e){this.errorComponents.includes(e)||this.errorComponents.push(e)}formSubmitted(){this.showErrors();const e=new P(this);return this.$emit("submit-raw",e),e.hasValidationErrors().then(t=>t?void 0:e.values()).then(e=>{if(void 0!==e)return this.$emit("submit",e),e})}applyInitialValues(){this.hasInitialValue&&(this.proxy=this.initialValues)}setFieldValue(e,t,r=!0){if(void 0===t){const t=this.proxy,r=e,i=(t[r],x(t,["symbol"==typeof r?r:r+""]));this.proxy=i}else!function(e,t,r){const i=t.split(".");let o=e;for(let e=0;e<i.length;e++){const t=i[e],s=t.match(/(.+)\[(\d+)\]$/);if(s){if(void 0===o[s[1]]&&(o[s[1]]=[]),o=o[s[1]],e===i.length-1){o[s[2]]=r;break}o=o[s[2]]}else{if(e===i.length-1){o[t]=r;break}void 0===o[t]&&(o[t]={}),o=o[t]}}}(this.proxy,e,t);r&&this.$emit("input",Object.assign({},this.proxy))}hasValidationErrors(){return Promise.all(this.registry.reduce((e,t)=>(e.push(t.performValidation()&&t.getValidationErrors()),e),[])).then(e=>e.some(e=>e.hasErrors))}showErrors(){this.childrenShouldShowErrors=!0,this.registry.forEach(e=>{e.formShouldShowErrors=!0})}hideErrors(){this.childrenShouldShowErrors=!1,this.registry.forEach(e=>{e.formShouldShowErrors=!1,e.behavioralErrorVisibility=!1})}setValues(e){const t=Array.from(new Set(Object.keys(e).concat(Object.keys(this.proxy))));let r=!1;t.forEach(t=>{this.registry.hasNested(t)&&this.registry.getNested(t).forEach((t,i)=>{l(m(e,i),m(this.proxy,i))||(this.setFieldValue(i,m(e,i),!1),r=!0),l(m(e,i),this.registry.get(i).proxy)||(this.registry.get(i).context.model=m(e,i))})}),this.applyInitialValues(),r&&this.$emit("input",Object.assign({},this.proxy))}};function $(e,t,r,i,o,s,a,n,l,h){"boolean"!=typeof a&&(l=n,n=a,a=!1);const u="function"==typeof r?r.options:r;let d;if(e&&e.render&&(u.render=e.render,u.staticRenderFns=e.staticRenderFns,u._compiled=!0,o&&(u.functional=!0)),i&&(u._scopeId=i),s?(d=function(e){(e=e||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext)||"undefined"==typeof __VUE_SSR_CONTEXT__||(e=__VUE_SSR_CONTEXT__),t&&t.call(this,l(e)),e&&e._registeredComponents&&e._registeredComponents.add(s)},u._ssrRegister=d):t&&(d=a?function(e){t.call(this,h(e,this.$root.$options.shadowRoot))}:function(e){t.call(this,n(e))}),d)if(u.functional){const e=u.render;u.render=function(t,r){return d.call(r),e(t,r)}}else{const e=u.beforeCreate;u.beforeCreate=e?[].concat(e,d):[d]}return r}V([o.Provide()],w.prototype,"formularioFieldValidation",null),V([o.Provide()],w.prototype,"formularioRegister",void 0),V([o.Provide()],w.prototype,"formularioDeregister",void 0),V([o.Provide()],w.prototype,"formularioSetter",void 0),V([o.Provide()],w.prototype,"getFormValues",void 0),V([o.Provide()],w.prototype,"path",void 0),V([o.Provide()],w.prototype,"removeErrorObserver",null),V([o.Model("input",{type:Object,default:()=>({})})],w.prototype,"formularioValue",void 0),V([o.Prop({type:[String,Boolean],default:!1})],w.prototype,"name",void 0),V([o.Prop({type:[Object,Boolean],default:!1})],w.prototype,"values",void 0),V([o.Prop({default:!1})],w.prototype,"errors",void 0),V([o.Prop({default:()=>[]})],w.prototype,"formErrors",void 0),V([o.Watch("formularioValue",{deep:!0})],w.prototype,"onFormularioValueChanged",null),V([o.Watch("mergedFormErrors")],w.prototype,"onMergedFormErrorsChanged",null),V([o.Watch("mergedFieldErrors",{immediate:!0})],w.prototype,"onMergedFieldErrorsChanged",null),V([o.Provide()],w.prototype,"addErrorObserver",null),w=V([o.Component({name:"FormularioForm"})],w);const j=w;var F=function(){var e=this,t=e.$createElement;return(e._self._c||t)("form",{on:{submit:function(t){return t.preventDefault(),e.formSubmitted(t)}}},[e._t("default",null,{errors:e.mergedFormErrors})],2)};F._withStripped=!0;const A=$({render:F,staticRenderFns:[]},void 0,j,void 0,!1,void 0,!1,void 0,void 0,void 0),N={BLUR:"blur",LIVE:"live",SUBMIT:"submit"};let R=class extends i{constructor(){super(...arguments),this.formularioRegisterRule=this.registerRule,this.formularioRemoveRule=this.removeRule,this.defaultId=this.$formulario.nextId(this),this.localAttributes={},this.localErrors=[],this.proxy=this.getInitialValue(),this.behavioralErrorVisibility="live"===this.errorBehavior,this.formShouldShowErrors=!1,this.validationErrors=[],this.pendingValidation=Promise.resolve(),this.ruleRegistry=[],this.messageRegistry={}}get context(){return this.defineModel({id:this.id||this.defaultId,name:this.nameOrFallback,attributes:this.elementAttributes,blurHandler:this.blurHandler.bind(this),disableErrors:this.disableErrors,errors:this.explicitErrors,allErrors:this.allErrors,formShouldShowErrors:this.formShouldShowErrors,getValidationErrors:this.getValidationErrors.bind(this),hasGivenName:this.hasGivenName,hasValidationErrors:this.hasValidationErrors.bind(this),imageBehavior:this.imageBehavior,performValidation:this.performValidation.bind(this),preventWindowDrops:this.preventWindowDrops,setErrors:this.setErrors.bind(this),showValidationErrors:this.showValidationErrors,uploadBehavior:this.uploadBehavior,uploadUrl:this.mergedUploadUrl,uploader:this.uploader||this.$formulario.getUploader(),validationErrors:this.validationErrors,value:this.value,visibleValidationErrors:this.visibleValidationErrors})}get parsedValidationRules(){const e={};return Object.keys(this.validationRules).forEach(t=>{e[h(t)]=this.validationRules[t]}),e}get messages(){const e={};return Object.keys(this.validationMessages).forEach(t=>{e[h(t)]=this.validationMessages[t]}),Object.keys(this.messageRegistry).forEach(t=>{e[h(t)]=this.messageRegistry[t]}),e}get elementAttributes(){const e=Object.assign({},this.localAttributes);return this.id?e.id=this.id:e.id=this.defaultId,this.hasGivenName&&(e.name=this.name),this.help&&(e["aria-describedby"]=e.id+"-help"),e}get nameOrFallback(){return""!==this.path?`${this.path}.${this.name}`:this.name}get hasGivenName(){return"boolean"!=typeof this.name}get mergedValidationName(){return this.validationName||this.name}get mergedUploadUrl(){return this.uploadUrl||this.$formulario.getUploadUrl()}get hasErrors(){return this.allErrors.length>0}get hasVisibleErrors(){return this.validationErrors&&this.showValidationErrors||!!this.explicitErrors.length}get allErrors(){return[...this.explicitErrors,...d(this.validationErrors)]}get visibleValidationErrors(){return this.showValidationErrors&&this.validationErrors.length?this.validationErrors:[]}get explicitErrors(){return[...d(this.errors),...this.localErrors].map(e=>({rule:null,context:null,message:e}))}get isVmodeled(){return!!(Object.prototype.hasOwnProperty.call(this.$options.propsData,"formularioValue")&&this._events&&Array.isArray(this._events.input)&&this._events.input.length)}get showValidationErrors(){return this.showErrors||this.formShouldShowErrors||this.behavioralErrorVisibility}onAttrsChanged(e){this.updateLocalAttributes(e)}onProxyChanged(e,t){this.errorBehavior===N.LIVE?this.performValidation():this.validationErrors=[],this.isVmodeled||l(e,t)||(this.context.model=e)}onFormularioValueChanged(e,t){this.isVmodeled&&!l(e,t)&&(this.context.model=e)}onShowValidationErrorsChanged(e){this.$emit("error-visibility",e)}created(){this.applyInitialValue(),this.formularioRegister&&"function"==typeof this.formularioRegister&&this.formularioRegister(this.nameOrFallback,this),this.disableErrors||"function"!=typeof this.addErrorObserver||this.addErrorObserver({callback:this.setErrors,type:"input",field:this.nameOrFallback}),this.updateLocalAttributes(this.$attrs),this.errorBehavior===N.LIVE&&this.performValidation()}beforeDestroy(){this.disableErrors||"function"!=typeof this.removeErrorObserver||this.removeErrorObserver(this.setErrors),"function"==typeof this.formularioDeregister&&this.formularioDeregister(this.nameOrFallback)}defineModel(e){return Object.defineProperty(e,"model",{get:this.modelGetter.bind(this),set:this.modelSetter.bind(this)})}modelGetter(){const e=this.isVmodeled?"formularioValue":"proxy";return void 0===this[e]?"":this[e]}modelSetter(e){l(e,this.proxy)||(this.proxy=e),this.$emit("input",e),this.context.name&&"function"==typeof this.formularioSetter&&this.formularioSetter(this.context.name,e)}blurHandler(){this.$emit("blur"),"blur"===this.errorBehavior&&(this.behavioralErrorVisibility=!0)}getInitialValue(){return f(this.$options.propsData,"value")?this.value:f(this.$options.propsData,"formularioValue")?this.formularioValue:""}applyInitialValue(){l(this.context.model,this.proxy)||(this.context.model=this.proxy)}updateLocalAttributes(e){l(e,this.localAttributes)||(this.localAttributes=e)}performValidation(){let e=p(this.validation,this.$formulario.rules(this.parsedValidationRules));return e=this.ruleRegistry.length?this.ruleRegistry.concat(e):e,this.pendingValidation=this.runRules(e).then(e=>this.didValidate(e)),this.pendingValidation}runRules(e){const t=([e,t,r])=>{let i=e({value:this.context.model,getFormValues:this.getFormValues.bind(this),name:this.context.name},...t);return i=i instanceof Promise?i:Promise.resolve(i),i.then(e=>!e&&this.getMessageObject(r,t))};return new Promise(r=>{const i=(e,o=[])=>{const s=e.shift();Array.isArray(s)&&s.length?Promise.all(s.map(t)).then(e=>e.filter(e=>!!e)).then(t=>(t=Array.isArray(t)?t:[]).length&&s.bail||!e.length?r(o.concat(t)):i(e,o.concat(t))):r([])};i(function(e){const t=[],r=e.findIndex(([,,e])=>"bail"===e.toLowerCase());if(r>=0){const i=e.splice(0,r+1).slice(0,-1);i.length&&t.push(i),e.map(e=>t.push(Object.defineProperty([e],"bail",{value:!0})))}else t.push(e);return t.reduce((e,t)=>{const r=(e,t=!1)=>{if(e.length<2)return Object.defineProperty([e],"bail",{value:t});const i=[],o=e.findIndex(([,,,e])=>"^"===e);if(o>=0){const s=e.splice(0,o);s.length&&i.push(...r(s,t)),i.push(Object.defineProperty([e.shift()],"bail",{value:!0})),e.length&&i.push(...r(e,t))}else i.push(e);return i};return e.concat(r(t))},[])}(e))})}didValidate(e){const t=!l(e,this.validationErrors);if(this.validationErrors=e,t){const e=this.getErrorObject();this.$emit("validation",e),this.formularioFieldValidation&&"function"==typeof this.formularioFieldValidation&&this.formularioFieldValidation(e)}}getMessageObject(e,t){const r={args:t,name:this.mergedValidationName,value:this.context.model,vm:this,formValues:this.getFormValues()};return{rule:e,context:r,message:this.getMessageFunc(e)(r)}}getMessageFunc(e){if(e=h(e),this.messages&&void 0!==this.messages[e])switch(typeof this.messages[e]){case"function":return this.messages[e];case"string":case"boolean":return()=>this.messages[e]}return t=>this.$formulario.validationMessage(e,t,this)}hasValidationErrors(){return new Promise(e=>{this.$nextTick(()=>{this.pendingValidation.then(()=>e(!!this.validationErrors.length))})})}getValidationErrors(){return new Promise(e=>{this.$nextTick(()=>this.pendingValidation.then(()=>e(this.getErrorObject())))})}getErrorObject(){return{name:this.context.nameOrFallback||this.context.name,errors:this.validationErrors.filter(e=>"object"==typeof e),hasErrors:!!this.validationErrors.length}}setErrors(e){this.localErrors=d(e)}registerRule(e,t,r,i=null){this.ruleRegistry.some(e=>e[2]===r)||(this.ruleRegistry.push([e,t,r]),null!==i&&(this.messageRegistry[r]=i))}removeRule(e){const t=this.ruleRegistry.findIndex(t=>t[2]===e);t>=0&&(this.ruleRegistry.splice(t,1),delete this.messageRegistry[e])}};V([o.Inject({default:void 0})],R.prototype,"formularioSetter",void 0),V([o.Inject({default:()=>()=>{}})],R.prototype,"formularioFieldValidation",void 0),V([o.Inject({default:void 0})],R.prototype,"formularioRegister",void 0),V([o.Inject({default:void 0})],R.prototype,"formularioDeregister",void 0),V([o.Inject({default:()=>()=>({})})],R.prototype,"getFormValues",void 0),V([o.Inject({default:void 0})],R.prototype,"addErrorObserver",void 0),V([o.Inject({default:void 0})],R.prototype,"removeErrorObserver",void 0),V([o.Inject({default:""})],R.prototype,"path",void 0),V([o.Provide()],R.prototype,"formularioRegisterRule",void 0),V([o.Provide()],R.prototype,"formularioRemoveRule",void 0),V([o.Model("input",{default:""})],R.prototype,"formularioValue",void 0),V([o.Prop({type:[String,Number,Boolean],default:!1})],R.prototype,"id",void 0),V([o.Prop({default:"text"})],R.prototype,"type",void 0),V([o.Prop({required:!0})],R.prototype,"name",void 0),V([o.Prop({default:!1})],R.prototype,"value",void 0),V([o.Prop({type:[String,Boolean,Array],default:!1})],R.prototype,"validation",void 0),V([o.Prop({type:[String,Boolean],default:!1})],R.prototype,"validationName",void 0),V([o.Prop({type:Object,default:()=>({})})],R.prototype,"validationRules",void 0),V([o.Prop({type:Object,default:()=>({})})],R.prototype,"validationMessages",void 0),V([o.Prop({type:[Array,String,Boolean],default:!1})],R.prototype,"errors",void 0),V([o.Prop({type:String,default:N.BLUR,validator:e=>[N.BLUR,N.LIVE,N.SUBMIT].includes(e)})],R.prototype,"errorBehavior",void 0),V([o.Prop({default:!1})],R.prototype,"showErrors",void 0),V([o.Prop({default:!1})],R.prototype,"disableErrors",void 0),V([o.Prop({default:!0})],R.prototype,"preventWindowDrops",void 0),V([o.Prop({default:"preview"})],R.prototype,"imageBehavior",void 0),V([o.Prop({default:!1})],R.prototype,"uploader",void 0),V([o.Prop({default:!1})],R.prototype,"uploadUrl",void 0),V([o.Prop({default:"live"})],R.prototype,"uploadBehavior",void 0),V([o.Watch("$attrs",{deep:!0})],R.prototype,"onAttrsChanged",null),V([o.Watch("proxy")],R.prototype,"onProxyChanged",null),V([o.Watch("formularioValue")],R.prototype,"onFormularioValueChanged",null),V([o.Watch("showValidationErrors",{immediate:!0})],R.prototype,"onShowValidationErrorsChanged",null),R=V([o.Component({name:"FormularioInput",inheritAttrs:!1})],R);const S=R;var D=function(){var e=this,t=e.$createElement;return(e._self._c||t)("div",{staticClass:"formulario-input",attrs:{"data-has-errors":e.hasErrors,"data-is-showing-errors":e.hasVisibleErrors,"data-type":e.type}},[e._t("default",null,{id:e.id,context:e.context,errors:e.errors,validationErrors:e.validationErrors})],2)};D._withStripped=!0;const I=$({render:D,staticRenderFns:[]},void 0,S,void 0,!1,void 0,!1,void 0,void 0,void 0);let U=class extends i{get groupPath(){return this.isArrayItem?`${this.path}[${this.name}]`:""===this.path?this.name:`${this.path}.${this.name}`}};V([o.Inject({default:""})],U.prototype,"path",void 0),V([o.Prop({required:!0})],U.prototype,"name",void 0),V([o.Prop({default:!1})],U.prototype,"isArrayItem",void 0),V([o.Provide("path")],U.prototype,"groupPath",null),U=V([o.Component({name:"FormularioGrouping"})],U);const _=U;var C=function(){var e=this.$createElement;return(this._self._c||e)("div",{staticClass:"formulario-group",attrs:{"data-type":"group"}},[this._t("default")],2)};C._withStripped=!0;const k=$({render:C,staticRenderFns:[]},void 0,_,void 0,!1,void 0,!1,void 0,void 0,void 0);return new class{constructor(){this.options={},this.defaults={components:{FormularioForm:A,FormularioInput:I,FormularioGrouping:k},library:a,rules:g,mimes:y,locale:!1,uploader:E,uploadUrl:!1,fileUrlKey:"url",uploadJustCompleteDuration:1e3,errorHandler:e=>e,plugins:[b],validationMessages:{},idPrefix:"formulario-"},this.registry=new Map,this.idRegistry={}}install(e,t){e.prototype.$formulario=this,this.options=this.defaults;let r=this.defaults.plugins;t&&Array.isArray(t.plugins)&&t.plugins.length&&(r=r.concat(t.plugins)),r.forEach(e=>"function"==typeof e?e(this):null),this.extend(t||{});for(const t in this.options.components)Object.prototype.hasOwnProperty.call(this.options.components,t)&&e.component(t,this.options.components[t])}nextId(e){const t=this.options,r=!(!e.$route||!e.$route.path)&&e.$route.path?e.$route.path.replace(/[/\\.\s]/g,"-"):"global";return f(this.idRegistry,r)||(this.idRegistry[r]=0),`${t.idPrefix}${r}-${++this.idRegistry[r]}`}extend(e){if("object"==typeof e)return this.options=function e(t,i,o=!0){const s={};for(const a in t)f(i,a)?r(i[a])&&r(t[a])?s[a]=e(t[a],i[a],o):o&&Array.isArray(t[a])&&Array.isArray(i[a])?s[a]=t[a].concat(i[a]):s[a]=i[a]:s[a]=t[a];for(const e in i)f(s,e)||(s[e]=i[e]);return s}(this.options,e),this;throw new Error(`VueFormulario extend() should be passed an object (was ${typeof e})`)}rules(e={}){return Object.assign(Object.assign({},this.options.rules),e)}validationMessage(e,t,r){return f(this.options.validationMessages,e)?this.options.validationMessages[e](r,t):this.options.validationMessages.default(r,t)}register(e){"FormularioForm"===e.$options.name&&e.name&&this.registry.set(e.name,e)}deregister(e){"FormularioForm"===e.$options.name&&e.name&&this.registry.has(e.name)&&this.registry.delete(e.name)}handle(e,t,r=!1){const i=r?e:this.options.errorHandler(e,t);if(t&&this.registry.has(t)){this.registry.get(t).applyErrors({formErrors:d(i.formErrors),inputErrors:i.inputErrors||{}})}return i}reset(e,t={}){this.resetValidation(e),this.setValues(e,t)}resetValidation(e){const t=this.registry.get(e);t.hideErrors(e),t.namedErrors=[],t.namedFieldErrors={}}setValues(e,t){if(t){this.registry.get(e).setValues(Object.assign({},t))}}getUploader(){return this.options.uploader||!1}getUploadUrl(){return this.options.uploadUrl||!1}getFileUrlKey(){return this.options.fileUrlKey||"url"}createUpload(e,t){return new n(e,t,this.options)}}}));