PageSourceSearch

https://www.theatertoolkit.com/App_Plugins/UmbracoForms/Assets/Themes/Default/umbracoforms-conditions.js

js theatertoolkit.com collected 2026-10-08 07:25:27 UTC 8,401 bytes, 242 lines download raw bytes

1var umbracoForms = umbracoForms || {};
2umbracoForms.conditions = function(form, fieldsetConditions, fieldConditions, values) {
3
4    //our conditions "class" - must always be newed to work as it uses a form instance to operate on
5    //load all the information from the dom and serverside info and then the class will take care of the rest
6
7    var _this = {};
8    _this.form = form;
9    _this.fieldsetConditions = fieldsetConditions;
10    _this.fieldConditions = fieldConditions;
11    _this.values = values;
12
13    //Iterates through all the form elements found on the page to update the registered value
14    function PopulateFieldValues(page, formValues) {
15
16        $("select", page).each(function() {
17            formValues[$(this).attr("id")] = $("option[value='" + $(this).val() + "']", $(this)).text();
18        });
19
20        $("textarea", page).each(function() {
21            formValues[$(this).attr("id")] = $(this).val();
22        });
23
24        // clear out all saved checkbox values to we can safely append
25        $('input[type=checkbox]', page).each(function() {
26            formValues[$(this).attr("name")] = null;
27        });
28
29        $("input", page).each(function() {
30
31            if ($(this).attr('type') == "text" || $(this).attr("type") == "hidden") {
32                formValues[$(this).attr("id")] = $(this).val();
33            }
34
35            if ($(this).attr('type') == "radio") {
36                if ($(this).is(':checked')) {
37                    formValues[$(this).attr("name")] = $(this).val();
38                }
39            }
40
41            if ($(this).attr('type') == "checkbox") {
42                if ($(this).attr('id') != $(this).attr('name')) {
43                    if ($(this).is(':checked')) {
44                        if (formValues[$(this).attr("name")] == null) {
45                            formValues[$(this).attr("name")] = $(this).val();
46                        } else {
47                            formValues[$(this).attr("name")] += "," + $(this).val();
48                        }
49                    }
50                } else {
51                    formValues[$(this).attr("name")] = $(this).is(':checked').toString();
52                }
53            }
54        });
55    }
56
57    /* Public api */
58
59    _this.operators = {
60        Is: function(value, expected) {
61            if ((value || "") === expected) {
62                return true;
63            }
64            var values = value.split(',');
65            var matchingExpected = $.grep(values,
66                function(o) {
67                    return o === expected;
68                });
69            return matchingExpected.length > 0;
70        },
71        IsNot: function(value, unexpected) {
72            var values = value.split(',');
73            var matchingUnexpected = $.grep(values,
74                function(o) {
75                    return o === unexpected;
76                });
77            return (value || "") !== unexpected && matchingUnexpected.length === 0;
78        },
79        GreaterThen: function(value, limit) {
80            return parseInt(value) > parseInt(limit);
81        },
82        LessThen: function(value, limit) {
83            return parseInt(value) < parseInt(limit);
84        },
85        StartsWith: function(value, criteria) {
86            return value && value.indexOf(criteria) === 0;
87        },
88        EndsWith: function(value, criteria) {
89            return value && value.indexOf(criteria) === value.length - criteria.length;
90        },
91        Contains: function(value, criteria) {
92            return value && value.indexOf(criteria) > -1;
93        }
94    };
95
96    _this.watch = function() {
97
98        //subscribe to change events
99        $("input, textarea, select", _this.form).change(function() {
100            PopulateFieldValues(_this.form, _this.values);
101
102            //process the conditions
103            _this.run();
104        });
105
106        //register all values from the current fields on the page
107        PopulateFieldValues(_this.form, this.values);
108
109        //the initial run-through of all the conditions
110        _this.run();
111    };
112
113    _this.run = function() {
114        var fsId,
115            fieldId,
116
117            /*
118            fsConditions = params.fsConditions || {},
119            fieldConditions = params.fieldConditions || {},
120            values = params.values || {},*/
121
122            cachedResults = {};
123
124        function evaluateRuleInstance(rule) {
125            var value = _this.values[rule.field],
126                func = _this.operators[rule.operator],
127                result = value !== null && func(value, rule.value);
128            return result;
129        }
130
131        function evaluateRule(rule) {
132            var dependencyIsVisible = true;
133
134            if (_this.fieldConditions[rule.field]) {
135                dependencyIsVisible = isVisible(rule.field, _this.fieldConditions[rule.field]);
136            }
137
138            if (dependencyIsVisible) {
139                return evaluateRuleInstance(rule);
140            } else {
141                return false;
142            }
143        }
144
145        function evaluateCondition(id, condition) {
146            // This was once pretty. Now it needs refactoring again. :)
147
148            var any = condition.logicType === "Any",
149                all = condition.logicType === "All",
150                fieldsetVisibilities = {},
151                hasHiddenFieldset = false,
152                success = true,
153                rule,
154                i;
155
156            for (i = 0; i < condition.rules.length; i++) {
157                rule = condition.rules[i];
158
159                if (id === rule.field || id === rule.fieldsetId) {
160                    throw new Error("Field or fieldset " + id + " has a condition on itself.");
161                }
162
163                if (fieldsetVisibilities[rule.fieldsetId] !== undefined) {
164                    continue;
165                }
166
167                if (_this.fieldsetConditions[rule.fieldsetId]) {
168
169                    fieldsetVisibilities[rule.fieldsetId] =
170                        isVisible(rule.fieldsetId, _this.fieldsetConditions[rule.fieldsetId]);
171
172                    if (!fieldsetVisibilities[rule.fieldsetId]) {
173                        hasHiddenFieldset = true;
174                    }
175                } else {
176                    fieldsetVisibilities[rule.fieldsetId] = true;
177                }
178            }
179
180            if (all && hasHiddenFieldset) {
181                return false;
182            }
183
184            for (i = 0; i < condition.rules.length; i++) {
185                rule = condition.rules[i];
186
187                if (fieldsetVisibilities[rule.fieldsetId]) {
188                    success = evaluateRule(condition.rules[i]);
189                } else {
190                    success = false;
191                }
192
193                if (any && success) {
194                    break;
195                }
196                if (all && !success) {
197                    break;
198                }
199            }
200            return success;
201        }
202
203        function evaluateConditionVisibility(id, condition) {
204            var show = condition.actionType === "Show",
205                cachedResult = cachedResults[id],
206                success = cachedResult === undefined
207                    ? (cachedResults[id] = evaluateCondition(id, condition))
208                    : cachedResult,
209                visible = !(success ^ show);
210            return visible;
211        }
212
213        function isVisible(id, condition) {
214            if (condition) {
215                return evaluateConditionVisibility(id, condition);
216            }
217            return true;
218        }
219
220        function handleCondition(element, id, condition, type) {
221            var shouldShow = isVisible(id, condition);
222            if (shouldShow) {
223                element.show();
224            } else {
225                element.hide();
226            }
227        }
228
229        for (fsId in _this.fieldsetConditions) {
230            handleCondition($("#" + fsId), fsId, _this.fieldsetConditions[fsId], "Fieldset");
231        }
232
233        for (fieldId in _this.fieldConditions) {
234            handleCondition($("#" + fieldId).closest(".umbraco-forms-field"),
235                fieldId,
236                _this.fieldConditions[fieldId],
237                "Field");
238        }
239    }
240
241    return _this;
242}

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.