jquery_carousel.module in jQuery carousel 7
Same filename and directory in other branches
Provide jquery carousel style plugin for views.
File
jquery_carousel.moduleView source
<?php
/**
* @file
* Provide jquery carousel style plugin for views.
*/
// Trying to make sure the theme.inc get's loaded.
include_once 'views/theme/jquery_carousel.theme.inc';
/**
* Implements hook_help().
*/
function jquery_carousel_help($path, $arg) {
$help_text = '';
switch ($path) {
case 'admin/help#jquery_carousel':
$help_text = '<p>' . t('jQuery carousel is based on the jquery plugin http://github.com/richardscarrott/jquery-ui-carousel. It allows developers to create carousels using the data entered through the content types.') . '</p>';
$help_text .= '<p>' . t('For more details, check the documentation on the project page(https://www.drupal.org/project/jquery_carousel)') . '</p>';
$help_text .= '<p>' . t('Demo available at http://jcarousel.qed42.webfactional.com');
break;
}
return $help_text;
}
/**
* Implements hook_views_api().
*/
function jquery_carousel_views_api() {
return array(
'api' => '3',
'path' => drupal_get_path('module', 'jquery_carousel') . '/views',
);
}
/**
* Implements hook_field_formatter_info().
*/
function jquery_carousel_field_formatter_info() {
return array(
// Key must be unique, so it's best to prefix with your module's name.
'jquery_carousel_images' => array(
// Label is is what is displayed in the select box in the UI.
'label' => t('jQuery Carousel'),
// Field types is the important bit!!
// List the field types your formatter is for.
'field types' => array(
'image',
),
// Settings form for jquery carousel formatter.
'settings' => _jquery_carousel_formatter_defaults(),
),
);
}
/**
* Formatter settings form.
*/
function _jquery_carousel_formatter_defaults($settings = array()) {
$settings += array(
'theme' => 'default',
'selector' => 'rs-carousel',
'style_name' => 'thumbnail',
'itemsPerTransition' => 'auto',
'orientation' => 'horizontal',
'loop' => FALSE,
'whitespace' => FALSE,
'nextPrevActions' => TRUE,
'pagination' => FALSE,
'speed' => 'normal',
'easing' => 'swing',
'autoScroll' => TRUE,
'pause' => 8000,
'continuous' => FALSE,
'touch' => TRUE,
);
return $settings;
}
/**
* Implements hook_field_formatter_settings_form().
*/
function jquery_carousel_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = _jquery_carousel_formatter_defaults($display['settings']);
$jquery_carousel_form = jquery_carousel_config_form();
$jquery_carousel_form['style_name'] = array(
'#type' => 'select',
'#title' => t('Image Style'),
'#description' => t('Select the image style to be associated.'),
'#options' => image_style_options(),
'#weight' => -1,
'#default_value' => '',
);
$jquery_carousel_form['selector']['#element_validate'] = array(
'jquery_carousel_config_form_validate',
);
foreach ($jquery_carousel_form as $key => $form) {
$jquery_carousel_form[$key]['#default_value'] = isset($settings[$key]) ? $settings[$key] : $jquery_carousel_form[$key]['#default_value'];
}
return $jquery_carousel_form;
}
/**
* Element validator for selector field.
*/
function jquery_carousel_config_form_validate($element, &$form_state, $form) {
$selector = $element['#value'];
_jquery_carousel_config_validate($element, $selector);
}
/**
* Validation logic for carousel config.
*/
function _jquery_carousel_config_validate($element, $selector, $type = 'element') {
preg_match('/[a-zA-Z|-]*/', $selector, $matches);
if (count($matches) === 1 && $matches[0] === $selector) {
}
else {
if ($type == 'view') {
form_set_error($element, t("Selector should any special characters or spaces. Only special character allowed is '-'"));
}
else {
form_error($element, t("Selector should any special characters or spaces. Only special character allowed is '-'"));
}
}
}
/**
* Implements hook_field_formatter_settings_summary().
*
* Returns a short summary for the current formatter settings of an instance.
*/
function jquery_carousel_field_formatter_settings_summary($field, $instance, $view_mode) {
$summary = array();
$summary[] = t('Displays multivalued image field content in form of a carousel.');
return implode('<br />', $summary);
}
/**
* Implements hook_field_formatter_view().
*/
function jquery_carousel_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
if (!$items) {
return $element;
}
// Initialize formatter settings.
$settings = _jquery_carousel_formatter_defaults($display['settings']);
// Theme the result.
$element[] = array(
'#markup' => theme('jquery_carousel_field_formatter', array(
'element' => $items,
'display' => $display,
'settings' => $settings,
)),
);
return $element;
}
/**
* Implements hook_theme().
*/
function jquery_carousel_theme() {
return array(
'jquery_carousel_field_formatter' => array(
'element' => array(),
'display' => '',
'settings' => array(),
),
);
}
/**
* Theme callback for jQeury carousel field formatter.
*/
function theme_jquery_carousel_field_formatter($vars) {
_jquery_carousel_settings_format($vars['settings']);
_jquery_carousel_include_css_js($vars['settings']);
$output = '';
if (is_array($vars['element']) && count($vars['element']) > 1) {
$selector_class = drupal_attributes(array(
'class' => $vars['settings']['selector'],
));
$output .= '<div ' . $selector_class . '">';
$output .= '<ul>';
foreach ($vars['element'] as $elem) {
$output .= '<li>';
$output .= theme('image_style', array(
'path' => $elem['uri'],
'style_name' => $vars['settings']['style_name'],
));
$output .= '</li>';
}
$output .= '</ul>';
$output .= '</div>';
}
else {
$output .= theme('image_style', array(
'path' => $vars['element'][0]['uri'],
'style_name' => $vars['settings']['style_name'],
));
}
return $output;
}
/**
* Template preprocessor for jquery carousel views style plugin.
*/
function template_preprocess_jquery_carousel(&$vars) {
_jquery_carousel_preprocess_jquery_carousel($vars);
}
/**
* Helper function to create config form. Reusable for field formatter & views.
*/
function jquery_carousel_config_form() {
$form = $themes = array();
foreach (jquery_carousel_themes() as $key => $theme) {
$themes[$key] = $theme['title'];
}
$themes[''] = t('None');
$form['theme'] = array(
'#type' => 'select',
'#title' => t('jQuery Carousel Theme'),
'#description' => t('Select the theme to be applied to the carousel'),
'#options' => $themes,
'#default_value' => '',
);
$form['selector'] = array(
'#type' => 'textfield',
'#title' => t('Selector for this carousel configuration'),
'#description' => t('The carousel will be wrapped in this class & JS with the config below gets applied to this selector. Please make sure the selector contains only alphabets. No special characters or spaces allowed.'),
'#required' => TRUE,
);
$form['itemsPerTransition'] = array(
'#type' => 'textfield',
'#title' => t('Items per transition'),
'#description' => t('number of items moved with each transition, default: auto'),
'#default_value' => '1',
);
$form['orientation'] = array(
'#type' => 'select',
'#title' => t('Orientation'),
'#description' => t('Sets the orientation of the carousel, either horizontal or vertical.'),
'#options' => array(
'horizontal' => t('Horizontal'),
'vertical' => t('Vertical'),
),
'#default_value' => 1,
);
$form['loop'] = array(
'#type' => 'select',
'#title' => t('Loop'),
'#description' => t('If set to true carousel will loop back to first or last page accordingly.'),
'#options' => array(
0 => t('False'),
1 => t('True'),
),
'#default_value' => 1,
'#boolean' => TRUE,
);
$form['whitespace'] = array(
'#type' => 'select',
'#title' => t('Whitespace'),
'#description' => t("If set to true the carousel will allow whitespace to be seen when there aren't enough items to fill the last page."),
'#options' => array(
0 => t('False'),
1 => t('True'),
),
'#default_value' => 1,
'#boolean' => TRUE,
);
$form['nextPrevActions'] = array(
'#type' => 'select',
'#title' => t('Next & Previous Links'),
'#description' => t('whether next and prev links will be included'),
'#options' => array(
0 => t('False'),
1 => t('True'),
),
'#default_value' => 1,
'#boolean' => TRUE,
);
$form['pagination'] = array(
'#type' => 'select',
'#title' => t('Pagination'),
'#description' => t('whether pagination links will be included'),
'#options' => array(
0 => t('False'),
1 => t('True'),
),
'#default_value' => 1,
'#boolean' => TRUE,
);
$form['speed'] = array(
'#type' => 'select',
'#title' => t('Speed'),
'#description' => t('Animation speed'),
'#options' => array(
'normal' => t('Normal'),
'fast' => t('Fast'),
'slow' => t('Slow'),
),
'#default_value' => 'normal',
);
$form['easing'] = array(
'#type' => 'select',
'#title' => t('Easing'),
'#description' => t('supports the jQuery easing plugin'),
'#options' => array(
'linear' => t('Linear'),
'swing' => t('Swing'),
),
'#default_value' => 'linear',
);
$form['autoScroll'] = array(
'#type' => 'select',
'#title' => t('Autoscroll'),
'#description' => t("Set to true to invoke auto scrolling, note when the mouse enters the carousel the interval will stop, it'll consequently begin when the mouse leaves."),
'#options' => array(
0 => t('False'),
1 => t('True'),
),
'#default_value' => 1,
'#boolean' => TRUE,
);
$form['pause'] = array(
'#type' => 'textfield',
'#title' => t('Pause'),
'#description' => t('Sets the amount of time in miliseconds the carousel waits before it automatically scrolls.'),
'#default_value' => 8000,
);
$form['continuous'] = array(
'#type' => 'select',
'#title' => t('Continuous'),
'#description' => t('If set to true the carousel will continuously loop through its pages rather than scrolling all the way back to the first page.'),
'#options' => array(
0 => t('False'),
1 => t('True'),
),
'#default_value' => 1,
'#boolean' => TRUE,
);
$jquery_update_project_link = l(t('jquery_update'), 'http://drupal.org/project/jquery_update');
$form['touch'] = array(
'#type' => 'select',
'#title' => t('Touch'),
'#description' => t('If set to true the carousel will become draggable allowing you to flick through pages. This feature requires you to enable !jquery_update_project_link.', array(
'!jquery_update_project_link' => $jquery_update_project_link,
)),
'#options' => array(
0 => t('False'),
1 => t('True'),
),
'#default_value' => 1,
'#boolean' => TRUE,
);
return $form;
}
/**
* Helper function to prepare the settings to be exposed in JS.
*
* Helps convert boolean 1s & 0s into boolean strings.
*/
function _jquery_carousel_settings_format(&$settings) {
$carousel_form = jquery_carousel_config_form();
foreach (array_keys($settings) as $key) {
if (isset($carousel_form[$key]['#boolean']) && $carousel_form[$key]['#boolean']) {
$settings[$key] = (bool) $settings[$key];
}
}
if (isset($settings['itemsPerTransition']) && empty($settings['itemsPerTransition'])) {
$settings['itemsPerTransition'] = "auto";
}
}
/**
* Helper function to inject required Css & Js.
*/
function _jquery_carousel_include_css_js($settings) {
$themes = jquery_carousel_themes();
drupal_add_library('system', 'ui.widget');
if (isset($settings['theme']) && $settings['theme']) {
drupal_add_css(drupal_get_path('module', $themes[$settings['theme']]['module']) . '/' . $themes[$settings['theme']]['file']);
}
drupal_add_css(libraries_get_path('jquery-ui-carousel') . '/dist/css/jquery.rs.carousel.css');
drupal_add_js(libraries_get_path('jquery-ui-carousel') . '/dist/js/jquery.rs.carousel.js');
drupal_add_js(libraries_get_path('jquery-ui-carousel') . '/dist/js/jquery.rs.carousel-autoscroll.js');
drupal_add_js(libraries_get_path('jquery-ui-carousel') . '/dist/js/jquery.rs.carousel-continuous.js');
drupal_add_js(libraries_get_path('jquery-ui-carousel') . '/dist/js/jquery.rs.carousel-touch.js');
drupal_add_js(drupal_get_path('module', 'jquery_carousel') . '/js/jquery_carousel.init.js');
if (isset($settings['touch']) && $settings['touch']) {
drupal_add_js(libraries_get_path('jquery-ui-carousel') . '/vendor/jquery.event.drag.js');
}
_jquery_carousel_settings_format($settings);
drupal_add_js(array(
'jquery_carousel' => array(
$settings['selector'] => $settings,
),
), 'setting');
}
/**
* Implements hook_carousel_theme_info().
*/
function jquery_carousel_carousel_theme_info() {
$themes = array();
$themes['default'] = array(
'title' => t('Default'),
'file' => 'jcarousel-themes/default/jquery-carousel-default.css',
);
$themes['glass'] = array(
'title' => t('Glass'),
'file' => 'jcarousel-themes/glass/jquery-carousel-glass.css',
);
return $themes;
}
/**
* Retrieves a list of all available jQuery Carousel themes.
*/
function jquery_carousel_themes() {
$themes =& drupal_static(__FUNCTION__);
// Don't rebuild if we already have a list of themes.
if (isset($themes)) {
return $themes;
}
// Build a list of themes from other modules.
$themes = array();
foreach (module_implements('carousel_theme_info') as $module) {
$function = $module . '_carousel_theme_info';
$module_themes = $function();
foreach ($module_themes as $key => $theme) {
$theme['module'] = $module;
$theme['file path'] = isset($theme['file path']) ? $theme['file path'] : drupal_get_path('module', $module);
$theme['title'] = isset($theme['title']) ? $theme['title'] : $key;
$themes[$key] = $theme;
}
}
ksort($themes);
return $themes;
}