root / ui / static / invitations.js @ 848e6d10
History | View | Annotate | Download (4 kB)
1 |
/*
|
---|---|
2 |
* jQuery dynamicField plugin
|
3 |
* Copyright 2009, Matt Quackenbush (http://www.quackfuzed.com/)
|
4 |
*
|
5 |
* Find usage demos at http://www.quackfuzed.com/demos/jQuery/dynamicField/index.cfm)
|
6 |
*
|
7 |
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
|
8 |
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
|
9 |
*
|
10 |
* Version: 1.0
|
11 |
* Date: 8/13/2009
|
12 |
*/
|
13 |
;(function($) { |
14 |
$.fn.dynamicField = function(options) { |
15 |
if ( $(this).attr("id") == undefined ) { |
16 |
throw "The dynamicField plugin could not be initialized.\n\nPlease check the selector."; |
17 |
return $; |
18 |
} |
19 |
|
20 |
var f = $(this); |
21 |
|
22 |
var settings = $.extend({ |
23 |
maxFields: 5, |
24 |
removeImgSrc: "/static/cross.png", |
25 |
spacerImgSrc: "/static/spacer.gif", |
26 |
addTriggerClass: "add-field-trigger", |
27 |
removeImgClass: "remove-field-trigger", |
28 |
hideClass: "hide", |
29 |
cloneContainerId: f.attr("id").replace(/^(.+)([_-][0-9]+)$/,"$1"), |
30 |
rowContainerClass: f.attr("class"), |
31 |
labelText: f.children("label") |
32 |
.html(), |
33 |
baseName: f.children("input") |
34 |
.attr("name")
|
35 |
.replace(/^(.+[_-])([0-9]+)$/,"$1"), |
36 |
baseNames: baseNames(),
|
37 |
addContainerId: "add-" + f.children("input") |
38 |
.attr("name")
|
39 |
.replace(/^(.+)([_-][0-9]+)$/,"$1") |
40 |
.replace(/_/g,"-") + "-container" |
41 |
},options); |
42 |
|
43 |
var getFields = function() { |
44 |
return $("div." + settings.rowContainerClass); |
45 |
}; |
46 |
|
47 |
function baseNames() { |
48 |
var names = new Array(); |
49 |
$.each(f.children("input"), function(index, child){ |
50 |
var name = child.name.replace(/^(.+[_-])([0-9]+)$/,"$1") |
51 |
names.push(name); |
52 |
}); |
53 |
return names;
|
54 |
} |
55 |
|
56 |
// handle hide/show, etc
|
57 |
var addRemoveBtnCk = function() { |
58 |
var fields = getFields();
|
59 |
var len = fields.length;
|
60 |
|
61 |
fields.each(function(i,elem) {
|
62 |
$(elem)
|
63 |
.children("img")
|
64 |
.attr({ |
65 |
"src":(len == 1) ? settings.spacerImgSrc : settings.removeImgSrc, |
66 |
"class":(len == 1) ? "" : settings.removeImgClass |
67 |
}); |
68 |
}); |
69 |
|
70 |
if ( len > (settings.maxFields-1) ) { |
71 |
$("div#" + settings.addContainerId).addClass(settings.hideClass); |
72 |
} else {
|
73 |
$("div#" + settings.addContainerId).removeClass(settings.hideClass); |
74 |
} |
75 |
}; |
76 |
|
77 |
// handle field removal
|
78 |
$("img." + settings.removeImgClass).live("click",function() { |
79 |
// remove the selected row
|
80 |
$(this).parent("div." + settings.rowContainerClass).remove(); |
81 |
|
82 |
// rebrand the remaining fields sequentially
|
83 |
getFields().each(function(i,elem) {
|
84 |
var pos = new Number(i+1); |
85 |
var d = $(elem) |
86 |
.attr("id",settings.cloneContainerId + "-" + pos); |
87 |
|
88 |
d.children("label")
|
89 |
.attr("for",settings.baseName + pos)
|
90 |
.html((pos > 1) ? "" : settings.labelText); |
91 |
|
92 |
names = settings.baseNames; |
93 |
d.children("input").each(function(i){ |
94 |
$(this).attr({ |
95 |
"id": names[i] + pos,
|
96 |
"name": names[i] + pos
|
97 |
}); |
98 |
}); |
99 |
}); |
100 |
|
101 |
addRemoveBtnCk(); |
102 |
}); |
103 |
|
104 |
// handle field add
|
105 |
$("div#" + settings.addContainerId + " span." + settings.addTriggerClass).click(function() { |
106 |
var len = getFields().length;
|
107 |
var pos = new Number(len+1); |
108 |
var newDiv = $("<div/>") |
109 |
.attr("id",settings.cloneContainerId + "-" + pos) |
110 |
.addClass(settings.rowContainerClass); |
111 |
|
112 |
$.each(settings.baseNames, function(index, name) { |
113 |
|
114 |
var input = $("<input/>").attr({ |
115 |
"id":name + pos,
|
116 |
"name":name + pos,
|
117 |
"value":"" |
118 |
}); |
119 |
newDiv.append(input); |
120 |
}); |
121 |
newDiv.append($("<img>").attr("src",settings.removeImgSrc)); |
122 |
|
123 |
if ( len > 0 ) { |
124 |
$("div#" + settings.cloneContainerId + "-" + len).after(newDiv); |
125 |
} else {
|
126 |
$("div#" + settings.addContainerId).before(newDiv); |
127 |
} |
128 |
|
129 |
addRemoveBtnCk(); |
130 |
}); |
131 |
}; |
132 |
})(jQuery); |