You are here

function ajax_facets_add_ajax_js in Ajax facets 7.3

Same name and namespace in other branches
  1. 7 ajax_facets.module \ajax_facets_add_ajax_js()
  2. 7.2 ajax_facets.module \ajax_facets_add_ajax_js()

Add required JS and handle single inclusion.

3 calls to ajax_facets_add_ajax_js()
FacetapiAjaxWidget::execute in plugins/facetapi/ajax_widget.inc
Implements FacetapiWidget::execute().
FacetapiAjaxWidgetRanges::execute in plugins/facetapi/ajax_widget_ranges.inc
Implements FacetapiWidget::execute().
FacetapiAjaxWidgetSelect::execute in plugins/facetapi/ajax_widget_select.inc
Implements FacetapiWidget::execute().

File

./ajax_facets.module, line 100

Code

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_js([
      'ajax_facets' => [
        'auto_update_facets' => variable_get('ajax_facets_auto_update_facets', 1),
      ],
    ], [
      'type' => 'setting',
    ]);
    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]) ? [
      $filter_key => $_GET[$filter_key],
    ] : [];
    $search_results = search_api_current_search();
    $processable_views = [];

    // Process $search_results if we have them.
    if (!empty($search_results)) {

      // Get displays from current search.
      foreach ($search_results as $key => $result) {
        if (substr_count($key, 'search_api_views')) {
          $facetapi_parts = explode(':', $key);
          $views_parts = explode(':', $result[0]
            ->getOption('search id'));
          $processable_views[] = [
            'view_name' => $views_parts[1],
            'view_display_id' => $views_parts[2],
            // Used to identify viewsDomId in settings.facetapi.
            'facetapi_view_display_id' => $facetapi_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,
        ]);
      }
    }
    $facet = $facet
      ->getFacet();
    $setting['facetapi'] = [
      '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, [
        'query' => $query,
      ]),
      'isHistoryJsExists' => $history_js_exists,
    ];
    drupal_add_js($setting, 'setting');
    drupal_add_library('system', 'drupal.ajax');
  }
}