forked from Rhyzz/repeatable-fields
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepeatable-fields.js
105 lines (82 loc) · 2.92 KB
/
repeatable-fields.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
/*
* 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) {
var default_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,
}
var settings = $.extend(default_settings, custom_settings);
// Initialize all repeatable field wrappers
initialize(this);
function initialize(parent) {
$(settings.wrapper, parent).each(function(index, element) {
var wrapper = this;
var container = $(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();
var row_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(typeof settings.before_add === 'function') {
settings.before_add(container);
}
var new_row = $(row_template).show().appendTo(container);
if(typeof settings.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();
var row = $(this).parents(settings.row).first();
if(typeof settings.before_remove === 'function') {
settings.before_remove(container, row);
}
row.remove();
if(typeof settings.after_remove === 'function') {
settings.after_remove(container);
}
});
if(settings.is_sortable === true && typeof $.ui !== 'undefined' && typeof $.ui.sortable !== 'undefined') {
var sortable_options = settings.sortable_options !== null ? settings.sortable_options : {};
sortable_options.handle = settings.move;
$(wrapper).find(settings.container).sortable(sortable_options);
}
});
}
function after_add(container, new_row) {
var row_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);