All of the jquery calls in this function should use the 'context' variable as the second parameter to restrict the scope of their effect. Without this, everytime Drupal.attachBehaviors is called for an AHA or other AJAX style request, the JS adds additional 'Show More' links into the blocks that utilize them.

Sorry for not making a patch, but since the file is very small I'm simply posting my example of how to correct the issue. This is the contents of my apachesolr.js and it is currently working well for me.

Drupal.behaviors.apachesolr = function(context) {
  $('.apachesolr-hidden-facet', context).hide();
  $('<a href="#" class="apachesolr-showhide"></a>', context).text(Drupal.t('Show more')).click(function() {
    if ($(this, context).parent().find('.apachesolr-hidden-facet:visible').length == 0) {
      $(this, context).parent().find('.apachesolr-hidden-facet').show();
      $(this, context).text(Drupal.t('Show fewer'));
    }
    else {
      $(this, context).parent().find('.apachesolr-hidden-facet').hide();
      $(this, context).text(Drupal.t('Show more'));
    }
    return false;
  }).appendTo($('.block-apachesolr_search:has(.apachesolr-hidden-facet), .block-apachesolr:has(.apachesolr-hidden-facet)', context));
}


Comments

Scott Reynolds’s picture

Actually, context really can't be relied on: http://lists.drupal.org/pipermail/development/2009-May/033059.html

So instead we should try to use classes to tell if its been processed or not. That is the best practice.

justindodge’s picture

Interesting link.

Well, so long as it's done in someway other than blindly reattaching to the whole document every time the issue would be solved for me. :)

pwolanin’s picture

Version: 6.x-1.0-rc4 » 6.x-1.x-dev

Drupal core uses the class approach:

Drupal.behaviors.collapse = function (context) {
  $('fieldset.collapsible > legend:not(.collapse-processed)', context).each(function() {
    var fieldset = $(this.parentNode);
    // Expand if there are errors inside
    if ($('input.error, textarea.error, select.error', fieldset).size() > 0) {
      fieldset.removeClass('collapsed');
    }

    // Turn the legend into a clickable link and wrap the contents of the fieldset
    // in a div for easier animation
    var text = this.innerHTML;
      $(this).empty().append($('<a href="#">'+ text +'</a>').click(function() {
        var fieldset = $(this).parents('fieldset:first')[0];
        // Don't animate multiple times
        if (!fieldset.animating) {
          fieldset.animating = true;
          Drupal.toggleFieldset(fieldset);
        }
        return false;
      }))
      .after($('<div class="fieldset-wrapper"></div>')
      .append(fieldset.children(':not(legend):not(.action)')))
      .addClass('collapse-processed');
  });
};
pwolanin’s picture

Status: Active » Needs review
StatusFileSize
new1.69 KB

Discussed w/ ksenzee and conclude the original approach is good enough and unlikely to be affected by the edge cases of using context.

Also, Earl supports using context: http://lists.drupal.org/pipermail/development/2009-May/033057.html

pwolanin’s picture

StatusFileSize
new1.17 KB

After further review - we really just need context in a couple places.

pwolanin’s picture

Version: 6.x-1.x-dev » 6.x-2.x-dev
Status: Needs review » Patch (to be ported)

committing to 6.x-1.x

robertdouglass’s picture

StatusFileSize
new1.21 KB

This patch takes the two context instances from #6 into consideration. Since the 6.2 file has more code I'm going to ask for a review of this issues from the jQuery gods - it may be that more context passing is needed.

Drupal.behaviors.apachesolr = function(context) {
  $('.apachesolr-hidden-facet', context).hide();
  $('<a href="#" class="apachesolr-showhide"></a>').text(Drupal.t('Show more')).click(function() {
    if ($(this).parent().find('.apachesolr-hidden-facet:visible').length == 0) {
      $(this).parent().find('.apachesolr-hidden-facet').show();
      $(this).text(Drupal.t('Show fewer'));
    }
    else {
      $(this).parent().find('.apachesolr-hidden-facet').hide();
      $(this).text(Drupal.t('Show more'));
    }
    return false;
  }).appendTo($(Drupal.settings.apachesolr_show_more_blocks, context));

  if (Drupal.settings.apachesolr_facetstyle == 'checkboxes') {
    // Find all facet links and give them a checkbox
    $('.apachesolr-facet', context).each(Drupal.apachesolr.addCheckbox);
    // Find all unclick links and turn them into checkboxes
    $('.apachesolr-unclick', context).each(Drupal.apachesolr.makeCheckbox);
  }
}

Drupal.apachesolr = {}

Drupal.apachesolr.addCheckbox = function() {
  // Put href in context scope to be visible in the anonymous function.
  var href = $(this).attr('href');
  $(this).before($('<input type="checkbox" />')
    .attr('class', 'facet-checkbox')
    .click(function(){
      window.location.href = href;
    })
  );
}

Drupal.apachesolr.makeCheckbox = function() {
  // Create a checked checkbox.
  var checkbox = $('<input type="checkbox" />')
    .attr('class', 'facet-checkbox')
    .attr('checked', true);
  // Put href in context scope to be visible in the anonymous function.
  var href = $(this).attr('href');
  checkbox.click(function(){
    window.location.href = href;
  });
  // Add the checkbox, hide the link.
  $(this).before(checkbox).hide();
}
claudiu.cristea’s picture

Version: 6.x-2.x-dev » 5.x-2.x-dev
StatusFileSize
new2.2 KB

Here's a patch against DRUPAL-5--2.

claudiu.cristea’s picture

Version: 5.x-2.x-dev » 6.x-2.x-dev

Committed to DRUPAL-5--2 in #326224.

Switched back ti DRUPAL-6--2.

robertdouglass’s picture

Status: Patch (to be ported) » Fixed

#666936 by pwolanin, robertDouglass, claudiu.cristea | justindodge: Fixed apachesolr.js - Drupal.behaviors.apachesolr does not respect context.

Fixed all around.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.