UNPKG

vormjs

Version:

Write your forms in JSON and HTML, use the same API.

2 lines 20 kB
"use strict";var _slicedToArray=function(e,t){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e)){var n=[],r=!0,o=!1,i=void 0;try{for(var l,a=e[Symbol.iterator]();!(r=(l=a.next()).done)&&(n.push(l.value),!t||n.length!==t);r=!0);}catch(u){o=!0,i=u}finally{try{!r&&a["return"]&&a["return"]()}finally{if(o)throw i}}return n}throw new TypeError("Invalid attempt to destructure non-iterable instance")};!function(){angular.module("vorm",[])}(),function(){angular.module("vorm").factory("VormEvent",["$document","$window",function(e,t){var n=void 0;try{{new t.CustomEvent("foo")}n=function(e,n){return new t.CustomEvent(e,{detail:n,bubbles:!0})}}catch(r){n=function(t,n){var r=e[0].createEvent("CustomEvent");return r.initCustomEvent(t,!0,!0,n),r}}return n}])}(),function(){angular.module("vorm").factory("VormFieldCtrl",["$rootScope","VormEvent","VormValueType","VormValueScope",function(e,t,n,r){return function(e,o){function i(){var e=void 0;switch(p){case n.SINGLE:e=null;break;case n.LIST:e=[null];break;case n.NAMED:e={}}return e}function l(e,t){var n=e.$viewChangeListeners,r=e.$modelValue;e.$viewChangeListeners=[],e.$modelValue=t,e.$$writeModelToScope(),e.listeners=n,e.$modelValue=r}function a(){var t=h[e];switch(p){case n.SINGLE:d[0]&&l(d[0],t);break;case n.LIST:_.each(d,function(e,n){l(e,t[n])});break;case n.NAMED:var r=d.concat();_.each(t,function(e,t){var n=_.find(d,{$name:t});n&&l(n,e),_.pull(r,n)}),_.each(r,function(e){l(e,void 0)})}}function u(){var e=void 0;switch(p){case n.SINGLE:e=d[0]?d[0].$modelValue:void 0;break;case n.LIST:e=_.pluck(d,"$modelValue");break;case n.NAMED:e={},_.each(d,function(t){e[t.$name]=t.$modelValue})}return e}function c(){h[e]=u(),m.triggerViewChange()}function v(t){var n=u();return h[e]=n,m.triggerModelChange(),t}var m={},d=[],s=[],f=[],g="vorm-field-",p=n.SINGLE,y=void 0,h=new r;m.triggerModelChange=function(){o.dispatchEvent(new t("modelchange",{name:e})),_.invoke(f,"call",null,e)},m.triggerViewChange=function(){o.dispatchEvent(new t("viewchange",{name:e})),_.invoke(s,"call",null,e)},m.getName=function(){return e},m.setName=function(){e=arguments[0]},m.addModel=function(t){d.push(t),t.$viewChangeListeners.push(c),t.$formatters.push(v),h[e]=u()},m.removeModel=function(t){_.pull(d,t),_.pull(t.$viewChangeListeners,c),_.pull(t.$formatters,v),h[e]=u()},m.getModels=function(){return d},m.getValue=function(){return h[e]},m.getValueType=function(){return p},m.setValueType=function(t){if(-1===[n.SINGLE,n.LIST,n.NAMED].indexOf(t))throw new Error("Unsupported VormValueType: "+n);p=t,h[e]=i()},m.setValue=function(t){h[e]=t,a()},m.setEmpty=function(){h[e]=i()},m.isRequired=function(){return y},m.setRequired=function(e){y=!!e},m.isEmpty=function(){return d.every(function(e){return e.$isEmpty(e.$viewValue)})},m.setValueScope=function(t){var n=h[e];h&&h.$destroy(),h=t,h[e]=n},m.getValueScope=function(){return h};var V=_("valid invalid dirty pristine touched untouched required empty".split(" ")).map(function(e){return g+e}).zipObject().mapValues(function(e,t){var n=t.substr(g.length);return m["is"+_.capitalize(n)]()});return m.getClassObj=function(){return V.value()},m.setEmpty(),m.viewChangeListeners=s,m.modelChangeListeners=f,"valid invalid dirty pristine touched untouched".split(" ").forEach(function(e){var t=_.capitalize(e),n="is"+t,r="$"+e,o="set"+t,i=-1!==["valid","pristine","untouched"].indexOf(e)?"every":"some";m[n]=function(){return d[i](function(e){return e[r]})},"valid"!==e&&"invalid"!==e&&(m[o]=function(){var e=arguments;d.forEach(function(t){t["$"+o].apply(t,e)})})}),h[e]=i(),m}}])}(),function(){angular.module("vorm").factory("VormModelDelegate",[function(){return function(e){var t={},n=null;return t.value=null,t.getName=function(){return e},t.setNgModel=function(t){n=t,n&&(n.$name=e)},t.unsetNgModel=function(){n=null},t.getNgModel=function(){return n},t.clearViewValue=function(){n.$setViewValue(null),n.$render()},t.getViewValue=function(){return n?n.$viewValue:t.value},t}}])}(),function(){angular.module("vorm").factory("VormValueScope",["$rootScope",function(e){return function(){var t=e.$new();return t}}])}(),function(){angular.module("vorm").constant("VormValueType",{SINGLE:"single",LIST:"list",NAMED:"named"})}(),function(){angular.module("vorm").directive("ngModel",["VormFieldCtrl",function(e){return{require:["ngModel","^?vormField","^?vormForm"],compile:function(){return function(t,n,r,o){var i=_slicedToArray(o,3),l=i[0],a=i[1],u=i[2];(a||u)&&(a||(a=new e(r.name||r.ngModel,n[0]),u&&(u.addField(a),t.$on("$destroy",function(){u.removeField(a)}))),a.addModel(l),t.$on("$destroy",function(){a.removeModel(l)}))}}}}])}(),function(){angular.module("vorm").directive("vormChange",["$parse",function(e){return{link:function(t,n,r){function o(e,n){i(t,{$event:e,$name:e.detail?e.detail.name:n})}var i=e(r.vormChange);n.bind("viewchange",o)}}}])}(),function(){angular.module("vorm").directive("vormControl",["$document",function(e){var t=function(){var t=e[0].createElement("div");return _(["","ms","moz","webkit"]).map(function(e){return e?e+"MatchesSelector":"matches"}).find(function(e){return e in t})}();return{restrict:"E",require:["vormControl","^vormField","^vormFieldConfig","^vormFocusableList","^vormFieldTemplate"],controller:["$scope","$element","$attrs",function(e,n,r){var o,i,l,a,u=this,c=Math.random().toString(36).slice(2);u.link=function(v){o=v[0],i=v[1],l=v[2],a=v[3],u.invokeData=i.invokeData,u.getData=i.getData,u.getConfig=i.getConfig,l.addId(c),a.getModelCompiler()(e,function(o){var i=void 0,l="input,keygen,meter,output,progress,select,textarea",a=n.find("vorm-control-replace"),c=e.$eval(r.delegate);a.replaceWith(o),o[0].className+=" "+a[0].className,i=o[0][t](l)?o:angular.element(o[0].querySelector(l)),i.attr("id",u.getInputId()),e.$$postDigest(function(){c.setNgModel(o.controller("ngModel"))}),e.$on("$destroy",function(){c.unsetNgModel()})})},u.isRequired=function(){return o&&o.isRequired()},u.getInputId=function(){return c},u.getViewValue=function(){return e.$eval(r.delegate).getViewValue()},u.getDisplayMode=function(){return i.getDisplayMode()},u.getOptions=angular.version.minor>=4?function(){return u.invokeData("options")}:function(){var e=void 0;return function(){var t=u.invokeData("options");return e===t||angular.equals(e,t)||(e=t),e}}(),e.$on("$destroy",function(){l.removeId(c)})}],controllerAs:"vormControl",link:function(e,t,n,r){r[0].link(r.slice(1))}}}])}(),function(){angular.module("vorm").directive("vormControlList",["VormModelDelegate","$document",function(e,t){return{require:["vormControlList","^vormFieldConfig","^?vormFocusableList","^vormField"],restrict:"E",controller:["$scope",function(n){function r(e){var t=n.$watchCollection(m.getModels,function(){m.triggerViewChange(),e(),t()})}function o(){if(v){var e=v.getId(),n=t[0].getElementById(e);n&&n.focus()}}function i(t){var n=void 0,r=void 0;switch(n=new e(t),m.getValueType()){case"list":r=m.getValue()[a.length];break;case"named":r=m.getValue()[t];break;case"single":r=m.getValue()}n.value=r,a.push(n)}var l=this,a=[],u=0/0,c=void 0,v=void 0,m=void 0;l.link=function(e){c=e[0],v=e[1],m=e[2],n.$watch(c.getLimit,function(e){l.setLimit(e)}),n.$watchCollection(function(){var e=void 0,t=m.getValue();switch(m.getValueType()){default:e=_.keys(t);break;case"single":e=null}return e},function(e){a=[],e||i(),_.each(e,function(e){i(e)})})},l.getDelegates=function(){return a},l.clearDelegate=function(e){e.clearViewValue()},l.getLimit=function(){return u},l.setLimit=function(e){u=Number(e)},l.reachedLimit=function(){return u>0&&a.length>=u},l.isClearButtonVisible=function(){return"list"===m.getValueType()},l.handleCreateClick=function(){m.setValue(m.getValue().concat(null)),r(o)},l.handleClearClick=function(e){if(1===a.length)e.clearViewValue();else{var t=m.getValue(),n=_.find(m.getModels(),{model:e.getNgModel()});t.splice(n,1),m.setValue(t),r(o)}}}],controllerAs:"vormControlList",link:function(e,t,n,r){r.shift().link(r)}}}])}(),function(){angular.module("vorm").directive("vormDelegateButton",[function(){return{require:["vormDelegateButton","^vormFieldConfig","^vormControlList","^vormField"],template:'<button class="vorm-delegate-button" type="button" ng-click="vormDelegateButton.handleClick()" ng-disabled="vormDelegateButton.isDisabled()" ng-show="vormDelegateButton.isVisible()">{{vormDelegateButton.getLabel()}}</button>',replace:!0,controller:[function(){var e=this,t=void 0,n=void 0,r=void 0;e.link=function(e){t=e[0],n=e[1],r=e[2]},e.handleClick=function(){n.handleCreateClick()},e.isDisabled=function(){return n.reachedLimit()},e.isVisible=function(){return"list"===r.getValueType()},e.getLabel=function(){var e=t.getConfig(),n=e?e.valueType:null,r=n&&n.addLabel?t.invoke(n.addLabel):"";return r}}],link:function(e,t,n,r){r.shift().link(r)},controllerAs:"vormDelegateButton"}}])}(),function(){angular.module("vorm").directive("vormDisplay",["vormTemplateService",function(e){return{restrict:"E",require:["vormDisplay","^vormControl","^vormFieldConfig"],controller:["$scope","$element",function(t,n){var r=this,o=void 0,i=void 0;r.link=function(l){var a=void 0,u=void 0,c=void 0;o=l[0],i=l[1],c=i.getConfig(),a=c.template?c.template.display:null,u=e.getDisplayCompiler(c.type,a),u(t,function(e){n.append(e)}),r.getViewValue=o.getViewValue,r.getModelValue=o.getModelValue}}],controllerAs:"vormDisplay",link:function(e,t,n,r){r.shift().link(r)}}}])}(),function(){angular.module("vorm").directive("vormField",["VormFieldCtrl",function(e){return{scope:!0,require:["vormField","^?vormForm"],controller:["$scope","$element","$attrs",function(t,n,r){var o=t.$eval(r.vormField)||r.name||r.ngModel,i=this;angular.extend(i,new e(o,n[0])),i.link=function(e){var n=_slicedToArray(e,1),r=n[0];r&&(r.addField(i),t.$on("$destroy",function(){r.removeField(i)}))}}],controllerAs:"vormField",link:function(e,t,n,r){r.shift().link(r)}}}])}(),function(){angular.module("vorm").directive("vormFieldConfig",["vormInvoke",function(e){return{require:["vormFieldConfig","vormField","^?vormForm"],controller:["$scope","$attrs",function(t,n){function r(){var e=void 0;return a?e=a.getValues():l&&(e={},e[l.getName()]=l.getValue()),e}var o=this,i=t.$eval(n.vormFieldConfig),l=void 0,a=void 0;o.link=function(e){l=e[0],a=e[1],l.setName(i.name),void 0!==i.valueType&&("string"==typeof i.valueType?l.setValueType(i.valueType):void 0!==i.valueType.type&&l.setValueType(i.valueType.type)),_.isArray(i.required)||"function"==typeof i.required?t.$watch(function(){return o.invoke(i.required)},function(e){l.setRequired(!!e)}):l.setRequired(i.required||!1),i.defaults&&l.setValue(o.invoke(i.defaults))},o.invoke=function(t){return e(t,{$values:r()})},o.invokeExpr=function(t){return e.expr(t,{$values:r()},l.getValueScope())},o.getConfig=function(){return i},o.invokeData=function(e){return o.invoke(i.data[e])},o.getLimit=function(){var e=1;return"list"===l.getValueType()&&(e=-1,i.valueType&&void 0!==i.valueType.limit&&(e=o.invoke(i.valueType.limit))),e},o.getDisplayMode=function(){return o.invokeExpr(i.disabled)?"display":"edit"}}],controllerAs:"vormFieldConfig",link:function(e,t,n,r){r.shift().link(r)}}}])}(),function(){angular.module("vorm").directive("vormFieldTemplate",["vormTemplateService",function(e){var t=angular.element(e.getDefaultTemplate());angular.element(t[0].querySelectorAll("vorm-replace")).replaceWith(e.getDefaultControlTemplate()),t.attr("vorm-field-config","vormFieldTemplate.getConfig()"),t.attr("vorm-focusable-list","");var n=t[0].outerHTML;return{scope:!0,restrict:"E",template:n,replace:!0,controller:["$scope","$attrs","$element",function(t,n,r){var o=this,i=t.$eval(n.config)||{},l=void 0;if(i=_.defaults(angular.copy(i),{name:n.name,type:n.type,label:n.label,template:t.$eval(n.template),required:t.$eval(n.required),data:t.$eval(n.data)||{}}),!i.name||!i.type)throw new Error("Missing one of required arguments: name, type ");l=e.getModelCompiler(i.type,i.modelTemplate),r.attr("vorm-field",i.name),o.getConfig=function(){return i},o.getModelCompiler=function(){return l}}],controllerAs:"vormFieldTemplate"}}])}(),function(){angular.module("vorm").directive("vormFieldWrapper",["vormTemplateService",function(e){var t=angular.element(e.getDefaultTemplate());t.find("vorm-replace").append("<ng-transclude></ng-transclude>"),t.attr("vorm-field-config","vormFieldWrapper.getConfig()"),t.attr("vorm-focusable-list","");var n=t[0].outerHTML;return{restrict:"A",transclude:!0,template:n,replace:!0,controller:["$attrs",function(e){var t=this,n={name:e.name,label:e.label};t.getConfig=function(){return n}}],controllerAs:"vormFieldWrapper"}}])}(),function(){angular.module("vorm").directive("vormFieldset",["vormInvoke",function(e){return{restrict:"E",require:["vormFieldset","^?vormForm"],template:'\n <fieldset>\n <vorm-field-template config="field" ng-repeat="field in vormFieldset.getFields() | filter:vormFieldset.isVisible:field">\n </vorm-field-template>\n </fieldset>\n ',replace:!0,controller:["$scope","$attrs",function(t,n){function r(){var e={};return i&&(e=i.getValues()),e}var o=this,i=void 0,l=void 0;o.link=function(e){i=e[0],i&&(l=i.getValueScope())},o.getFields=function(){return t.$eval(n.fields)},o.isVisible=function(t){return null===t.when||void 0===t.when?!0:!!e.expr(t.when,{$values:r()},l)}}],controllerAs:"vormFieldset",link:function(e,t,n,r){r.shift().link(r)}}}])}(),function(){angular.module("vorm").directive("vormFocusableList",[function(){return{controller:[function(){var e=this,t=[];e.addId=function(e){t.push(e)},e.removeId=function(e){_.pull(t,e)},e.getId=function(){return t[t.length-1]}}]}}])}(),function(){angular.module("vorm").directive("vormForm",["VormValueScope",function(e){return{scope:!0,require:["form"],controller:["$element",function(t){function n(){var e=arguments;_.each(l,function(t){t.apply(o,e)})}function r(e){return _.find(i,function(t){return t.getName()===e})}var o=this,i=[],l=[],a=[],u=new e;return o.addField=function(e){i.push(e),e.viewChangeListeners.push(n),e.setValueScope(u)},o.removeField=function(e){_.pull(i,e),_.pull(e.viewChangeListeners,n)},o.getFields=function(){return i},o.getValues=function(){var e=_(i).indexBy(function(e){return e.getName()}).mapValues(function(e){return e.getValue()}).value();return e},o.getValue=function(e){return r(e).getValue()},o.setValue=function(e,t){r(e).setValue(t)},o.getValueScope=function(){return u},o.changeListeners=l,o.submitListeners=a,"valid invalid dirty pristine touched untouched".split(" ").forEach(function(e){var t=e.substr(0,1).toUpperCase()+e.substr(1),n="is"+t,r="set"+t,l=-1!==["valid","pristine","untouched"].indexOf(e)?"every":"some";o[n]=function(){return i[l](function(e){return e[n]()})},"valid"!==e&&"invalid"!==e&&(o[r]=function(){var e=arguments;i.forEach(function(t){t[r].apply(t,e)})})}),t.bind("submit",function(){_.invoke(a,"call",null,o.getValues())}),o}],controllerAs:"vormForm"}}])}(),function(){angular.module("vorm").factory("vormInvoke",["$injector","$parse",function(e,t){function n(t,n){var r=void 0;return t?r=_.isArray(t)&&"function"==typeof _.last(t)||void 0!==t.$inject?e.invoke(t,null,n?angular.copy(n):null):"function"==typeof t?t():t:t}var r=function(e,t){return n(e,t)};return r.expr=function(e,r,o){var i=void 0;return i="string"==typeof e?t(e)(o,r):n(e,r)},r}])}(),function(){angular.module("vorm").directive("vormLabel",[function(){return{require:["vormLabel","^vormFieldConfig","^vormFocusableList"],template:'<label class="vorm-field-label">{{vormLabel.getLabel()}}</label>',replace:!0,controller:["$scope","$element",function(e,t){var n=this,r=void 0,o=void 0;n.link=function(n){r=n[0],o=n[1],e.$watch(o.getId,function(e){t.attr("for",e)})},n.getLabel=function(){return r.invoke(r.getConfig().label)}}],controllerAs:"vormLabel",link:function(e,t,n,r){r[0].link(r.slice(1))}}}])}(),function(){angular.module("vorm").directive("vormSubmit",["$parse",function(e){return{require:["vormForm"],link:function(t,n,r,o){function i(){u(t,{$values:a.getValues()})}var l=_slicedToArray(o,1),a=l[0],u=void 0;u=e(r.vormSubmit),n.bind("submit",i)}}}])}(),function(){angular.module("vorm").provider("vormTemplateService",[function(){function e(e){u.model=_.mapValues(u.model,function(t,n){return e(t,n)})}function t(e){u.display=_.mapValues(u.display,function(t,n){return e(t,n)})}function n(e){var t=e(angular.element(i));t.attr("vorm-field",""),t.find("vorm-control").attr("limit","vormFieldConfig.getLimit()"),i=t[0].outerHTML}function r(e){var t=angular.element("<p></p>");t.append(e(angular.element(l))),l=t[0].innerHTML}function o(e,t,n){u.model[e]=t,n&&(u.display[e]=n)}var i=void 0,l=void 0,a={model:{},display:{}},u={model:{},display:{"null":angular.element("<span>{{vormDisplay.getViewValue()}}</span>")}},c={};return i='<div ng-class="vormField.getClassObj()"><vorm-label></vorm-label><vorm-replace></vorm-replace></div>',l='<vorm-control-list><vorm-control ng-repeat="delegate in vormControlList.getDelegates()" delegate="delegate"><vorm-edit ng-show="vormControl.getDisplayMode()===\'edit\'"><vorm-control-replace></vorm-control-replace><button class="vorm-control-clear-button" type="button" ng-click="vormControlList.handleClearClick(delegate)" ng-show="vormControlList.isClearButtonVisible()">x</button></vorm-edit><vorm-display ng-show="vormControl.getDisplayMode()===\'display\'"></vorm-display></vorm-control><vorm-delegate-button></vorm-delegate-button></vorm-control-list>',n(function(){return angular.element(i)}),r(function(){return angular.element(l)}),{$get:["$compile",function(e){function t(n,r,o){var i=void 0,l=a[n];if(i=o?e(o):l[r],i||"display"!==n||(i=t(n,null)),!i)throw new Error(""+_.capitalize(n)+" template for "+r+" not found");return i}return c.getDefaultTemplate=function(){return i},c.getDefaultControlTemplate=function(){return l},c.getModelCompiler=function(e,n){return t("model",e,n)},c.getDisplayCompiler=function(e,n){return t("display",e,n)},a.model=_.mapValues(u.model,function(t){var n=void 0;return _.some(t,function(e){var t=void 0;return e.hasAttribute("ng-model")?n=angular.element(e):(t=e.querySelector("[ng-model]"))&&(n=angular.element(t)),!!n}),n||(n=t),n.attr("ng-model","delegate.value"),n.attr("name","{{delegate.getName()}}"),n.attr("ng-required","vormControl.isRequired()"),e(t)}),a.display=_.mapValues(u.display,function(t){return e(t)}),c}],modifyModelTemplates:e,modifyDisplayTemplates:t,modifyControlTemplate:r,modifyTemplate:n,registerType:o}}])}(),function(){angular.module("vorm").config(["vormTemplateServiceProvider",function(e){var t=_("date datetime datetime-local email month number password search tel text time url week checkbox".split(" ")).zipObject().mapValues(function(e,t){var n=_.includes("text search tel url email number password".split(" "),t)?"placeholder=\"{{vormControl.invokeData('placeholder')}}\"":"",r='<input type="'+t+'" '+n+" ng-model/>";return"checkbox"===t&&(r='<label for="{{vormControl.getInputId()}}">'+r+'{{vormControl.invokeData("checkboxLabel")}}</label>'),r}).value();for(var n in t)e.registerType(n,angular.element(t[n]))}])}(),function(){angular.module("vorm").config(["vormTemplateServiceProvider",function(e){var t=angular.element('<div class="vorm-radio-group"><label ng-repeat="option in vormControl.getOptions()"><input type="radio" ng-model name="{{::vormField.getName()}}" value="{{::option.value}}"/>{{::option.label}}</label></div>');e.registerType("radio",t)}])}(),function(){angular.module("vorm").config(["vormTemplateServiceProvider",function(e){var t=angular.element('<select ng-options="option.value as option.label for option in vormControl.getOptions()"><option value="" ng-show="!!vormControl.invokeData(\'notSelectedLabel\')">{{vormControl.invokeData(\'notSelectedLabel\')}}</option></select>');e.registerType("select",t)}])}(),function(){angular.module("vorm").config(["vormTemplateServiceProvider",function(e){var t=angular.element("<textarea placeholder=\"{{vormControl.invokeData('placeholder')}}\"></textarea>");e.registerType("textarea",t)}])}(); //# sourceMappingURL=vorm.min.js.map