diff --git a/js/facets/soft-limit.js b/js/facets/soft-limit.js index 9f1191b..69c455d 100644 --- a/js/facets/soft-limit.js +++ b/js/facets/soft-limit.js @@ -1,70 +1,74 @@ -//# sourceURL=modules/contrib/islandora/modules/advanced_search/js/facets/soft-limit.js /** * @file - * Overrides the soft-limit.js behavior from the 'facets' module. - * As when having many facets the original version causes the page to slow down and snap to hidden when rendering. + * Provides the soft limit functionality. */ + (function ($) { - 'use strict'; + 'use strict'; - Drupal.behaviors.facetSoftLimit = { - attach: function (context, settings) { - if (settings.facets.softLimit !== 'undefined') { - $.each(settings.facets.softLimit, function (facet, limit) { - Drupal.facets.applySoftLimit(facet, limit, settings); + Drupal.behaviors.facetSoftLimit = { + attach: function (context, settings) { + if (settings.facets.softLimit !== 'undefined') { + $.each(settings.facets.softLimit, function (facet, limit) { + 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 || {}; - /** - * 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); - }); - } - - // 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; - }) - }); - }; + // Add "Show more" / "Show less" links. + facetsList.filter(function () { + return ($(this).find('li').length > limit && $(this).parent().find('a.facets-soft-limit-link').length < 1); + }).each(function () { + var facet = $(this); + var showLessLabel = settings.facets.softLimitSettings[facet_id].showLessLabel; + var showMoreLabel = settings.facets.softLimitSettings[facet_id].showMoreLabel; + $('') + .text(showMoreLabel).attr("aria-expanded", "false") + .on('click', function () { + if (facet.find('li:hidden').length > 0) { + facet.find('li:gt(' + zero_based_limit + ')').slideDown(); + facet.find('li:lt(' + (zero_based_limit + 2) + ') input').focus(); + $(this).addClass('open').text(showLessLabel).attr("aria-expanded", "true"); + } + else { + facet.find('li:gt(' + zero_based_limit + ')').slideUp(); + $(this).removeClass('open').text(showMoreLabel).attr("aria-expanded", "false"); + } + return false; + }).insertAfter($(this)); + }); + }; })(jQuery);