Skip to content

Instantly share code, notes, and snippets.

@stborchert
Created July 16, 2013 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stborchert/6006938 to your computer and use it in GitHub Desktop.
Save stborchert/6006938 to your computer and use it in GitHub Desktop.
(function($) {
/**
* Change multi-value dropdown to single-value dropdown and back (visually).
*/
Drupal.behaviors.collapsibleFilterRewriteMultipleSelect = {
attach: function(context, settings) {
$.each(Drupal.settings.collapsibleFilter.multiple_selects, function(name, settings) {
$('select#edit-' + name)
.once('collapsible-filter-multiple-rewrite')
.each(function() {
var selectionCount = $('option:selected', $(this)).length;
if (selectionCount <= 1) {
// Set size of select to 1 if there is not more than 1 selected.
$(this).attr('size', 1);
// Remove attribute "multiple".
$(this).removeAttr('multiple');
// Set default option.
if (selectionCount === 0 || $(this).val() === 'All') {
$(this).val('All');
}
// Add link to expand the dropdown.
$expand = $('<a>')
.addClass('select-expander')
.attr('href', '#')
.attr('title', Drupal.t('Expand selection'))
.html('[+]')
.click(function() {
// Get corresponding select element.
$select = $(this)
.parent('.form-type-select')
.find('.collapsible-filter-multiple-rewrite-processed');
// Expand element.
$select.attr('size', settings.size)
.attr('multiple', 'multiple');
$(this).remove();
})
.appendTo($(this).parent());
}
});
});
}
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment