You are here

jcarousel.module in jCarousel 7.3

Provides integration with 3rd party modules and the jCarousel library.

File

jcarousel.module
View source
<?php

/**
 * @file
 * Provides integration with 3rd party modules and the jCarousel library.
 */

/**
 * Implements hook_help().
 */
function jcarousel_help($path, $arg) {

  // @todo Rewrite this.
  switch ($path) {
    case 'admin/help#jcarousel':
      $output = '';
      return $output;
  }
}

/**
 * Implements hook_menu().
 */
function jcarousel_menu() {
  $items['jcarousel/ajax/views'] = array(
    'page callback' => 'jcarousel_views_ajax',
    'access callback' => TRUE,
    'file' => 'includes/jcarousel.views.inc',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_init().
 */
function jcarousel_init() {

  // Global JS settings required for all carousels.
  drupal_add_js(array(
    'jcarousel' => array(
      'ajaxPath' => url('jcarousel/ajax/views'),
    ),
  ), 'setting');

  // Load the jcarousel library via Libraries API.
  $library = libraries_load('jcarousel');
  drupal_add_js(drupal_get_path('module', 'jcarousel') . '/js/jcarousel.js');

  // Load the default jCarousel stylesheet.
  drupal_add_css(drupal_get_path('module', 'jcarousel') . '/css/jcarousel.css');
}

/**
 * Implements hook_theme().
 */
function jcarousel_theme() {
  return array(
    'jcarousel' => array(
      'variables' => array(
        'items' => NULL,
        'options' => NULL,
        'identifier' => NULL,
      ),
    ),
  );
}

/**
 * Implements hook_views_api().
 */
function jcarousel_views_api() {
  return array(
    'api' => 2.0,
    'path' => drupal_get_path('module', 'jcarousel') . '/includes',
    'template path' => drupal_get_path('module', 'jcarousel') . '/includes',
  );
}

/**
 * Implements hook_libraries_info().
 */
function jcarousel_libraries_info() {
  $libraries['jcarousel'] = array(
    'name' => 'jCarousel',
    'vendor url' => 'http://sorgalla.com/jcarousel/',
    'download url' => 'http://sorgalla.com/jcarousel/dist/#full-download',
    'files' => array(
      'js' => array(
        'jquery.jcarousel.min.js',
      ),
    ),
    'version arguments' => array(
      'file' => 'jquery.jcarousel.min.js',
      'pattern' => '@jCarousel\\s+-\\s+v([0-9a-zA-Z\\.-]+)\\s*-@',
      'lines' => 3,
    ),
    'variants' => array(
      'development' => array(
        'files' => array(
          'js' => array(
            'jquery.jcarousel.js',
          ),
        ),
        'version arguments' => array(
          'file' => 'jquery.jcarousel.js',
          'pattern' => '@jCarousel\\s+-\\s+v([0-9a-zA-Z\\.-]+)\\s*-@',
          'lines' => 3,
        ),
      ),
    ),
  );
  return $libraries;
}

/**
 * Implements hook_jcarousel_skin_info().
 */
function jcarousel_jcarousel_skin_info() {
  $skins = array();
  $skins['default'] = array(
    'title' => t('Default'),
    'file' => 'skins/default/jcarousel-default.css',
  );
  $skins['tango'] = array(
    'title' => t('Tango'),
    'file' => 'skins/tango/jcarousel-tango.css',
  );
  return $skins;
}

/**
 * Retrieves a list of all available Carousel skins.
 */
function jcarousel_skins() {
  static $skins;

  // Don't rebuild if we already have a list of skins.
  if (isset($skins)) {
    return $skins;
  }

  // Build a list of skins from other modules.
  $skins = array();
  foreach (module_implements('jcarousel_skin_info') as $module) {
    $function = $module . '_jcarousel_skin_info';
    $module_skins = $function();
    foreach ($module_skins as $key => $skin) {
      $skin['module'] = $module;
      $skin['file path'] = isset($skin['file path']) ? $skin['file path'] : drupal_get_path('module', $module);
      $skin['title'] = isset($skin['title']) ? $skin['title'] : $key;
      $skins[$key] = $skin;
    }
  }
  ksort($skins);
  return $skins;
}

/**
 * Creates a jCarousel element on the page.
 *
 * @param $items
 *   The items to appear in the carousel.
 * @param $options
 *   The arguments to apply to the selected element when creating the jCarousel.
 *   The arguments are passed through as an associative array using the
 *   jCarousel configuration options:
 *   http://sorgalla.com/projects/jcarousel/#Configuration
 *
 *   The special option "skin" may also be specified to use any skin provided
 *   by jcarousel_skins() and hook_jcarousel_skin_info().
 * @param $id
 *   An ID for this carousel. If not provided, one will be generated
 *   automatically. Note that this is not the "id" attribute on the HTML list,
 *   instead it is one of the classes added to the UL tag.
 */
function theme_jcarousel($variables) {
  $items = $variables['items'];
  $options = $variables['options'];
  $identifier = $variables['identifier'];
  static $unique;
  if (!isset($identifier)) {
    $unique = isset($unique) ? $unique + 1 : 1;
    $identifier = 'jcarousel-id-' . $unique;
  }
  $options['skin'] = array_key_exists('skin', $options) ? $options['skin'] : 'default';

  //jcarousel_add($identifier, $options);
  $classes = 'jcarousel ' . $identifier;
  if (!empty($options['skin'])) {
    $classes .= ' jcarousel-skin-' . $options['skin'];
  }
  $output = '<ul class="' . $classes . '">';
  if (is_array($items)) {
    foreach ($items as $item) {
      $output .= '<li>' . $item . '</li>';
    }
  }
  $output .= '</ul>';
  return $output;
}
function jcarousel_data_attributes($options, $format_attributes = TRUE) {
  $data_attributes = array();
  foreach (jcarousel_filter_data_attributes($options) as $k => $v) {
    if (isset($v)) {
      if (is_bool($v)) {
        $v = $v ? 'true' : 'false';
      }
      $data_attributes["data-{$k}"] = check_plain($v);
    }
  }
  return $format_attributes ? drupal_attributes($data_attributes) : $data_attributes;
}
function jcarousel_filter_data_attributes($options) {
  static $library_options = array(
    'list' => TRUE,
    'items' => TRUE,
    'animation' => TRUE,
    'transitions' => TRUE,
    'wrap' => TRUE,
    'vertical' => TRUE,
    'rtl' => TRUE,
    'center' => TRUE,
    'responsive' => TRUE,
    'skinfile' => TRUE,
  );
  static $jcarousel_options = array(
    'jcarousel' => TRUE,
    'start' => TRUE,
  );
  return array_intersect_key($options, $library_options + $jcarousel_options);
}

Functions

Namesort descending Description
jcarousel_data_attributes
jcarousel_filter_data_attributes
jcarousel_help Implements hook_help().
jcarousel_init Implementation of hook_init().
jcarousel_jcarousel_skin_info Implements hook_jcarousel_skin_info().
jcarousel_libraries_info Implements hook_libraries_info().
jcarousel_menu Implements hook_menu().
jcarousel_skins Retrieves a list of all available Carousel skins.
jcarousel_theme Implements hook_theme().
jcarousel_views_api Implements hook_views_api().
theme_jcarousel Creates a jCarousel element on the page.