forked from dshimkoski/jquery-inputs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.inputs.min.js
13 lines (12 loc) · 2.43 KB
/
jquery.inputs.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
jQuery-inputs 2013-01-10T00:06 commit ID 1e95cf9734505ab9eb231c2317f0bc7c030bff1a
A jQuery plugin that allows set/get on form inputs using hierarchical JSON structures
https://github.com/willowsystems/jquery-inputs
MIT license: http://www.opensource.org/licenses/mit-license.php
Copyright (c) 2012, Willow Systems Corporation (ddotsenko -[at]- willow-systems.com )
Copyright (c) 2011, Denny Shimkoski (denny.webdev -[at]- gmail -[dot]- com )
*/
(function(){var j=function(c){var j=function(a,c){if(0===c.length)return[a];for(var b=c[0],g=1,d=c.length;d>g;)a=a.split(c[g]).join(b),g+=1;return a.split(b)},m=function(a){return null!=a&&null!=a.separators?a.separators:["|","_",":","."]},n={set:function(a,k){var b=c(this),g=m(k);b.find(":input").add(b.filter(":input")).each(function(){var d=c(this),b=j(d.prop("name"),g),l=!0,e=a;b.forEach(function(a){try{if(e=e[a],void 0==e)throw new TypeError("jQuery.Input: Path traversal in data object for '"+
a+"' of '"+d.prop("name")+"' was cut short by an incompatible object");}catch(c){l=!1}});d.is(":checkbox, :radio")?(d.prop("checked",!1).data("defaultValue",!1),l&&(Array.isArray(e)?e.some(function(a){return d.prop("value")==new String(a)})&&d.prop("checked",!0).data("defaultValue",!0):d.prop("value")==new String(e)&&d.prop("checked",!0).data("defaultValue",!0))):l?d.val(e).data("defaultValue",e):(b=d[0].defaultValue,d.val(b).data("defaultValue",b))})},get:function(a,k){var b;b=a?c(a,this):c(this);
b=b.filter(":input").add(b.find(":input"));var g={},d=m(k);c.each(b.serializeArray(),function(){var a=j(this.name,d),b=this.value,e=a.pop(),h=g,f=h;a.forEach(function(d){f=h[d];if(null==f)h[d]={};else if(!c.isPlainObject(f))throw new TypeError("Value '"+b+"' cannot be assigned to key '"+a.join(".")+"' as another element on this path terminates with an non-object object.");h=h[d]});f=h[e];if(null==f)h[e]=b;else if(b!=f)if(Array.isArray(f)&&"string"==typeof b)-1==f.indexOf(b)&&f.push(b);else if("string"==
typeof b&&"string"==typeof f)h[e]=[f,b];else throw new TypeError("Value cannot be assigned to key '"+a.join(".")+"' as another element on this path continues with object chain.");});return g}};c.fn.inputs=function(a){a||(a="get");if(n[a])return n[a].apply(this,Array.prototype.slice.call(arguments,1));c.error('Method "'+a+'" does not exist on jQuery.inputs')};return c};"function"===typeof define&&null!=define.amd?define(["jquery"],function(c){return null==c.fn.inputs?j(c):c}):j(this.jQuery)}).call(this);