PageSourceSearch

https://standict.eu/modules/contrib/facets/js/checkbox-widget.js?tk2eed

js standict.eu collected 2026-09-24 21:26:37 UTC 4,358 bytes, 158 lines download raw bytes

1/**
2 * @file
3 * Transforms links into checkboxes.
4 */
5
6(function ($, Drupal, once) {
7  Drupal.facets = Drupal.facets || {};
8  Drupal.behaviors.facetsCheckboxWidget = {
9    attach: (context) => {
10      Drupal.facets.makeCheckboxes(context);
11    },
12  };
13
14  window.onbeforeunload = function (e) {
15    if (Drupal.facets) {
16      const $checkboxWidgets = $('.js-facets-checkbox-links');
17      if ($checkboxWidgets.length > 0) {
18        $checkboxWidgets.each(function (index, widget) {
19          const $widget = $(widget);
20          const $widgetLinks = $widget.find('.facet-item > a');
21          $widgetLinks.each(Drupal.facets.updateCheckbox);
22        });
23      }
24    }
25  };
26
27  /**
28   * Turns all facet links into checkboxes.
29   */
30  Drupal.facets.makeCheckboxes = function (context) {
31    // Find all checkbox facet links and give them a checkbox.
32    const $checkboxWidgets = $(
33      once('facets-checkbox-transform', '.js-facets-checkbox-links', context),
34    );
35
36    if ($checkboxWidgets.length > 0) {
37      $checkboxWidgets.each(function (index, widget) {
38        const $widget = $(widget);
39        const $widgetLinks = $widget.find('.facet-item > a');
40
41        // Add correct CSS selector for the widget. The Facets JS API will
42        // register handlers on that element.
43        $widget.addClass('js-facets-widget');
44
45        // Transform links to checkboxes.
46        $widgetLinks.each(Drupal.facets.makeCheckbox);
47
48        // We have to trigger attaching of behaviours, so that Facets JS API can
49        // register handlers on checkbox widgets.
50        Drupal.attachBehaviors(this.parentNode, Drupal.settings);
51      });
52    }
53
54    // Set indeterminate value on parents having an active trail.
55    $('.facet-item--expanded.facet-item--active-trail > input').prop(
56      'indeterminate',
57      true,
58    );
59  };
60
61  /**
62   * Replace a link with a checked checkbox.
63   */
64  Drupal.facets.makeCheckbox = function () {
65    const $link = $(this);
66    const active = $link.hasClass('is-active');
67    const description = $link.html();
68    const href = $link.attr('href');
69    const id = $link.data('drupal-facet-item-id');
70    const type = $link.data('drupal-facet-widget-element-class');
71
72    const checkbox = $(
73      '<input type="checkbox" class="form-checkbox form-check-input">',
74    )
75      .attr('id', id)
76      .attr(
77        'name',
78        `${$(this)
79          .closest('.js-facets-widget')
80          .data('drupal-facet-filter-key')}[]`,
81      )
82      .addClass(type)
83      .data($link.data())
84      .data('facetsredir', href);
85    checkbox[0].value = $link.data('drupal-facet-filter-value');
86
87    const singleSelectionGroup = $(this).data(
88      'drupal-facet-single-selection-group',
89    );
90    if (singleSelectionGroup) {
91      checkbox.addClass(singleSelectionGroup);
92    }
93
94    if (type === 'facets-link') {
95      checkbox.hide();
96    }
97
98    const label = $(
99      `<label for="${id}" class="form-check-label">${description}</label>`,
100    );
101
102    checkbox.on('change.facets', function (e) {
103      e.preventDefault();
104
105      const $widget = $(this).closest('.js-facets-widget');
106
107      Drupal.facets.disableFacet($widget);
108      $widget.trigger('facets_filter', [href]);
109    });
110
111    if (active) {
112      checkbox.prop('checked', true);
113      label.addClass('is-active');
114      label.find('.js-facet-deactivate').remove();
115    }
116
117    $link.before(checkbox).before(label).hide();
118  };
119
120  /**
121   * Update checkbox active state.
122   */
123  Drupal.facets.updateCheckbox = function () {
124    const $link = $(this);
125    const active = $link.hasClass('is-active');
126
127    if (!active) {
128      $link.parent().find('input.facets-checkbox').prop('checked', false);
129    }
130  };
131
132  /**
133   * Disable all facet checkboxes in the facet and apply a 'disabled' class.
134   *
135   * @param {object} $facet
136   *   jQuery object of the facet.
137   */
138  Drupal.facets.disableFacet = function ($facet) {
139    $facet.addClass('facets-disabled');
140    $('input.facets-checkbox, input.facets-link', $facet).click(
141      Drupal.facets.preventDefault,
142    );
143    $('input.facets-checkbox, input.facets-link', $facet).attr(
144      'disabled',
145      true,
146    );
147  };
148
149  /**
150   * Event listener for easy prevention of event propagation.
151   *
152   * @param {object} e
153   *   Event.
154   */
155  Drupal.facets.preventDefault = function (e) {
156    e.preventDefault();
157  };
158})(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.