/profiles/acquia/modules/facetapi/facetapi.js

https://bitbucket.org/agiza/farming-drupal · JavaScript · 107 lines · 71 code · 9 blank · 27 comment · 17 complexity · f0e6f63e1c6c64b668a403ba22e3a30e MD5 · raw file

  1. (function ($) {
  2. Drupal.behaviors.facetapi = {
  3. attach: function(context, settings) {
  4. // Iterates over facet settings, applies functionality like the "Show more"
  5. // links for block realm facets.
  6. // @todo We need some sort of JS API so we don't have to make decisions
  7. // based on the realm.
  8. if (settings.facetapi) {
  9. for (var index in settings.facetapi.facets) {
  10. if (null != settings.facetapi.facets[index].makeCheckboxes) {
  11. // Find all checkbox facet links and give them a checkbox.
  12. $('#' + settings.facetapi.facets[index].id + ' a.facetapi-checkbox', context).each(Drupal.facetapi.makeCheckbox);
  13. }
  14. if (null != settings.facetapi.facets[index].limit) {
  15. // Applies soft limit to the list.
  16. Drupal.facetapi.applyLimit(settings.facetapi.facets[index]);
  17. }
  18. }
  19. }
  20. }
  21. }
  22. /**
  23. * Class containing functionality for Facet API.
  24. */
  25. Drupal.facetapi = {}
  26. /**
  27. * Applies the soft limit to facets in the block realm.
  28. */
  29. Drupal.facetapi.applyLimit = function(settings) {
  30. if (settings.limit > 0 && !$('ul#' + settings.id).hasClass('facetapi-processed')) {
  31. // Only process this code once per page load.
  32. $('ul#' + settings.id).addClass('facetapi-processed');
  33. // Ensures our limit is zero-based, hides facets over the limit.
  34. var limit = settings.limit - 1;
  35. $('ul#' + settings.id).find('li:gt(' + limit + ')').hide();
  36. // Adds "Show more" / "Show fewer" links as appropriate.
  37. $('ul#' + settings.id).filter(function() {
  38. return $(this).find('li').length > settings.limit;
  39. }).each(function() {
  40. $('<a href="#" class="facetapi-limit-link"></a>').text(Drupal.t('Show more')).click(function() {
  41. if ($(this).prev().find('li:hidden').length > 0) {
  42. $(this).prev().find('li:gt(' + limit + ')').slideDown();
  43. $(this).addClass('open').text(Drupal.t('Show fewer'));
  44. }
  45. else {
  46. $(this).prev().find('li:gt(' + limit + ')').slideUp();
  47. $(this).removeClass('open').text(Drupal.t('Show more'));
  48. }
  49. return false;
  50. }).insertAfter($(this));
  51. });
  52. }
  53. }
  54. /**
  55. * Constructor for the facetapi redirect class.
  56. */
  57. Drupal.facetapi.Redirect = function(href) {
  58. this.href = href;
  59. }
  60. /**
  61. * Method to redirect to the stored href.
  62. */
  63. Drupal.facetapi.Redirect.prototype.gotoHref = function() {
  64. window.location.href = this.href;
  65. }
  66. /**
  67. * Replace an unclick link with a checked checkbox.
  68. */
  69. Drupal.facetapi.makeCheckbox = function() {
  70. var $link = $(this);
  71. if (!$link.hasClass('facetapi-checkbox-processed')) {
  72. var active;
  73. if ($link.hasClass('facetapi-inactive')) {
  74. active = false;
  75. }
  76. else if ($link.hasClass('facetapi-active')) {
  77. active = true;
  78. }
  79. else {
  80. // Not a facet link.
  81. return;
  82. }
  83. var checkbox = active ? $('<input type="checkbox" class="facetapi-checkbox" checked="true" />') : $('<input type="checkbox" class="facetapi-checkbox" />');
  84. // Get the href of the link that is this DOM object.
  85. var href = $link.attr('href');
  86. redirect = new Drupal.facetapi.Redirect(href);
  87. checkbox.click($.proxy(redirect, 'gotoHref'));
  88. if (active) {
  89. // Add the checkbox, hide the link.
  90. $link.before(checkbox).hide();
  91. }
  92. else {
  93. $link.before(checkbox);
  94. }
  95. $link.removeClass('facetapi-checkbox').addClass('facetapi-checkbox-processed');
  96. }
  97. }
  98. })(jQuery);