You are here

jquery_ui_filter.module in jQuery UI filter 6

Same filename and directory in other branches
  1. 7 jquery_ui_filter.module

Converts static HTML to a jQuery UI accordian or tabs widget.

File

jquery_ui_filter.module
View source
<?php

/**
 * @file
 * Converts static HTML to a jQuery UI accordian or tabs widget.
 */

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

  // Add css
  if ($jquery_ui_css = variable_get('jquery_ui_filter_css', jquery_ui_get_path() . '/themes/base/jquery-ui.css')) {
    drupal_add_css($jquery_ui_css);
  }

  // Add script and settings.
  drupal_add_js(drupal_get_path('module', 'jquery_ui_filter') . '/jquery_ui_filter.js');
  $settings = array(
    'jQueryUiFilter' => array(
      'disabled' => drupal_match_path($_GET['q'], variable_get('jquery_ui_filter_disabled_pages', 'print*')) ? 1 : 0,
    ),
  );
  drupal_add_js($settings, 'setting');
}

/**
 * Implementation of hook_menu().
 */
function jquery_ui_filter_menu() {
  $items = array();

  // Menu
  $items['admin/settings/jquery_ui_filter'] = array(
    'title' => 'jQuery UI filter',
    'description' => 'Configure global settings for the jQuery UI filter.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'jquery_ui_filter_general_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'jquery_ui_filter.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );

  // General
  $items['admin/settings/jquery_ui_filter/settings'] = array(
    'title' => 'General',
    'description' => 'Configure general settings for the jQuery UI filter.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'jquery_ui_filter_general_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'jquery_ui_filter.admin.inc',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -1,
  );

  // Demo
  $items['admin/settings/jquery_ui_filter/demo'] = array(
    'title' => 'Demo',
    'description' => 'Demo accordion, dialog, and tabs settings for the jQuery UI filter.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'jquery_ui_filter_demo_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'jquery_ui_filter.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );
  return $items;
}

////////////////////////////////////////////////////////////////////////////////

// Shared widget functions

////////////////////////////////////////////////////////////////////////////////

/**
 * Jquery UI filter settings callback.
 */
function _jquery_ui_filter_settings_callback($name) {
  $t_args = array(
    '@href' => url('admin/settings/jquery_ui_filter/' . $name),
    '@name' => $name,
  );
  $form = array();
  $form['jquery_ui_filter_' . $name] = array(
    '#type' => 'fieldset',
    '#title' => t('jQuery UI @name', $t_args),
    '#description' => t('To configure this filter, please goto the global jQuery UI <a href="@href">@name</a> configuration form.', $t_args),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  return $form;
}

/**
 * Jquery UI filter process replacer: Inserts jQuery UI widget classes into text.
 */
function _jquery_ui_filter_process_replacer($type, $html, $collapsed = FALSE) {

  // Add jquery ui filter classes and wrap content in a container <div>.
  $header_tag = variable_get("jquery_ui_filter_{$type}_header_tag", 'h3');
  if (preg_match_all("#(<{$header_tag}[^>]*>)(.*?)(</{$header_tag}>)(.*?)(?=<{$header_tag}|\$)#s", $html, $matches)) {
    foreach ($matches[0] as $index => $match) {
      $header_open_tag = $matches[1][$index];
      $header_attributes = _jquery_ui_filter_parse_tag_attributes($header_open_tag);
      $header_text = $matches[2][$index];
      $header_close_tag = $matches[3][$index];
      $content = $matches[4][$index];
      $header_attributes['class'] .= "jquery-ui-filter-header jquery-ui-filter-{$type}-header";
      $replace_with = '<' . $header_tag . drupal_attributes($header_attributes) . '>' . $header_text . $header_close_tag . '<div class="jquery-ui-filter-container jquery-ui-filter-' . $type . '-container">' . $content . '</div>';
      $html = str_replace($matches[0][$index], $replace_with, $html);
    }
  }

  // Build jquery ui filter widget wrapper.
  $classes = array(
    'jquery-ui-filter',
    'jquery-ui-filter-' . $type,
  );
  if ($collapsed) {
    $classes[] = 'jquery-ui-filter-' . $type . '-collapsed';
  }
  $attributes = array(
    'id' => form_clean_id('jquery-ui-filter'),
    'class' => implode(' ', $classes),
  );
  return '<div' . drupal_attributes($attributes) . '>' . $html . '</div>';
}

/**
 * Parses a tag's attributes into an associated array.
 */
function _jquery_ui_filter_parse_tag_attributes($tag) {
  preg_match_all('/(\\w+)\\s*=\\s*"([^"]+)"/', $tag, $matches);
  $attributes = array(
    'class' => '',
  );

  // Always include class attribute.
  for ($i = 0, $len = count($matches[1]); $i < $len; $i++) {
    $attributes[$matches[1][$i]] = htmlspecialchars_decode($matches[2][$i], ENT_QUOTES);
  }
  return $attributes;
}

/**
 * Custom array merge function that merge multidimensional arrays.
 */
function _jquery_ui_filter_array_merge($destination, $source) {
  foreach ($source as $key => $value) {
    if (isset($destination[$key]) && is_array($destination[$key]) && is_array($source[$key])) {
      $destination[$key] = _jquery_ui_filter_array_merge($destination[$key], $source[$key]);
    }
    else {
      $destination[$key] = $source[$key];
    }
  }
  return $destination;
}

Functions

Namesort descending Description
jquery_ui_filter_init Implementation of hook_init().
jquery_ui_filter_menu Implementation of hook_menu().
_jquery_ui_filter_array_merge Custom array merge function that merge multidimensional arrays.
_jquery_ui_filter_parse_tag_attributes Parses a tag's attributes into an associated array.
_jquery_ui_filter_process_replacer Jquery UI filter process replacer: Inserts jQuery UI widget classes into text.
_jquery_ui_filter_settings_callback Jquery UI filter settings callback.