function jcarousel_views_add in jCarousel 6.2
Same name and namespace in other branches
- 8.3 includes/jcarousel.views.inc \jcarousel_views_add()
- 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 : ++$dom_counter,
);
foreach ($view->style_plugin->options as $key => $option) {
if ($option) {
$options[$key] = is_numeric($option) ? (int) $option : $option;
}
}
// Views 3 ? Views 3 : Views 2.
$count_query = isset($view->query->count_query) ? $view->query->count_query : db_rewrite_sql($view->build_info['count_query'], $view->base_table, $view->base_field, array(
'view' => &$view,
));
$count_args = isset($view->query->query_args) ? $view->query->query_args : $view->build_info['query_args'];
// 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.
$replacements = module_invoke_all('views_query_substitutions', $view);
$count_query = str_replace(array_keys($replacements), $replacements, $count_query);
if (is_array($count_args)) {
foreach ($count_args as $arg_id => $arg) {
$count_args[$arg_id] = str_replace(array_keys($replacements), $replacements, $arg);
}
}
$result = db_query($count_query, $count_args);
$count = 0;
while ($row = db_fetch_array($result)) {
$count++;
}
// 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 ($count == 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'] = $count;
// 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 = views_css_safe('jcarousel_dom_' . $options['view_options']['jcarousel_dom_id']);
return jcarousel_add($identifier, $options);
}