PageSourceSearch

https://primaryconnections.org.au/modules/contrib/webform/js/webform.element.other.js?tm4ukb

js primaryconnections.org.au collected 2026-09-29 16:35:17 UTC 5,100 bytes, 162 lines download raw bytes

1/**
2 * @file
3 * JavaScript behaviors for other elements.
4 */
5
6(function ($, Drupal, once) {
7  /**
8   * Toggle other input (text) field.
9   *
10   * @param {boolean} show
11   *   TRUE will display the text field. FALSE with hide and clear the text field.
12   * @param {object} $element
13   *   The input (text) field to be toggled.
14   * @param {string} effect
15   *   Effect.
16   */
17  function toggleOther(show, $element, effect) {
18    var $input = $element.find('input');
19    var hideEffect = (effect === false) ? 'hide' : 'slideUp';
20    var showEffect = (effect === false) ? 'show' : 'slideDown';
21
22    if (show) {
23      // Limit the other inputs width to the parent's container.
24      // If the parent container is not visible it's width will be 0
25      // and ignored.
26      var width = $element.parent().width();
27      if (width) {
28        $element.width(width);
29      }
30
31      // Display the element.
32      $element[showEffect]();
33      // If not initializing, then focus the other element.
34      if (effect !== false) {
35        $input.trigger('focus');
36      }
37      // Require the input.
38      $input.prop('required', true).attr('aria-required', 'true');
39      // Restore the input's value.
40      var value = $input.data('webform-value');
41      if (typeof value !== 'undefined') {
42        $input.val(value);
43        var input = $input.get(0);
44        // Move cursor to the beginning of the other text input.
45        // @see https://stackoverflow.com/questions/21177489/selectionstart-selectionend-on-input-type-number-no-longer-allowed-in-chrome
46        if ($.inArray(input.type, ['text', 'search', 'url', 'tel', 'password']) !== -1) {
47          input.setSelectionRange(0, 0);
48        }
49      }
50      // Refresh CodeMirror used as other element.
51      $element.parent().find('.CodeMirror').each(function (index, $element) {
52        $element.CodeMirror.refresh();
53      });
54    }
55    else {
56      // Hide the element.
57      $element[hideEffect]();
58      // Save the input's value.
59      if ($input.val() !== '') {
60        $input.data('webform-value', $input.val());
61      }
62      // Empty and un-required the input.
63      $input.val('').prop('required', false).removeAttr('aria-required');
64    }
65  }
66
67  /**
68   * Attach handlers to select other elements.
69   *
70   * @type {Drupal~behavior}
71   */
72  Drupal.behaviors.webformSelectOther = {
73    attach(context) {
74      $(once('webform-select-other', '.js-webform-select-other', context)).each(function () {
75        var $element = $(this);
76
77        var $select = $element.find('select');
78        var $input = $element.find('.js-webform-select-other-input');
79
80        $select.on('change', function () {
81          var isOtherSelected = $select
82            .find('option[value="_other_"]')
83            .is(':selected');
84          toggleOther(isOtherSelected, $input);
85        });
86
87        var isOtherSelected = $select
88          .find('option[value="_other_"]')
89          .is(':selected');
90        toggleOther(isOtherSelected, $input, false);
91      });
92    }
93  };
94
95  /**
96   * Attach handlers to checkboxes other elements.
97   *
98   * @type {Drupal~behavior}
99   */
100  Drupal.behaviors.webformCheckboxesOther = {
101    attach(context) {
102      $(once('webform-checkboxes-other', '.js-webform-checkboxes-other', context)).each(function () {
103        var $element = $(this);
104        var $checkbox = $element.find('input[value="_other_"]');
105        var $input = $element.find('.js-webform-checkboxes-other-input');
106
107        $checkbox.on('change', function () {
108          toggleOther(this.checked, $input);
109        });
110
111        toggleOther($checkbox.is(':checked'), $input, false);
112      });
113    }
114  };
115
116  /**
117   * Attach handlers to radios other elements.
118   *
119   * @type {Drupal~behavior}
120   */
121  Drupal.behaviors.webformRadiosOther = {
122    attach(context) {
123      $(once('webform-radios-other', '.js-webform-radios-other', context)).each(function () {
124        var $element = $(this);
125
126        var $radios = $element.find('input[type="radio"]');
127        var $input = $element.find('.js-webform-radios-other-input');
128
129        $radios.on('change', function () {
130          toggleOther(($radios.filter(':checked').val() === '_other_'), $input);
131        });
132
133        toggleOther(($radios.filter(':checked').val() === '_other_'), $input, false);
134      });
135    }
136  };
137
138  /**
139   * Attach handlers to buttons other elements.
140   *
141   * @type {Drupal~behavior}
142   */
143  Drupal.behaviors.webformButtonsOther = {
144    attach(context) {
145      $(once('webform-buttons-other', '.js-webform-buttons-other', context)).each(function () {
146        var $element = $(this);
147
148        var $buttons = $element.find('input[type="radio"]');
149        var $input = $element.find('.js-webform-buttons-other-input');
150        var $container = $(this).find('.js-webform-webform-buttons');
151
152        // Create set onchange handler.
153        $container.on('change', function () {
154          toggleOther(($(this).find(':radio:checked').val() === '_other_'), $input);
155        });
156
157        toggleOther(($buttons.filter(':checked').val() === '_other_'), $input, false);
158      });
159    }
160  };
161
162})(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.