Update soft-limit.js

This commit is contained in:
Kyle Huynh 2023-02-28 10:25:53 -05:00 committed by GitHub
parent 812ca8a37d
commit f1a680e5ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,70 +1,74 @@
//# sourceURL=modules/contrib/islandora/modules/advanced_search/js/facets/soft-limit.js
/** /**
* @file * @file
* Overrides the soft-limit.js behavior from the 'facets' module. * Provides the soft limit functionality.
* As when having many facets the original version causes the page to slow down and snap to hidden when rendering.
*/ */
(function ($) { (function ($) {
'use strict'; 'use strict';
Drupal.behaviors.facetSoftLimit = { Drupal.behaviors.facetSoftLimit = {
attach: function (context, settings) { attach: function (context, settings) {
if (settings.facets.softLimit !== 'undefined') { if (settings.facets.softLimit !== 'undefined') {
$.each(settings.facets.softLimit, function (facet, limit) { $.each(settings.facets.softLimit, function (facet, limit) {
Drupal.facets.applySoftLimit(facet, limit, settings); Drupal.facets.applySoftLimit(facet, limit, settings);
});
}
}
};
Drupal.facets = Drupal.facets || {};
/**
* Applies the soft limit UI feature to a specific facets list.
*
* @param {string} facet
* The facet id.
* @param {string} limit
* The maximum amount of items to show.
* @param {object} settings
* Settings.
*/
Drupal.facets.applySoftLimit = function (facet, limit, settings) {
var zero_based_limit = (limit - 1);
var facet_id = facet;
var facetsList = $('ul[data-drupal-facet-id="' + facet_id + '"]');
// In case of multiple instances of a facet, we need to key them.
if (facetsList.length > 1) {
facetsList.each(function (key, $value) {
$(this).attr('data-drupal-facet-id', facet_id + '-' + key);
});
}
// Hide facets over the limit.
facetsList.each(function () {
$(this).children('li:gt(' + zero_based_limit + ')').once('applysoftlimit').hide();
}); });
}
}
};
Drupal.facets = Drupal.facets || {};
/** // Add "Show more" / "Show less" links.
* Applies the soft limit UI feature to a specific facets list. facetsList.filter(function () {
* return ($(this).find('li').length > limit && $(this).parent().find('a.facets-soft-limit-link').length < 1);
* @param {string} facet }).each(function () {
* The facet id. var facet = $(this);
* @param {string} limit var showLessLabel = settings.facets.softLimitSettings[facet_id].showLessLabel;
* The maximum amount of items to show. var showMoreLabel = settings.facets.softLimitSettings[facet_id].showMoreLabel;
* @param {object} settings $('<a href="#" class="facets-soft-limit-link"></a>')
* Settings. .text(showMoreLabel).attr("aria-expanded", "false")
*/ .on('click', function () {
Drupal.facets.applySoftLimit = function (facet, limit, settings) { if (facet.find('li:hidden').length > 0) {
var zero_based_limit = (limit - 1); facet.find('li:gt(' + zero_based_limit + ')').slideDown();
var facet_id = facet; facet.find('li:lt(' + (zero_based_limit + 2) + ') input').focus();
var facetsList = $('ul[data-drupal-facet-id="' + facet_id + '"]'); $(this).addClass('open').text(showLessLabel).attr("aria-expanded", "true");
}
// In case of multiple instances of a facet, we need to key them. else {
if (facetsList.length > 1) { facet.find('li:gt(' + zero_based_limit + ')').slideUp();
facetsList.each(function (key, $value) { $(this).removeClass('open').text(showMoreLabel).attr("aria-expanded", "false");
$(this).attr('data-drupal-facet-id', facet_id + '-' + key); }
}); return false;
} }).insertAfter($(this));
});
// Add "Show more" / "Show less" links. };
facetsList.filter(function () {
return $(this).next('ul').length == 1; // Has expanding list.
}).each(function () {
var facet = $(this);
var expand = facet.next('ul');
var link = expand.next('a');
var showLessLabel = settings.facets.softLimitSettings[facet_id].showLessLabel;
var showMoreLabel = settings.facets.softLimitSettings[facet_id].showMoreLabel;
link.text(showMoreLabel)
.once()
.on('click', function () {
if (!expand.is(":visible")) {
expand.slideDown();
$(this).addClass('open').text(showLessLabel);
}
else {
expand.slideUp();
$(this).removeClass('open').text(showMoreLabel);
}
return false;
})
});
};
})(jQuery); })(jQuery);