diff --git a/ajax_facets.module b/ajax_facets.module index 7504334..273b851 100755 --- a/ajax_facets.module +++ b/ajax_facets.module @@ -95,63 +95,6 @@ function ajax_facets_facetapi_empty_behaviors() { } /** - * Add required JS and handle single inclusion. - */ -function ajax_facets_add_ajax_js($facet) { - static $included = FALSE; - if (!$included) { - $included = TRUE; - $history_js_exists = FALSE; - $module_path = drupal_get_path('module', 'ajax_facets'); - drupal_add_js($module_path . '/misc/ajax_facets.js'); - drupal_add_css($module_path . '/misc/ajax_facets.css'); - $search_path = $facet->getAdapter()->getSearchPath(); - $filter_key = $facet->getAdapter()->getUrlProcessor()->getFilterKey(); - // Note that we add in query only filter params and exclude pages and etc... - $query = (isset($_GET[$filter_key])) ? array($filter_key => $_GET[$filter_key]) : array(); - $current_views = search_api_current_search(); - $processable_views = array(); - - // Add history.js file if exists. - if (module_exists('libraries')) { - $history_js_path = libraries_get_path('history.js'); - - if ($history_js_path) { - $history_js_exists = TRUE; - drupal_add_js($history_js_path . '/scripts/bundled/html4+html5/jquery.history.js', array('group' => JS_LIBRARY)); - } - } - - if (!empty($current_views)) { - // Get displays from current search. - $keys = array_keys($current_views); - foreach ($keys as $key) { - if (substr_count($key, 'search_api_views')) { - $parts = explode(':', $key); - $processable_views[] = array( - 'view_name' => $parts[1], - 'view_display_id' => $parts[2] - ); - } - } - } - - $facet = $facet->getFacet(); - $setting['facetapi'] = array( - 'defaultQuery' => isset($_GET[$filter_key]) ? $_GET[$filter_key] : '', - 'searchUrl' => url($search_path), - 'index_id' => $facet['map options']['index id'], - 'views' => $processable_views, - 'facet_field' => $facet['map options']['field']['key'], - 'applyPath' => url($search_path, array('query' => $query)), - 'isHistoryJsExists' => $history_js_exists, - ); - drupal_add_js($setting, 'setting'); - drupal_add_library('system', 'drupal.ajax'); - } -} - -/** * Return Drupal formed url for reset current facet filter. */ function ajax_facets_facet_build_reset_path($facet, $adapter) { @@ -313,22 +256,70 @@ function ajax_facets_form_facetapi_facet_display_form_alter(&$form, $form_state) * Implements hook_views_pre_render(). */ function ajax_facets_views_pre_render(&$view) { - // We use static because we should collect data from all the views. - static $setting; - - // Save settings of rendered views, to use them in request for AJAX facets. - $name_display = $base = "$view->name:$view->current_display"; - $i = 0; - // Use unique key as in search_api_current_search(). - while (isset($setting['facetapi']['view_args'][$name_display])) { - $name_display = $base . '-' . ++$i; - } - $setting['facetapi']['view_args'][$name_display] = $view->args; - $setting['facetapi']['exposed_input'][$name_display] = $view->exposed_raw_input; - $setting['facetapi']['view_path'][$name_display] = $view->get_path(); - $setting['facetapi']['view_dom_id'][$name_display] = $view->dom_id; + // Only add the settings and js files on the search view. + $active_searchers = facetapi_get_active_searchers(); + $active_index = str_replace('search_api@', '', reset($active_searchers)); + $render_view = str_replace('search_api_index_', '', $view->base_table); - drupal_add_js($setting, 'setting'); + if ($render_view == $active_index) { + static $setting; + + $history_js_exists = FALSE; + $module_path = drupal_get_path('module', 'ajax_facets'); + drupal_add_js($module_path . '/misc/ajax_facets.js'); + drupal_add_css($module_path . '/misc/ajax_facets.css'); + // Filter key hardcoded as in pages.inc. Would be nicer to get this value from the facetapi config. + // Note that we add in query only filter params and exclude pages and etc... + $query = isset($_GET['f']) ? array('f' => $_GET['f']) : array(); + $current_views = search_api_current_search(); + $processable_views = []; + + if (!empty($current_views)) { + // Get displays from current search. + $keys = array_keys($current_views); + foreach ($keys as $key) { + if (substr_count($key, 'search_api_views')) { + $parts = explode(':', $key); + $processable_views[] = [ + 'view_name' => $parts[1], + 'view_display_id' => $parts[2] + ]; + } + } + } + + // Add history.js file if exists. + if (module_exists('libraries')) { + $history_js_path = libraries_get_path('history.js'); + + if ($history_js_path) { + $history_js_exists = TRUE; + drupal_add_js($history_js_path . '/scripts/bundled/html4+html5/jquery.history.js', ['group' => JS_LIBRARY]); + } + } + + // Save settings of rendered views, to use them in request for AJAX facets. + $name_display = $base = "$view->name:$view->current_display"; + $i = 0; + // Use unique key as in search_api_current_search(). + while (isset($setting['facetapi']['view_args'][$name_display])) { + $name_display = $base . '-' . ++$i; + } + + $setting['facetapi']['view_args'][$name_display] = is_array($view->args) ? implode('/', $view->args) : $view->args; + $setting['facetapi']['exposed_input'][$name_display] = $view->exposed_raw_input; + $setting['facetapi']['view_path'][$name_display] = $view->get_path(); + $setting['facetapi']['view_dom_id'][$name_display] = $view->dom_id; + $setting['facetapi']['defaultQuery'] = $query ? $query['f'] : ''; + $setting['facetapi']['views'] = $processable_views; + $setting['facetapi']['isHistoryJsExists'] = $history_js_exists; + $setting['facetapi']['index_id'] = $render_view; + $setting['facetapi']['searchUrl'] = url($view->get_url()); + $setting['facetapi']['applyPath'] = url($view->get_url(), ['query' => $query]); + + drupal_add_js($setting, 'setting'); + drupal_add_library('system', 'drupal.ajax'); + } } /** diff --git a/plugins/facetapi/ajax_widget.inc b/plugins/facetapi/ajax_widget.inc index 9465a01..64df2c3 100755 --- a/plugins/facetapi/ajax_widget.inc +++ b/plugins/facetapi/ajax_widget.inc @@ -124,8 +124,6 @@ class FacetapiAjaxWidget extends FacetapiWidgetCheckboxLinks { . render($item_list) . '' ); - - ajax_facets_add_ajax_js($this->facet); } /** diff --git a/plugins/facetapi/ajax_widget_ranges.inc b/plugins/facetapi/ajax_widget_ranges.inc index 4bd4f1a..1ad55a4 100755 --- a/plugins/facetapi/ajax_widget_ranges.inc +++ b/plugins/facetapi/ajax_widget_ranges.inc @@ -125,7 +125,5 @@ class FacetapiAjaxWidgetRanges extends FacetapiAjaxWidget { . '' . '' ); - - ajax_facets_add_ajax_js($this->facet); } } diff --git a/plugins/facetapi/ajax_widget_select.inc b/plugins/facetapi/ajax_widget_select.inc index 28550af..3ab173a 100755 --- a/plugins/facetapi/ajax_widget_select.inc +++ b/plugins/facetapi/ajax_widget_select.inc @@ -158,7 +158,5 @@ class FacetapiAjaxWidgetSelect extends FacetapiAjaxWidget { . render($select) . '' ); - - ajax_facets_add_ajax_js($this->facet); } }