You are here

function jcarousel_views_add in jCarousel 8.3

Same name and namespace in other branches
  1. 6.2 includes/jcarousel.views.inc \jcarousel_views_add()
  2. 7.2 includes/jcarousel.views.inc \jcarousel_views_add()

Adds necessary CSS and JS for Views-based carousels.

1 call to jcarousel_views_add()
template_preprocess_jcarousel_view in includes/jcarousel.views.inc
Preprocess function for jcarousel-view.tpl.php.

File

includes/jcarousel.views.inc, line 105
Views integration for jCarousel module.

Code

function jcarousel_views_add($view, $display_id = NULL) {
  static $dom_counter = 0;
  if (!isset($display_id)) {
    $display_id = empty($view->current_display) ? 'default' : $view->current_display;
  }

  // Save the settings for the carousel, these will be used by the JavaScript.
  $options = array();

  // Keep track of each settings addition and give a unique ID. This can be
  // useful when displaying the same view multiple times with different
  // arguments (i.e. such as in a panel).
  $options['view_options'] = array(
    'view_args' => check_plain(implode('/', $view->args)),
    'view_path' => check_plain($_GET['q']),
    'view_base_path' => $view
      ->get_path(),
    'view_display_id' => $display_id,
    'view_name' => $view->name,
    'jcarousel_dom_id' => isset($view->jcarousel_dom_id) ? $view->jcarousel_dom_id : $view->name . '_' . $display_id . '_' . ++$dom_counter,
  );
  foreach ($view->style_plugin->options as $key => $option) {
    if ($option) {
      $options[$key] = is_numeric($option) ? (int) $option : $option;
    }
  }

  // By default limit the scrolling to the same number of items as are visible
  // to avoid display glitches.
  if (empty($options['scroll']) && !empty($options['visible'])) {
    $options['scroll'] = $options['visible'];
  }

  // By default limit the scrolling to the same number of items as are visible
  // to avoid display glitches.
  if (empty($options['scroll']) && !empty($options['visible'])) {
    $options['scroll'] = $options['visible'];
  }

  // Get the total number of items in this view.
  if ($view->build_info['count_query']) {
    $count_query = $view->build_info['count_query']
      ->countQuery();
    $count = $count_query
      ->execute()
      ->fetchField();

    // Views may populate total_rows later but since we've already generated
    // this value we might as well make it available.
    $view->total_rows = $count;
  }

  // If there is just one item disable the auto-scroll and rotation.
  if ($view->total_rows == 1) {
    $options['wrap'] = NULL;
    $options['auto'] = 0;
  }

  // Calculate proper value for start if a negative value was chosen
  if ($options['start'] < 0) {
    $options['start'] += $count + 1;
  }

  // Determine AJAX functionality in a backwards-compatible way. Versions prior
  // to jCarousel 2.6 used the view-level "Use AJAX" option instead of a style
  // setting. We check $view->style_options here intentionally instead of
  // $view->style_plugin->options.
  $use_ajax = isset($view->style_options['ajax']) ? $view->style_options['ajax'] : $view->use_ajax;

  // If using AJAX, adjust the view's positioning based on the current page.
  if ($use_ajax) {
    $options['ajax'] = TRUE;
    $options['size'] = $view->total_rows;

    // Views 2:
    if (isset($view->pager)) {

      // Enable and adjust the pager to get the correct page.
      $use_pager = $view->pager['use_pager'];
      $view->pager['use_pager'] = TRUE;
      $view
        ->build($display_id);
      $view->pager['use_pager'] = $use_pager;

      // Create generic variable names.
      $pager_current_page = $view->pager['current_page'];
      $pager_items_per_page = $view->pager['items_per_page'];
      $pager_offset = $view->pager['offset'];
    }
    else {

      // Adjusting the query is not necessary.
      $view
        ->build($display_id);

      // Create generic variable names.
      $pager_current_page = $view->current_page;
      $pager_items_per_page = $view->items_per_page;
      $pager_offset = $view->offset;
    }

    // If starting in the middle of a view, initialize the carousel at that
    // position. Strangely the carousel must pre-load empty LI items all the way
    // up until the active item, making this inefficient for large lists.
    if ($pager_current_page) {

      // TODO: Pagers and carousels do not work well together. jCarousel should
      // give items the class "jcarousel-item-[offset]", but instead it always
      // starts with "1", making it impossible to define a prepopulated list
      // as the middle of an AJAX view.
      $options['start'] = $pager_current_page * $pager_items_per_page + ($pager_offset + 1);
      $options['offset'] = $pager_current_page * $pager_items_per_page + ($pager_offset + 1);
    }
    elseif ($pager_offset) {
      $options['start'] = $pager_offset + 1;
      $options['offset'] = $pager_offset + 1;
    }
  }
  $identifier = drupal_clean_css_identifier('jcarousel_dom_' . $options['view_options']['jcarousel_dom_id']);
  return jcarousel_add($identifier, $options);
}