UNPKG

mk-databridge

Version:

A jQuery plugin to read/set data to forms. Supports nested objects, boolean and array checkboxes, on fly data formatting, non-input elements.

1 lines 4.25 kB
!function(t){var e="set",n="get",r={$root:null,$inputs:null,options:null,data:null,errorsList:[],init:function(e,n){return this.clear(),this.options=t.extend({},a,n),this.root=e,this.$root=t(e),this.inputs=_collectElements(this.root,this.options),this},clear:function(){return this.$root=null,this.$inputs=null,this.options=null,this.data=null,this._checkboxes={},this},set:function(t){this.data=t;var n=this;[].forEach.call(this.root.querySelectorAll("[type=radio]"),function(t){t.checked=!1}),this.errorsList=[];for(var r in this.inputs)if(this.inputs.hasOwnProperty(r)){var o=this.inputs[r],a=i(r,t);o.mapper?a=o.mapper.call(o.el,a,e,this):"checkbox"===o.type?(void 0===a&&(a=!1),null===a&&(a=!1)):(void 0===a&&(a=this.options.undefined),null===a&&(a=this.options["null"])),s.call(n,o,a)}},get:function(){this.data={};var t=this;this.errorsList=[];for(var e in this.inputs)if(this.inputs.hasOwnProperty(e)){var r=this.inputs[e],i=f.call(t,r);if(r.mapper)try{mappedval=r.mapper.call(r.el,i,n,this),i=mappedval}catch(o){this.errorsList.push({varname:e,value:i,input:r.el,message:o.message})}p(t.data,e,i)}return this.errorsList.length&&console.warn("Errors during data mapping occured!",this.errorsList),this.data},getErrors:function(){return this.errorsList.length?this.errorsList:null}};_collectElements=function(t,e){var n=t.querySelectorAll("[name], [data-name]"),r={};return[].forEach.call(n,function(t){var n=o(t);r[n]||(r[n]={});var i=r[n];i.type=_type(t),i.isInput="INPUT"===t.tagName,i.mapper=u(t,e),"checkbox"===t.type||"radio"===t.type?(i.el||(i.el=[]),i.el.push(t)):i.el=t}),r},_type=function(t){var e=t.tagName.toLowerCase();if("input"===e){var n=t.getAttribute("type");n&&(e=n)}return e},_collectCheckboxes=function(t){var e=t.querySelectorAll("[name][type=checkbox]"),n={};return e.forEach(function(t){var e=o(t);n[e]||(n[e]=[]),n[e].push(t)}),n};var i=function(t,e){for(var n=t.split("."),r=e||window,i=0;i<n.length&&void 0!==r;i++)r=r[n[i]];return r},o=function(t){var e=t.getAttribute("name")||t.getAttribute("data-name");return e=e.replace(/\[/g,"."),e=e.replace(/\]/g,"")};t.fn.databridge=function(t,e){if(r.init(this.get(0),e),"string"==typeof t){switch(t){case"errors":return r.getErrors()}return this}return t?(r.set(t),this):r.get()};var a={mappers:window,undefined:"","null":"",errorClass:"databridge-error",booleanCeckboxes:!0,mapUndefined:!1,mapNull:!1,undefinedVal:"undefined",nullVal:"null"},u=function(t,e){t instanceof Array&&(t=t[0]);var n=t.getAttribute("data-map")||t.getAttribute("data-mapper");if(!n)return null;var r=i(n,e&&e.mappers||window);if(!r)throw l('Mapper "'+n+'" does not found');if("function"!=typeof r)throw l('Mapper "'+n+'" is not a function');return r},l=function(t){var e=new Error(t);return e.name="Mapper Error",e},s=function(t,e){var n;return n=c[t.type]?t.type:t.isInput?"input":"element",c[n].call(this,t.el,e)},c={input:function(t,e){t.value=e},element:function(t,e){t.innerHTML=e},textarea:function(t,e){t.value=e,t.innerHTML=e},checkbox:function(t,e){if(!(e instanceof Array)&&"boolean"!=typeof e)throw new Error("checkbox value must be an Array or Boolean");if(1===t.length)t[0].checked=Boolean(e);else{if(e instanceof Array)var n=function(t){return e.indexOf(t)!==-1};else var n=function(){return Boolean(e)};t.forEach(function(t){t.checked=n(t.value)})}},radio:function(t,e){t.forEach(function(t){String(e)===t.value&&(t.checked=!0)})},select:function(t,e){var n=t.querySelectorAll("option");e=String(e);for(var r=0;r<n.length;r++)e===n[r].value?n[r].selected=!0:n[r].selected=!1}},h=function(t){return/^\d+$/.test(t)},p=function(t,e,n){var r=e.split(".");if(1===r.length)return void(t[e]=n);for(var i,o=t,a=0;a<r.length;a++)i=r[a],h(i)&&(i=Number(i)),o[i]||(a+1===r.length?o[i]=n:o[i]=h(r[a+1])?[]:{}),o=o[i]},f=function(t){var e;return e=d[t.type]?t.type:t.isInput?"input":"element",d[e].call(this,t.el)},d={element:function(t){return t.innerHTML},input:function(t){return t.value},textarea:function(t,e){return t.value},checkbox:function(t){if(1===t.length)e=t[0].checked;else{var e=[];t.forEach(function(t){t.checked&&e.push(t.value)})}return e},radio:function(t){var e=null;return t.forEach(function(t){t.checked&&(e=t.value)}),e},select:function(e){return t(e).val()}}}(jQuery);