PageSourceSearch

https://www.historytoday.com/modules/contrib/facets/js/checkbox-widget.js?v=10.4.7

js historytoday.com collected 2026-10-01 09:16:39 UTC 3,613 bytes, 128 lines download raw bytes

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