You are here

function jcarousel_views_options in jCarousel 7.3

Adds necessary CSS and JS for Views-based carousels.

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

File

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

Code

function jcarousel_views_options($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 : ++$dom_counter,
  );
  foreach ($view->style_plugin->options as $key => $option) {
    if ($option) {
      $options[$key] = is_numeric($option) ? (int) $option : $option;
    }
  }

  // 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 already 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;
  }

  // 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;
    }
  }
  return $options;
}