forked from Rhyzz/repeatable-fields
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepeatable-fields.js
More file actions
Latest commit
105 lines (82 loc) · 2.92 KB
/
Copy pathrepeatable-fields.js
File metadata and controls
105 lines (82 loc) · 2.92 KB
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
/*
* jQuery Repeatable Fields v1.1.4
* http://www.rhyzz.com/repeatable-fields.html
*
* Copyright (c) 2014 Rhyzz
* License MIT
*/
(function($){
$.fn.repeatable_fields=function(custom_settings){
vardefault_settings={
wrapper: '.wrapper',
container: '.container',
row: '.row',
add: '.add',
remove: '.remove',
move: '.move',
template: '.template',
is_sortable: true,
before_add: null,
after_add: after_add,
before_remove: null,
after_remove: null,
sortable_options: null,
}
varsettings=$.extend(default_settings,custom_settings);
// Initialize all repeatable field wrappers
initialize(this);
functioninitialize(parent){
$(settings.wrapper,parent).each(function(index,element){
varwrapper=this;
varcontainer=$(wrapper).children(settings.container);
// Disable all form elements inside the row template
$(container).children(settings.template).hide().find(':input').each(function(){
jQuery(this).prop('disabled',true);
});
$(wrapper).on('click',settings.add,function(event){
event.stopImmediatePropagation();
varrow_template=$($(container).children(settings.template).clone().removeClass(settings.template.replace('.',''))[0].outerHTML);
// Enable all form elements inside the row template
jQuery(row_template).find(':input').each(function(){
jQuery(this).prop('disabled',false);
});
if(typeofsettings.before_add==='function'){
settings.before_add(container);
}
varnew_row=$(row_template).show().appendTo(container);
if(typeofsettings.after_add==='function'){
settings.after_add(container,new_row);
}
// The new row might have it's own repeatable field wrappers so initialize them too
initialize(new_row);
});
$(wrapper).on('click',settings.remove,function(event){
event.stopImmediatePropagation();
varrow=$(this).parents(settings.row).first();
if(typeofsettings.before_remove==='function'){
settings.before_remove(container,row);
}
row.remove();
if(typeofsettings.after_remove==='function'){
settings.after_remove(container);
}
});
if(settings.is_sortable===true&&typeof$.ui!=='undefined'&&typeof$.ui.sortable!=='undefined'){
varsortable_options=settings.sortable_options!==null ? settings.sortable_options : {};
sortable_options.handle=settings.move;
$(wrapper).find(settings.container).sortable(sortable_options);
}
});
}
functionafter_add(container,new_row){
varrow_count=$(container).children(settings.row).filter(function(){
return!jQuery(this).hasClass(settings.template.replace('.',''));
}).length;
$('*',new_row).each(function(){
$.each(this.attributes,function(index,element){
this.value=this.value.replace(/{{row-count-placeholder}}/,row_count-1);
});
});
}
}
})(jQuery);