PluginProbe
Repeater Fields for Gravity Forms / 2.4.5
Repeater Fields for Gravity Forms v2.4.5
3.2.0 3.1.1 3.1.0 3.0.4 3.0.3 3.0.2 3.0.1 3.0.0 2.5.1 2.5.0 2.4.6 trunk 2.0.5 2.0.9 2.1.0 2.3.2 2.3.7 2.4.1 2.4.3 2.4.4 2.4.5
repeater-for-gravity-forms / libs / wp_repeater.js

wp_repeater.js in Repeater Fields for Gravity Forms 2.4.5, at libs/wp_repeater.js

904 lines 35.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function ($) {
2 "use strict";
3 jQuery(document).ready(function ($) {
4 jQuery(document).on('change keyup', '.gfield input, .gfield select, .gfield textarea', function (event) {
5 gf_raw_input_change(event, this);
6 });
7 $("body").on('change', ".container-repeater-field .gform-grid-col--size-auto", function (e) {
8 var self = $(this);
9 var value = $(this).val();
10 var input_ids = $(this).attr("name");
11 if (input_ids === undefined) {
12 return;
13 }
14 input_ids = input_ids.split("_");
15 var input_id = input_ids[1];
16 var form_ids = self.closest("form").attr("id");
17 form_ids = form_ids.split("_");
18 var form_id = form_ids[1];
19 var field_ids = input_ids[1].split(".");
20 if (field_ids[1] == 2) {
21 return;
22 }
23 var field_id = field_ids[0];
24 const obj = {
25 [input_id]: value,
26 [field_id + ".2"]: ""
27 }
28 var $nextSelect = $(this).closest(".ginput_container").find("#input_" + form_id + "_" + field_id + "_2_container select")
29 $.post(gformChainedSelectData.ajaxUrl, {
30 action: 'gform_get_next_chained_select_choices',
31 input_id: input_id,
32 form_id: form_id,
33 field_id: field_id,
34 value: obj,
35 nonce: gformChainedSelectData.nonce
36 }, function (response) {
37 if (!response) {
38 return;
39 }
40 var choices = $.parseJSON(response),
41 optionsMarkup = '';
42 $nextSelect.find('option').remove();
43 if (choices.length <= 0) {
44 //self.resetSelects( $select, true );
45 } else {
46 var hasSelectedChoice = false;
47 $.each(choices, function (i, choice) {
48 var selected = choice.isSelected ? 'selected="selected"' : '';
49 if (selected) {
50 hasSelectedChoice = true;
51 }
52 optionsMarkup += '<option value="' + choice.value + '"' + selected + '>' + choice.text + '</option>';
53 });
54 $nextSelect.show().append(optionsMarkup);
55 // the placeholder will be selected by default, rather than removing it and re-adding, just force the noOptions option to be selected
56 if (choices[0].noOptions) {
57 var $noOption = $nextSelect.find('option:last-child').clone(),
58 $nextSelects = $nextSelect.parents('span').nextAll().find('select');
59 $nextSelects.append($noOption);
60 $nextSelects.add($nextSelect)
61 .addClass('gf_no_options')
62 .find('option:last-child').prop('selected', true);
63 //self.toggleCompleted( true );
64 } else {
65 $nextSelect
66 .removeClass('gf_no_options')
67 //.prop( 'disabled', false ).show();
68 .toggleSelect(false, self);
69 if (hasSelectedChoice) {
70 $nextSelect.change();
71 }
72 }
73 }
74 //self.resizeSelects();
75 });
76 })
77 gform.addFilter('gform_is_value_match', function (isMatch, formId, rule) {
78 var check_repeater = rule['fieldId'].toString().split("-");
79 if (check_repeater.length < 2) {
80 return isMatch;
81 } else {
82 var $ = jQuery,
83 inputId = rule['fieldId'],
84 baseInputId = check_repeater[0],
85 randId = check_repeater[1],
86 baseFieldId = parseInt(baseInputId, 10),
87 inputIndex = gformExtractInputIndex(baseInputId),
88 isInputSpecific = inputIndex !== false,
89 $inputs;
90 if (isInputSpecific) {
91 $inputs = $('#input_{0}_{1}_{2}-{3}, #choice_{0}_{1}_{2}-{3}'.gformFormat(formId, baseFieldId, inputIndex, randId));
92 } else {
93 $inputs = $('input[id="input_{0}_{1}-{2}"], input[id^="input_{0}_{1}_"][id$="-{2}"], input[id^="choice_{0}_{1}_"][id$="-{2}"], select#input_{0}_{1}-{2}, textarea#input_{0}_{1}-{2}'.gformFormat(formId, baseFieldId, randId));
94 }
95 var isCheckable = $.inArray($inputs.attr('type'), ['checkbox', 'radio']) !== -1;
96 var isMatch = isCheckable ? gf_is_match_checkable($inputs, rule, formId, inputId) : gf_is_match_default($inputs.eq(0), rule, formId, inputId);
97 return isMatch;
98 }
99 });
100 gform.addFilter('gform_field_meta_raw_input_change', function (form, input, event) {
101 var htmlId = input.attr('id');
102 var fieldId = yeeaddons_gf_get_input_id_by_html_id(htmlId);
103 var formId = gf_get_form_id_by_html_id(htmlId);
104 if (formId !== undefined) {
105 var check_ids = htmlId.split("-");
106 if (check_ids.length > 1) {
107 form = { fieldId: fieldId, formId: formId };
108 }
109 }
110 return form;
111 });
112 function yeeaddons_gf_get_input_id_by_html_id(htmlId) {
113 var ids = htmlId ? htmlId.split('_') : [];
114 if (ids.length >= 3) {
115 var type = ids[0];
116 if (type === 'choice') {
117 var fieldId = ids[2];
118 var randSuffix = '';
119 var lastPart = ids[ids.length - 1];
120 if (lastPart && lastPart.indexOf('-') !== -1) {
121 var parts = lastPart.split('-');
122 randSuffix = '-' + parts[1];
123 }
124 return fieldId + randSuffix;
125 } else if (type === 'input') {
126 if (ids.length === 3) {
127 return ids[2];
128 } else if (ids.length >= 4) {
129 var fieldId = ids[2];
130 var indexPart = ids[3];
131 return fieldId + '.' + indexPart;
132 }
133 }
134 }
135 return htmlId;
136 }
137 gform.addAction('gform_input_change', function (elem, formId, fieldId) {
138 if (!window.gf_form_conditional_logic) {
139 return;
140 }
141 var dependentFieldIds = rgars(gf_form_conditional_logic, [formId, 'fields', (fieldId)].join('/'));
142 if (dependentFieldIds) {
143 gf_apply_rules(formId, dependentFieldIds);
144 }
145 }, 10);
146 get_repeater_data_name();
147 var names_upload = [];
148 jQuery(document).on("gform_page_loaded", function (e, form_id) {
149 //jQuery(".gform-datepicker").datepicker('destroy');
150 get_repeater_data_name();
151 });
152 jQuery(document).on('gform_post_render', function (event, form_id, current_page) {
153 //jQuery(".gform-datepicker").datepicker('destroy');
154 get_repeater_data_name();
155 });
156 gform.addFilter('gform_file_upload_markup', function (html, file, up, strings, imagesUrl, response) {
157 var repeater = $('#' + file.id).closest(".container-repeater-field").find(".gform_multifile_upload")
158 var val_repeater = repeater.val();
159 var link = response.data.uploaded_filename;
160 if (val_repeater == "") {
161 repeater.val(link)
162 } else {
163 repeater.val(val_repeater + ", " + link);
164 }
165 return html.replace("onclick", "data-onclick");;
166 });
167 var repeater_fields_htmls = {};
168 //condition out repeater out
169 gform.addAction('gform_post_conditional_logic_field_action1', function (formId, action, targetId, defaultValues, isInit) {
170 if (targetId != "") {
171 var dat_select = targetId.replace("#", '');
172 $(".gf-field-repeater-data-html").each(function () {
173 var html_data = $(this).val();
174 html_data = $(html_data);
175 if (action == "hide") {
176 $('[data-js-reload="' + dat_select + '"]', html_data).css("display", "none");
177 } else {
178 $('[data-js-reload="' + dat_select + '"]', html_data).css("display", "block");
179 }
180 $(this).val("<div class='container-repeater-field'>" + html_data.html() + '</div>');
181 })
182 if (action == "hide") {
183 $('[data-js-reload="' + dat_select + '"]').css("display", "none");
184 } else {
185 $('[data-js-reload="' + dat_select + '"]').css("display", "block");
186 }
187 }
188 });
189 $("body").on("click", ".gform_delete_file", function (e) {
190 e.preventDefault();
191 var str = $(this).data("onclick");
192 const regex = /\((.*?)\)/gm;
193 var m = "";
194 var a = "";
195 while ((m = regex.exec(str)) !== null) {
196 // This is necessary to avoid infinite loops with zero-width matches
197 if (m.index === regex.lastIndex) {
198 regex.lastIndex++;
199 }
200 a = m[1].split(',');;
201 }
202 gformDeleteUploadedFileRepeater(a[0], a[1], $(this));
203 return;
204 })
205 function gformDeleteUploadedFileRepeater(formId, fieldId, deleteButton) {
206 if (deleteButton.closest(".repeater-field-warp-item-data").length > 0) {
207 var rand_id = deleteButton.closest(".container-repeater-field").data("id");
208 var fileIndex = jQuery(deleteButton).parent().index();
209 var parent = jQuery("#field_" + formId + "_" + fieldId);
210 parent.find('input[type="file"],.validation_message,#extensions_message_' + formId + '_' + fieldId).removeClass("gform_hidden");
211 parent.find(".ginput_post_image_file").show();
212 //parent.find("input[type=\"text\"]").val('');
213 var filesJson = jQuery('#gform_uploaded_files_' + formId).val();
214 if (filesJson) {
215 var files = jQuery.secureEvalJSON(filesJson);
216 if (files) {
217 var inputName = "input_" + fieldId;
218 var full_name = deleteButton.closest(".repeater-field-warp-item-data").find('input[name="gform_multifile_upload_' + formId + '_' + fieldId + '__' + rand_id + '"]').val();
219 if (full_name == "") {
220 full_name = [];
221 } else {
222 full_name = full_name.split(",");
223 }
224 var $multfile = parent.find("#gform_multifile_upload_" + formId + "_" + fieldId + "__" + rand_id);
225 var remove_name = deleteButton.closest(".ginput_preview").find(".gfield_fileupload_filename").html();
226 var index1 = full_name.indexOf(remove_name);
227 if (index1 !== -1) {
228 full_name.splice(index1, 1);
229 }
230 deleteButton.closest(".repeater-field-warp-item-data").find('input[name="gform_multifile_upload_' + formId + '_' + fieldId + '__' + rand_id + '"]').val(full_name.join(","));
231 deleteButton.closest(".ginput_preview").remove();
232 if ($multfile.length > 0) {
233 files[inputName].splice(fileIndex, 1);
234 var settings = $multfile.data('settings');
235 var max = settings.gf_vars.max_files;
236 jQuery("#" + settings.gf_vars.message_id + "__" + rand_id).html('');
237 if (files[inputName].length < max)
238 gfMultiFileUploader.toggleDisabled(settings, false);
239 } else {
240 files[inputName] = null;
241 }
242 jQuery('#gform_uploaded_files_' + formId).val(jQuery.toJSON(files));
243 }
244 }
245 } else {
246 var parent = jQuery("#field_" + formId + "_" + fieldId);
247 var fileIndex = jQuery(deleteButton).parent().index();
248 deleteButton.closest(".ginput_preview").remove();
249 //displaying single file upload field
250 parent.find('input[type="file"],.validation_message,#extensions_message_' + formId + '_' + fieldId).removeClass("gform_hidden");
251 //displaying post image label
252 parent.find(".ginput_post_image_file").show();
253 //clearing post image meta fields
254 parent.find("input[type=\"text\"]").val('');
255 //removing file from uploaded meta
256 var filesJson = jQuery('#gform_uploaded_files_' + formId).val();
257 if (filesJson) {
258 var files = jQuery.secureEvalJSON(filesJson);
259 if (files) {
260 var inputName = "input_" + fieldId;
261 var $multfile = parent.find("#gform_multifile_upload_" + formId + "_" + fieldId);
262 if ($multfile.length > 0) {
263 files[inputName].splice(fileIndex, 1);
264 var settings = $multfile.data('settings');
265 var max = settings.gf_vars.max_files;
266 jQuery("#" + settings.gf_vars.message_id).html('');
267 if (files[inputName].length < max)
268 gfMultiFileUploader.toggleDisabled(settings, false);
269 } else {
270 files[inputName] = null;
271 }
272 jQuery('#gform_uploaded_files_' + formId).val(jQuery.toJSON(files));
273 }
274 }
275 }
276 }
277 var input_ids = [];
278 function change_name_and_ids(item, field_end = null, key = null) {
279 if (key == null) {
280 var id_rand = Math.floor(Math.random() * 10000);
281 } else {
282 var id_rand = key;
283 }
284 var datas = JSON.parse(field_end.find(".gf-field-repeater-data").val());
285 var datas_ids = datas.id;
286 datas_ids.push(id_rand);
287 datas.id = datas_ids;
288 field_end.find(".gf-field-repeater-data").val(JSON.stringify(datas));
289 item = $(item);
290 item.attr("data-id", id_rand);
291 $(".gfield_visibility_visible", item).each(function () {
292 var id = $(this).attr("id");
293 $(this).attr("id", id + "-" + id_rand);
294 $(this).attr("data-js-reload", id + "-" + id_rand);
295 })
296 $(".gform_fileupload_multifile", item).each(function () {
297 var id = $(this).attr("id");
298 var container_item = $(this).closest(".gfield gfield--type-fileupload");
299 var settings = $(this).attr("data-settings");
300 settings = jQuery.parseJSON(settings);
301 $.each(settings, function (index, value) {
302 switch (index) {
303 case "browse_button":
304 case "container":
305 case "drop_element":
306 case "filelist":
307 settings[index] = value + "__" + id_rand;
308 $("#" + value, item).attr("id", value + "__" + id_rand);
309 break;
310 case "multipart_params":
311 //settings["multipart_params"]["field_id"] = value.field_id + "__"+id_rand;
312 break;
313 }
314 });
315 item.append('<input type="hidden" name="' + id + '" class="gform_multifile_upload" />');
316 $(this).attr("id", id + "__" + id_rand);
317 $(this).attr("data-settings", JSON.stringify(settings));
318 names_upload.push(id + "__" + id_rand);
319 })
320 $("input", item).each(function () {
321 var name = $(this).attr("name");
322 if (typeof name !== 'string' || name == "MAX_FILE_SIZE") {
323 return;
324 }
325 var type = $(this).attr("type");
326 var id = $(this).attr("id");
327 input_ids.push(id);
328 if (name != "" && name.endsWith('[]')) {
329 name = name.replace(/\[\]$/, '');
330 $(this).attr("name", name + "__" + id_rand + "[]");
331 } else {
332 $(this).attr("name", name + "__" + id_rand);
333 }
334 $(this).attr("id", id + "-" + id_rand);
335 var value_check = localStorage.getItem(id + "-" + id_rand);
336 if (type == "checkbox") {
337 $(this).closest("div").find("label").attr("for", id + "-" + id_rand);
338 if (value_check != null) {
339 $(this).attr("checked", "checked");
340 }
341 } else if (type == "file") {
342 $(this).attr("id", id + "-" + id_rand);
343 } else if (type == "radio") {
344 $(this).attr("id", id + "-" + id_rand);
345 $(this).closest("div, li").find("label").attr("for", id + "-" + id_rand);
346 var value_check = localStorage.getItem(name + "__" + id_rand);
347 if (value_check != null) {
348 var old_check = $(this).val();
349 if (old_check == value_check) {
350 $(this).attr("checked", true);
351 }
352 }
353 }
354 else {
355 // ensure the field label's "for" attribute corresponds with the field's ID
356 $(this).closest("div").parent().find("label").attr("for", id + "-" + id_rand);
357 if (value_check != null) {
358 $(this).val(value_check);
359 }
360 }
361 })
362 $("textarea", item).each(function () {
363 var name = $(this).attr("name");
364 var id = $(this).attr("id");
365 $(this).attr("name", name + "__" + id_rand);
366 $(this).attr("id", id + "-" + id_rand);
367 // ensure the field label's "for" attribute corresponds with the field's ID
368 $(this).closest("div").parent().find("label").attr("for", id + "-" + id_rand);
369 var value_check = localStorage.getItem(id + "-" + id_rand);
370 if (value_check != null) {
371 $(this).val(value_check);
372 }
373 })
374 $("select", item).each(function () {
375 var name = $(this).attr("name");
376 var id = $(this).attr("id");
377 if (name.includes("[")) {
378 name = name.replace(/\[\]/g, '');
379 $(this).attr("name", name + "__" + id_rand + "[]");
380 } else {
381 $(this).attr("name", name + "__" + id_rand);
382 }
383 $(this).attr("id", id + "-" + id_rand);
384 // ensure the field label's "for" attribute corresponds with the field's ID
385 $(this).closest("div").parent().find("label").attr("for", id + "-" + id_rand);
386 var value_check = localStorage.getItem(id + "-" + id_rand);
387 if (value_check != null) {
388 $(this).val(value_check);
389 }
390 })
391 return item;
392 }
393 function add_repeater_data(button, key = null) {
394 var start_field;
395 if (key == null) {
396 var key = Math.floor(Math.random() * 10000);
397 }
398 var item = $('<div class="repeater-field-item"><div class="repeater-field-header"></div><div class="repeater-field-content"></div></div>');
399 button.prevAll().each(function (index) {
400 var item = $(this).clone();
401 if (item.hasClass("gfield--type-repeater_start")) {
402 start_field = $(this);
403 return false;
404 }
405 })
406 var html_field = get_repeater_data(button, key);
407 var header = get_repeater_data_header(start_field);
408 item.find(".repeater-field-header").append(header);
409 item.find(".repeater-field-content").append(html_field);
410 button.find(".repeater-field-warp-item").append(item);
411 update_repeater_count_header();
412 $("input").trigger("done_load_repeater");
413 var form_ids = button.attr("id").split("_");
414 if (yeeaddons_gf_repeater_data.pro == "ok") {
415 var form_id = form_ids[1];
416 if (window["gf_form_conditional_logic"] !== undefined) {
417 if (typeof window["gf_form_conditional_logic"][form_id] !== "undefined") {
418 var dependents = window["gf_form_conditional_logic"][form_id].dependents;
419 var dependents_new = dependents;
420 var dependents_new_id = [];
421 $.each(dependents, function (key_1, value) {
422 var datas_logic = [];
423 $.each(value, function (key_2, value_2) {
424 datas_logic.push(value_2 + "-" + key);
425 })
426 if (key_1.search("-") < 0) {
427 dependents_new[key_1 + "-" + key] = datas_logic;
428 dependents_new_id.push(key_1 + "-" + key);
429 } else {
430 dependents_new_id.push(key_1);
431 }
432 });
433 var fields = window["gf_form_conditional_logic"][form_id].fields;
434 var fields_new = fields;
435 $.each(fields, function (key_1, value) {
436 var datas_logic = [];
437 $.each(value, function (key_2, value_2) {
438 datas_logic.push(value_2 + "-" + key);
439 })
440 if (key_1.search("-") < 0) {
441 fields_new[key_1 + "-" + key] = datas_logic;
442 }
443 });
444 var logic = window["gf_form_conditional_logic"][form_id].logic;
445 //console.log(window["gf_form_conditional_logic"][form_id]);
446 var logic_new = logic;
447 $.each(logic, function (key_1, value) {
448 if (key_1.search("-") < 0) {
449 // use old and add new repeater
450 logic_new[key_1 + "-" + key] = yeeaddons_change_id_logic(value, key);
451 } else {
452 //da them
453 }
454 });
455 var animation = window["gf_form_conditional_logic"][form_id].animation;
456 var defaults = window["gf_form_conditional_logic"][form_id].defaults;
457 window["gf_form_conditional_logic"][form_id] = { animation: animation, defaults: defaults, dependents: dependents_new, fields: fields_new, logic: logic_new, "ok": "ok" };
458 gf_apply_rules(form_id, dependents_new_id, true);
459 }
460 }
461 }
462 //end logic
463 var j = 0;
464 $.each(names_upload, function (key_1, value) {
465 gfMultiFileUploader.setup("#" + value);
466 names_upload.splice(j, 1);
467 j++;
468 });
469 var input_mask = yeeaddons_gf_repeater_data.input_mask;
470 $.each(input_ids, function (key_1, value_1) {
471 if (value_1 in input_mask) {
472 $('#' + value_1 + "-" + key).mask(input_mask[value_1]).bind('keypress', function (e) { if (e.which == 13) { jQuery(this).blur(); } })
473 }
474 });
475 conditional_logic_custom(key);
476 input_ids = [];
477 $('.gform-datepicker').each(function () {
478 var $element = $(this);
479 initSingleDatepicker($element);
480 $element.addClass('initialized');
481 });
482 }
483 function yeeaddons_change_id_logic(value, key) {
484 var field_rules_inner = [];
485 if (value && Array.isArray(value.field.rules)) { // added this line to fix js issue in add more to autofill multiple data
486 $.each(value.field.rules, function (key_2, value_2) {
487 if (value_2.fieldId.search("-") < 0) {
488 var field_id_1 = value_2.fieldId + "-" + key;
489 }
490 field_rules_inner.push({ fieldId: field_id_1, operator: value_2.operator, value: value_2.value });
491 })
492 var rules = field_rules_inner;
493 var field = { actionType: value.field.actionType, enabled: value.field.enabled, logicType: value.field.logicType, rules: rules };
494 return { field: field, nextButton: value.nextButton, section: value.section };
495 } else {
496 return value;
497 }
498
499 }
500 function conditional_logic_custom(rand) {
501 return;
502 var value_check = $("#input_1_13-" + rand).val();
503 if (value_check == "Other" || value_check == "other") {
504 $("#input_1_14-" + rand).closest(".gfield").addClass("hidden");
505 } else {
506 $("#input_1_14-" + rand).closest(".gfield").removeClass("hidden");
507 }
508 }
509 gform.addAction('gform_input_change', function (elem, formId, fieldId) {
510 var datas = $(elem).attr("name").split("__");;
511 if (datas.length > 1) {
512 conditional_logic_custom(datas[1]);
513 }
514 }, 10);
515 function get_repeater_data(step_field, key = null) {
516 var data_html = step_field.find(".gf-field-repeater-data-html").val();
517 if (data_html == "") {
518 data_html = step_field.find(".gf-field-repeater-data-html").attr('value');
519 }
520 var html_step = change_name_and_ids(data_html, step_field, key);
521 return html_step;
522 }
523 function get_repeater_data_name() {
524 var i = 1;
525 $(".gfield--type-repeater_start").each(function () {
526 if ($(this).data("installed") == "installed") {
527 return;
528 }
529 $(this).data("installed", "installed");
530 $(this).addClass("installed");
531 var html_step = $("<div class='container-repeater-field'></div>");
532 var names = [];
533 var step_field = "";
534 var elements = $(this).nextAll();
535 var value = "";
536 elements.each(function (index) {
537 var item = $(this).clone();
538 if (item.hasClass("gfield--type-repeater_end")) {
539 $(this).attr("data-id", i);
540 value = $(this).find("input").val();
541 if (value == "") {
542 value = $(this).find("input").attr("value");
543 }
544 step_field = $(this);
545 $(this).find(".gf-field-repeater-data").val(JSON.stringify({ "count": 1, "fields": names, "id": [] }));
546 $(this).find(".gf-field-repeater-data").attr("value", JSON.stringify({ "count": 1, "fields": names, "id": [] }));
547 return false;
548 }
549 $(this).remove();
550 html_step.append(item);
551 var check_name = null;
552 if (item.find(".ginput_container_fileupload").length > 0) {
553 var name = item.find("input[type=file]").attr("name");
554 if (name === undefined) {
555 name = item[0].id;
556 name = name.split("field");
557 names.push("gform_multifile_upload" + name[1]);
558 } else {
559 names.push(item.find("input[type=file]").attr("name"));
560 }
561 } else {
562 if (item.find("input").attr("name")) {
563 var custom_n = item.find("input").attr("name");
564 custom_n = custom_n.replace(/\[\]$/, '');
565 names.push(custom_n);
566 } else if (item.find("textarea").attr("name")) {
567 names.push(item.find("textarea").attr("name"));
568 } else if (item.find("select").attr("name")) {
569 names.push(item.find("select").attr("name"));
570 } else {
571 var type = item.find("input").attr("type");
572 names.push(item.find("input").attr("name"));
573 }
574 }
575 })
576 var text_html = "<div class='container-repeater-field'>" + html_step.html() + "</div>";
577 step_field.find(".gf-field-repeater-data-html").val(text_html);
578 step_field.find(".gf-field-repeater-data-html").attr("value", text_html);
579 var initial_rows = 1;
580 initial_rows = step_field.find(".repeater-field-warp-item-data").data("initial_rows");
581 var initial_rows_map_field_check = step_field.find(".repeater-field-warp-item-data").data("initial_rows_map_check");
582 if (initial_rows_map_field_check != "" && initial_rows_map_field_check !== undefined) {
583 var initial_rows_map_field = step_field.find(".repeater-field-warp-item-data").data("initial_rows_map");
584 var initial_rows_map_number = $("#" + initial_rows_map_field).val();
585 if (initial_rows_map_number == "") {
586 initial_rows_map_number = $("#" + initial_rows_map_field).attr("value");
587 }
588 if (initial_rows_map_number == "") {
589 initial_rows_map_number = 0;
590 }
591 $("#" + initial_rows_map_field).attr("data-repeater", step_field.find(".repeater-field-warp-item-data").data("map_id"));
592 $("#" + initial_rows_map_field).attr("repeater_initial_rows", "ok");
593 initial_rows = initial_rows_map_number;
594 step_field.find(".gf-repeater-field-button-add").addClass("hidden");
595 step_field.addClass("repeater-remove-toolbar");
596 }
597 if (initial_rows === undefined || initial_rows === "") {
598 initial_rows = 1;
599 }
600 if (value != "") {
601 value = JSON.parse(value);
602 initial_rows = value.count;
603 var data_arr_ids = value.id;
604 setTimeout(function () {
605 for (var j = 0; j < initial_rows; j++) {
606 add_repeater_data(step_field.closest(".gfield--type-repeater_end"), data_arr_ids[j]);
607 }
608 }, 100);
609 } else {
610 setTimeout(function () {
611 for (var j = 0; j < initial_rows; j++) {
612 add_repeater_data(step_field.closest(".gfield--type-repeater_end"));
613 }
614 }, 100);
615 }
616 i++;
617 })
618 }
619 $("body").on("change", "[repeater_initial_rows='ok']", function (e) {
620 var repeater_id = $(this).data("repeater");
621 $("#" + repeater_id).find(".repeater-field-item").remove();
622 var number = $(this).val();
623 if (number == "") {
624 number = $(this).attr("value");
625 }
626 for (let i = 0; i < number; i++) {
627 $("#" + repeater_id).find(".gf-repeater-field-button-add").click();
628 }
629 })
630 function get_repeater_data_header(start_field) {
631 var html_step = start_field.find(".repeater-field-header-data").val();
632 if (html_step == "") {
633 html_step = start_field.find(".repeater-field-header-data").attr("value");
634 }
635 return html_step;
636 }
637 function update_repeater_count_header() {
638 $(".gfield--type-repeater_end").each(function () {
639 var i = 1;
640 $(".repeater-field-item", $(this)).each(function () {
641 $(this).find(".repeater-field-header-count").html(i);
642 i++;
643 })
644 var data_js = $(this).find(".gf-field-repeater-data").val();
645 if (data_js == "") {
646 $(this).find(".gf-field-repeater-data").attr("value");
647 }
648 var datas = JSON.parse(data_js);
649 datas.count = i - 1;
650 $(this).find(".gf-field-repeater-data").val(JSON.stringify(datas));
651 $(this).find(".gf-field-repeater-data").attr("value", JSON.stringify(datas));
652 });
653 }
654 function check_max_row(step_field) {
655 var max = step_field.find(".repeater-field-warp-item-data").data("limit");
656 var number_item = $('.repeater-field-item', step_field).length;
657 if (number_item >= max) {
658 return false;
659 } else {
660 return true;
661 }
662 }
663 function check_min_row(step_field) {
664 var min = step_field.find(".repeater-field-warp-item-data").data("initial_rows");
665 var number_item = $('.repeater-field-item', step_field).length;
666 if (number_item <= min) {
667 return false;
668 } else {
669 return true;
670 }
671 }
672 function removeAR(arr) {
673 var what, a = arguments, L = a.length, ax;
674 while (L > 1 && arr.length) {
675 what = a[--L];
676 while ((ax = arr.indexOf(what)) !== -1) {
677 arr.splice(ax, 1);
678 }
679 }
680 return arr;
681 }
682 $("body").on("click", ".gf-repeater-field-button-add", function (e) {
683 e.preventDefault();
684 if (check_max_row($(this).closest(".gfield--type-repeater_end"))) {
685 add_repeater_data($(this).closest(".gfield--type-repeater_end"));
686 } else {
687 $(this).addClass('hidden');
688 }
689 })
690 $("body").on("click", ".repeater-field-header-acctions-toogle", function (e) {
691 e.preventDefault();
692 if ($(this).hasClass("icon-down-open")) {
693 $(this).removeClass("icon-down-open");
694 $(this).addClass("icon-up-open");
695 } else {
696 $(this).addClass("icon-down-open");
697 $(this).removeClass("icon-up-open");
698 }
699 $(this).closest(".repeater-field-item").find(".repeater-field-content").slideToggle("slow");
700 $(this).closest(".repeater-field-item").find(".repeater-field-header").toggleClass('repeater-content-show');
701 })
702 $("body").on("click", ".repeater-field-header-acctions-remove", function (e) {
703 e.preventDefault();
704 $(this).closest(".gfield--type-repeater_end").find(".gf-repeater-field-button-add").removeClass('hidden');
705 if (check_min_row($(this).closest(".gfield--type-repeater_end"))) {
706 var id = $(this).closest(".repeater-field-item").find(".container-repeater-field").data("id");
707 var data_js = $(this).closest(".gfield--type-repeater_end").find(".gf-field-repeater-data").val();
708 if (data_js == "") {
709 data_js = $(this).closest(".gfield--type-repeater_end").find(".gf-field-repeater-data").attr("value");
710 }
711 var datas = JSON.parse(data_js);
712 var datas_ids = datas.id;
713 datas_ids = removeAR(datas_ids, id);
714 datas.id = datas_ids;
715 $(this).closest(".gfield--type-repeater_end").find(".gf-field-repeater-data").val(JSON.stringify(datas));
716 $(this).closest(".gfield--type-repeater_end").find(".gf-field-repeater-data").attr("value", JSON.stringify(datas));
717 $(this).closest(".repeater-field-item").remove();
718 } else {
719 }
720 update_repeater_count_header();
721 })
722 //date pick
723 function getDatepickerI18n() {
724 var i18n = gform_i18n.datepicker;
725 return {
726 dayNamesMin: [
727 i18n.days.sunday,
728 i18n.days.monday,
729 i18n.days.tuesday,
730 i18n.days.wednesday,
731 i18n.days.thursday,
732 i18n.days.friday,
733 i18n.days.saturday,
734 ],
735 monthNamesShort: [
736 i18n.months.january,
737 i18n.months.february,
738 i18n.months.march,
739 i18n.months.april,
740 i18n.months.may,
741 i18n.months.june,
742 i18n.months.july,
743 i18n.months.august,
744 i18n.months.september,
745 i18n.months.october,
746 i18n.months.november,
747 i18n.months.december,
748 ],
749 firstDay: i18n.firstDay,
750 iconText: i18n.iconText,
751 };
752 }
753 /**
754 * @function getDatepickerBaseOptions
755 * @description Return base options object that configures the datepicker.
756 * @param $element The datepicker trigger.
757 * @since 2.5
758 *
759 * @returns {{
760 * suppressDatePicker: boolean,
761 * changeMonth: boolean,
762 * changeYear: boolean,
763 * onClose: onClose,
764 * yearRange: string,
765 * dateFormat: string,
766 * showOn: string,
767 * dayNamesMin: *[],
768 * monthNamesShort: *[],
769 * beforeShow: (function(*, *): boolean),
770 * showOtherMonths: boolean
771 * }}
772 */
773 function getDatepickerBaseOptions($element) {
774 var i18n = getDatepickerI18n();
775 var isThemeDatepicker = $element.closest('.gform_wrapper').length > 0;
776 var isPreview = $('#preview_form_container').length > 0;
777 var isRTL = window.getComputedStyle($element[0], null).getPropertyValue('direction') === 'rtl';
778 var formTheme = isThemeDatepicker ? $element.closest('.gform_wrapper').data('form-theme') : 'gravity-theme';
779 var formId = isThemeDatepicker ? $element.closest('.gform_wrapper').attr('id').replace('gform_wrapper_', '') : '';
780 var formPageInstance = isThemeDatepicker ? $element.closest('.gform_wrapper').attr('data-form-index') : '';
781 return {
782 yearRange: '-100:+20',
783 showOn: 'focus',
784 dateFormat: 'mm/dd/yy',
785 dayNamesMin: i18n.dayNamesMin,
786 monthNamesShort: i18n.monthNamesShort,
787 firstDay: i18n.firstDay,
788 changeMonth: true,
789 changeYear: true,
790 isRTL: isRTL,
791 showOtherMonths: isThemeDatepicker,
792 suppressDatePicker: false,
793 onClose: function () {
794 var self = this;
795 $element.focus();
796 this.suppressDatePicker = true;
797 setTimeout(function () {
798 self.suppressDatePicker = false;
799 }, 200);
800 },
801 beforeShow: function (input, inst) {
802 // Remove any classes that were added before as it could have been added to a different datepicker.
803 inst.dpDiv[0].classList.remove('gform-theme-datepicker');
804 inst.dpDiv[0].classList.remove('gravity-theme');
805 inst.dpDiv[0].classList.remove('gform-theme');
806 inst.dpDiv[0].classList.remove('gform-legacy-datepicker');
807 inst.dpDiv[0].classList.remove('gform-theme--framework');
808 inst.dpDiv[0].classList.remove('gform-theme--foundation');
809 inst.dpDiv[0].classList.remove('gform-theme--orbital');
810 if (isThemeDatepicker) {
811 inst.dpDiv[0].classList.add('gform-theme-datepicker');
812 $(inst.dpDiv[0]).attr('data-parent-form', formId + '_' + formPageInstance);
813 }
814 if (formTheme === undefined || formTheme === 'gravity-theme') {
815 $(inst.dpDiv[0]).addClass('gravity-theme');
816 } else if (formTheme === 'legacy') {
817 $(inst.dpDiv[0]).addClass('gform-legacy-datepicker');
818 }
819 else {
820 $(inst.dpDiv[0]).addClass('gform-theme--' + formTheme);
821 if (formTheme === 'orbital') {
822 $(inst.dpDiv[0]).addClass('gform-theme--framework');
823 $(inst.dpDiv[0]).addClass('gform-theme--foundation');
824 }
825 }
826 if (isRTL && isPreview) {
827 var $inputContainer = $(input).closest('.gfield');
828 var rightOffset = $(document).outerWidth() - ($inputContainer.offset().left + $inputContainer.outerWidth());
829 inst.dpDiv[0].style.right = rightOffset + 'px';
830 }
831 return !this.suppressDatePicker;
832 },
833 };
834 }
835 /**
836 * @function initSingleDatepicker
837 * @description Initialize a datepicker assigning various additional options based on the trigger element.
838 * @param $element The datepicker trigger.
839 * @since 2.4
840 */
841 function initSingleDatepicker($element) {
842 var i18n = getDatepickerI18n();
843 var inputId = $element.attr('id') ? $element.attr('id') : '';
844 var optionsObj = getDatepickerBaseOptions($element);
845 if ($element.hasClass('dmy')) {
846 optionsObj.dateFormat = 'dd/mm/yy';
847 } else if ($element.hasClass('dmy_dash')) {
848 optionsObj.dateFormat = 'dd-mm-yy';
849 } else if ($element.hasClass('dmy_dot')) {
850 optionsObj.dateFormat = 'dd.mm.yy';
851 } else if ($element.hasClass('ymd_slash')) {
852 optionsObj.dateFormat = 'yy/mm/dd';
853 } else if ($element.hasClass('ymd_dash')) {
854 optionsObj.dateFormat = 'yy-mm-dd';
855 } else if ($element.hasClass('ymd_dot')) {
856 optionsObj.dateFormat = 'yy.mm.dd';
857 }
858 if ($element.hasClass('gdatepicker_with_icon')) {
859 optionsObj.showOn = 'both';
860 optionsObj.buttonImage = $element.parent().siblings("[id^='gforms_calendar_icon_input']").val();
861 optionsObj.buttonImageOnly = true;
862 optionsObj.buttonText = i18n.iconText;
863 } else {
864 optionsObj.showOn = 'focus';
865 }
866 inputId = inputId.split('_');
867 // allow the user to override the datepicker options object
868 optionsObj = gform.applyFilters('gform_datepicker_options_pre_init', optionsObj, inputId[1], inputId[2], $element);
869 $element.datepicker(optionsObj);
870 // We give the input focus after selecting a date which differs from default Datepicker behavior; this prevents
871 // users from clicking on the input again to open the datepicker. Let's add a manual click event to handle this.
872 if ($element.is(':input')) {
873 $element.click(function () {
874 $element.datepicker('show');
875 });
876 }
877 }
878 })
879 $(document).on("change", ".gfield--type-repeater_end input,.gfield--type-repeater_end textarea,.gfield--type-repeater_end select", function (event) {
880 var id = $(this).attr("id");
881 if (typeof (Storage) !== "undefined") {
882 if ($(this).attr("name") != "" && typeof $(this).attr("name") != 'undefined') {
883 var type = $(this).attr("type");
884 var name = $(this).attr("id");
885 if (type == "checkbox") {
886 if ($("#" + name + ":checked").length > 0) {
887 var value = $("#" + name + ":checked").val();
888 localStorage.setItem(id, value);
889 } else {
890 localStorage.removeItem(id);
891 }
892 } else if (type == "radio") {
893 var id = $(this).attr("name");
894 var value = $(this).val();
895 localStorage.setItem(id, value);
896 }
897 else {
898 var value = $(this).val();
899 localStorage.setItem(id, value);
900 }
901 }
902 }
903 });
904 })(jQuery);