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