You are here

fancybox.module in fancyBox 6

Same filename and directory in other branches
  1. 5 fancybox.module
  2. 7.2 fancybox.module
  3. 7 fancybox.module

Provides the Fancybox jQuery plugin for displaying images, HTML content and multimedia in an elegant box.

Authors: Joakim Stai <http://drupal.org/user/88701> (Drupal 6 version) Nicola Sanino <http://drupal.org/user/72237> (Drupal 5 version)

Sponsors: NodeOne <http://nodeone.se> Sony Ericsson <http://labs.sonyericsson.com>

File

fancybox.module
View source
<?php

/**
 * @file
 * Provides the Fancybox jQuery plugin for displaying images, HTML content and
 * multimedia in an elegant box.
 *
 * Authors:
 *   Joakim Stai <http://drupal.org/user/88701> (Drupal 6 version)
 *   Nicola Sanino <http://drupal.org/user/72237> (Drupal 5 version)
 *
 * Sponsors:
 *   NodeOne <http://nodeone.se>
 *   Sony Ericsson <http://labs.sonyericsson.com>
 */
define('FANCYBOX_DEFAULT_PATH', 'sites/all/libraries/fancybox');

/**
 * Implementation of hook_init().
 */
function fancybox_init() {
  $settings = variable_get('fancybox_settings', array());
  if (!empty($settings) && fancybox_active($settings)) {
    fancybox_activate($settings);
  }
}

/**
 * Include the Fancybox JS.
 */
function fancybox_activate($settings = NULL) {
  static $active = FALSE;
  if (!$active) {
    if ($settings == NULL) {
      $settings = variable_get('fancybox_settings', array());
    }

    // Add Fancybox files.
    $path = variable_get('fancybox_path', FANCYBOX_DEFAULT_PATH) . '/';
    $files = variable_get('fancybox_files', array());

    // TODO: add easing

    //drupal_add_js($path .'jquery.easing-1.3.pack.js');
    drupal_add_css($path . $files['css']);
    drupal_add_js($path . $files['js']);

    // Expose settings to the DOM.
    $exposed_settings['options'] = $settings['options'];
    $exposed_settings['selector'] = $settings['activation']['selector'];
    drupal_add_js(array(
      'fancybox' => $exposed_settings,
    ), 'setting');

    // Initialize Fancybox.
    drupal_add_js(drupal_get_path('module', 'fancybox') . '/js/fancybox.js');
    $active = TRUE;
  }
}

/**
 * Implementation of hook_perm().
 */
function fancybox_perm() {
  return array(
    'administer fancybox',
  );
}

/**
 * Implementation of hook_menu().
 */
function fancybox_menu() {
  $items = array();
  $base = array(
    'access arguments' => array(
      'administer fancybox',
    ),
    'file' => 'fancybox.admin.inc',
  );
  $items['admin/settings/fancybox'] = $base + array(
    'title' => 'Fancybox',
    'description' => 'Configure the settings for Fancybox.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'fancybox_admin_settings_form',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function fancybox_theme() {
  $theme = array();
  $base = array(
    'file' => 'fancybox.theme.inc',
  );
  $theme['imagefield_image_imagecache_fancybox'] = $base + array(
    'arguments' => array(
      'namespace' => NULL,
      'field' => NULL,
      'path' => NULL,
      'alt' => NULL,
      'title' => NULL,
      'gid' => NULL,
      'attributes' => NULL,
    ),
  );
  if (function_exists('imagecache_presets') && module_exists('imagefield')) {
    foreach (imagecache_presets() as $preset) {
      $theme['fancybox_formatter_' . $preset['presetname'] . '|fancybox'] = $base + array(
        'function' => 'theme_fancybox_formatter_imagefield',
        'arguments' => array(
          'element' => NULL,
        ),
      );
    }
  }

  /* emvideo field support */
  $theme['fancybox_formatter_emvideo_fancybox_video'] = array(
    'arguments' => array(
      'element' => NULL,
    ),
    'file' => 'fancybox.theme.inc',
  );
  $theme['fancybox_emvideo'] = array(
    'arguments' => array(
      'field' => NULL,
      'item' => NULL,
      'formatter' => NULL,
      'node' => NULL,
    ),
    'file' => 'fancybox.theme.inc',
  );
  return $theme;
}

/**
 * Check whether Fancybox should be initialized for the current URL.
 */
function fancybox_active($settings) {

  // If PHP is selected, evaluate it.
  if ($settings['activation']['activation_type'] == 'php') {
    return drupal_eval($settings['activation']['activation_pages']);
  }

  // Compare defined paths with the current path.
  $path = drupal_get_path_alias($_GET['q']);
  $page_match = drupal_match_path($path, $settings['activation']['activation_pages']);
  if ($path != $_GET['q']) {
    $page_match = $page_match || drupal_match_path($_GET['q'], $settings['activation']['activation_pages']);
  }

  // Decide whether to include or exclude pages.
  if ($settings['activation']['activation_type'] == 'exclude') {
    return !$page_match;
  }
  else {
    return $page_match;
  }
}

/**
 * Define form elements for the Fancybox plugin's options.
 */
function fancybox_options($values = array()) {

  /*
    Fancybox 1.3.1 options:

    TODO: autoDimensions 	true 	For inline and ajax views, resizes the view to the element recieves. Make sure it has dimensions otherwise this will give unexpected results
    TODO: modal 	false 	When true, 'overlayShow' is set to 'true' and 'hideOnOverlayClick', 'hideOnContentClick', 'enableEscapeButton', 'showCloseButton' are set to 'false'
    TODO: ajax 	{ } 	Ajax options (error, success will be overwritten)
    TODO: swf 	{wmode: 'transparent'} 	Flashvars to put on the swf object
    TODO: titleFormat 	null 	Callback to customize title area. You can set any html - custom image counter or even custom navigation
  */
  $easing_options = array(
    'none' => t('None'),
    'swing' => t('Swing'),
    'easeInQuad' => t('Quad in'),
    'easeOutQuad' => t('Quad out'),
    'easeInOutQuad' => t('Quad in/out'),
    'easeInCubic' => t('Cubic in'),
    'easeOutCubic' => t('Cubic out'),
    'easeInOutCubic' => t('Cubic in/out'),
    'easeInQuart' => t('Quart in'),
    'easeOutQuart' => t('Quart out'),
    'easeInOutQuart' => t('Quart in/out'),
    'easeInQuint' => t('Quint in'),
    'easeOutQuint' => t('Quint out'),
    'easeInOutQuint' => t('Quint in/out'),
    'easeInSine' => t('Sine in'),
    'easeOutSine' => t('Sine out'),
    'easeInOutSine' => t('Sine in/out'),
    'easeInExpo' => t('Expo in'),
    'easeOutExpo' => t('Expo out'),
    'easeInOutExpo' => t('Expo in/out'),
    'easeInCirc' => t('Circular in'),
    'easeOutCirc' => t('Circular out'),
    'easeInOutCirc' => t('Circular in/out'),
    'easeInElastic' => t('Elastic in'),
    'easeOutElastic' => t('Elastic out'),
    'easeInOutElastic' => t('Elastic in/out'),
    'easeInBack' => t('Back in'),
    'easeOutBack' => t('Back out'),
    'easeInOutBack' => t('Back in/out'),
    'easeInBounce' => t('Bounce in'),
    'easeOutBounce' => t('Bounce out'),
    'easeInOutBounce' => t('Bounce in/out'),
  );
  for ($i = 0; $i <= 10; $i++) {
    $float = (double) $i / 10;
    $opacity_options[(string) $float] = $i * 10 . '%';
  }
  $options = array();
  $options['appearance'] = array(
    '#type' => 'fieldset',
    '#title' => t('Appearance'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $options['appearance']['width'] = array(
    '#type' => 'textfield',
    '#title' => t('Width'),
    '#description' => t("Width for content types 'iframe' and 'swf'. Also set for inline content if 'autoDimensions' is set to 'false'"),
    '#default_value' => isset($values['width']) ? $values['width'] : 425,
    '#size' => 5,
  );
  $options['appearance']['height'] = array(
    '#type' => 'textfield',
    '#title' => t('Height'),
    '#description' => t("Height for content types 'iframe' and 'swf'. Also set for inline content if 'autoDimensions' is set to 'false'"),
    '#default_value' => isset($values['height']) ? $values['height'] : 355,
    '#size' => 5,
  );
  $options['appearance']['padding'] = array(
    '#type' => 'textfield',
    '#title' => t('Padding'),
    '#description' => t('Space between FancyBox wrapper and content'),
    '#default_value' => isset($values['padding']) ? $values['padding'] : 10,
    '#size' => 5,
  );
  $options['appearance']['margin'] = array(
    '#type' => 'textfield',
    '#title' => t('Margin'),
    '#description' => t('Space between viewport and FancyBox wrapper'),
    '#default_value' => isset($values['margin']) ? $values['margin'] : 20,
    '#size' => 5,
  );
  $options['appearance']['titleShow'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show title'),
    '#description' => t('If checked, shows the title.'),
    '#default_value' => isset($values['titleShow']) ? $values['titleShow'] : TRUE,
  );
  $options['appearance']['titlePosition'] = array(
    '#type' => 'select',
    '#title' => t('Title position'),
    '#description' => t('The position of title.'),
    '#options' => array(
      'outside' => t('Outside'),
      'inside' => t('Inside'),
      'over' => t('Over'),
    ),
    '#default_value' => isset($values['titlePosition']) ? $values['titlePosition'] : 'outside',
  );
  $options['appearance']['scrolling'] = array(
    '#type' => 'select',
    '#title' => t('Content scrolling'),
    '#description' => t('Set the overflow CSS property to create or hide scrollbars'),
    '#options' => array(
      'auto' => t('Auto'),
      'yes' => t('Yes'),
      'no' => t('No'),
    ),
    '#default_value' => isset($values['scrolling']) ? $values['scrolling'] : 'auto',
  );
  $options['appearance']['showCloseButton'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show close button'),
    '#description' => t('Toggle close button'),
    '#default_value' => isset($values['showCloseButton']) ? $values['showCloseButton'] : TRUE,
  );
  $options['appearance']['showNavArrows'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show navigation arrows'),
    '#description' => t('Toggle navigation arrows'),
    '#default_value' => isset($values['showNavArrows']) ? $values['showNavArrows'] : TRUE,
  );
  $options['appearance']['cyclic'] = array(
    '#type' => 'checkbox',
    '#title' => t('Cyclic'),
    '#description' => t('When checked, galleries will be cyclic, allowing you to keep pressing next/back'),
    '#default_value' => isset($values['cyclic']) ? $values['cyclic'] : FALSE,
  );
  $options['appearance']['opacity'] = array(
    '#type' => 'checkbox',
    '#title' => t('Opacity'),
    '#description' => t('If checked,  transparency of content is changed for elastic transitions'),
    '#default_value' => isset($values['opacity']) ? $values['opacity'] : FALSE,
  );
  $options['appearance']['overlayShow'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show overlay'),
    '#description' => t('If checked, shows the overlay behind the item'),
    '#default_value' => isset($values['overlayShow']) ? $values['overlayShow'] : TRUE,
  );
  $options['appearance']['overlayColor'] = array(
    '#type' => 'textfield',
    '#title' => t('Overlay color'),
    '#description' => t('Color of the overlay (i.e. #666666)'),
    '#default_value' => isset($values['overlayColor']) ? $values['overlayColor'] : '#666',
    '#size' => 8,
  );
  $options['appearance']['overlayOpacity'] = array(
    '#type' => 'select',
    '#title' => t('Overlay opacity'),
    '#description' => t('Opacity of overlay (from 0% to 100%)'),
    '#options' => $opacity_options,
    '#default_value' => isset($values['overlayOpacity']) ? $values['overlayOpacity'] : 0.3,
  );
  $options['appearance']['hideOnOverlayClick'] = array(
    '#type' => 'checkbox',
    '#title' => t('Close on overlay click'),
    '#description' => t('If checked, clicking the overlay closes FancyBox'),
    '#default_value' => isset($values['hideOnOverlayClick']) ? $values['hideOnOverlayClick'] : FALSE,
  );
  $options['appearance']['hideOnContentClick'] = array(
    '#type' => 'checkbox',
    '#title' => t('Close on content click'),
    '#description' => t('If checked, clicking the content closes FancyBox'),
    '#default_value' => isset($values['hideOnContentClick']) ? $values['hideOnContentClick'] : TRUE,
  );
  $options['appearance']['enableEscapeButton'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable Escape button'),
    '#description' => t('If checked, pressing Esc button closes FancyBox'),
    '#default_value' => isset($values['enableEscapeButton']) ? $values['enableEscapeButton'] : TRUE,
  );
  $options['appearance']['centerOnScroll'] = array(
    '#type' => 'checkbox',
    '#title' => t('Keep centered when scrolling'),
    '#description' => t('If checked, item stays centered when user scrolls the page'),
    '#default_value' => isset($values['centerOnScroll']) ? $values['centerOnScroll'] : TRUE,
  );
  $options['appearance']['autoScale'] = array(
    '#type' => 'checkbox',
    '#title' => t('Scale images'),
    '#description' => t('If checked, FancyBox is scaled to fit in viewport'),
    '#default_value' => isset($values['autoScale']) ? $values['autoScale'] : TRUE,
  );
  $options['effects'] = array(
    '#type' => 'fieldset',
    '#title' => t('Effects'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $options['effects']['easingIn'] = array(
    '#type' => 'select',
    '#title' => t('Easing effect on open'),
    '#description' => t('Easing effect used when opening an item.'),
    '#options' => $easing_options,
    '#default_value' => isset($values['easingIn']) ? $values['easingIn'] : 'swing',
  );
  $options['effects']['easingOut'] = array(
    '#type' => 'select',
    '#title' => t('Easing effect on close'),
    '#description' => t('Easing effect used when closing an item.'),
    '#options' => $easing_options,
    '#default_value' => isset($values['easingOut']) ? $values['easingOut'] : 'swing',
  );
  $options['effects']['changeFade'] = array(
    '#type' => 'select',
    '#title' => t('Change fade'),
    '#description' => t('Speed of the content fading while changing gallery items'),
    '#options' => array(
      'fast' => t('Fast'),
    ),
    '#default_value' => isset($values['changeFade']) ? $values['changeFade'] : 'fast',
  );
  $options['effects']['changeSpeed'] = array(
    '#type' => 'textfield',
    '#title' => t('Change speed'),
    '#description' => t('Speed of resizing when changing gallery items, in milliseconds'),
    '#default_value' => isset($values['changeSpeed']) ? $values['changeSpeed'] : 300,
    '#size' => 8,
  );
  $options['effects']['transitionIn'] = array(
    '#type' => 'select',
    '#title' => t('Transition in'),
    '#description' => t('The transition type'),
    '#options' => array(
      'elastic' => t('Elastic'),
      'fade' => t('Fade'),
      'none' => t('None'),
    ),
    '#default_value' => isset($values['transitionIn']) ? $values['transitionIn'] : 'fade',
  );
  $options['effects']['speedIn'] = array(
    '#type' => 'textfield',
    '#title' => t('Transition in speed'),
    '#description' => t('Speed of the fade and elastic transitions, in milliseconds'),
    '#default_value' => isset($values['speedIn']) ? $values['speedIn'] : 300,
    '#size' => 8,
  );
  $options['effects']['transitionOut'] = array(
    '#type' => 'select',
    '#title' => t('Transition out'),
    '#description' => t('The transition type'),
    '#options' => array(
      'elastic' => t('Elastic'),
      'fade' => t('Fade'),
      'none' => t('None'),
    ),
    '#default_value' => isset($values['transitionOut']) ? $values['transitionOut'] : 'fade',
  );
  $options['effects']['speedOut'] = array(
    '#type' => 'textfield',
    '#title' => t('Transition out speed'),
    '#description' => t('Speed of the fade and elastic transitions, in milliseconds'),
    '#default_value' => isset($values['speedOut']) ? $values['speedOut'] : 300,
    '#size' => 8,
  );
  $options['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $options['advanced']['onStart'] = array(
    '#type' => 'textfield',
    '#title' => t('Callback on start'),
    '#description' => t('Enter the name of a JavaScript function to be called right before attempting to load the content.'),
    '#default_value' => isset($values['onStart']) ? $values['onStart'] : '',
  );
  $options['advanced']['onComplete'] = array(
    '#type' => 'textfield',
    '#title' => t('Callback on open'),
    '#description' => t('Enter the name of a JavaScript function to be called once the content is displayed.'),
    '#default_value' => isset($values['onComplete']) ? $values['onComplete'] : '',
  );
  $options['advanced']['onClosed'] = array(
    '#type' => 'textfield',
    '#title' => t('Callback on close'),
    '#description' => t('Enter the name of a JavaScript function to be called once FancyBox is closed.'),
    '#default_value' => isset($values['onClosed']) ? $values['onClosed'] : '',
  );
  $options['advanced']['onCleanup'] = array(
    '#type' => 'textfield',
    '#title' => t('Callback on cleanup'),
    '#description' => t('Enter the name of a JavaScript function to be called just before closing.'),
    '#default_value' => isset($values['onCleanup']) ? $values['onCleanup'] : '',
  );
  $options['advanced']['onCancel'] = array(
    '#type' => 'textfield',
    '#title' => t('Callback on cancel'),
    '#description' => t('Enter the name of a JavaScript function to be called after loading is canceled.'),
    '#default_value' => isset($values['onCancel']) ? $values['onCancel'] : '',
  );
  return $options;
}

/*****************************************************************************
 * INTEGRATION WITH THIRD PARTY MODULES
 *****************************************************************************/

/**
 * Implementation of hook_field_formatter_info().
 *
 * Add Fancybox and ImageCache formatters to CCK image fields if Imagefield and
 * ImageCache modules are enabled.
 */
function fancybox_field_formatter_info() {
  $formatters = array();
  if (module_exists('imagecache')) {
    $presets = array();
    foreach (imagecache_presets() as $preset) {
      $presets[$preset['presetname'] . '|fancybox'] = array(
        'label' => t('Fancybox: @preset image', array(
          '@preset' => $preset['presetname'],
        )),
        'field types' => array(
          'image',
          'filefield',
        ),
      );
    }
    if (is_array($presets)) {
      $formatters = array_merge($presets);
    }
  }
  if (module_exists('emvideo')) {
    $formatters['emvideo_fancybox_video'] = array(
      'label' => t('Fancybox: Image Thumbnail -> Full Size Video'),
      'field types' => array(
        'emvideo',
      ),
    );
  }
  return $formatters;
}

/**
 * Implementation of hook_insert_styles().
 */
function fancybox_insert_styles() {
  $insert_styles = array();
  if (module_exists('imagecache')) {
    foreach (imagecache_presets() as $preset) {
      $insert_styles[$preset['presetname'] . '|fancybox'] = array(
        'label' => t('Fancybox: @preset image', array(
          '@preset' => $preset['presetname'],
        )),
      );
    }
  }
  return $insert_styles;
}

/**
 * Implementation of hook_insert_content().
 */
function fancybox_insert_content($item, $style, $widget) {
  return fancybox_imagefield_image_imagecache(NULL, $item, $style['name'], NULL);
}

/**
 * Implementation of hook_field_formatter().
 */
function fancybox_imagefield_image_imagecache($field, $item, $formatter, $node) {
  $settings = variable_get('fancybox_settings', array());
  if ($settings['imagefield']['use_list_field'] && $item['list'] == 0) {
    return null;
  }
  if (!isset($item['filepath']) && isset($item['fid'])) {
    $file = field_file_load($item['fid']);
    $item['filepath'] = $file['filepath'];
  }

  // Image caption.
  $item_data = $item['data'];
  $image_title = $item_data['description'];
  $image_title = !empty($image_title) ? $image_title : $item_data['title'];
  $image_title = !empty($image_title) ? $image_title : $item_data['alt'];
  if ($settings['imagefield']['use_node_title']) {
    $image_title = $node->title;
  }
  $loop_item = '';
  $nid = $item['nid'] ? $item['nid'] : ($node->nid ? $node->nid : '');
  switch ($settings['imagefield']['grouping']) {
    case 1:
      $loop_item = $nid . '-' . $field;
      break;
    case 2:
      $loop_item = 'all';
      break;
  }
  list($namespace, $presetname) = explode('|', $formatter, 2);
  if ($preset = imagecache_preset_by_name($namespace)) {
    return theme('imagefield_image_imagecache_fancybox', $namespace, $field, $item['filepath'], $image_title, $loop_item);
  }
}

Functions

Namesort descending Description
fancybox_activate Include the Fancybox JS.
fancybox_active Check whether Fancybox should be initialized for the current URL.
fancybox_field_formatter_info Implementation of hook_field_formatter_info().
fancybox_imagefield_image_imagecache Implementation of hook_field_formatter().
fancybox_init Implementation of hook_init().
fancybox_insert_content Implementation of hook_insert_content().
fancybox_insert_styles Implementation of hook_insert_styles().
fancybox_menu Implementation of hook_menu().
fancybox_options Define form elements for the Fancybox plugin's options.
fancybox_perm Implementation of hook_perm().
fancybox_theme Implementation of hook_theme().

Constants

Namesort descending Description
FANCYBOX_DEFAULT_PATH @file Provides the Fancybox jQuery plugin for displaying images, HTML content and multimedia in an elegant box.