You are here

jquerymobile.module in jQuery Mobile module 7.2

Same filename and directory in other branches
  1. 6.3 jquerymobile.module
  2. 7 jquerymobile.module

File

jquerymobile.module
View source
<?php

/**
 * @file
 * Provides the jQuery Mobile library to modules and themes that request it.
 */
require_once drupal_get_path('module', 'jquerymobile') . '/jquerymobile.inc';

/**
 * Implements hook_init().
 */
function jquerymobile_init() {
  foreach (module_list() as $module) {
    if (is_file(drupal_get_path('module', 'jquerymobile') . '/modules/' . str_replace('_', '-', $module) . '.inc')) {
      require_once drupal_get_path('module', 'jquerymobile') . '/modules/' . str_replace('_', '-', $module) . '.inc';
    }
  }
}

/**
 * Implements hook_menu().
 */
function jquerymobile_menu() {
  $items = array();
  $items['admin/config/jquerymobile'] = array(
    'title' => 'jQuery Mobile',
    'description' => 'jQuery Mobile tools.',
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
  );
  $items['admin/config/jquerymobile/settings'] = array(
    'title' => 'jQuery Mobile',
    'description' => 'Administer jQuery Mobile Settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'jquerymobile_admin_settings',
    ),
    'access arguments' => array(
      'manage jquerymobile settings',
    ),
    'file' => 'jquerymobile.admin.inc',
    'weight' => 0,
  );

  // Theme configuration subtabs.
  $items['admin/config/jquerymobile/settings/global'] = array(
    'title' => 'Global settings',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 20,
  );
  foreach (list_themes() as $theme) {
    if ($theme->status && _jquerymobile_is_mobile_theme($theme->name)) {
      $items['admin/config/jquerymobile/settings/' . $theme->name] = array(
        'type' => MENU_LOCAL_TASK,
        'title' => $theme->info['name'],
        'description' => 'Administer jQuery Mobile Settings.',
        'page callback' => 'drupal_get_form',
        'page arguments' => array(
          'jquerymobile_admin_local_settings',
          $theme->name,
        ),
        'access callback' => '_jquerymobile_access_theme',
        'access arguments' => array(
          $theme,
        ),
        'file' => 'jquerymobile.admin.inc',
      );
    }
  }
  return $items;
}

/**
 * Implements hook_menu_alter().
 */
function jquerymobile_menu_alter(&$items) {
  $items['node']['page callback'] = 'jquerymobile_page_default';
}

/**
 * Implements of hook_help().
 */
function jquerymobile_help($path, $arg) {
  switch ($path) {
    case 'admin/help#jquerymobile':
    case 'admin/config/jquerymobile/settings':
      return t('The jquerymobile module allows you to set HTML attributes on the element of individual menu items, exposing certain values needed to enhance the <a href="http://drupal.org/project/mobile_jquery">mobile jquery</a> theme.');
  }
}

/**
 * Implements hook_permission().
 */
function jquerymobile_permission() {
  return array(
    'manage jquerymobile settings' => array(
      'title' => 'Manage jQuery Mobile Settings',
    ),
    'manage jquerymobile files settings' => array(
      'title' => 'Manage jQuery Mobile Files Settings',
    ),
    'manage jquerymobile theme settings' => array(
      'title' => 'Manage jQuery Mobile Theme Settings',
    ),
    'manage jquerymobile advanced settings' => array(
      'title' => 'Manage jQuery Mobile Advanced Settings',
    ),
  );
}

/**
 * Implements hook_library().
 */
function jquerymobile_library() {
  $libraries = array();
  $min = variable_get('jquerymobile_minify', FALSE) ? '.min' : '';
  $path = variable_get('jquerymobile_library_path', 'sites/all/libraries');
  switch (variable_get('jquerymobile_server_type', 'local')) {
    case 'hosted':
      $jquery_path = 'http://code.jquery.com';
      $jqm_path = 'http://code.jquery.com/mobile/' . variable_get('jquerymobile_jquerymobile_version', '1.0.1');
      $option = 'external';
      break;
    case 'other':
      $jqm_path = variable_get('jquerymobile_jqm_files_path', NULL);
      $jquery_path = variable_get('jquerymobile_jquery_files_path', NULL);
      $option = 'external';
      break;
    default:
      $jqm_path = $path . '/jquery.mobile-' . variable_get('jquerymobile_jquerymobile_version', '1.0.1');
      $jquery_path = $path . '/jquery-' . variable_get('jquerymobile_jquery_version', '1.6.4');
      $option = 'file';
      break;
  }
  $libraries['jquerymobile'] = array(
    'title' => 'jQuery Mobile',
    'website' => 'http://jquerymobile.com/',
    'version' => variable_get('jquerymobile_jquerymobile_version', '1.0.1'),
    'js' => array(
      $jqm_path . '/jquery.mobile-' . variable_get('jquerymobile_jquerymobile_version', '1.0.1') . $min . '.js' => array(
        'group' => JS_THEME,
        'weight' => 102,
        'type' => $option,
      ),
    ),
    'css' => array(
      $jqm_path . '/jquery.mobile-' . variable_get('jquerymobile_jquerymobile_version', '1.0.1') . $min . '.css' => array(
        'weight' => 101,
        'type' => $option,
        'group' => CSS_THEME,
        'every_page' => TRUE,
        'media' => 'all',
        'preprocess' => FALSE,
      ),
    ),
  );
  $libraries['jquerymobile.custom'] = array(
    'title' => 'jQuery Mobile',
    'website' => 'http://jquerymobile.com/',
    'version' => variable_get('jquerymobile_jquerymobile_version', '1.0.1'),
    'js' => array(
      $jqm_path . '/jquery.mobile-' . variable_get('jquerymobile_jquerymobile_version', '1.0.1') . $min . '.js' => array(
        'group' => JS_THEME,
        'weight' => 102,
        'type' => $option,
      ),
    ),
    'css' => array(
      $jqm_path . '/jquery.mobile.structure-' . variable_get('jquerymobile_jquerymobile_version', '1.0.1') . $min . '.css' => array(
        'weight' => 101,
        'type' => $option,
        'group' => CSS_THEME,
        'every_page' => TRUE,
        'media' => 'all',
        'preprocess' => FALSE,
      ),
    ),
  );
  $libraries['jquerymobile.jquery'] = array(
    'title' => 'jQuery',
    'website' => 'http://code.jquery.com/',
    'version' => variable_get('jquerymobile_jquery_version', '1.6.4'),
    'js' => array(
      $jquery_path . '/jquery-' . variable_get('jquerymobile_jquery_version', '1.6.4') . $min . '.js' => array(
        'group' => JS_THEME,
        'weight' => 101,
        'type' => $option,
        'scope' => 'header',
        'every_page' => TRUE,
        'preprocess' => TRUE,
        'cache' => TRUE,
        'defer' => FALSE,
      ),
    ),
  );
  return $libraries;
}

/**
 * Implements hook_element_info_alter().
 */
function jquerymobile_element_info_alter(&$type) {
  global $theme_key;
  if (_jquerymobile_is_mobile_theme($theme_key)) {

    // Add a custom pre_render function to checkboxes elements.
    if (isset($type['checkboxes']['#pre_render'])) {
      $type['checkboxes']['#pre_render'][] = 'jquerymobile_pre_render_checkboxes';
    }
    else {
      $type['checkboxes']['#pre_render'] = array(
        'jquerymobile_pre_render_checkboxes',
      );
    }

    // Add default #multiple value to checkboxes as false.
    if (!isset($type['checkbox']['#multiple'])) {
      $type['checkbox']['#multiple'] = FALSE;
    }

    // Add a new process function to actions type.
    // Add a custom pre_render function to checkboxes elements.
    if (isset($type['actions']['#process'])) {
      $type['actions']['#process'][] = 'jquerymobile_process_actions';
    }
    else {
      $type['actions']['#process'] = array(
        'jquerymobile_process_actions',
      );
    }
    if (isset($type['text_format'])) {
      $type['text_format']['#process'][] = 'jquerymobile_process_text_format';
    }
  }
}

/**
 * Implements hook_entity_info_alter().
 */
function jquerymobile_entity_info_alter(&$entity_info) {
  $entity_info['node']['view modes']['jquerymobile_teaser'] = array(
    'label' => t('jQM teaser'),
    'custom settings' => TRUE,
  );
}

/**
 * Prerender the checkboxes.
 *
 * @todo: make flip switches work
 */
function jquerymobile_pre_render_checkboxes($element) {
  global $theme_key;
  if (_jquerymobile_is_mobile_theme($theme_key)) {
    $count = count($element['#options']);
    if ($count != 1) {
      foreach ($element['#options'] as $key => $choice) {
        $element[$key]['#multiple'] = TRUE;
      }
    }
  }
  return $element;
}

/**
 * Implements hook_process_actions().
 */
function jquerymobile_process_actions($element) {
  global $theme_key;
  if (_jquerymobile_is_mobile_theme($theme_key)) {
    $number_letters = array(
      1 => 'a',
      2 => 'b',
      3 => 'c',
      4 => 'd',
      5 => 'e',
    );
    $children = element_children($element);
    $count = count($children) - 1;
    if ($count) {
      $element['#attributes']['class'][] = 'ui-grid-' . $number_letters[$count];
      $grid_block_count = 1;
      foreach ($children as $key) {
        $element[$key]['#prefix'] = '<div class="ui-block-' . $number_letters[$grid_block_count] . '">';
        $element[$key]['#suffix'] = '</div>';
        $grid_block_count++;
      }
    }
  }
  return $element;
}

/**
 * Implements hook_process_text_format().
 */
function jquerymobile_process_text_format($element) {
  global $theme_key;
  if (_jquerymobile_is_mobile_theme($theme_key)) {
    $element['format']['#prefix'] = '<div data-role="collapsible" data-collapsed="true"><h3>' . t('Text Format') . '</h3>';
    $element['format']['#suffix'] = '</div>';
    $element['format']['help']['#weight'] = 100;

    // Remove element title since it is added above.
    $element['format']['format']['#title'] = NULL;
  }
  return $element;
}

/**
 * Implements template_preprocess_link().
 *
 * Enable additonal attributes for link elements.
 */
function jquerymobile_preprocess_link(&$variables) {

  // Check if the link needs processing.
  if (isset($variables['options']['jquerymobile']['button']) && TRUE === isset($variables['options']['jquerymobile']['button'])) {
    _jquerymobile_button_settings($variables['options']['attributes'], $variables['options']['jquerymobile']);
  }
}

/**
 * Implements template_preprocess_button().
 *
 * Enable additional attribute for button elements.
 */
function jquerymobile_preprocess_button(&$variables) {
  _jquerymobile_button_settings($variables['element']['attributes'], $variables['element'], '#');
}

/**
 * List of available icons.
 *
 * Values are saved in the form 'position'  => array(<attribute settings>)
 * They are saved this way to simplify sending the appropriate options
 * to drupal_attributes() or you can use the top level keys as the value to
 * pass along with data-iconpos
 *
 * For example: top => array('data-iconpos' => 'top')
 *
 * @return array
 *   Returns an array of icon positions
 */
function jquerymobile_icon_position_options() {
  return array(
    'top' => array(
      'data-iconpos' => 'top',
    ),
    'left' => array(
      'data-iconpos' => 'left',
    ),
    'right' => array(
      'data-iconpos' => 'right',
    ),
    'bottom' => array(
      'data-iconpos' => 'bottom',
    ),
    'notext' => array(
      'data-iconpos' => 'notext',
    ),
  );
}

/**
 * List of available icons.
 *
 * @return array
 *   Returns an array of icon names
 */
function jquerymobile_icon_options() {
  return array(
    'arrow-l' => 'arrow-l',
    'arrow-r' => 'arrow-r',
    'arrow-u' => 'arrow-u',
    'arrow-d' => 'arrow-d',
    'delete' => 'delete',
    'plus' => 'plus',
    'minus' => 'minus',
    'check' => 'check',
    'gear' => 'gear',
    'refresh' => 'refresh',
    'forward' => 'forward',
    'back' => 'back',
    'grid' => 'grid',
    'star' => 'star',
    'alert' => 'alert',
    'info' => 'info',
    'home' => 'home',
    'search' => 'search',
  );
}

/**
 * Generate the attributes array for the given button settings.
 *
 * @param array &$attributes
 *   Reference to the attributes array
 * @param array &$settings
 *   Reference to the settings array
 * @param string $prefix
 *   (optional) Prefix character for options in the settings array
 *   Valid values are '' and '#'.
 */
function _jquerymobile_button_settings(&$attributes, &$settings, $prefix = '') {

  // Assign the prefix the proper value if not blank.
  if (!empty($prefix)) {
    $prefix = '#';
  }

  // Add the button role.
  if (isset($settings[$prefix . 'button']) && $settings[$prefix . 'button'] === TRUE) {
    $attributes['data-role'] = 'button';
  }

  // Add the rounded corner option.
  if (isset($settings[$prefix . 'corners'])) {
    $attributes['data-corners'] = $settings[$prefix . 'corners'] ? 'true' : 'false';
  }

  // Add an icon and any supplementary options.
  if (isset($settings[$prefix . 'icon'])) {

    // Validate the icon option.
    $icons = jquerymobile_icon_options();
    if (isset($icons[$settings[$prefix . 'icon']])) {

      // Assign the icon.
      $attributes['data-icon'] = $icons[$settings[$prefix . 'icon']];

      // Check for any other icon options.
      // Set icon position.
      if (isset($settings[$prefix . 'icon_position'])) {

        // Validate the icon position.
        $positions = jquerymobile_icon_position_options();
        if (isset($positions[$settings[$prefix . 'icon_position']])) {
          $attributes['data-iconpos'] = $settings[$prefix . 'icon_position'];
        }
      }

      // Set icon shadow.
      if (isset($settings[$prefix . 'icon_shadow'])) {
        $attributes['data-iconshadow'] = $settings[$prefix . 'icon_shadow'] ? 'true' : 'false';
      }
    }
  }

  // Mark the button to be inline.
  if (isset($settings[$prefix . 'inline'])) {
    $attributes['data-inline'] = $settings[$prefix . 'inline'] ? 'true' : 'false';
  }

  // Add the shadow property.
  if (isset($settings[$prefix . 'shadow'])) {
    $attributes['data-shadow'] = $settings[$prefix . 'shadow'] ? 'true' : 'false';
  }
  if (isset($settings[$prefix . 'swatch'])) {

    // Validate the option.
    if (ctype_alpha($settings[$prefix . 'swatch']) && strlen($settings[$prefix . 'swatch']) == 1) {
      $attributes['data-theme'] = drupal_strtolower($settings[$prefix . 'swatch']);
    }
  }
}

Functions

Namesort descending Description
jquerymobile_element_info_alter Implements hook_element_info_alter().
jquerymobile_entity_info_alter Implements hook_entity_info_alter().
jquerymobile_help Implements of hook_help().
jquerymobile_icon_options List of available icons.
jquerymobile_icon_position_options List of available icons.
jquerymobile_init Implements hook_init().
jquerymobile_library Implements hook_library().
jquerymobile_menu Implements hook_menu().
jquerymobile_menu_alter Implements hook_menu_alter().
jquerymobile_permission Implements hook_permission().
jquerymobile_preprocess_button Implements template_preprocess_button().
jquerymobile_preprocess_link Implements template_preprocess_link().
jquerymobile_pre_render_checkboxes Prerender the checkboxes.
jquerymobile_process_actions Implements hook_process_actions().
jquerymobile_process_text_format Implements hook_process_text_format().
_jquerymobile_button_settings Generate the attributes array for the given button settings.