diff --git better_exposed_filters.js better_exposed_filters.js index d818783..48dd3ae 100644 --- better_exposed_filters.js +++ better_exposed_filters.js @@ -254,6 +254,67 @@ } }; + // This is only needed to provide ajax functionality + Drupal.behaviors.better_exposed_filters_select_as_links = { + attach: function(context, settings) { + + $('.bef-select-as-links', context).once(function() { + var $element = $(this); + + // Check if ajax submission is enabled. If it's not enabled then we + // don't need to attach our custom submission handling, because the + // links are already properly build. + + // First check if any ajax views are contained in the current page. + if (typeof settings.views == 'undefined' || typeof settings.views.ajaxViews == 'undefined') { + return; + } + + // Now check that the view for which the current filter block is used, + // is part of the configured ajax views. + var $uses_ajax = false; + $.each(settings.views.ajaxViews, function(i, item) { + var $view_name = item.view_name.replace(/_/g, '-'); + var $view_display_id = item.view_display_id.replace(/_/g, '-'); + var $id = 'views-exposed-form-' + $view_name + '-' + $view_display_id; + var $form_id = $element.parents('form').attr('id'); + if ($form_id == $id) { + $uses_ajax = true; + return; + } + }); + + // If no ajax is used for form submission, we quit here. + if (!$uses_ajax) { + return; + } + + // Attach selection toggle and form submit on click to each link. + $(this).find('a').click(function(event) { + var $wrapper = $(this).parents('.bef-select-as-links'); + var $options = $wrapper.find('select option'); + // We have to prevent the page load triggered by the links. + event.preventDefault(); + event.stopPropagation(); + // Un select old select value. + $wrapper.find('select option').removeAttr('selected'); + + // Set the corresponding option inside the select element as selected + var link_text = $(this).text(); + $selected = $options.filter(function() { + return $(this).text() == link_text; + }); + $selected.attr('selected', 'selected'); + $wrapper.find('.bef-new-value').val($selected.val()); + $wrapper.find('a').removeClass('active'); + $(this).addClass('active'); + // Submit the form. + $wrapper.parents('form').find('.views-submit-button input[type=submit]').click(); + }); + }); + } + }; + /* * Helper functions */ diff --git better_exposed_filters.theme better_exposed_filters.theme index 5966dc3..0e39bb5 100644 --- better_exposed_filters.theme +++ better_exposed_filters.theme @@ -461,9 +461,9 @@ function theme_select_as_links($vars) { // Collect selected values so we can properly style the links later. $selected_options = array(); - if (empty($element['#value'])) { - if (!empty($element['#default_values'])) { - $selected_options[] = $element['#default_values']; + if (strlen((string) $element['#value']) == 0) { + if (!empty($element['#default_value'])) { + $selected_options[] = $element['#default_value']; } } else { @@ -545,26 +545,25 @@ function theme_select_as_links($vars) { '#type' => 'bef-link', '#name' => $id, ); - if (array_search($key, $selected_options) === FALSE) { - $elem['#children'] = l($value, bef_replace_query_string_arg($name, $key, $multiple, FALSE, $path)); - //$output .= theme('form_element', array('element' => $elem)); - $element_output = theme('form_element', array('element' => $elem)); - - if ($element['#name'] == 'sort_bef_combine' && !empty($element['#settings']['toggle_links'])) { - $sort_pair = explode(' ', $key); - if (count($sort_pair) == 2) { - // Highlight the link if it is the selected sort_by (can be either - // asc or desc, it doesn't matter). - if (strpos($selected_options[0], $sort_pair[0]) === 0) { - $element_output = str_replace('form-item', 'form-item selected', $element_output); - } - } + + $link_options = array(); + // Add "active" class to the currently active filter link. + if (in_array((string) $key, $selected_options)) { + $link_options['attributes'] = array('class' => 'active'); + } + $url = bef_replace_query_string_arg($name, $key, $multiple, FALSE, $path); + $elem['#children'] = l($value, $url, $link_options); + $element_output = theme('form_element', array('element' => $elem)); + + if ($element['#name'] == 'sort_bef_combine' && !empty($element['#settings']['toggle_links'])) { + $sort_pair = explode(' ', $key); + if (count($sort_pair) == 2) { + // Highlight the link if it is the selected sort_by (can be either + // asc or desc, it doesn't matter). + if (strpos($selected_options[0], $sort_pair[0]) === 0) { + $element_output = str_replace('form-item', 'form-item selected', $element_output); } - } else { - $elem['#children'] = l($value, bef_replace_query_string_arg($name, $key, $multiple, TRUE, $path)); - _form_set_class($elem, array('bef-select-as-links-selected')); - //$output .= str_replace('form-item', 'form-item selected', theme('form_element', array('element' => $elem))); - $element_output = str_replace('form-item', 'form-item selected', theme('form_element', array('element' => $elem))); + } } $output .= $element_output; @@ -578,14 +577,18 @@ function theme_select_as_links($vars) { $output = ''; diff --git better_exposed_filters_exposed_form_plugin.inc better_exposed_filters_exposed_form_plugin.inc index f43bdaa..d932208 100644 --- better_exposed_filters_exposed_form_plugin.inc +++ better_exposed_filters_exposed_form_plugin.inc @@ -741,6 +741,7 @@ Title Desc|Z -> A Leave the replacement value blank to remove an option al case 'bef_links': case 'bef_toggle_links': + $bef_add_js = TRUE; $form['sort_bef_combine']['#theme'] = 'select_as_links'; // Exposed form displayed as blocks can appear on pages other than @@ -842,6 +843,20 @@ Title Desc|Z -> A Leave the replacement value blank to remove an option al } } } + elseif (isset($settings['sort']) && !empty($form['sort_by'])) { + if ('bef_links' == $settings['sort']['bef_format']) { + $bef_add_js = TRUE; + $form['sort_by']['#theme'] = 'select_as_links'; + + // Exposed form displayed as blocks can appear on pages other than + // the view results appear on. This can cause problems with + // select_as_links options as they will use the wrong path. We + // provide a hint for theme functions to correct this. + if (!empty($this->display->display_options['exposed_block'])) { + $form['sort_by']['#bef_path'] = $this->display->display_options['path']; + } + } + } /* Ends: if (isset($settings['sort'])) { */ /* @@ -863,6 +878,7 @@ Title Desc|Z -> A Leave the replacement value blank to remove an option al case 'bef_links': if (count($form['items_per_page']['#options']) > 1) { + $bef_add_js = TRUE; $form['items_per_page']['#theme'] = 'select_as_links'; $form['items_per_page']['#items_per_page'] = max($form['items_per_page']['#default_value'], key($form['items_per_page']['#options'])); @@ -1152,6 +1168,7 @@ Title Desc|Z -> A Leave the replacement value blank to remove an option al case 'bef_links': $show_apply = TRUE; + $bef_add_js = TRUE; $form[$field_id]['#theme'] = 'select_as_links'; // Exposed form displayed as blocks can appear on pages other than