1/** 2 * @file 3 * JavaScript behaviors for checkboxes. 4 */ 5 6(function ($, Drupal, once) { 7 8 'use strict'; 9 10 /** 11 * Adds check all or none checkboxes support. 12 * 13 * @type {Drupal~behavior} 14 * 15 * @see https://www.drupal.org/project/webform/issues/3068998 16 */ 17 Drupal.behaviors.webformCheckboxesAllorNone = { 18 attach: function (context) { 19 $(once('webform-checkboxes-all-or-none', '[data-options-all], [data-options-none]', context)) 20 .each(function () { 21 var $element = $(this); 22 23 var options_all_value = $element.data('options-all'); 24 var options_none_value = $element.data('options-none'); 25 26 // Get all checkboxes. 27 var $checkboxes = $element.find('input[type="checkbox"]'); 28 29 // Get all options/checkboxes. 30 var $options = $checkboxes 31 .not('[value="' + options_all_value + '"]') 32 .not('[value="' + options_none_value + '"]'); 33 34 // Get options all and none checkboxes. 35 var $options_all = $element 36 .find(':checkbox[value="' + options_all_value + '"]'); 37 var $options_none = $element 38 .find(':checkbox[value="' + options_none_value + '"]'); 39 40 // All of the above. 41 if ($options_all.length) { 42 $options_all.on('click', toggleCheckAllEventHandler); 43 if ($options_all.prop('checked')) { 44 toggleCheckAllEventHandler(); 45 } 46 } 47 48 // None of the above. 49 if ($options_none.length) { 50 $options_none.on('click', toggleCheckNoneEventHandler); 51 toggleCheckNoneEventHandler(); 52 } 53 54 $options.on('click', toggleCheckboxesEventHandler); 55 toggleCheckboxesEventHandler(); 56 57 /** 58 * Toggle check all checkbox checked state. 59 */ 60 function toggleCheckAllEventHandler() { 61 if ($options_all.prop('checked')) { 62 // Uncheck options none. 63 if ($options_none.is(':checked')) { 64 $options_none 65 .prop('checked', false) 66 .trigger('change', ['webform.states']); 67 } 68 // Check check all unchecked options. 69 $options.not(':checked') 70 .prop('checked', true) 71 .trigger('change', ['webform.states']); 72 } 73 else { 74 // Check uncheck all checked options. 75 $options.filter(':checked') 76 .prop('checked', false) 77 .trigger('change', ['webform.states']); 78 } 79 } 80 81 /** 82 * Toggle check none checkbox checked state. 83 */ 84 function toggleCheckNoneEventHandler() { 85 if ($options_none.prop('checked')) { 86 $checkboxes 87 .not('[value="' + options_none_value + '"]') 88 .filter(':checked') 89 .prop('checked', false) 90 .trigger('change', ['webform.states']); 91 } 92 } 93 94 /** 95 * Toggle check all checkbox checked state. 96 */ 97 function toggleCheckboxesEventHandler() { 98 var isAllChecked = ($options.filter(':checked').length === $options.length); 99 if ($options_all.length 100 && $options_all.prop('checked') !== isAllChecked) { 101 $options_all 102 .prop('checked', isAllChecked) 103 .trigger('change', ['webform.states']); 104 } 105 var isOneChecked = $options.is(':checked'); 106 if ($options_none.length 107 && isOneChecked) { 108 $options_none 109 .prop('checked', false) 110 .trigger('change', ['webform.states']); 111 } 112 } 113 }); 114 } 115 }; 116 117})(jQuery, Drupal, once);
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.