-
Notifications
You must be signed in to change notification settings - Fork 42
/
autofields.min.js
6 lines (6 loc) · 7.22 KB
/
autofields.min.js
1
2
3
4
5
6
/**
* @license Autofields v2.2.8
* (c) 2021 Justin Maier http://justmaier.github.io/angular-autoFields-bootstrap
* License: MIT
*/
"use strict";angular.module("autofields.core",[]).provider("$autofields",function(){var d={},p={CamelToTitle:function(t){return t.replace(/([A-Z])/g," $1").replace(/^./,function(t){return t.toUpperCase()})},CamelToDash:function(t){return t.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()},LabelText:function(t){return t.label||p.CamelToTitle(t.property)}};d.helpers=p,d.settings={classes:{container:[],input:[],label:[]},attributes:{container:{class:"$type"},input:{id:"$property_clean",name:"$property_clean",type:"$type",ngModel:"$data['$property']",placeholder:"$placeholder"},label:{}},container:'<div class="autofields" ng-form name="$form"></div>',scope:{}},d.setAttributes=function(n,r,a,t){-1!=r.property.indexOf(".")&&"$data['$property']"==t.ngModel&&(t.ngModel="$data.$property"),angular.forEach(t,function(t,e){t&&"string"==typeof t&&(t=t.replace(/\$form/g,n.formStr).replace(/\$schema/g,n.schemaStr).replace(/\$type/g,r.type||"text").replace(/\$property_clean/g,r.property.replace(/\[|\]|\./g,"")).replace(/\$property/g,r.property).replace(/\$data/g,n.dataStr).replace(/\$placeholder/g,null!=r.placeholder?r.placeholder:p.LabelText(r))),a.attr(p.CamelToDash(e),t)})};d.field=function(n,r,t,e){var a,i,o,l,s,u={fieldContainer:(i=n,o=r,s=angular.element("<div/>"),l=angular.extend({},d.settings.attributes.container,i.options.attributes.container,l),d.setAttributes(i,o,s,l),s.addClass((i.options||d.settings).classes.container.join(" ")),s),label:""!=r.label?(l=n,i=r,s=angular.element("<label/>"),a=angular.extend({},d.settings.attributes.label,l.options.attributes.label,a),d.setAttributes(l,i,s,a),s.addClass((l.options||d.settings).classes.label.join(" ")),s.html(p.LabelText(i)),s):null,input:function(t,e,n,r){n=angular.element(n);return r=angular.extend({},d.settings.attributes.input,t.options.attributes.input,r,e.attr),d.setAttributes(t,e,n,r),n.addClass((t.options||d.settings).classes.input.join(" ")),n}(n,r,t,e)};u.fieldContainer.append(u.label).append(u.input);var c=[];return angular.forEach(f,function(t,e){u=t(n,r,u,c),c.push(e)}),u},d.updateScope=function(n){angular.forEach(d.settings.scope,function(e,t){n[t]="function"==typeof e?function(){var t=Array.prototype.slice.call(arguments,0);t.unshift(n),e.apply(this,t)}:e})};var a={};d.registerHandler=function(t,e){t=Array.isArray(t)?t:[t],angular.forEach(t,function(t){a[t]=e})};var f={};return d.registerMutator=function(a,i,o){f[a]&&!o.override||(f[a]=function(t,e,n,r){if(o&&"string"==typeof o.require&&-1==r.indexOf(o.require)&&(n=f[o.require]),-1==r.indexOf(a))return i(t,e,n)})},d.createField=function(t,e,n){var r=null==e.type?a.text:a[e.type];if(null!=r)return r(t,e,n);console.warn(e.type+" not supported - field ignored")},d.$get=function(){return{settings:d.settings,createField:d.createField,updateScope:d.updateScope}},d}).directive("autoFields",["$autofields","$compile",function(o,l){return{restrict:"E",priority:1,replace:!0,compile:function(){return function(e,t,n){var r={schemaStr:n.fields||n.autoFields,optionsStr:n.options,dataStr:n.data,formStr:n.form||"autofields",classes:n.class,container:null,formScope:null},a={extendDeep:function(n){return angular.forEach(arguments,function(t){t!==n&&angular.forEach(t,function(t,e){n[e]&&n[e].constructor&&n[e].constructor===Object?a.extendDeep(n[e],t):n[e]=t})}),n}};r.options=angular.copy(o.settings);function i(t){t=t||e[r.schemaStr],r.container.html(""),angular.forEach(t,function(t,e){e=o.createField(r,t,e);r.container.append(e)}),null!=r.formScope&&r.formScope.$destroy(),r.formScope=e.$new(),r.formScope.data=e[r.dataStr],r.formScope.fields=t,o.updateScope(r.formScope),l(r.container)(r.formScope)}e.$watch(r.optionsStr,function(t,e){a.extendDeep(r.options,t),t!==e&&i()},!0),e.$watch(r.schemaStr,function(t){i(t)},!0),e.$watch(r.formStr,function(t){r.container.attr("name",r.formStr)}),e.$watch(function(){return n.class},function(t){r.classes=n.class,r.container.attr("class",r.classes)}),r.container=angular.element(r.options.container),r.container.attr("name",r.formStr),r.container.attr("class",r.classes),t.replaceWith(r.container)}}}}]),angular.module("autofields.standard",["autofields.core"]).config(["$autofieldsProvider",function(i){i.settings.fixUrl=!0,i.registerHandler(["text","email","url","date","number","password"],function(t,e,n){var r=i.field(t,e,"<input/>"),t=e.fixUrl||t.options.fixUrl;return"url"==e.type&&t&&r.input.attr("fix-url",""),r.fieldContainer}),i.settings.defaultOption="Select One",i.registerHandler("select",function(t,e,n){var r='<select><option value="">'+(e.defaultOption||t.options.defaultOption)+"</option></select>",a={ngOptions:e.list};return i.field(t,e,r,a).fieldContainer}),i.settings.textareaRows=3,i.registerHandler("textarea",function(t,e,n){var r=e.rows||t.options.textareaRows;return i.field(t,e,"<textarea/>",{rows:r}).fieldContainer}),i.registerHandler("checkbox",function(t,e,n){e=i.field(t,e,"<input/>");return e.label&&e.label.prepend(e.input),e.fieldContainer}),i.settings.displayAttributes=(i.settings.displayAttributes||[]).concat(["ng-if","ng-show","ng-hide"]),i.registerMutator("displayAttributes",function(t,e,n){return e.attr&&angular.forEach(i.settings.displayAttributes,function(t){var e=n.input.attr(t);e&&(n.fieldContainer.attr(t,e),n.input.removeAttr(t))}),n})}]).directive("fixUrl",[function(){return{restrict:"A",require:"ngModel",link:function(t,e,n,r){var a=/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w\.\-\?\=\&]*)$/i;e.bind("blur",function(){var e=r.$modelValue;null!=e&&(angular.forEach(r.$formatters,function(t){e=t(e)}),r.$viewValue=e,r.$render())});e=function(t){var e,n;return a.test(t)&&(n=null!=(e=t.match(a))[1]&&""!=e[1]?e[1]:"http://",n+=e[2]+"."+e[3],void 0!==e[4]&&(n+=e[4]),t=n),t};r.$formatters.push(e),r.$parsers.unshift(e)}}}]),angular.module("autofields.validation",["autofields.core"]).config(["$autofieldsProvider",function(t){var o=function(t){return t.replace(/([A-Z])/g," $1").replace(/^./,function(t){return t.toUpperCase()})};t.settings.validation={enabled:!0,showMessages:!0,defaultMsgs:{required:"This field is required",minlength:"This is under minimum length",maxlength:"This exceeds maximum length",min:"This is under the minumum value",max:"This exceeds the maximum value",email:"This is not a valid email address",valid:""},invalid:"$form.$property_clean.$invalid && $form.$property_clean.$dirty",valid:"$form.$property_clean.$valid"},t.settings.attributes.container.ngClass="{'invalid':"+t.settings.validation.invalid+", 'valid':"+t.settings.validation.valid+"}",t.registerMutator("validation",function(r,a,i){return i.validation=r.options.validation.enabled&&!1!==a.validate,i.validation?(i.msgs=[],r.options.validation.showMessages&&(angular.forEach(angular.extend({},r.options.validation.defaultMsgs,a.msgs),function(t,e){var n;(a.msgs&&null!=a.msgs[e]||a.type==e||a.attr&&(null!=a.attr[e]||null!=a.attr["ng"+o(e)]))&&(n=a.property.replace(/\[|\]|\./g,""),i.msgs.push("("+r.formStr+"."+n+".$error."+e+"? '"+t+"' : '')"))}),i.validMsg=(a.msgs&&a.msgs.valid?a.msgs:r.options.validation.defaultMsgs).valid,i.msgs.length),i):(i.fieldContainer.removeAttr("ng-class"),i)})}]),angular.module("autofields",["autofields.standard","autofields.validation"]),angular.module("autoFields",["autofields"]);