-
Notifications
You must be signed in to change notification settings - Fork 2
/
FormBuilderMultiplier.js
132 lines (105 loc) · 3.88 KB
/
FormBuilderMultiplier.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
/**
* Activate "add row" buttons for multiplier
*/
$(document).ready(function() {
/**
* Helper function for formatting string, takes placeholders in
* the form {0}, {1} ... {n}.
*/
var formatString = function(str) {
var args = [];
for(var i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
if(typeof str !== 'string') return str;
return str.replace(/\{(\d+)\}/g, function(match, p1) {
var num = parseInt(p1);
return (num >= args.length) ? p1 : args[num];
});
}
/**
* Attach click listener Buttons
*/
$('button.fb-multiplier-add-row').each(function(idx, el) {
var mname = $(el).data('multiply');
var $multiplier = $('#Inputfield_' + mname);
var $inner = $multiplier.find('div.Inputfields').first();
/**
* Clone our original set of fields in the fieldset. On submission errors,
* we might have a few sets already, so only use those with the correct
* CSS class.
*/
var $tpl = $('<div/>');
$inner.children('div.fb-multiplier-orig-field.label, div.Inputfield:has(input.fb-multiplier-orig-field), div.Inputfield:has(select.fb-multiplier-orig-field), div.Inputfield:has(textarea.fb-multiplier-orig-field)').clone().each(function(ind, fld) {
$(fld).find('input, select, textarea').removeClass('fb-multiplier-orig-field');
$tpl.append(fld);
});
var rowTpl = $tpl.html();
var $counter = $('#' + mname + '__multiplier_rows').first();
/**
* Attach the handler
*/
$(el).click(function(evt) {
evt.preventDefault();
var curCnt = parseInt($counter.val());
var curTpl = '' + '<div class="multiplier_clone InputfieldContent uk-form-controls" data-count-' + parseInt(curCnt + 1) +'>' + rowTpl + '</div>';
var rowLimit = parseInt($(el).data('multiply-limit'));
if(isNaN(rowLimit)) rowLimit = 0;
if(rowLimit != 0 && curCnt >= rowLimit) {
alert(formatString(FBMultiplier.config.messages.rowlimit, rowLimit));
$(el).prop('disabled', true);
return;
}
var flds = ['id', 'name', 'for'];
// Increment row index number in id, name and for attributes
$.each(flds, function(idxF, fld) {
curTpl = curTpl.replace(new RegExp('(' + fld + '=")([^"]+)_(\\d+)"', 'mg'), function(m, m1, m2, m3) {
return '' + m1 + m2 + '_' + (parseInt(m3) + curCnt) + '"';
});
});
// Increment row index number in class, this one doesn't have trailing quotes
curTpl = curTpl.replace(/(class="[^"]+_)(\d+)(\s|")/mg, function(m, m1, m2, m3) {
return '' + m1 + (parseInt(m2) + curCnt) + m3;
});
// Increment row index number in the field label
curTpl = curTpl.replace(new RegExp('(<label[^>]+>[^<]+ \\[#)(\\d+)(\\]<)', 'mg'), function(m, m1, m2, m3) {
return '' + m1 + (parseInt(m2) + curCnt) + m3;
});
var $new = $(curTpl);
// Empty created fields
$($new).find('input, select, textarea').val('');
// Clear possible errors on fields
$($new).find('p.uk-text-danger').remove();
$new.insertBefore($(el).closest('.Inputfield'));
$counter.val(1 + curCnt);
if(rowLimit != 0 && curCnt == rowLimit - 1) {
$(el).addClass('uk-button-danger');
}
$(el).closest('.Inputfields').find('button.fb-multiplier-remove-row').prop('disabled',false);
$(window).trigger("resize");
});
/**
* Attach click listener to all "Remove row" buttons
*/
$('button.fb-multiplier-remove-row').each(function(idx, el) {
var curCnt = parseInt($counter.val());
if(curCnt < 2) { $(el).prop('disabled', true);};
$(el).click(function(evt) {
evt.preventDefault();
var curCnt = parseInt($counter.val());
if(curCnt===1) {
return false;
}
if(curCnt===2) {
$(el).prop('disabled',true)
};
// Remove fields dynamicly created
$('.multiplier_clone').last().remove();
// Remove fiedls created via PHP
$('.fb-multiplier-cloned-field-'+curCnt).remove();
$counter.val(curCnt - 1);
$(window).trigger("resize");
});
});
});
});