-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery-nested-forms.js
148 lines (127 loc) · 5.15 KB
/
jquery-nested-forms.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* jQuery Nested Forms Plugin
* version: 1.0.0 (2010-02-12)
* @requires jQuery v1.9.0 or later
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* http://github.com/andyferra/nested-forms
*/
;(function($) {
$.fn.has_nested_forms = function(nested_form_selector, options) {
return this.each(function(){
var parent = this;
var $parent = $(parent);
function inferred_name(){ return nested_form_selector.replace('.', '') }
var defaults = {
name : inferred_name(),
add_href_selector : 'a[href="#add_'+inferred_name()+'"]',
remove_href_selector : 'a[href="#remove_'+inferred_name()+'"]',
delete_field_selector : null,
focus_field_selector : 'input[type!=submit][type!=radio][type!=checkbox]:visible:first',
depth : 1,
cycle_classes : ['odd', 'even'],
enable_keyboard : true,
always_show_one : true,
add_shortcut : function(e){ return e.keyCode == 13 && e.shiftKey; }, // Shift-Enter
remove_shortcut : function(e){ return e.keyCode == 8 && e.shiftKey; }, // Shift-Backspace
after_add : function(){},
after_remove : function(){},
index_offset : 0
}
var opts = $.extend(defaults, options);
$('body').on('click', opts.add_href_selector, function(){
create_clone();
return false;
});
$('body').on('click', opts.remove_href_selector, function(){
delete_clone(this);
return false;
});
$('body').on('keydown', $parent.find(nested_form_selector+' input'), function(e){
if (opts.enable_keyboard){
if (opts.add_shortcut(e)) {
create_clone();
return false;
} else if (opts.remove_shortcut(e)) {
delete_clone(this);
return false;
}
}
});
function delete_clone(insider){
var deleted = $(insider).parents(nested_form_selector);
deleted.find(delete_field_selector()).val(1);
deleted.hide();
cycle_classes();
opts.after_remove.call(deleted);
if (opts.always_show_one && $parent.find(nested_form_selector+':visible').length == 0) {
create_clone();
}
}
function delete_field_selector(){
if (opts.delete_field_selector) { return opts.delete_field_selector }
if ($parent.find(nested_form_selector).find('input[id$="_destroy"]').length) {
return 'input[id$="_destroy"]';
} else {
return 'input[id$="_delete"]';
}
}
function create_clone(){
var new_nested_form = $parent.find(nested_form_selector+':first').clone().show();
// clear the form
new_nested_form.find('input[type=text], input[type=file], select, textarea').val('');
new_nested_form.find('input[type=checkbox]').attr('checked', false);
new_nested_form.find('input[type=radio]').attr('checked', false);
new_nested_form.find('input[type=radio]:first').attr('checked', true);
// rails attributes
new_nested_form.find('input[id$="_destroy"]').val(0);
// update the field indices
var new_item_index = $parent.find(nested_form_selector).length + opts.index_offset;
new_nested_form.find('input, textarea, select').each(function(){
if ($(this).attr('id') != null){
var name_nth_count = 0;
$(this).attr('name', $(this).attr('name').replace(/\[\d+\]/g, function(str){
name_nth_count++;
return (name_nth_count == opts.depth) ? '['+new_item_index+']' : str;
}));
var id_nth_count = 0;
$(this).attr('id', $(this).attr('id').replace(/_\d+_/g, function(str){
id_nth_count++;
return (id_nth_count == opts.depth) ? '_'+new_item_index+'_' : str;
}));
}
});
new_nested_form.find('label').each(function(){
var for_nth_count = 0;
$(this).attr('for', $(this).attr('for').replace(/_\d+_/g, function(str){
for_nth_count++;
return (for_nth_count == opts.depth) ? '_'+new_item_index+'_' : str;
}));
});
$parent.append(new_nested_form);
focus_on_first_field(new_nested_form);
cycle_classes();
opts.after_add.call(new_nested_form);
}
function focus_on_first_field(nested_form){
if (opts.focus_field_selector) {
nested_form.find(opts.focus_field_selector).focus();
}
}
function cycle_classes(){
if (opts.cycle_classes) {
var current_cycle_index = 0;
$parent.find(nested_form_selector+':visible').each(function(){
$(this).removeClass(opts.cycle_classes.join(" "));
$(this).addClass(opts.cycle_classes[current_cycle_index]);
current_cycle_index = (current_cycle_index == opts.cycle_classes.length-1) ? 0 : current_cycle_index+1;
});
}
}
cycle_classes();
});
}
})(jQuery);