You are here

tabs.module in Javascript Tools 5

API for creating tabbed pages.

File

tabs/tabs.module
View source
<?php

/**
 * @file
 * API for creating tabbed pages.
 */

/**
 * Implementation of hook_menu().
 */
function tabs_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/tabs',
      'title' => t('Tabs'),
      'description' => t('Configuration for tabs'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'tabs_admin_settings',
      ),
    );
  }
  else {
    tabs_load();
  }
  return $items;
}

/**
 * Menu callback for admin settings.
 */
function tabs_admin_settings() {
  $form = array();
  $form['jstools_history_remote'] = array(
    '#type' => 'radios',
    '#title' => t('History plugin'),
    '#description' => t('Load the history-remote plugin, which ties tab switching to browser back and forward buttons. The plugin can cause errors in combination with certain other Javascript functionality. Disable to avoid such errors. Note that disabling this setting will affect other modules if you use them: Active Search and Dynamicload.'),
    '#default_value' => variable_get('jstools_history_remote', 0),
    '#options' => array(
      t('disabled'),
      t('enabled'),
    ),
  );
  $form['tabs_slide'] = array(
    '#type' => 'radios',
    '#title' => t('Slide effect'),
    '#description' => t('Apply slide effect when changing tabs.'),
    '#default_value' => variable_get('tabs_slide', 0),
    '#options' => array(
      t('disabled'),
      t('enabled'),
    ),
  );
  $form['tabs_fade'] = array(
    '#type' => 'radios',
    '#title' => t('Fade effect'),
    '#description' => t('Apply fade effect when changing tabs.'),
    '#default_value' => variable_get('tabs_fade', 0),
    '#options' => array(
      t('disabled'),
      t('enabled'),
    ),
  );
  $form['tabs_speed'] = array(
    '#type' => 'radios',
    '#title' => t('Effect speed'),
    '#description' => t('Speed at which to apply effects.'),
    '#default_value' => variable_get('tabs_speed', 'slow'),
    '#options' => array(
      'slow' => t('slow'),
      'fast' => t('fast'),
    ),
  );
  $form['tabs_auto_height'] = array(
    '#type' => 'radios',
    '#title' => t('Fixed height'),
    '#description' => t('Set all tabs to have the height of the tallest tab. If not enabled, content will adjust to fit when tabs are changed. Note: fixed height is not fully compatible with slide and fade effects.'),
    '#default_value' => variable_get('tabs_auto_height', 0),
    '#options' => array(
      t('disabled'),
      t('enabled'),
    ),
  );
  $form['tabs_navigation'] = array(
    '#type' => 'radios',
    '#title' => t('Navigation buttons'),
    '#description' => t('Enable to add "next" and "previous" buttons to the bottom of all tab sets.'),
    '#default_value' => variable_get('tabs_navigation', 0),
    '#options' => array(
      t('disabled'),
      t('enabled'),
    ),
  );
  $form['tabs_nav_next'] = array(
    '#type' => 'textfield',
    '#title' => t('Next button caption'),
    '#description' => t('The text to be displayed in the \'next\' button.'),
    '#default_value' => variable_get('tabs_nav_next', t('next')),
  );
  $form['tabs_nav_prev'] = array(
    '#type' => 'textfield',
    '#title' => t('Previous button caption'),
    '#description' => t('The text to be displayed in the \'previous\' button.'),
    '#default_value' => variable_get('tabs_nav_prev', t('previous')),
  );
  $form = system_settings_form($form);
  return $form;
}

/**
 * Process a tabset prior to rendering.
 */
function tabs_process_tabset($element) {
  static $names = array();

  // Ensure name is unique.
  $i = 0;

  // Assign a name, reading from the first parent (the key of this tabset element).
  $name = $element['#tabset_name'] = $element['#tabset_name'] ? $element['#tabset_name'] : (isset($element['#parents']) && count($element['#parents']) ? $element['#parents'][0] : 'tabset');

  // In case we have duplicate names...
  while (in_array($element['#tabset_name'], $names)) {
    $element['#tabset_name'] = $name . $i;
    $i++;
  }
  $names[] = $element['#tabset_name'];

  // Add class.
  if (!isset($element['#attributes'])) {
    $element['#attributes'] = array();
  }
  $element['#attributes']['class'] = (isset($element['#attributes']['class']) ? $element['#attributes']['class'] . ' ' : '') . 'drupal-tabs' . (isset($element['#tabs_navigation']) && $element['#tabs_navigation'] ? ' tabs-navigation' : '');
  $index = 1;

  // Sort the elements by weight.
  // Because uasort doesn't keep the original order for equal values,
  // we assign a nominal weight to all elements.
  foreach (element_children($element) as $count => $key) {
    if (!isset($element[$key]['#weight']) || $element[$key]['#weight'] == 0) {
      $element[$key]['#weight'] = $count / 1000;
    }
  }
  uasort($element, '_element_sort');
  foreach (element_children($element) as $key) {
    if (isset($element[$key]['#type']) && $element[$key]['#type'] == 'tabpage') {

      // Display any #description before the #content.
      $element[$key]['#content'] = ($element[$key]['#description'] ? '<div class="description">' . $element[$key]['#description'] . '</div>' : '') . $element[$key]['#content'];
      $element[$key]['#tabset_name'] = $element['#tabset_name'];
      $element[$key]['#index'] = $index++;
    }
  }
  return $element;
}

/**
 * Return rendered tabset.
 *
 * @themable
 */
function theme_tabset($element) {
  $output = '<div id="tabs-' . $element['#tabset_name'] . '"' . drupal_attributes($element['#attributes']) . '>';
  $output .= '<ul class="anchors">';
  foreach (element_children($element) as $key) {
    if ($element[$key]['#type'] && $element[$key]['#type'] == 'tabpage') {
      $output .= '<li' . drupal_attributes($element[$key]['#attributes']) . '><a href="#tabs-' . $element['#tabset_name'] . '-' . $element[$key]['#index'] . '">' . $element[$key]['#title'] . '</a></li>';
    }
  }
  $output .= '</ul>';
  $output .= $element['#children'];
  $output .= '</div>';
  return $output;
}

/**
 * Return rendered content of a tab.
 *
 * @themable
 */
function theme_tabpage($element) {
  $output = '<div id="tabs-' . $element['#tabset_name'] . '-' . $element['#index'] . '" class="fragment">';
  $output .= '<h2 class="drupal-tabs-title">' . $element['#title'] . '</h2>';
  $output .= $element['#content'] . $element['#children'];
  $output .= '</div>';
  return $output;
}

/**
 * Add required js and css files.
 */
function tabs_load() {
  static $loaded = FALSE;
  if (!$loaded) {
    $path = drupal_get_path('module', 'tabs');
    jstools_add_js(array(
      $path . '/jquery.tabs.pack.js',
      $path . '/tabs.js',
    ));
    if (variable_get('jstools_history_remote', 0)) {
      drupal_add_js(drupal_get_path('module', 'jstools') . '/jquery.history_remote.pack.js');
    }
    drupal_add_js(array(
      'tabs' => array(
        'slide' => (bool) variable_get('tabs_slide', 0),
        'fade' => (bool) variable_get('tabs_fade', 0),
        'speed' => variable_get('tabs_speed', 'slow'),
        'auto_height' => (bool) variable_get('tabs_auto_height', 0),
        'next_text' => variable_get('tabs_nav_next', t('next')),
        'previous_text' => variable_get('tabs_nav_prev', t('previous')),
      ),
    ), 'setting');
    drupal_add_css($path . '/drupal-tabs.css');
    drupal_add_css($path . '/tabs.css');
    drupal_set_html_head('
      <!-- Additional IE/Win specific style sheet (Conditional Comments) -->
      <!--[if IE]>
      <style type="text/css" media="all">@import "' . base_path() . $path . '/tabs-ie.css";</style>
      <![endif]-->
    ');
    $loaded = TRUE;
  }
}

/**
 * Render a tabset 'manually' (when not rendering as part of a regular form render).
 */
function tabs_render($form) {
  return drupal_render(form_builder('tabset', $form));
}

/**
 * Implementation of hook_elements().
 */
function tabs_elements() {
  $type = array();
  $type['tabset'] = array(
    '#tabs_navigation' => variable_get('tabs_navigation', 0) ? TRUE : FALSE,
    '#process' => array(
      'tabs_process_tabset' => array(),
    ),
  );
  $type['tabpage'] = array(
    '#content' => '',
  );
  return $type;
}

Functions

Namesort descending Description
tabs_admin_settings Menu callback for admin settings.
tabs_elements Implementation of hook_elements().
tabs_load Add required js and css files.
tabs_menu Implementation of hook_menu().
tabs_process_tabset Process a tabset prior to rendering.
tabs_render Render a tabset 'manually' (when not rendering as part of a regular form render).
theme_tabpage Return rendered content of a tab.
theme_tabset Return rendered tabset.