-
Notifications
You must be signed in to change notification settings - Fork 3
/
jquery.repeater.min.js
1 lines (1 loc) · 10.3 KB
/
jquery.repeater.min.js
1
!function(k){"use strict";function x(t){return t}function T(t){return k.isArray(t)}function c(t){return!T(t)&&t instanceof Object}function o(t,e){return k.inArray(e,t)}function w(t,e){for(var n in t)t.hasOwnProperty(n)&&e(t[n],n,t)}function C(t){return t[t.length-1]}function V(){var t,n={};return w((t=arguments,Array.prototype.slice.call(t)),function(t){w(t,function(t,e){n[e]=t})}),n}function E(t,e,n){return T(t)?(a=e,c=[],w(t,function(t,e,n){c.push(a(t,e,n))}),c):(i=e,r=n,u={},w(t,function(t,e,n){e=r?r(e,t):e,u[e]=i(t,e,n)}),u);var i,r,u,a,c}function i(t,n,i){return E(t,function(t,e){return t[n].apply(t,i||[])})}var f,n;function r(t,i){var e,r,u,a=(r={},(e=e||{}).publish=function(t,e){w(r[t],function(t){t(e)})},e.subscribe=function(t,e){r[t]=r[t]||[],r[t].push(e)},e.unsubscribe=function(n){w(r,function(t){var e=o(t,n);-1!==e&&t.splice(e,1)})},e),n=t.$;return a.getType=function(){throw'implement me (return type. "text", "radio", etc.)'},a.$=function(t){return t?n.find(t):n},a.disable=function(){a.$().prop("disabled",!0),a.publish("isEnabled",!1)},a.enable=function(){a.$().prop("disabled",!1),a.publish("isEnabled",!0)},i.equalTo=function(t,e){return t===e},i.publishChange=function(t,e){var n=a.get();i.equalTo(n,u)||a.publish("change",{e:t,domElement:e}),u=n},a}function u(t,e){var n=r(t,e);return n.get=function(){return n.$().val()},n.set=function(t){n.$().val(t)},n.clear=function(){n.set("")},e.buildSetter=function(e){return function(t){e.call(n,t)}},n}function a(t,e){t=T(t)?t:[t],e=T(e)?e:[e];var n=!0;return t.length!==e.length?n=!1:w(t,function(t){-1===o(e,t)&&(n=!1)}),n}function s(t){var e={};return(t=u(t,e)).getType=function(){return"button"},t.$().on("change",function(t){e.publishChange(t,this)}),t}function p(t){var e={},n=u(t,e);return n.getType=function(){return"checkbox"},n.get=function(){var t=[];return n.$().filter(":checked").each(function(){t.push(f(this).val())}),t},n.set=function(t){t=T(t)?t:[t],n.$().each(function(){f(this).prop("checked",!1)}),w(t,function(t){n.$().filter('[value="'+t+'"]').prop("checked",!0)})},e.equalTo=a,n.$().change(function(t){e.publishChange(t,this)}),n}function l(t){return(t=n(t,{})).getType=function(){return"email"},t}function h(t){var e={},n=r(t,e);return n.getType=function(){return"file"},n.get=function(){return C(n.$().val().split("\\"))},n.clear=function(){this.$().each(function(){f(this).wrap("<form>").closest("form").get(0).reset(),f(this).unwrap()})},n.$().change(function(t){e.publishChange(t,this)}),n}function d(t){var e={};return(t=u(t,e)).getType=function(){return"hidden"},t.$().change(function(t){e.publishChange(t,this)}),t}function m(t){var e={},i=r(t,e);return i.getType=function(){return"file[multiple]"},i.get=function(){for(var t=i.$().get(0).files||[],e=[],n=0;n<(t.length||0);n+=1)e.push(t[n].name);return e},i.clear=function(){this.$().each(function(){f(this).wrap("<form>").closest("form").get(0).reset(),f(this).unwrap()})},i.$().change(function(t){e.publishChange(t,this)}),i}function v(t){var e={},n=u(t,e);return n.getType=function(){return"select[multiple]"},n.get=function(){return n.$().val()||[]},n.set=function(t){n.$().val(""===t?[]:T(t)?t:[t])},e.equalTo=a,n.$().change(function(t){e.publishChange(t,this)}),n}function g(t){return(t=n(t,{})).getType=function(){return"password"},t}function y(t){var e={},n=u(t,e);return n.getType=function(){return"radio"},n.get=function(){return n.$().filter(":checked").val()||null},n.set=function(t){t?n.$().filter('[value="'+t+'"]').prop("checked",!0):n.$().each(function(){f(this).prop("checked",!1)})},n.$().change(function(t){e.publishChange(t,this)}),n}function b(t){var e={};return(t=u(t,e)).getType=function(){return"range"},t.$().change(function(t){e.publishChange(t,this)}),t}function $(t){var e={};return(t=u(t,e)).getType=function(){return"select"},t.$().change(function(t){e.publishChange(t,this)}),t}function O(t){var e={};return(t=u(t,e)).getType=function(){return"textarea"},t.$().on("change keyup keydown",function(t){e.publishChange(t,this)}),t}function j(t){return(t=n(t,{})).getType=function(){return"url"},t}function q(t){function e(e,t){(c(t)?t:u.find(t)).each(function(){var t=f(this).attr("name");r[t]=a[e]({$:f(this)})})}var r={},u=t.$,a=t.constructorOverride||{button:s,text:n,url:j,email:l,password:g,range:b,textarea:O,select:$,"select[multiple]":v,radio:y,checkbox:p,file:h,"file[multiple]":m,hidden:d},t=function(e,t){var n=[],i=c(t)?t:u.find(t);c(t)?r[i.attr("name")]=a[e]({$:i}):(i.each(function(){-1===o(n,f(this).attr("name"))&&n.push(f(this).attr("name"))}),w(n,function(t){r[t]=a[e]({$:u.find('input[name="'+t+'"]')})}))};return u.is("input, select, textarea")?u.is('input[type="button"], button, input[type="submit"]')?e("button",u):u.is("textarea")?e("textarea",u):u.is('input[type="text"]')||u.is("input")&&!u.attr("type")?e("text",u):u.is('input[type="password"]')?e("password",u):u.is('input[type="email"]')?e("email",u):u.is('input[type="url"]')?e("url",u):u.is('input[type="range"]')?e("range",u):u.is("select")?u.is("[multiple]")?e("select[multiple]",u):e("select",u):u.is('input[type="file"]')?u.is("[multiple]")?e("file[multiple]",u):e("file",u):u.is('input[type="hidden"]')?e("hidden",u):u.is('input[type="radio"]')?t("radio",u):u.is('input[type="checkbox"]')?t("checkbox",u):e("text",u):(e("button",'input[type="button"], button, input[type="submit"]'),e("text",'input[type="text"]'),e("password",'input[type="password"]'),e("email",'input[type="email"]'),e("url",'input[type="url"]'),e("range",'input[type="range"]'),e("textarea","textarea"),e("select","select:not([multiple])"),e("select[multiple]","select[multiple]"),e("file",'input[type="file"]:not([multiple])'),e("file[multiple]",'input[type="file"][multiple]'),e("hidden",'input[type="hidden"]'),t("radio",'input[type="radio"]'),t("checkbox",'input[type="checkbox"]')),r}f=jQuery,n=function(t){var e={},t=u(t,e);return t.getType=function(){return"text"},t.$().on("change keyup keydown",function(t){e.publishChange(t,this)}),t},f.fn.inputVal=function(t){var e=f(this),n=q({$:e});return e.is("input, textarea, select")?void 0===t?n[e.attr("name")].get():(n[e.attr("name")].set(t),e):void 0===t?i(n,"get"):(w(t,function(t,e){n[e].set(t)}),e)},f.fn.inputOnChange=function(e){var t=f(this),n=q({$:t});return w(n,function(t){t.subscribe("change",function(t){e.call(t.domElement,t.e)})}),t},f.fn.inputDisable=function(){var t=f(this);return i(q({$:t}),"disable"),t},f.fn.inputEnable=function(){var t=f(this);return i(q({$:t}),"enable"),t},f.fn.inputClear=function(){var t=f(this);return i(q({$:t}),"clear"),t},k.fn.repeaterVal=function(){var t,i,r=function(t){if(1===t.length&&(0===t[0].key.length||1===t[0].key.length&&!t[0].key[0]))return t[0].val;w(t,function(t){t.head=t.key.shift()});var e,n,i=(e={},w(t,function(t){e[t.head]||(e[t.head]=[]),e[t.head].push(t)}),e);return/^[0-9]+$/.test(t[0].head)?(n=[],w(i,function(t){n.push(r(t))})):(n={},w(i,function(t,e){n[e]=r(t)})),n};return r((t=k(this).inputVal(),i=[],w(t,function(t,e){var n=[];"undefined"!==e&&(n.push(e.match(/^[^\[]*/)[0]),n=n.concat(E(e.match(/\[[^\]]*\]/g),function(t){return t.replace(/[\[\]]/g,"")})),i.push({val:t,key:n}))}),i))},k.fn.repeater=function(g){var y,b,$;return g=g||{},k(this).each(function(){function c(t,n){return t.filter(function(){return!n||0===k(this).closest((e="selector",E(n,function(t){return t[e]}).join(","))).length;var e})}function n(){return c(a.find("[data-repeater-item]"),g.repeaters)}var t=k(this),i=g.show||function(){k(this).show()},e=g.hide||function(t){t()},a=t.find("[data-repeater-list]").first(),r=a.find("[data-repeater-item]").first().clone().hide(),u=c(c(k(this).find("[data-repeater-item]"),g.repeaters).first().find("[data-repeater-delete]"),g.repeaters);g.isFirstItemUndeletable&&u&&u.remove();function o(){var t=a.data("repeater-list");return g.$parent?g.$parent.data("item-name")+"["+t+"]":t}function f(t){g.repeaters&&t.each(function(){var e=k(this);w(g.repeaters,function(t){e.find(t.selector).repeater(V(t,{$parent:e}))})})}function s(t,e,n){t&&w(t,function(t){n.call(e.find(t.selector)[0],t)})}var p=function(t,r,u){t.each(function(n){var i=k(this);i.data("item-name",r+"["+n+"]"),c(i.find("[name]"),u).each(function(){var t=k(this),e=t.attr("name").match(/\[[^\]]+\]/g),e=e?C(e).replace(/\[|\]/g,""):t.attr("name"),e=r+"["+n+"]["+e+"]"+(t.is(":checkbox")||t.attr("multiple")?"[]":"");t.attr("name",e),s(u,i,function(t){var e=k(this);p(c(e.find("[data-repeater-item]"),t.repeaters||[]),r+"["+n+"]["+e.find("[data-repeater-list]").first().data("repeater-list")+"]",t.repeaters)})})}),a.find("input[name][checked]").removeAttr("checked").prop("checked",!0)};p(n(),o(),g.repeaters),f(n()),g.initEmpty&&n().remove(),g.ready&&g.ready(function(){p(n(),o(),g.repeaters)});function l(t){var e=r.clone();m(e,t),g.repeaters&&f(e),i.call(e.get(0))}function h(t){v(t);var e=r.clone();m(e,t),g.repeaters&&f(e),i.call(e.get(0))}var d,m=(d=function(t,e,n){var i,r,u,a;(e||g.defaultValues)&&(i={},c(t.find("[name]"),n).each(function(){var t=k(this).attr("name").match(/\[([^\]]*)(\]|\]\[\])$/)[1];i[t]=k(this).attr("name")}),t.inputVal(E((r=e||g.defaultValues,u=function(t,e){return i[e]},T(r)?(a=[],w(r,function(t,e,n){u(t,e,n)&&a.push(t)})):(a={},w(r,function(t,e,n){u(t,e,n)&&(a[e]=t)})),a),x,function(t){return i[t]}))),s(n,t,function(i){var r=k(this);c(r.find("[data-repeater-item]"),i.repeaters).each(function(){var n,t=r.find("[data-repeater-list]").data("repeater-list");e&&e[t]?(n=k(this).clone(),r.find("[data-repeater-item]").remove(),w(e[t],function(t){var e=n.clone();d(e,t,i.repeaters||[]),r.find("[data-repeater-list]").append(e)})):d(k(this),i.defaultValues,i.repeaters||[])})})},function(t,e){a.append(t),p(n(),o(),g.repeaters),t.find("[name]").each(function(){k(this).inputClear()}),d(t,e||g.defaultValues,g.repeaters)}),v=function(t){k.each(t,function(t,e){r.find('[name="'+t+'"]').each(function(){var n;k(this).is("select")&&(k(this).find("option:not(:first)").remove(),n=k(this),k.each(e,function(t,e){n.append('<option value="'+e.id+'">'+e.val+"</option>")}))})})};y=function(t){n().remove(),w(t,l)},b=function(t){n().remove(),w(t,h)},$=function(t){n().remove();var e=r.clone();k(this).find("[data-repeater-list]").append(e.show()),k.fn.repeater={}},c(t.find("[data-repeater-create]"),g.repeaters).click(function(){l()}),a.on("click","[data-repeater-delete]",function(){var t=k(this).closest("[data-repeater-item]").get(0);e.call(t,function(){k(t).remove(),p(n(),o(),g.repeaters)})})}),this.setList=y,this.setOption=b,this.destory=$,this}}(jQuery);