-
Notifications
You must be signed in to change notification settings - Fork 1
/
partial_packageoption_js.pdt
569 lines (492 loc) · 19.6 KB
/
partial_packageoption_js.pdt
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
<script>
var container_selector = '<?php echo ($container_selector ?? null);?>';
var triggers = <?php echo ($triggers ?? json_encode([]));?>;
var evaluations = <?php echo ($evaluations ?? json_encode([]));?>;
var defaults = <?php echo ($defaults ?? json_encode([]));?>;
var pre_logic_element_states = {};
$(document).ready(function () {
// Add events to trigger option display evaluations when one of the appropriate fields change
for (trigger_element in triggers) {
$(container_selector).on('change', trigger_element, {trigger_element: trigger_element}, evaluateOptions);
};
var tempFetchSummary = null;
if (typeof fetchSummary === "function") {
tempFetchSummary = fetchSummary;
fetchSummary = function(){};
}
// Trigger option evaluations
$('input,textarea,select', container_selector).change();
if (typeof tempFetchSummary === "function") {
fetchSummary = tempFetchSummary;
fetchSummary();
}
});
function evaluateOptions(e)
{
// Get triggering option
var trigger_element = e.data.trigger_element;
// Make a list of elements affected by the trigger element
var options_to_evaluate = [triggers[trigger_element]];
// Determine whether elements are disabled at the beginning of logic evaluations
for (option_id in evaluations) {
var selector = "*[name='configoptions[" + option_id + "]";
pre_logic_element_states[selector] = evaluateDisabledStatus(selector);
}
// Loop over the affected fields, evaluating the logic for each
var options_evaluated = 0;
for (var i = 0; i < options_to_evaluate.length; i++) {
for (option_id in options_to_evaluate[i]) {
var value_ids = options_to_evaluate[i][option_id];
var changed = false;
for (value_id in value_ids) {
changed = evaluateOption(option_id, value_id) || changed;
};
// If a change was made to the affected field, search for triggers associated with it,
// and add affected fields to the evaluation list
var option_selecter = "*[name='configoptions[" + option_id + "]']";
if (changed && (option_selecter in triggers)) {
options_to_evaluate[options_to_evaluate.length] = triggers[option_selecter];
}
// Continue looping over the list until all evaluations are resolved. It is likely possible to
// set up an infinite loop in this way, so perhaps we have a cap on evaluations
// and log or output an notice when the limit is reached
options_evaluated++;
if (options_evaluated > <?php echo ($maximum_evaluations ?? null);?>) {
break;
}
};
};
// This is a small concession to the Order plugin which triggers a summary fetch after all evaluations are complete
if (typeof fetchSummary === "function") {
fetchSummary();
}
// Reset the list of pre-logic element states (disabled or enabled)
pre_logic_element_states = {};
}
/**
* Evaluates the given option/value to see if it should be displayed or hidden
*/
function evaluateOption(option_id, value_id)
{
var changed = false;
var condition_sets = evaluations[option_id][value_id];
for (condition_set_id in condition_sets) {
var conditions = condition_sets[condition_set_id];
var target_element = conditions.target_element;
// Show fields by default and only hide them if they have display condition sets and fail them all
var show_field = true;
for (condition_id in conditions) {
if (condition_id === 'target_element') {
continue;
}
var evaluation = evaluateCondition(conditions[condition_id]);
// One condition in the set has failed, and thus the set evaluates as false
if (!evaluation) {
show_field = false;
break;
}
};
if (show_field) {
changed = enablePackageOptionField(target_element);
// One show condition set has been fulfilled, show field and bypass other checks
break;
} else {
changed = disablePackageOptionField(target_element, conditions[condition_id].hide_on_disable);
}
};
return changed;
}
/**
* Determines whether the field associated with the target element is disabled or enabled
*
* @param {String} target_element
*/
function evaluateDisabledStatus(target_element)
{
var disabled = true;
if ($(target_element).is('option')) {
disabled = $(target_element).parent().prop('disabled');
} else if ($(target_element).is('input[type=radio]')) {
disabled = $(target_element).prop('disabled');
$(target_element).siblings('input[type=radio]').each(function () {
if (!$(this).prop('disabled')) {
disabled = false;
return false;
}
});
} else {
if (!$(target_element).prop('disabled')) {
disabled = false;
}
}
return disabled ? 'disabled' : 'enabled';
}
/**
* Evaluates a condition
*
* @param {Object} condition
*/
function evaluateCondition(condition)
{
var evaluation = true;
var condition_value = condition.values[0];
// Convert numberic strings to integers for proper condition evaluations
if (typeof condition_value !== 'number' && $.isNumeric(condition_value)) {
condition_value = parseInt(condition_value);
}
// Convert numberic strings to integers for proper condition evaluations
var field_value = $(condition.trigger_selectors[0]).val();
if (typeof field_value !== 'number' && $.isNumeric(field_value)) {
field_value = parseInt(field_value);
}
if (condition.operator === 'notin') {
evaluation = evaluateNotIn(condition);
} else if (condition.operator === 'in') {
evaluation = evaluateIn(condition);
} else if (condition.operator === '!=') {
evaluation = evaluateNotEqual(condition, field_value);
} else if (condition.operator === '=') {
evaluation = evaluateEqual(condition, field_value);
} else if (condition.operator === '>') {
evaluation = field_value > condition_value;
} else if (condition.operator === '<') {
evaluation = field_value < condition_value;
}
return evaluation;
}
/**
* Show the given package option/value
*
* @param {Object} condition
*/
function evaluateIn(condition)
{
evaluation = false;
// Check each trigger to see if they are select or match a condition value
condition.trigger_selectors.every(function (trigger_selector) {
var trigger_field = $(trigger_selector);
if (trigger_field
&& trigger_field.prop('nodeName')
&& trigger_field.prop('nodeName').toLowerCase() === 'option'
) {
// Handle 'select' trigger fields
evaluation = trigger_field.prop('selected') === true;
} else if (trigger_field
&& (trigger_field.prop('type') === 'checkbox' || trigger_field.prop('type') === 'radio')
) {
// Handle 'checkbox' and 'radio' trigger fields
evaluation = trigger_field.prop('checked') === true;
} else {
// Handle 'text', 'textarea', and 'password' trigger fields
var field_value = trigger_field.val();
// Convert numberic strings to integers for proper condition evaluations
if (typeof field_value !== 'number' && $.isNumeric(field_value)) {
field_value = parseInt(field_value);
}
evaluation = field_value !== '' && condition.values.includes(field_value);
}
// Condition fullfilled, stop evaluations
if (evaluation) {
return false;
} else {
return true;
}
});
return evaluation;
}
/**
* Show the given package option/value
*
* @param {Object} condition
*/
function evaluateNotIn(condition)
{
evaluation = true;
// Check each trigger to see if they are select or match a condition value
condition.trigger_selectors.every(function (trigger_selector) {
var trigger_field = $(trigger_selector);
if (trigger_field
&& trigger_field.prop('nodeName')
&& trigger_field.prop('nodeName').toLowerCase() === 'option'
) {
// Handle 'select' trigger fields
evaluation = trigger_field.prop('selected') === false;
} else if (trigger_field
&& (trigger_field.prop('type') === 'checkbox' || trigger_field.prop('type') === 'radio')
) {
// Handle 'checkbox' and 'radio' trigger fields
evaluation = trigger_field.prop('checked') === false;
} else {
// Handle 'text', 'textarea', and 'password' trigger fields
var field_value = trigger_field.val();
// Convert numberic strings to integers for proper condition evaluations
if (typeof field_value !== 'number' && $.isNumeric(field_value)) {
field_value = parseInt(field_value);
}
evaluation = !condition.values.includes(field_value);
}
// Condition not fullfilled, stop evaluations
if (!evaluation) {
return false;
} else {
return true;
}
});
return evaluation;
}
/**
* Show the given package option/value
*
* @param {Object} condition
* @param {String} field_value
*/
function evaluateNotEqual(condition, field_value)
{
// Get the right trigger field
var trigger_field = $(condition.trigger_selectors[0]);
if (trigger_field
&& trigger_field.prop('nodeName')
&& trigger_field.prop('nodeName').toLowerCase() === 'option'
) {
// Handle 'select' trigger fields
return trigger_field.prop('selected') === false;
} else if (trigger_field
&& (trigger_field.prop('type') === 'checkbox' || trigger_field.prop('type') === 'radio')
) {
// Handle 'checkbox' and 'radio' trigger fields
return trigger_field.prop('checked') === false;
} else {
// Handle 'text', 'textarea', 'password', and 'quantity' trigger fields
return field_value !== condition.values[0];
}
}
/**
* Show the given package option/value
*
* @param {Object} condition
* @param {String} field_value
*/
function evaluateEqual(condition, field_value)
{
// Get the right trigger field
var trigger_field = $(condition.trigger_selectors[0]);
if (trigger_field
&& trigger_field.prop('nodeName')
&& trigger_field.prop('nodeName').toLowerCase() === 'option'
) {
// Handle 'select' trigger fields
return trigger_field.prop('selected') === true;
} else if (trigger_field
&& (trigger_field.prop('type') === 'checkbox' || trigger_field.prop('type') === 'radio')
) {
// Handle 'checkbox' and 'radio' trigger fields
return trigger_field.prop('checked') === true;
} else {
// Handle 'text', 'textarea', 'password', and 'quantity' trigger fields
return field_value === condition.values[0];
}
}
/**
* Show the given package option/value
*
* @param {String} target_element
*/
function enablePackageOptionField(target_element)
{
// Show the parent div
var parent_element = $(target_element).parents("div.form-group");
parent_element.show();
// Show this label for this element and enable it
$(target_element).parent('label').show();
$(target_element).prop("disabled", false);
// Show non-quantity fields
if ($('input[data-type=quantity]', parent_element).length === 0) {
$(target_element).show();
} else {
$(target_element).slider('enable');
}
// Set the default when a field is shown
var changed = false;
if (pre_logic_element_states[target_element.split(']')[0] + ']'] === 'disabled' && (target_element in defaults)) {
setDefault(target_element);
changed = true;
}
// Handle showing select fields
changed = showIfSelectField(target_element) || changed;
return changed;
}
/**
* Defaults the target element
*
* @param {String} target_element
*/
function setDefault(target_element)
{
if ($(target_element).is('option')) {
$(target_element).prop("selected", "selected");
} else if ($(target_element).is('input[type=radio]')) {
$(target_element).siblings('input[type=radio]').prop("checked", false);
$(target_element).prop("checked", "checked");
} else if ($(target_element).is('input[type=checkbox]')) {
$(target_element).prop("checked", defaults[target_element] === '1' ? "checked" : false);
} else if ($(target_element).is('input[data-type=quantity]')) {
$(target_element).val(defaults[target_element]);
$(target_element).slider('setValue', defaults[target_element]);
} else {
$(target_element).val(defaults[target_element]);
}
}
/**
* Handle show action for select fields
*
* @param {String} target_element
*/
function showIfSelectField(target_element)
{
var changed = false;
// Show and enable the parent select field if this element is an option
if ($(target_element).parent("select").length > 0) {
$(target_element).parent("select").show();
$(target_element).parent("select").prop("disabled", false);
// Show the select parent li
$(target_element).parent("select").parents("div.form-group").show();
// Select the current option if there are none currently selected
var select_option = true;
$(target_element).parent("select").find("option").each(function () {
if ($(this).prop("selected") && !$(this).prop("disabled")) {
select_option = false;
}
});
if (select_option && defaults.hasOwnProperty(target_element)) {
$(target_element).prop("selected", "selected");
changed = true;
}
}
return changed;
}
/**
* Hide the given package option/value
*
* @param {String} target_element
*/
function disablePackageOptionField(target_element, hide)
{
var changed = false;
$(target_element).prop("disabled", "disabled");
var parent_element = $(target_element).parents("div.form-group");
if (hide) {
if ($(target_element).parent("select").length === 0
&& $('input[type=radio]', parent_element).length === 0
) {
parent_element.hide();
} else if ($('input[data-type=quantity]', parent_element).length === 0) {
$(target_element).hide();
}
}
if ($('input[data-type=quantity]', parent_element).length > 0) {
$(target_element).slider('disable');
}
changed = disableIfCheckboxField(target_element);
changed = disableIfSelectField(target_element, hide) || changed;
changed = disableIfRadioField(target_element, hide) || changed;
changed = disableIfTextField(target_element) || changed;
return changed;
}
/**
* Handle disable action for checkbox fields
*
* @param {String} target_element
*/
function disableIfCheckboxField(target_element)
{
var changed = false;
var parent_element = $(target_element).parents("div.form-group");
// Handle checkbox elements
if ($('input[type=checkbox]', parent_element).length > 0) {
$(target_element).prop("checked", false);
changed = true;
}
return changed;
}
/**
* Handle disable action for select fields
*
* @param {String} target_element
*/
function disableIfSelectField(target_element, hide)
{
var changed = false;
// Handle select elements
if ($(target_element).parent("select").length > 0) {
var disable_select = true;
$(target_element).parent("select").find("option").each(function () {
if (!$(this).prop("disabled")) {
disable_select = false;
}
});
if (disable_select) {
// All options are disabled, disable the parent select
$(target_element).parent("select").prop("disabled", false);
if (hide) {
// Hide if set to do so
$(target_element).parent("select").hide();
// Hide the select parent li
$(target_element).parent("select").parents("div.form-group").hide();
}
}
// Unselect this option
$(target_element).prop("selected", false);
// Select the first available option
$(target_element).parent("select").find("option").each(function () {
if (!$(this).prop("disabled")) {
$(this).prop("selected", "selected");
changed = true;
return false;
}
});
}
return changed;
}
/**
* Handle disable action for radio fields
*
* @param {String} target_element
*/
function disableIfRadioField(target_element, hide)
{
var changed = false;
var parent_element = $(target_element).parents("div.form-group");
var hide_parent = true;
// Handle radio elements
if ($('input[type=radio]', parent_element).length > 0) {
$(target_element).prop("checked", false);
changed = true;
if (hide) {
$(target_element).parent('label').hide();
$('input[type=radio]', parent_element).each(function () {
if ($(this).prop("disabled") !== true) {
hide_parent = false;
}
});
if (hide_parent) {
parent_element.hide();
}
}
}
return changed;
}
/**
* Handle disable action for text fields
*
* @param {String} target_element
*/
function disableIfTextField(target_element)
{
var changed = false;
if ($(target_element).is('input[type=text],textarea')) {
$(target_element).val('');
changed = true;
}
return changed;
}
</script>