1/** 2 * @file 3 * Transforms links into a dropdown list. 4 */ 5 6(function ($, Drupal, once) { 7 8 'use strict'; 9 10 Drupal.facets = Drupal.facets || {}; 11 Drupal.behaviors.facetsDropdownWidget = { 12 attach: function (context, settings) { 13 Drupal.facets.makeDropdown(context, settings); 14 } 15 }; 16 17 /** 18 * Turns all facet links into a dropdown with options for every link. 19 * 20 * @param {object} context 21 * Context. 22 * @param {object} settings 23 * Settings. 24 */ 25 Drupal.facets.makeDropdown = function (context, settings) { 26 // Find all dropdown facet links and turn them into an option. 27 $(once('facets-dropdown-transform', '.js-facets-dropdown-links')).each(function () { 28 var $ul = $(this); 29 var $links = $ul.find('.facet-item a'); 30 var $dropdown = $('<select></select>'); 31 // Preserve all attributes of the list. 32 $ul.each(function () { 33 $.each(this.attributes,function (idx, elem) { 34 $dropdown.attr(elem.name, elem.value); 35 }); 36 }); 37 // Remove the class which we are using for .once(). 38 $dropdown.removeClass('js-facets-dropdown-links'); 39 40 $dropdown.addClass('facets-dropdown'); 41 $dropdown.addClass('js-facets-widget'); 42 $dropdown.addClass('js-facets-dropdown'); 43 44 var id = $(this).data('drupal-facet-id'); 45 // Add aria-labelledby attribute to reference label. 46 $dropdown.attr('aria-labelledby', "facet_" + id + "_label"); 47 var default_option_label = settings.facets.dropdown_widget[id]['facet-default-option-label']; 48 49 // Add empty text option first. 50 var $default_option = $('<option></option>') 51 .attr('value', '') 52 .text(default_option_label); 53 $dropdown.append($default_option); 54 55 $ul.prepend('<li class="default-option"><a href="' + window.location.href.split('?')[0] + '">' + Drupal.checkPlain(default_option_label) + '</a></li>'); 56 57 var has_active = false; 58 $links.each(function () { 59 var $link = $(this); 60 var active = $link.hasClass('is-active'); 61 var $option = $('<option></option>') 62 .attr('value', $link.attr('href')) 63 .data($link.data()); 64 if (active) { 65 has_active = true; 66 // Set empty text value to this link to unselect facet. 67 $default_option.attr('value', $link.attr('href')); 68 $ul.find('.default-option a').attr("href", $link.attr('href')); 69 $option.attr('selected', 'selected'); 70 $link.find('.js-facet-deactivate').remove(); 71 } 72 $option.text(function () { 73 // Add hierarchy indicator in case hierarchy is enabled. 74 var $parents = $link.parent('li.facet-item').parents('li.facet-item'); 75 var prefix = ''; 76 for (var i = 0; i < $parents.length; i++) { 77 prefix += '-'; 78 } 79 return prefix + ' ' + $link.text().trim(); 80 }); 81 $dropdown.append($option); 82 }); 83 84 // Go to the selected option when it's clicked. 85 $dropdown.on('change.facets', function () { 86 var anchor = $($ul).find("[data-drupal-facet-item-id='" + $(this).find(':selected').data('drupalFacetItemId') + "']"); 87 var $linkElement = (anchor.length > 0) ? $(anchor) : $ul.find('.default-option a'); 88 var url = $linkElement.attr('href'); 89 90 $(this).trigger('facets_filter', [ url ]); 91 }); 92 93 // Append empty text option. 94 if (!has_active) { 95 $default_option.attr('selected', 'selected'); 96 } 97 98 // Replace links with dropdown. 99 $ul.after($dropdown).hide(); 100 Drupal.attachBehaviors($dropdown.parent()[0], Drupal.settings); 101 }); 102 }; 103 104})(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.