PageSourceSearch

https://www.thehewittfertilitycentre.org.uk/App_Plugins/UmbracoForms/Assets/umbracoforms-conditions.js

js thehewittfertilitycentre.org.uk collected 2026-10-04 13:27:33 UTC 5,501 bytes, 154 lines download raw bytes

1var umbracoForms = umbracoForms || {};
2(function (uf) {
3    var conditions = uf.conditions || {},
4        operators = conditions.operators || {
5            Is: function (value, expected) {
6                return (value || "") === expected;
7            },
8            IsNot: function (value, unexpected) {
9                return (value || "") !== unexpected;
10            },
11            GreaterThen: function (value, limit) {
12                return parseInt(value) > parseInt(limit);
13            },
14            LessThen: function (value, limit) {
15                return parseInt(value) < parseInt(limit);
16            },
17            StartsWith: function (value, criteria) {
18                return value && value.indexOf(criteria) === 0;
19            },
20            EndsWith: function (value, criteria) {
21                return value && value.indexOf(criteria) === value.length - criteria.length;
22            },
23            Contains: function (value, criteria) {
24                return value && value.indexOf(criteria) > -1;
25            }
26        };
27
28    uf.conditions = conditions;
29    uf.conditions.operators = operators;
30
31    conditions.handle = function (params) {
32        var fsId,
33            fieldId,
34            fsConditions = params.fsConditions || {},
35            fieldConditions = params.fieldConditions || {},
36            values = params.values || {},
37            cachedResults = {};
38
39        function evaluateRuleInstance(rule) {
40            var value = values[rule.field],
41                func = operators[rule.operator],
42                result = value !== null && func(value, rule.value);
43            console.log(rule.field + ": " + value + " " + rule.operator + " " + rule.value + " = " + result + "\n");
44            return result;
45        }
46
47        function evaluateRule(rule) {
48            var dependencyIsVisible = true;
49
50            if (fieldConditions[rule.field]) {
51                dependencyIsVisible = isVisible(rule.field, fieldConditions[rule.field]);
52            }
53
54            if (dependencyIsVisible) {
55                return evaluateRuleInstance(rule);
56            } else {
57                return false;
58            }
59        }
60
61        function evaluateCondition(id, condition) {
62            // This was once pretty. Now it needs refactoring again. :)
63
64            var any = condition.logicType === "Any",
65                all = condition.logicType === "All",
66                fieldsetVisibilities = {},
67                hasHiddenFieldset = false,
68                success = true,
69                rule,
70                i;
71
72            for (i = 0; i < condition.rules.length; i++) {
73                rule = condition.rules[i];
74
75                if (id === rule.field || id === rule.fieldsetId) {
76                    throw new Error("Field or fieldset " + id + " has a condition on itself.");
77                }
78
79                if (fieldsetVisibilities[rule.fieldsetId] !== undefined) {
80                    continue;
81                }
82
83                if (fsConditions[rule.fieldsetId]) {
84                    fieldsetVisibilities[rule.fieldsetId] = isVisible(rule.fieldsetId, fsConditions[rule.fieldsetId]);
85                    if (!fieldsetVisibilities[rule.fieldsetId]) {
86                        hasHiddenFieldset = true;
87                    }
88                } else {
89                    fieldsetVisibilities[rule.fieldsetId] = true;
90                }
91            }
92
93            if (all && hasHiddenFieldset) {
94                return false;
95            }
96
97            for (i = 0; i < condition.rules.length; i++) {
98                rule = condition.rules[i];
99
100                if (fieldsetVisibilities[rule.fieldsetId]) {
101                    success = evaluateRule(condition.rules[i]);
102                } else {
103                    success = false;
104                }
105
106                if (any && success) {
107                    break;
108                }
109                if (all && !success) {
110                    break;
111                }
112            }
113            return success;
114        }
115
116        function evaluateConditionVisibility(id, condition) {
117            var show = condition.actionType === "Show",
118                cachedResult = cachedResults[id],
119                success = cachedResult === undefined
120                    ? (cachedResults[id] = evaluateCondition(id, condition))
121                    : cachedResult,
122                visible = !(success ^ show);
123            return visible;
124        }
125
126        function isVisible(id, condition) {
127            if (condition) {
128                return evaluateConditionVisibility(id, condition);
129            }
130            return true;
131        }
132
133        function handleCondition(element, id, condition, type) {
134            console.log(type + " " + id);
135            var shouldShow = isVisible(id, condition);
136            if (shouldShow) {
137                console.log("showing " + id + "\n");
138                element.show();
139            } else {
140                console.log("hiding " + id + "\n");
141                element.hide();
142            }
143        }
144
145        for (fsId in fsConditions) {
146            handleCondition($("#" + fsId), fsId, fsConditions[fsId], "Fieldset");
147        }
148
149        for (fieldId in fieldConditions) {
150            handleCondition($("#" + fieldId).closest(".contourField"), fieldId, fieldConditions[fieldId], "Field");
151        }
152    }
153
154}(umbracoForms));

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.