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 var type = $link.data('drupal-facet-widget-element-class'); 70 71 var checkbox = $('<input type="checkbox" class="form-checkbox form-check-input">') 72 .attr('id', id) 73 .attr('name', $(this).closest('.js-facets-widget').data('drupal-facet-filter-key') + '[]') 74 .addClass(type) 75 .val($link.data('drupal-facet-filter-value')) 76 .data($link.data()) 77 .data('facetsredir', href); 78 79 var single_selection_group = $(this).data('drupal-facet-single-selection-group'); 80 if (single_selection_group) { 81 checkbox.addClass(single_selection_group); 82 } 83 84 if (type === 'facets-link') { 85 checkbox.hide(); 86 } 87 88 var label = $('<label for="' + id + '" class="form-check-label">' + description + '</label>'); 89 90 checkbox.on('change.facets', function (e) { 91 e.preventDefault(); 92 93 var $widget = $(this).closest('.js-facets-widget'); 94 95 Drupal.facets.disableFacet($widget); 96 $widget.trigger('facets_filter', [href]); 97 }); 98 99 if (active) { 100 checkbox.prop('checked', true); 101 label.addClass('is-active'); 102 label.find('.js-facet-deactivate').remove(); 103 } 104 105 $link.before(checkbox).before(label).hide(); 106 107 }; 108 109 /** 110 * Update checkbox active state. 111 */ 112 Drupal.facets.updateCheckbox = function () { 113 var $link = $(this); 114 var active = $link.hasClass('is-active'); 115 116 if (!active) { 117 $link.parent().find('input.facets-checkbox').prop('checked', false); 118 } 119 }; 120 121 /** 122 * Disable all facet checkboxes in the facet and apply a 'disabled' class. 123 * 124 * @param {object} $facet 125 * jQuery object of the facet. 126 */ 127 Drupal.facets.disableFacet = function ($facet) { 128 $facet.addClass('facets-disabled'); 129 $('input.facets-checkbox, input.facets-link', $facet).click(Drupal.facets.preventDefault); 130 $('input.facets-checkbox, input.facets-link', $facet).attr('disabled', true); 131 }; 132 133 /** 134 * Event listener for easy prevention of event propagation. 135 * 136 * @param {object} e 137 * Event. 138 */ 139 Drupal.facets.preventDefault = function (e) { 140 e.preventDefault(); 141 }; 142 143})(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.