Newer
Older
Joris Vercammen
committed
/**
* @file
* Transforms links into a dropdown list.
*/
Joris Vercammen
committed
(function ($, Drupal, once) {
Joris Vercammen
committed
'use strict';
Jimmy Henderickx
committed
Drupal.facets = Drupal.facets || {};
Joris Vercammen
committed
Drupal.behaviors.facetsDropdownWidget = {
attach: function (context, settings) {
Dragos Dumitrescu
committed
Drupal.facets.makeDropdown(context, settings);
Joris Vercammen
committed
}
};
/**
* Turns all facet links into a dropdown with options for every link.
*
* @param {object} context
* Context.
* @param {object} settings
* Settings.
Joris Vercammen
committed
*/
Dragos Dumitrescu
committed
Drupal.facets.makeDropdown = function (context, settings) {
Joris Vercammen
committed
// Find all dropdown facet links and turn them into an option.
Markus Kalkbrenner
committed
$(once('facets-dropdown-transform', '.js-facets-dropdown-links')).each(function () {
Brandon Williams
committed
var $ul = $(this);
var $links = $ul.find('.facet-item a');
var $dropdown = $('<select></select>');
// 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');
Joris Vercammen
committed
$dropdown.addClass('js-facets-dropdown');
Joris Vercammen
committed
Dragos Dumitrescu
committed
var id = $(this).data('drupal-facet-id');
Mike Madison
committed
// Add aria-labelledby attribute to reference label.
$dropdown.attr('aria-labelledby', "facet_" + id + "_label");
Dragos Dumitrescu
committed
var default_option_label = settings.facets.dropdown_widget[id]['facet-default-option-label'];
Mike Madison
committed
Joris Vercammen
committed
// Add empty text option first.
var $default_option = $('<option></option>')
Joris Vercammen
committed
.attr('value', '')
.text(default_option_label);
$dropdown.append($default_option);
Joris Vercammen
committed
Ide Braakman
committed
$ul.prepend('<li class="default-option"><a href="' + window.location.href.split('?')[0] + '">' + Drupal.checkPlain(default_option_label) + '</a></li>');
Nilesh Lohar
committed
Joris Vercammen
committed
var has_active = false;
$links.each(function () {
var $link = $(this);
var active = $link.hasClass('is-active');
var $option = $('<option></option>')
Brandon Williams
committed
.attr('value', $link.attr('href'))
.data($link.data());
Joris Vercammen
committed
if (active) {
has_active = true;
// Set empty text value to this link to unselect facet.
$default_option.attr('value', $link.attr('href'));
Nilesh Lohar
committed
$ul.find('.default-option a').attr("href", $link.attr('href'));
Joris Vercammen
committed
$option.attr('selected', 'selected');
$link.find('.js-facet-deactivate').remove();
Joris Vercammen
committed
}
Blake Thompson
committed
// Add hierarchy indicator in case hierarchy is enabled.
var $parents = $link.parent('li.facet-item').parents('li.facet-item');
var prefix = '';
for (var i = 0; i < $parents.length; i++) {
prefix += '-';
}
return prefix + ' ' + $link.text().trim();
});
Joris Vercammen
committed
$dropdown.append($option);
});
// Go to the selected option when it's clicked.
Brandon Williams
committed
$dropdown.on('change.facets', function () {
Nilesh Lohar
committed
var anchor = $($ul).find("[data-drupal-facet-item-id='" + $(this).find(':selected').data('drupalFacetItemId') + "']");
var $linkElement = (anchor.length > 0) ? $(anchor) : $ul.find('.default-option a');
var url = $linkElement.attr('href');
$(this).trigger('facets_filter', [ url ]);
Joris Vercammen
committed
});
// Append empty text option.
if (!has_active) {
$default_option.attr('selected', 'selected');
}
Brandon Williams
committed
// Replace links with dropdown.
Joris Vercammen
committed
$ul.after($dropdown).hide();
Alex Tkachev
committed
Drupal.attachBehaviors($dropdown.parent()[0], Drupal.settings);
Joris Vercammen
committed
});
};
Joris Vercammen
committed
})(jQuery, Drupal, once);