cf7mls.js
19.4 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
(function ($) {
//jQuery time
var current_fs, next_fs, previous_fs; //fieldsets
var left, opacity, scale; //fieldset properties which we will animate
var animating; //flag to prevent quick multi-click glitches
var has_response = false;
var next_clicked = false;
jQuery(document).ready(function ($) {
$("form.wpcf7-form").each(function (index, el) {
var totalFieldset = 0;
var this_form = $(el);
var findFieldset = $(el).find("fieldset.fieldset-cf7mls");
if (findFieldset.length > 0) {
this_form.addClass('cf7mls')
$.each(findFieldset, function (i2, el2) {
if (i2 == 0) {
$(el2).addClass("cf7mls_current_fs");
}
jQuery(el2).attr("data-cf7mls-order", i2);
totalFieldset = totalFieldset + 1;
//disable next button if the fieldset has wpcf7-acceptance
var acceptances = jQuery(el2).find("input:checkbox.wpcf7-acceptance");
if (acceptances.length) {
cf7mls_toggle_next_btn(acceptances, el2);
}
});
$.each(findFieldset, function (i2, el2) {
if (i2 == totalFieldset - 1) {
$(el2)
.find(".cf7mls_next")
.remove();
}
});
$(el).attr("data-count-fieldset", totalFieldset);
//on form submit
if(cf7mls_object.disable_submit == 'true') {
if (cf7mls_object.disable_enter_key) {
$(el)
.find('[type="submit"]')
.click(function (event) {
var findFieldset = $(el).find(
"fieldset.fieldset-cf7mls.cf7mls_current_fs"
);
if (findFieldset.data("cf7mls-order") != totalFieldset - 1) {
findFieldset.find(".cf7mls_next").click();
return false;
}
});
} else {
$(el).submit(function (event) {
var findFieldset = $(el).find(
"fieldset.fieldset-cf7mls.cf7mls_current_fs"
);
if (findFieldset.data("cf7mls-order") != totalFieldset - 1) {
findFieldset.find(".cf7mls_next").click();
return false;
}
});
}
}
}
});
jQuery(document).on(
"click",
"form.wpcf7-form input:checkbox.wpcf7-acceptance",
function (event) {
//event.preventDefault();
var $this = jQuery(this);
var parent_fieldset = $this.closest("fieldset.fieldset-cf7mls");
if (parent_fieldset.length) {
var acceptances = jQuery(parent_fieldset).find(
"input:checkbox.wpcf7-acceptance"
);
if (acceptances.length) {
cf7mls_toggle_next_btn(acceptances, parent_fieldset);
}
}
}
);
$(document).on("click", ".cf7mls_next", function (event) {
if(next_clicked == false) {
next_clicked = true;
event.preventDefault();
var $this = $(this);
$this.addClass("sending");
current_fs = $this.closest(".fieldset-cf7mls");
next_fs = current_fs.next();
var form = $this.parent().closest("form.wpcf7-form");
if(form.hasClass("sent")) {
form.removeClass("sent");
form.addClass("init");
form.attr('data-status', 'init');
if (form.find('.wpcf7-response-output').length) {
form.find('.wpcf7-response-output').html("");
}
}
//signature fields
current_fs.find('.wpcf7-form-control-signature-global-wrap').each(function(j, wrapper){
var f_id = $(wrapper).attr('data-field-id')
var canvas = $(wrapper).find('canvas')
if(typeof signatures != 'undefined') {
$.each(signatures, function(s_i, signature){
if(signature.canvas.id == canvas.attr('id')) {
if (!signature.signature.isEmpty()){
$('input[name="'+f_id+'"]').val(signature.canvas.toDataURL())
}else{
$('input[name="'+f_id+'"]').val("")
}
}
})
}
});
var fd = new FormData(form[0]);
$.ajax({
url: cf7mls_object.ajax_url + "/v1/cf7mls_validation",
type: "POST",
crossDomain: true,
data: fd,
processData: false,
contentType: false
})
.done(function (json) {
$this.removeClass("sending");
/*
* Insert _form_data_id if 'json variable' has
*/
if (typeof json._cf7mls_db_form_data_id != "undefined") {
if (!form.find('input[name="_cf7mls_db_form_data_id"]').length) {
form.append(
'<input type="hidden" name="_cf7mls_db_form_data_id" value="' +
json._cf7mls_db_form_data_id +
'" />'
);
}
}
if (!json.success) {
var checkError = 0;
var firstError = null;
//reset error messages
current_fs
.find(".wpcf7-form-control-wrap")
.removeClass("cf7mls-invalid");
current_fs.find(".cf7mls-invalid").removeClass("cf7mls-invalid");
current_fs
.find(".wpcf7-form-control-wrap .wpcf7-not-valid-tip")
.remove();
current_fs
.find(".wpcf7-form-control-wrap .wpcf7-not-valid")
.removeClass("wpcf7-not-valid");
if (has_response) {
current_fs
.find(".wpcf7-response-output.wpcf7-validation-errors")
.removeClass("wpcf7-validation-errors");
} else {
current_fs
.find(".wpcf7-response-output.wpcf7-validation-errors")
.remove();
}
$.each(json.invalid_fields, function (index, el) {
if (
current_fs.find('input[name="' + index + '"]').length ||
current_fs.find('input[name="' + index + '[]"]').length ||
current_fs.find('select[name="' + index + '"]').length ||
current_fs.find('select[name="' + index + '[]"]').length ||
current_fs.find('textarea[name="' + index + '"]').length ||
current_fs.find('textarea[name="' + index + '[]"]').length ||
current_fs.find('input[data-name="' + index + '"]').length ||
current_fs.find('input[data-name="' + index + '[]"]').length
) {
checkError = checkError + 1;
//var controlWrap = $('.wpcf7-form-control-wrap.' + index, form);
var controlWraps = [
$('[name="' + index + '"]', form).parent(),
$('[name="' + index + '[]"]', form).parent(),
$('[data-name="' + index + '"]', form).parent(),
$('[data-name="' + index + '[]"]', form).parent()
];
$.each(controlWraps, function (i1, e1) {
if(e1.hasClass("wpcf7-form-control-wrap") || e1.parents(".wpcf7-form-control-wrap").length || e1.parents(".wpcf7-checkbox").length || e1.parents(".wpcf7-radio").length) {
var _c = "wpcf7-not-valid-tip";
if (e1.hasClass("form-group")) {
e1.addClass("has-error");
_c += " help-block";
}
if(firstError === null) {
firstError = e1;
}
e1.addClass("cf7mls-invalid");
e1.find("span.wpcf7-not-valid-tip").remove();
e1.find(".wpcf7-validates-as-required").addClass(
"wpcf7-not-valid"
);
if (e1.parents(".wpcf7-checkbox").length) {
e1.parents(".wpcf7-checkbox").after(
'<span role="alert" class="' +
_c +
'">' +
el.reason +
"</span>"
);
} else if (e1.parents(".wpcf7-radio").length) {
e1.parents(".wpcf7-radio").after(
'<span role="alert" class="' +
_c +
'">' +
el.reason +
"</span>"
);
} else if (e1.parents(".wpcf7-form-control-wrap").length) {
e1.parents(".wpcf7-form-control-wrap").append(
'<span role="alert" class="' +
_c +
'">' +
el.reason +
"</span>"
);
} else {
e1.append(
'<span role="alert" class="' +
_c +
'">' +
el.reason +
"</span>"
);
}
}
});
//return false;
}
});
if (checkError == 0) {
json.success = true;
has_response = false;
// jQuery("html, body").animate(
// {
// scrollTop: jQuery($this.closest("form")).offset().top - 110
// },
// 1000
// );
} else {
$('html, body').animate({
scrollTop: $(firstError).offset().top - 100
}, 500);
if (current_fs.find(".wpcf7-response-output").length) {
has_response = true;
$icon = '';
$icon += '<svg class="wpcf7-icon-wraning" width="18px" height="18px" x="0px" y="0px" viewBox="0 0 1000 1000" enable-background="new 0 0 1000 1000" xml:space="preserve">';
$icon += '<g><path d="M499.9,361.6c-12.7,0-23,10.3-23,23v352.2c0,12.7,10.3,23,23,23s23-10.3,23-23V384.6C522.9,371.9,512.6,361.6,499.9,361.6z"/>';
$icon += '<path d="M500.1,240.2c-12.7,0-23,10.3-23,23v30.6c0,12.7,10.3,23,23,23c12.7,0,23-10.3,23-23v-30.6C523.1,250.5,512.8,240.2,500.1,240.2z"/>';
$icon += '<path d="M500,10C229.4,10,10,229.4,10,500c0,270.6,219.4,490,490,490c270.6,0,490-219.4,490-490C990,229.4,770.6,10,500,10z M500,944.1C254.8,944.1,55.9,745.2,55.9,500C55.9,254.8,254.8,55.9,500,55.9S944.1,254.8,944.1,500C944.1,745.2,745.2,944.1,500,944.1z"/></g></svg>';
current_fs
.find(".wpcf7-response-output")
.addClass("wpcf7-validation-errors")
.show()
.html($icon + json.message);
} else {
has_response = false;
$icon = '';
$icon += '<svg class="wpcf7-icon-wraning" width="18px" height="18px" x="0px" y="0px" viewBox="0 0 1000 1000" enable-background="new 0 0 1000 1000" xml:space="preserve">';
$icon += '<g><path d="M499.9,361.6c-12.7,0-23,10.3-23,23v352.2c0,12.7,10.3,23,23,23s23-10.3,23-23V384.6C522.9,371.9,512.6,361.6,499.9,361.6z"/>';
$icon += '<path d="M500.1,240.2c-12.7,0-23,10.3-23,23v30.6c0,12.7,10.3,23,23,23c12.7,0,23-10.3,23-23v-30.6C523.1,250.5,512.8,240.2,500.1,240.2z"/>';
$icon += '<path d="M500,10C229.4,10,10,229.4,10,500c0,270.6,219.4,490,490,490c270.6,0,490-219.4,490-490C990,229.4,770.6,10,500,10z M500,944.1C254.8,944.1,55.9,745.2,55.9,500C55.9,254.8,254.8,55.9,500,55.9S944.1,254.8,944.1,500C944.1,745.2,745.2,944.1,500,944.1z"/></g></svg>';
current_fs.append(
'<div class="wpcf7-response-output wpcf7-display-none wpcf7-validation-errors" style="display: block;" role="alert">' + $icon +
json.message +
"</div>"
);
}
}
}
if (json.success) {
/*
current_fs.fadeOut('400', function() {
next_fs.fadeIn('400');
});
});
*/
//reset error messages
current_fs
.find(".wpcf7-form-control-wrap")
.removeClass("cf7mls-invalid");
current_fs.find(".cf7mls-invalid").removeClass("cf7mls-invalid");
current_fs.find(".wpcf7-not-valid").removeClass("wpcf7-not-valid");
current_fs
.find(".wpcf7-form-control-wrap .wpcf7-not-valid-tip")
.remove();
/*
* Instead of hiding current fs, we hide all
*/
/*
current_fs.css({
height: '0px',
overflow: 'hidden',
opacity: '0',
'visibility': 'hidden'
}).removeClass('cf7mls_current_fs');
*/
form.find(".cf7mls_current_fs").addClass("cf7mls_back_fs");
form.find(".fieldset-cf7mls").removeClass("cf7mls_current_fs");
next_fs.addClass("cf7mls_current_fs");
next_fs.find('input, textarea').first().focus();
if (form.find(".cf7mls_progress_bar").length) {
let allow_choose_step = form.find('.cf7mls_progress_bar').attr('data-allow-choose-step');
let order_cur = Number(form.find('fieldset.cf7mls-choose-step').attr('data-cf7mls-order'));
let step_cur = Number(form.find(".cf7mls_progress_bar li.current").attr('data-counter')) - 1;
// allow choose step
if(allow_choose_step === 'on') {
form.find('fieldset.fieldset-cf7mls').css({display: ''});
form.find("fieldset.fieldset-cf7mls").removeClass("cf7mls-choose-step");
}
if((allow_choose_step === 'on') &&
((order_cur !== step_cur) && ((order_cur === 0) || order_cur))
) {
form.find("fieldset.fieldset-cf7mls").removeClass("cf7mls_back_fs");
form.find("fieldset.fieldset-cf7mls").removeClass("cf7mls_current_fs");
form.find('fieldset.fieldset-cf7mls').each(function( index, el ) {
if(index > step_cur) {
return;
}
$(el).addClass("cf7mls_back_fs");
if(index === step_cur) {
$(el).removeClass("cf7mls_back_fs");
$(el).addClass("cf7mls_current_fs");
}
});
}else {
var cur = form.find("fieldset.fieldset-cf7mls").index(current_fs);
var nex = form.find("fieldset.fieldset-cf7mls").index(next_fs);
// if(cf7mls_object.is_rtl == '1') {
// cur = 0 - cur
// nex = 0 - nex
// }
}
}
dhScrollTo(form);
return false;
} else {}
next_clicked = false;
})
.fail(function () {
$this.removeClass("sending");
next_clicked = false;
// console.log("Validation error");
})
.always(function () {
$this.removeClass("sending");
next_clicked = false;
// console.log("Validation complete");
});
return false;
}
});
$(document).on("click", ".cf7mls_back", function (event) {
var $this = $(this);
var form = $this.parent().closest("form.wpcf7-form");
$(".wpcf7-response-output.wpcf7-display-none")
.removeClass("wpcf7-validation-errors")
.removeAttr("style")
.html("");
$(".wpcf7-response-output.wpcf7-display-none.wpcf7-mail-sent-ok").hide();
current_fs = $this.closest(".fieldset-cf7mls");
previous_fs = current_fs.prev();
//reset error messages
current_fs.find(".wpcf7-form-control-wrap").removeClass("cf7mls-invalid");
current_fs.find(".cf7mls-invalid").removeClass("cf7mls-invalid");
current_fs.find(".wpcf7-not-valid").removeClass("wpcf7-not-valid");
current_fs.find(".wpcf7-form-control-wrap .wpcf7-not-valid-tip").remove();
/*
* Instead of hide current fs, we hide all
*/
/*current_fs.css({
height: '0px',
overflow: 'hidden',
opacity: '0',
'visibility': 'hidden'
}).removeClass('cf7mls_current_fs');*/
form.find(".fieldset-cf7mls").removeClass("cf7mls_current_fs");
previous_fs.addClass("cf7mls_current_fs").removeClass("cf7mls_back_fs");
dhScrollTo(form);
return false;
});
$(document).on("click", ".cf7mls-auto-next-step", function (event) {
var $this = $(this);
if($this.is("input")) {
$(document).on("change", $this, function (event) {
var currentFieldSet = $this.parent().closest("fieldset.fieldset-cf7mls");
$(currentFieldSet).find( ".cf7mls_next" ).trigger('click');
});
}else {
var currentFieldSet = $this.parent().closest("fieldset.fieldset-cf7mls");
$(currentFieldSet).find( ".cf7mls_next" ).trigger('click');
}
});
$(document).on("wpcf7mailsent", ".wpcf7-form", function (event) {
$(this).find(".fieldset-cf7mls-wrapper fieldset.fieldset-cf7mls").each(function(index) {
if($(this).hasClass('cf7mls_back_fs')) {
$(this).removeClass('cf7mls_back_fs');
}
if(index != 0) {
if($(this).hasClass('cf7mls_current_fs')) {
$(this).removeClass('cf7mls_current_fs');
}
} else {
if(!$(this).hasClass('cf7mls_current_fs')) {
$(this).addClass('cf7mls_current_fs');
}
}
});
});
});
function cf7mls_is_not_last_fieldset(fieldset, total_fieldset) {
var order = fieldset.data("cf7mls-order");
return order != total_fieldset;
}
function dhScrollTo(el) {
if (el.find(".fieldset-cf7mls-wrapper.no-scroll").length || el.hasClass('cf7mls-no-scroll')) {
return;
}
if (cf7mls_object.scroll_step == "true") {
$("html, body").animate({
scrollTop: el.offset().top - 110
},
"slow"
);
} else if (cf7mls_object.scroll_step == "scroll_to_top") {
$("html, body").animate({
scrollTop: $("body").offset().top - 110
},
"slow"
);
}
}
function cf7mls_toggle_next_btn(acceptances, fieldset) {
if (acceptances.length > 0) {
var ii = 0;
jQuery.each(acceptances, function (i, v) {
if (jQuery(v).is(":checked")) {
//console.log('checked');
} else {
ii++;
}
});
if (ii > 0) {
//console.log(ii);
jQuery(fieldset)
.find(".cf7mls_next")
.attr("disabled", "disabled");
} else {
jQuery(fieldset)
.find(".cf7mls_next")
.removeAttr("disabled");
}
}
}
})(jQuery);