-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathrepeatable-fields.js
200 lines (158 loc) · 5.59 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* jQuery Repeatable Fields v1.5.0
* http://www.rhyzz.com/repeatable-fields.html
*
* Copyright (c) 2014-2018 Rhyzz
* License MIT
*/
(function($) {
"use strict";
$.fn.repeatable_fields = function(custom_settings) {
var self = this;
self.after_add = function(container, new_row) {
var row_count = $(container).attr('data-rf-row-count');
row_count++;
$('*', new_row).each(function() {
$.each(this.attributes, function() {
this.value = this.value.replace(self.settings.row_count_placeholder, row_count - 1);
});
});
$(container).attr('data-rf-row-count', row_count);
};
self.default_settings = {
wrapper: '.wrapper',
container: '.container',
row: '.row',
add: '.add',
remove: '.remove',
move: '.move',
move_up: '.move-up',
move_down: '.move-down',
move_steps: '.move-steps',
template: '.template',
is_sortable: true,
before_add: null,
after_add: self.after_add,
before_remove: null,
after_remove: null,
sortable_options: null,
row_count_placeholder: '{{row-count-placeholder}}',
};
self.settings = $.extend({}, self.default_settings, custom_settings);
self.initialize = function(parent) {
$(self.settings.wrapper, parent).each(function() {
var wrapper = this;
var container = $(wrapper).children(self.settings.container);
// Disable all form elements inside the row template
$(container).children(self.settings.template).hide().find(':input').each(function() {
$(this).prop('disabled', true);
});
var row_count = $(container).children(self.settings.row).filter(function() {
return !$(this).hasClass(self.settings.template.replace('.', ''));
}).length;
$(container).attr('data-rf-row-count', row_count);
$(wrapper).on('click', self.settings.add, function(event) {
event.stopImmediatePropagation();
var row_template = $($(container).children(self.settings.template).clone().removeClass(self.settings.template.replace('.', ''))[0].outerHTML);
// Enable all form elements inside the row template
$(row_template).find(':input').each(function() {
$(this).prop('disabled', false);
});
if(typeof self.settings.before_add === 'function') {
self.settings.before_add(container);
}
var new_row = $(row_template).show().appendTo(container);
if(typeof self.settings.after_add === 'function') {
self.settings.after_add(container, new_row, self.settings.after_add);
}
// The new row might have it's own repeatable field wrappers so initialize them too
self.initialize(new_row);
});
$(wrapper).on('click', self.settings.remove, function(event) {
event.stopImmediatePropagation();
var row = $(this).parents(self.settings.row).first();
if(typeof self.settings.before_remove === 'function') {
self.settings.before_remove(container, row);
}
row.remove();
if(typeof self.settings.after_remove === 'function') {
self.settings.after_remove(container);
}
});
if(self.settings.is_sortable === true) {
if(typeof $.ui !== 'undefined' && typeof $.ui.sortable !== 'undefined') {
var sortable_options = self.settings.sortable_options !== null ? self.settings.sortable_options : {};
sortable_options.handle = self.settings.move;
$(wrapper).find(self.settings.container).sortable(sortable_options);
}
$(wrapper).find(self.settings.container).on('click', function(event) {
if(!$(event.target).is(self.settings.move_up) && !$(event.target).is(self.settings.move_down)) {
return;
}
var steps = 1;
if($(event.target).siblings(self.settings.move_steps).length === 1) {
var custom_steps = parseInt($(event.target).siblings(self.settings.move_steps).val(), 10);
if(isNaN(custom_steps) === false && (custom_steps > 0 || custom_steps === -1)) {
steps = custom_steps;
}
}
var current_row = $(event.target).closest(self.settings.row);
var i = 0;
if($(event.target).is(self.settings.move_up) === true) {
var previous_row;
for(i = 0; steps === -1 ? true : i < steps; i++) {
if(previous_row === undefined) {
if(current_row.prev().not(self.settings.template).length === 1) {
previous_row = current_row.prev();
}
else {
break;
}
}
else {
if(previous_row.prev().not(self.settings.template).length === 1) {
previous_row = previous_row.prev();
}
else {
break;
}
}
}
if(previous_row !== undefined) {
previous_row.before(current_row);
}
}
else if($(event.target).is(self.settings.move_down) === true) {
var next_row;
for(i = 0; steps === -1 ? true : i < steps; i++) {
if(next_row === undefined) {
if(current_row.next().length === 1) {
next_row = current_row.next();
}
else {
break;
}
}
else {
if(next_row.next().length === 1) {
next_row = next_row.next();
}
else {
break;
}
}
}
if(next_row !== undefined) {
next_row.after(current_row);
}
}
event.stopImmediatePropagation();
});
}
});
};
// Initialize all repeatable field wrappers
self.initialize(self);
return self;
};
})(jQuery);