/** * @file * Transforms links into a dropdown list. */ (function ($) { 'use strict'; Drupal.facets = Drupal.facets || {}; Drupal.behaviors.facetsDropdownWidget = { attach: function (context, settings) { Drupal.facets.makeDropdown(context, settings); } }; /** * Turns all facet links into a dropdown with options for every link. * * @param {object} context * Context. * @param {object} settings * Settings. */ Drupal.facets.makeDropdown = function (context, settings) { // Find all dropdown facet links and turn them into an option. $('.js-facets-dropdown-links').once('facets-dropdown-transform').each(function () { var $ul = $(this); var $links = $ul.find('.facet-item a'); var $dropdown = $(''); // Preserve all attributes of the list. $ul.each(function () { $.each(this.attributes,function (idx, elem) { $dropdown.attr(elem.name, elem.value); }); }); // Remove the class which we are using for .once(). $dropdown.removeClass('js-facets-dropdown-links'); $dropdown.addClass('facets-dropdown'); $dropdown.addClass('js-facets-widget'); $dropdown.addClass('js-facets-dropdown'); var id = $(this).data('drupal-facet-id'); // Add aria-labelledby attribute to reference label. $dropdown.attr('aria-labelledby', "facet_" + id + "_label"); var default_option_label = settings.facets.dropdown_widget[id]['facet-default-option-label']; // Add empty text option first. var $default_option = $('') .attr('value', '') .text(default_option_label); $dropdown.append($default_option); $ul.prepend('