diff --git a/better_exposed_filters.js b/better_exposed_filters.js index 7bfcf66..295be76 100644 --- a/better_exposed_filters.js +++ b/better_exposed_filters.js @@ -245,6 +245,102 @@ } }; + // 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 built. + + // 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 *[type=submit]').click(); + }); + }); + } + }; + + Drupal.behaviors.betterExposedFiltersRequiredFilter = { + attach: function(context, settings) { + // Required checkboxes should re-check all inputs if a user un-checks + // them all. + $('.bef-select-as-checkboxes', context).once('bef-required-filter').ajaxComplete(function (e, xhr, s) { + var $element = $(this); + + 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 $view_name; + var $view_display_id; + var $uses_ajax = false; + $.each(settings.views.ajaxViews, function(i, item) { + $view_name = item.view_name; + $view_display_id = item.view_display_id; + var $id = 'views-exposed-form-' + $view_name.replace(/_/g, '-') + '-' + $view_display_id.replace(/_/g, '-'); + var $form_id = $element.parents('form').attr('id'); + if ($form_id == $id) { + $uses_ajax = true; + return; + } + }); + + var $filter_name = $('input', this).attr('name').slice(0, -2); + if (Drupal.settings.better_exposed_filters.views[$view_name].displays[$view_display_id].filters[$filter_name].required && $('input:checked', this).length == 0) { + $('input', this).prop('checked', true); + } + }); + } + } + /* * Helper functions */ diff --git a/better_exposed_filters.module b/better_exposed_filters.module index f53ae12..62af4b8 100644 --- a/better_exposed_filters.module +++ b/better_exposed_filters.module @@ -209,3 +209,32 @@ function better_exposed_filters_i18n_string_info() { ); return $groups; } + +/** + * Implements hook_preprocess_views_view(). + */ +function better_exposed_filters_preprocess_views_view(&$vars) { + $filters = array(); + foreach ($vars['view']->filter as $filter) { + if ($filter->options['exposed']) { + $identifier = $filter->options['is_grouped'] ? $filter->options['group_info']['identifier'] : $filter->options['expose']['identifier']; + $filters[$identifier] = array( + 'required' => $filter->options['expose']['required'] ? true : false, + ); + } + } + + $bef_js = array( + 'views' => array( + $vars['view']->name => array( + 'displays' => array( + $vars['view']->current_display => array( + 'filters' => $filters, + ), + ), + ), + ), + ); + + drupal_add_js(array('better_exposed_filters' => $bef_js), 'setting'); +} diff --git a/better_exposed_filters.theme b/better_exposed_filters.theme index 47c5480..3c1ac3d 100644 --- a/better_exposed_filters.theme +++ b/better_exposed_filters.theme @@ -458,8 +458,8 @@ 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 (!empty($element['#default_value'])) { + $selected_options[] = $element['#default_value']; } } else { @@ -541,26 +541,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; @@ -574,14 +573,18 @@ function theme_select_as_links($vars) { $output = ''; diff --git a/better_exposed_filters_exposed_form_plugin.inc b/better_exposed_filters_exposed_form_plugin.inc index 2da6c53..7625b56 100644 --- a/better_exposed_filters_exposed_form_plugin.inc +++ b/better_exposed_filters_exposed_form_plugin.inc @@ -802,6 +802,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 @@ -902,6 +903,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'])) { */ /* @@ -923,6 +938,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'])); @@ -1213,6 +1229,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[$filter_id]['#theme'] = 'select_as_links'; // Exposed form displayed as blocks can appear on pages other than