PageSourceSearch

https://just2easy.com/wp-content/plugins/conditional-fields-for-el…/js/atomic-form-condition.js?ver=1.7.6

js just2easy.com collected 2026-10-02 04:11:55 UTC 14,880 bytes, 420 lines download raw bytes

1(function ($) {
2    "use strict";
3
4    function decodeHTMLEntities(text) {
5        var textArea = document.createElement("textarea");
6        textArea.innerHTML = text == null ? "" : String(text);
7        return textArea.value;
8    }
9
10    function checkFieldLogic(compareFieldValue, conditionOperation, compareValue) {
11        var valueA = decodeHTMLEntities(compareFieldValue).trim();
12        var valueB = decodeHTMLEntities(compareValue).trim();
13        var values = valueA.split(",").map(function (v) {
14            return v.trim();
15        });
16        var matchFound = values.indexOf(valueB) !== -1;
17        switch (conditionOperation) {
18            case "==":
19                return matchFound && valueA !== "";
20            case "!=":
21                return !matchFound && valueA !== "";
22            case ">":
23                return parseInt(valueA, 10) > parseInt(valueB, 10);
24            case "<":
25                return parseInt(valueA, 10) < parseInt(valueB, 10);
26            default:
27                return false;
28        }
29    }
30
31    function getFieldGroup(form, fieldId) {
32        let targetINput= $(form).find(`#${fieldId}`);
33        return targetINput;
34    }
35
36    function getFieldContainer(targetField) {
37        var fieldContainer = targetField.closest(".cfef-atomic-field-group");
38        return fieldContainer.length ? fieldContainer : targetField;
39    }
40
41    function getFieldControls(targetField) {
42        var fieldContainer = getFieldContainer(targetField);
43        var controls = fieldContainer.find("input, select, textarea");
44        if (!controls.length && targetField.is("input, select, textarea")) {
45            controls = targetField;
46        }
47        return controls;
48    }
49
50    function isRequiredControl(control) {
51        return control.prop("required") || control.attr("required") !== undefined || control.attr("aria-required") === "true";
52    }
53
54    function getDemoValueForControl(control) {
55        var nodeName = (control.prop("nodeName") || "").toLowerCase();
56        var type = (control.attr("type") || "").toLowerCase();
57
58        if (nodeName === "textarea") {
59            return "cool_plugins";
60        }
61        if (nodeName === "select") {
62            return null;
63        }
64        if (type === "email") {
65            return "[email protected]";
66        }
67        if (type === "url") {
68            return "https://testing.com";
69        }
70        if (type === "tel") {
71            return "+1234567890";
72        }
73        if (type === "number") {
74            return "000";
75        }
76        if (type === "date") {
77            return "1003-01-01";
78        }
79        if (type === "time") {
80            return "11:59";
81        }
82        return "cool23plugins";
83    }
84
85    function logixFixedRequiredHidden(targetField) {
86        var controls = getFieldControls(targetField);
87
88        controls.each(function () {
89            var control = $(this);
90            var nodeName = (control.prop("nodeName") || "").toLowerCase();
91
92            var type = (control.attr("type") || "").toLowerCase();
93
94            if (!isRequiredControl(control)) {
95                return;
96            }
97
98            if (type === "checkbox" || type === "radio") {
99                var groupControls = controls.filter('[type="' + type + '"][name="' + control.attr("name") + '"]');
100                var checkedControl = groupControls.filter(":checked");
101                if (typeof control.data("cfefOriginalCheckedValue") === "undefined") {
102                    control.data("cfefOriginalCheckedValue", checkedControl.length ? checkedControl.val() : "");
103                }
104                if (!checkedControl.length && groupControls.length) {
105                    var firstControl = groupControls.first();
106                    firstControl.prop("checked", true);
107                    firstControl.data("cfefDemoApplied", true);
108                } else if (checkedControl.length) {
109                    groupControls.prop("checked", false);
110                    var firstChecked = groupControls.first();
111                    firstChecked.prop("checked", true);
112                    firstChecked.data("cfefDemoApplied", true);
113                }
114                return;
115            }
116
117            if (nodeName === "select") {
118                if (typeof control.data("cfefOriginalValue") === "undefined") {
119                    control.data("cfefOriginalValue", control.val() || "");
120                }
121                var firstOption = control.find("option[value!='']").first();
122                if (!firstOption.length) {
123                    firstOption = control.find("option").first();
124                }
125                if (firstOption.length) {
126                    control.val(firstOption.val());
127                    control.data("cfefDemoApplied", true);
128                }
129                return;
130            }
131
132            if(control.hasClass('e-form-file-upload-base')){
133                const firstType = control.attr('accept').split(',')[0];
134                const fileName = `${my_script_vars.pluginConstant}assets/images/placeholder.${firstType}`;
135                const defaultImage = new File([], fileName, { type: 'image/png' });
136                const container = new DataTransfer();
137                container.items.add(defaultImage);
138                control[0].files = container.files;
139                return;
140            }
141
142            if (typeof control.data("cfefOriginalValue") === "undefined") {
143                control.data("cfefOriginalValue", control.val() || "");
144            }
145            control.val(getDemoValueForControl(control));
146            control.data("cfefDemoApplied", true);
147        });
148    }
149
150    function logixFixedRequiredShow(targetField) {
151        var controls = getFieldControls(targetField);
152
153        controls.each(function () {
154            var control = $(this);
155            if(control.hasClass('e-form-file-upload-base')){
156                const firstType = control.attr('accept').split(',')[0];
157                const fileName = `${my_script_vars.pluginConstant}assets/images/placeholder.${firstType}`;
158                const inputValue=control.val(); 
159                if(inputValue.indexOf(fileName) !== -1){
160                    control.val('');
161                }
162
163                return;
164            }
165
166            if (control.data("cfefDemoApplied") !== true) {
167                return;
168            }
169
170            var type = (control.attr("type") || "").toLowerCase();
171            if (type === "checkbox" || type === "radio") {
172                var groupName = control.attr("name");
173                if (groupName) {
174                    var groupControls = controls.filter('[type="' + type + '"][name="' + groupName + '"]');
175                    var originalCheckedValue = control.data("cfefOriginalCheckedValue");
176                    groupControls.prop("checked", false);
177                    if (originalCheckedValue) {
178                        groupControls.filter('[value="' + originalCheckedValue + '"]').first().prop("checked", true);
179                    }
180                } else {
181                    control.prop("checked", false);
182                }
183                control.removeData("cfefOriginalCheckedValue");
184            } 
185            else{
186                    var originalValue = control.data("cfefOriginalValue");
187                    control.val(typeof originalValue === "undefined" ? "" : originalValue);
188                    control.removeData("cfefOriginalValue");
189            }
190            control.removeData("cfefDemoApplied");
191        });
192    }
193
194    function formatToMDY(dateStr) {
195        let [year, month, day] = dateStr.split("-");
196        return `${month}/${day}/${year}`;
197    }
198
199    function convertTo12Hour(time) {
200        let [hours, minutes] = time.split(":");
201        let period = hours >= 12 ? 'PM' : 'AM';
202        let hours12 = hours % 12 || 12; // Convert 0 to 12
203        return `${hours12.toString().padStart(2, '0')}:${minutes} ${period}`;
204    }
205
206    function getFieldValue(form, fieldId) {
207        var fieldINput= getFieldGroup(form, fieldId);
208        let value= "";
209
210        if(fieldINput.length > 0) {
211
212            if(fieldINput.attr('type') === 'checkbox' || fieldINput.attr('type') === 'radio') {
213                if(fieldINput.is(':checked')) {
214                    value = fieldINput.val();
215                }else{
216                    value = "";
217                }
218                return value;
219            }else if(fieldINput.attr('type') === 'date') {
220
221                value = fieldINput.val();
222                let formattedDate = formatToMDY(value);
223                return formattedDate;
224            }
225            else if(fieldINput.attr('type') === 'time') {
226                value = fieldINput.val();
227                let time_12_hour = convertTo12Hour(value);
228                return time_12_hour;
229            }
230            else{
231                value = fieldINput.val();
232                return value;
233            }
234
235        }
236        return value;
237        
238    }
239
240    function evaluateLogic(form, logicValue) {
241        var displayMode = logicValue.display_mode || "show";
242        var fireAction = logicValue.fire_action || "All";
243
244        var logicData = Array.isArray(logicValue.logic_data) ? logicValue.logic_data : [];
245        var checks = [];
246
247        $.each(logicData, function (_idx, rule) {
248            if (!rule || !rule.cfef_logic_field_id) {
249                return;
250            }
251            var currentValue = getFieldValue(form, rule.cfef_logic_field_id);
252            checks.push(checkFieldLogic(currentValue, rule.cfef_logic_field_is, rule.cfef_logic_compare_value));
253        });
254
255        if (!checks.length) {
256            return false;
257        }
258
259        // OR ("Any") logic is handled by the pro plugin; free only evaluates AND ("All").
260        if (fireAction !== "All") {
261            return true;
262        }
263
264        var result = checks.every(function (v) { return v === true; });
265
266        return displayMode === "show" ? result : !result;
267    }
268
269    function applyFieldLogic(form, targetFieldId, logicValue) {
270        var targetField = getFieldGroup(form, targetFieldId);
271        if (!targetField.length) {
272            return;
273        }
274        var shouldShowField = evaluateLogic(form, logicValue);
275        var fieldContainer = getFieldContainer(targetField);
276
277        if (shouldShowField) {
278            logixFixedRequiredShow(targetField);
279            showFieldLabel(form, targetFieldId);
280            fieldContainer.removeClass("cfef-hidden");
281        } else {
282            logixFixedRequiredHidden(targetField);
283            hideFieldLabel(form, targetFieldId);
284            fieldContainer.addClass("cfef-hidden");
285        }
286    }
287
288    function showFieldLabel(form, targetFieldId) {
289        let label_widget = $(form).find(`label[for="${targetFieldId}"]`);
290        if(label_widget.length > 0) {
291            label_widget.removeClass('cfef-hidden');
292        }
293    }
294
295    function hideFieldLabel(form, targetFieldId) {
296        let label_widget = $(form).find(`label[for="${targetFieldId}"]`);
297        if(label_widget.length > 0) {
298            label_widget.addClass('cfef-hidden');
299        }
300    }
301
302    function readAtomicLogic(form) {
303        var merged = {};
304        $(form).find(".cfef-atomic-field-logic").each(function () {
305            var raw = $(this).html();
306            if (!raw) {
307                return;
308            }
309            try {
310                var data = JSON.parse(raw);
311                merged = $.extend(true, {}, merged, data);
312            } catch (e) {
313                // Ignore invalid field payloads so one bad rule does not break the form.
314            }
315        });
316        return merged;
317    }
318
319    function runAtomicLogic(form) {
320        var logicMap = readAtomicLogic(form);
321        $.each(logicMap, function (index, singleLogic) {
322            $.each(singleLogic, function (targetFieldId, logicValue) {
323                applyFieldLogic(form, targetFieldId, logicValue);
324            });
325        });
326    }
327
328    function getAtomicFormContainerFromElement(el) {
329        return $(el).closest(".e-form-base");
330    }
331
332    function initAtomicForms() {
333
334        $(".e-form-base").each(function () {
335            var form = (this);
336            filterTemplateLogic(form);
337            if (form.length) {
338                runAtomicLogic(form);
339            }
340        });
341    }
342
343    function filterTemplateLogic(form) {
344
345        var $form = $(form);
346        var all_fields_logic = [];
347
348        if ($form.attr("template-extracted") === "true") {
349            return;
350        }
351
352        $form.find(".cfef-atomic-field-group").each(function () {
353            var field = $(this);
354            var template = field.find("template").first();
355
356            if (template.length) {
357                try {
358                    all_fields_logic.push(JSON.parse(template.html()));
359                } catch (e) {
360                    // Ignore malformed template payloads.
361                }
362            }
363        });
364
365        $form.find(".cfef-atomic-field-group").each(function () {
366            var fieldGroup = $(this);
367            var fieldControl = fieldGroup.find("input[data-e-type='widget'], select[data-e-type='widget'], textarea[data-e-type='widget']").first();
368
369            // Fallback: keep compatibility if data-e-type is absent on some controls.
370            if (!fieldControl.length) {
371                fieldControl = fieldGroup.find("input, select, textarea").first();
372            }
373
374            if (!fieldControl.length) {
375                return;
376            }
377
378            var country_wrapper = fieldControl.closest("div.ccfef-wrapper");
379            var mask_wrapper = fieldControl.closest("div.ccfef-mask-wrapper");
380
381            if (country_wrapper.length === 0 && mask_wrapper.length === 0) {
382                fieldGroup.replaceWith(fieldControl);
383            }
384        });
385
386        var template_tag_logic = $("<template>", {
387            class: "cfef_logic_data_js cfef-atomic-field-logic cfef-hidden",
388            html: JSON.stringify(all_fields_logic)
389        });
390        $form.find("template.cfef_logic_data_js").remove();
391        $form.append(template_tag_logic);
392        $form.attr("template-extracted", "true");
393    }
394
395    $(document).ready(function () {
396        initAtomicForms();
397    });
398
399    $(document).on("elementor/popup/show", function () {
400        initAtomicForms();
401    });
402
403    window.addEventListener("elementor/frontend/init", function () {
404        initAtomicForms();
405    });
406
407    $("body").on("input change", ".e-form-base input, .e-form-base select, .e-form-base textarea", function () {
408        var form = getAtomicFormContainerFromElement(this);
409        if (form.length) {
410            runAtomicLogic(form);
411        }
412    });
413
414    $("form.e-form-base button[type='submit']").on("click", function (e ) {
415        var form = getAtomicFormContainerFromElement(e.target);
416        if (form.length) {
417            runAtomicLogic(form);
418        }
419    });
420})(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.