-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.addremovetextbox.min.js
33 lines (33 loc) · 4.92 KB
/
jquery.addremovetextbox.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
* AddRemoveTextbox v1.2.1
* https://www.github.com/kloverde/jquery-AddRemoveTextbox
*
* Donations: https://paypal.me/KurtisLoVerde/5
*
* Copyright (c) 2016, Kurtis LoVerde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
!function(a){"use strict";a.fn.addRemoveTextbox=function(b){function h(a){throw"AddRemoveTextbox error: "+a}function i(b){var e=a("input[id^='"+d+"']");null!=c.maxFields&&e.length>c.maxFields&&h("There are more fields in the '"+d+"' set ("+e.length+") than are allowed by maxFields ("+c.maxFields+")"),e.each(function(){var b=a(this),c=b.parent();l().insertAfter(b),c.is(":last-child")&&k().appendTo(c)}),q()}function j(b,c){var d=a("<span></span>");return null!=b&&d.addClass(b),null!=c&&d.attr("title",c),d}function k(){var b=j(c.addButtonClass,c.addButtonTooltip);return b.on("click",function(){var b=n(a(this));a(r("#"+b)).focus()}),b}function l(){var b=j(c.removeButtonClass,c.removeButtonTooltip);return b.on("click",function(){p(a(this))}),b}function m(){var b=null,c=null,e=a("input[id^='"+d+"']");return e.each(function(){var d=a(this);null==b&&(b=d),c=d,b.attr("id")>d.attr("id")||(b=d)}),{highestBox:b,lastBox:c,numBoxes:e.length}}function n(a){var b=a.parent(),d=b.clone(!0),e=d.children("input"),f=o();return e.val(""),e.attr("id",f.nextId),e.attr("name",e.attr("id")),a.remove(),null!=c.maxFields&&f.numBoxes+1>=c.maxFields&&d.find("span[class='"+c.addButtonClass+"']").remove(),d.insertAfter(b),"function"==typeof c.addCallback&&c.addCallback(f.nextId,f.nextId),f.nextId}function o(){var a=m();null!=c.maxFields&&a.numBoxes>=c.maxFields&&h("Field count limit reached for "+d);var b=a.highestBox,e=parseInt(b.attr("id").replace(/.*(\d+).*$/,"$1")),g=e+1,i=f?d+"["+g+"]":d+g;return{nextId:i,numBoxes:a.numBoxes}}function p(b){var e=b.parent(),f=e.prev(),g=e.children("input"),h=g.val(),i=g.attr("id"),j=g.attr("name"),l=!1;a("input[id^='"+d+"']").each(function(){if(a(this).attr("id")!==i)return void(l=!0)}),l?(e.is(":last-child")?k().appendTo(f):e.parent().last().find("span[class='"+c.addButtonClass+"']").length||k().appendTo(e.siblings(":last")),e.remove(),q()):g.val(""),"function"==typeof c.removeCallback&&c.removeCallback(i,j,h)}function q(){if(c.contiguous===!0){var b=c.startingNumber;a("input[id^="+d+"]").each(function(){var c=a(this),e=f?d+"["+b+"]":d+b;c.attr("id",e),c.attr("name",e),b++})}}function r(a){return a.replace(/(\[|\])/g,"\\$1")}var c=a.extend({addButtonClass:"addButton",addButtonTooltip:null,removeButtonClass:"removeButton",removeButtonTooltip:null,maxFields:null,contiguous:!1,startingNumber:0,addCallback:null,removeCallback:null},b);0!==this.length&&null!=this||h("No matching field(s) found");var d=this.attr("id").replace(/\[?[0-9]*\]?$/,""),e=null!=this.attr("id").match(/[0-9]+$/),f=null!=this.attr("id").match(/\[[0-9]\]$/);if(e||f?e&&f&&h("Invalid state: ID '"+this.attr("id")+"' notation not detected correctly"):h("Invalid ID attribute '"+this.attr("id")+"': IDs must be nunmbered like id1, id2, or use array notation, like id[0], id[1]"),null!=c.maxFields){var g="maxFields ("+c.maxFields+") must be an integer greater than 1";isNaN(c.maxFields)?h(g):((c.maxFields<2||c.maxFields%1!==0)&&h(g),c.maxFields=parseInt(c.maxFields))}if(c.contiguous===!0&&null!=c.startingNumber){var g="startingNumber ("+c.startingNumber+") must be an integer greater than or equal to 0";isNaN(c.startingNumber)?h(g):((c.startingNumber<0||c.startingNumber%1!==0)&&h(g),c.startingNumber=parseInt(c.startingNumber))}i(this)}}(jQuery);