1(function ($) { 2 'use strict'; 3 4 Drupal.behaviors.facetapi = { 5 attach: function(context, settings) { 6 // Iterates over facet settings, applies functionality like the "Show more" 7 // links for block realm facets. 8 // @todo We need some sort of JS API so we don't have to make decisions 9 // based on the realm. 10 if (settings.facetapi) { 11 for (var index in settings.facetapi.facets) { 12 if (null != settings.facetapi.facets[index].makeCheckboxes) { 13 Drupal.facetapi.makeCheckboxes(settings.facetapi.facets[index].id); 14 } 15 if (null != settings.facetapi.facets[index].limit) { 16 // Applies soft limit to the list. 17 Drupal.facetapi.applyLimit(settings.facetapi.facets[index]); 18 } 19 } 20 } 21 } 22 }; 23 24 /** 25 * Class containing functionality for Facet API. 26 */ 27 Drupal.facetapi = {}; 28 29 /** 30 * Applies the soft limit to facets in the block realm. 31 */ 32 Drupal.facetapi.applyLimit = function(settings) { 33 let ul = $('ul#' + settings.id); 34 if (settings.limit > 0 && !ul.hasClass('facetapi-processed')) { 35 // Only process this code once per page load. 36 ul.addClass('facetapi-processed'); 37 38 // Ensures our limit is zero-based, hides facets over the limit. 39 let limit = settings.limit - 1; 40 ul.find('li:gt(' + limit + ')').hide(); 41 42 // Adds "Show more" / "Show fewer" links as appropriate. 43 ul.filter(function() { 44 return $(this).find('li').length > settings.limit; 45 }).each(function() { 46 $('<a href="#" class="facetapi-limit-link"></a>').text(settings.showMoreText).click(function() { 47 if ($(this).hasClass('open')) { 48 $(this).siblings().find('li:gt(' + limit + ')').slideUp(); 49 $(this).removeClass('open').text(settings.showMoreText); 50 } 51 else { 52 $(this).siblings().find('li:gt(' + limit + ')').slideDown(); 53 $(this).addClass('open').text(Drupal.t(settings.showFewerText)); 54 } 55 return false; 56 }).insertAfter($(this)); 57 }); 58 } 59 }; 60 61 /** 62 * Constructor for the facetapi redirect class. 63 */ 64 Drupal.facetapi.Redirect = function(href) { 65 this.href = href; 66 }; 67 68 /** 69 * Method to redirect to the stored href. 70 */ 71 Drupal.facetapi.Redirect.prototype.gotoHref = function() { 72 window.location.href = this.href; 73 }; 74 75 /** 76 * Turns all facet links into checkboxes. 77 * Ensures the facet is disabled if a link is clicked. 78 */ 79 Drupal.facetapi.makeCheckboxes = function(facet_id) { 80 let $facet = $('#' + facet_id), 81 $items = $('a.facetapi-checkbox, span.facetapi-checkbox', $facet); 82 83 // Find all checkbox facet links and give them a checkbox. 84 $items.once('facetapi-makeCheckbox').each(Drupal.facetapi.makeCheckbox); 85 $items.once('facetapi-disableClick').click(function (e) { 86 Drupal.facetapi.disableFacet($facet); 87 }); 88 }; 89 90 /** 91 * Disable all facet links and checkboxes in the facet and apply a 'disabled' 92 * class. 93 */ 94 Drupal.facetapi.disableFacet = function ($facet) { 95 let $elem = $(this); 96 // Apply only for links. 97 if ($elem[0].tagName === 'A') { 98 $facet.addClass('facetapi-disabled'); 99 $('a.facetapi-checkbox').click(Drupal.facetapi.preventDefault); 100 $('input.facetapi-checkbox', $facet).attr('disabled', true); 101 } 102 }; 103 104 /** 105 * Event listener for easy prevention of event propagation. 106 */ 107 Drupal.facetapi.preventDefault = function (e) { 108 e.preventDefault(); 109 }; 110 111 /** 112 * Replace an unclick link with a checked checkbox. 113 */ 114 Drupal.facetapi.makeCheckbox = function() { 115 let $elem = $(this), 116 active = $elem.hasClass('facetapi-active'); 117 118 if (!active && !$elem.hasClass('facetapi-inactive')) { 119 // Not a facet element. 120 return; 121 } 122 123 // Derive an ID and label for the checkbox based on the associated link. 124 // The label is required for accessibility, but it duplicates information 125 // in the link itself, so it should only be shown to screen reader users. 126 let id = this.id + '--checkbox', 127 description = $elem.find('.element-invisible').html(), 128 label = $('<label class="element-invisible" for="' + id + '">' + description + '</label>'); 129 130 // Link for elements with count 0. 131 if ($elem[0].tagName === 'A') { 132 var checkbox = $('<input type="checkbox" class="facetapi-checkbox" id="' + id + '" />'), 133 // Get the href of the link that is this DOM object. 134 href = $elem.attr('href'), 135 redirect = new Drupal.facetapi.Redirect(href); 136 } 137 // Link for elements with count more than 0. 138 else { 139 checkbox = $('<input disabled type="checkbox" class="facetapi-checkbox" id="' + id + '" />'); 140 } 141 142 checkbox.click(function (e) { 143 Drupal.facetapi.disableFacet($elem.parents('ul.facetapi-facetapi-checkbox-links')); 144 redirect.gotoHref(); 145 }); 146 147 if (active) { 148 checkbox.prop('checked', true); 149 // Add the checkbox and label, hide the link. 150 $elem.before(label).before(checkbox).hide(); 151 } 152 else { 153 $elem.before(label).before(checkbox); 154 } 155 }; 156 157})(jQuery);
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.