This repository was archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvue-form.min.js
6 lines (5 loc) · 7.73 KB
/
vue-form.min.js
1
2
3
4
5
6
/*!
* vue-form v0.3.13
* Released under the MIT License.
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.VueForm=e()}(this,function(){"use strict";function t(t){"undefined"!=typeof console&&h&&console.warn("[VueForm warn]: "+t)}function e(t){return"string"==typeof t}function n(t){return null!==t&&"object"==typeof t}function i(t){return void 0===t}function a(t,e,n,i){t.addEventListener(e,n,i)}function o(t,e,n,i){t.removeEventListener(e,n,i)}function r(t,e){return t?t.getAttribute(e):null}function l(t,e){var n=document.createEvent("HTMLEvents");n.initEvent(e,!0,!1),t.dispatchEvent(n)}function s(t){return v.camelize(t)}function u(t,e){t.splice(t.indexOf(e),1)}function d(t,e){var i,a;if("number"==typeof t.length)for(i=0;i<t.length;i++)e.call(t[i],t[i],i);else if(n(t))for(a in t)t.hasOwnProperty(a)&&e.call(t[a],t[a],a);return t}function f(t){return function(e){e.preventDefault(),x.validate(e.target,!0)&&t(e)}}function c(t){c.installed||(m(t),t.fields=$,t.component("fields",y(t)),t.validator=x,t.filter("valid",f),t.directive("validator",w),t.directive("validate",F),t.config.optionMergeStrategies.fields=t.config.optionMergeStrategies.props)}var h=!1,v={},p=Array.isArray,m=function(t){v=t.util,h=t.config.debug||!t.config.silent},g=Object.assign||function(t){for(var e=arguments,n=1;n<arguments.length;n++){var i=e[n];for(var a in i)Object.prototype.hasOwnProperty.call(i,a)&&(t[a]=i[a])}return t},b={name:"field",props:["field","class"],data:function(){return g({name:"",type:"text",label:"",attrs:{},options:[],default:void 0},this.field)},created:function(){this.key='["'+this.name.replace(/\./g,'"]["')+'"]'},computed:{attrs:{get:function(){return this.enable&&!this.$parent.evaluate(this.enable)?g({disabled:"true"},this.$data.attrs):this.$data.attrs},cache:!1},value:{get:function(){var t=this.$parent.getField(this);return i(t)&&!i(this.default)&&(t=this.default)&&this.$parent.setField(this,t),t},set:function(t){i(this.value)&&!t||this.$parent.setField(this,t,this.value)},cache:!1}},methods:{filterOptions:function(e){var i=this,a=[];return e?(d(e,function(t,e){n(t)?a.push({label:e,options:i.filterOptions(t)}):a.push({text:e,value:t})}),a):(t("Invalid options provided for "+this.name),a)}},filters:{options:function(t){return this.filterOptions(t)}}},y=function(i){return{name:"fields",props:{config:{type:[Array,Object],default:function(){return[]}},values:{type:Object}},created:function(){var a=this.$options,o=a.fields,r=a.components;this.fields&&this.values?d(g({},i.fields,o),function(t,a){e(t)&&(t={template:t}),n(t)&&(t.name=t.name||"field-"+a,t=i.extend(b).extend(t)),r[a]=t}):t("Invalid config or model provided")},computed:{fields:function(){return this.filterFields(this.config)}},methods:{getField:function(t){return this.values instanceof i&&"getField"in this.values?this.values.getField(t):this.$get("values"+t.key)},setField:function(t,e,n){this.values instanceof i&&"setField"in this.values?this.values.setField(t,e,n):this.$set("values"+t.key,e)},filterFields:function(n){var i=this,a=p(n),o=[];return d(n,function(n,r){e(n.name)||a||(n=g({name:r},n)),e(n.type)||(n=g({type:"text"},n)),e(n.name)?n.show&&!i.evaluate(n.show)||o.push(n):t("Field name missing "+JSON.stringify(n))}),o},evaluate:function(t,n){if(n=n||this.values,e(t)){var a=new i({data:n}),o=a.$eval(t);return a.$destroy(),o}return t.call(this,n,this)}},fields:{},components:{},template:'<div>\n\n <div v-for="field in fields">\n <label v-if="field.type != \'checkbox\'">{{ field.label }}</label>\n <component :is="field.type" :field="field"></component>\n </div>\n\n</div>\n'}},$={text:'<input type="text" v-bind="attrs" v-model="value">',textarea:'<textarea v-bind="attrs" v-model="value"></textarea>',radio:'<template v-for="option in options | options">\n <input type="radio" v-bind="attrs" :name="name" :value="option.value" v-model="value"> <label>{{ option.text }}</label>\n </template>',checkbox:'<input type="checkbox" v-bind="attrs" v-model="value">',select:'<select v-bind="attrs" v-model="value">\n <template v-for="option in options | options">\n <optgroup :label="option.label" v-if="option.label">\n <option v-for="opt in option.options" :value="opt.value">{{ opt.text }}</option>\n </optgroup>\n <option :value="option.value" v-else>{{ option.text }}</option>\n </template>\n </select>',range:'<input type="range" v-bind="attrs" v-model="value">',number:'<input type="number" v-bind="attrs" v-model="value">'},x={dirs:[],types:Object.freeze({required:function(t,e){return"boolean"!=typeof e&&(e=!0),"boolean"==typeof t?!e||t:!e||!(null===t||0===t.length)},numeric:function(t){return/^[-+]?[0-9]+$/.test(t)},integer:function(t){return/^(?:[-+]?(?:0|[1-9][0-9]*))$/.test(t)},float:function(t){return/^(?:[-+]?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/.test(t)},alpha:function(t){return/^([A-Z]+)?$/i.test(t)},alphanum:function(t){return/^([0-9A-Z]+)?$/i.test(t)},email:function(t){return/^([a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*)?$/i.test(t||"[email protected]")},url:function(t){return/^((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)?$/.test(t)},minlength:function(t,e){return t&&t.length&&t.length>=+e},maxlength:function(t,e){return t&&t.length&&t.length<=+e},length:function(t){return t&&t.length==+arg},min:function(t,e){return t>=+e},max:function(t,e){return t<=+e},pattern:function(t,e){var n=e.match(new RegExp("^/(.*?)/([gimy]*)$"));return new RegExp(n[1],n[2]).test(t)}}),add:function(t){this.dirs.push(t)},remove:function(t){u(this.dirs,t)},instance:function(t){do{if(t._validator)return t._validator;t=t.parentElement}while(t)},validate:function(t,e){var n=this,i=this.instance(t),a={valid:!0};if(i)return this.dirs.forEach(function(t){var o=t.validate(),r=t.el,l=t.name;n.instance(r)===i&&(!r._touched&&e&&(r._touched=!0),r._touched||o||(o=!0,a.valid=!1),a[l]||(a[l]={valid:!0,invalid:!1,dirty:r._dirty,touched:r._touched}),a[l][t.type]=!o,e&&a.valid&&!o&&r.focus(),a[l].valid&&!o&&(a[l].valid=!1,a[l].invalid=!0,a.valid=!1))}),a.invalid=!a.valid,i.results(a),e&&a.invalid&&l(i.el,"invalid"),a.valid}},w={bind:function(){var t=this,e=this.arg||this.expression;this.name=s(e),this.el._validator=this,this.vm.$set(this.name),this.vm.$on("hook:compiled",function(){x.validate(t.el)})},unbind:function(){this.vm.$delete(this.name)},results:function(t){this.vm.$set(this.name,g({validate:this.validate.bind(this)},t))},validate:function(){return x.validate(this.el,!0)}},F={priority:500,bind:function(){var t=r(this.el,"name");t&&(this.name=s(t),this.type=this.arg,this.value=this.el.value,this.el._dirty=!1,this.el._touched=!1,a(this.el,"blur",this.listener.bind(this)),a(this.el,"input",this.listener.bind(this)),x.add(this))},unbind:function(){o(this.el,"blur",this.listener),o(this.el,"input",this.listener),x.remove(this)},update:function(t){this.args=t},listener:function(t){(!O.target||"A"!==O.target.tagName&&"BUTTON"!==O.target.tagName)&&("blur"==t.type&&(this.el._touched=!0),this.el.value!=this.value&&(this.el._dirty=!0),x.validate(this.el))},validate:function(){var t=this.validator();if(t)return t.call(this.vm,this.el.value,this.args)},validator:function(){var t,e=this,n=this.vm;do{if((t=n.$options.validators||{})[e.type])return t[e.type];n=n.$parent}while(n);return x.types[this.type]}},O={target:null,handler:function(t){var e=t.target;O.target=e,setTimeout(function(){return O.target=null},0)}};return a(document,"mousedown",O.handler),a(document,"pointerdown",O.handler),a(document,"touchstart",O.handler),"undefined"!=typeof window&&window.Vue&&window.Vue.use(c),c});