You are here

function date_popup_js_settings_id in Date 6.2

Same name and namespace in other branches
  1. 7.3 date_popup/date_popup.module \date_popup_js_settings_id()
  2. 7 date_popup/date_popup.module \date_popup_js_settings_id()
  3. 7.2 date_popup/date_popup.module \date_popup_js_settings_id()

Create a unique CSS id name and output a single inline JS block for each startup function to call and settings array to pass it. This used to create a unique CSS class for each unique combination of function and settings, but using classes requires a DOM traversal and is much slower than an id lookup. The new approach returns to requiring a duplicate copy of the settings/code for every element that uses them, but is much faster. We could combine the logic by putting the ids for each unique function/settings combo into Drupal.settings and searching for each listed id.

@returns The CSS id to assign to the element that should have $func($settings) invoked on it.

Parameters

$pfx: The CSS class prefix to search the DOM for. TODO : unused ?

$func: The jQuery function to invoke on each DOM element containing the returned CSS class.

$settings: The settings array to pass to the jQuery function.

2 calls to date_popup_js_settings_id()
date_popup_process_date in date_popup/date_popup.module
Process the date portion of the element.
date_popup_process_time in date_popup/date_popup.module
Process the time portion of the element.

File

date_popup/date_popup.module, line 142
A module to enable jquery calendar and time entry popups. Requires the Date API.

Code

function date_popup_js_settings_id($id, $func, $settings) {
  static $js_added = FALSE;
  static $id_count = array();

  // Make sure popup date selector grid is in correct year.
  if (!empty($settings['yearRange'])) {
    $parts = explode(':', $settings['yearRange']);

    // Set the default date to 0 or the lowest bound if the date ranges do not include the current year
    // Necessary for the datepicker to render and select dates correctly
    $defaultDate = $parts[0] > 0 || 0 > $parts[1] ? $parts[0] : 0;

    // The 1.7 version of datepicker renders the range of year options
    // relative to the drawn year in the popup, and will re-render the options
    // whenever the year changes.
    if (strpos(jquery_ui_get_version(), '1.7') === 0 && ($parts[0] >= 0 || 0 >= $parts[1])) {
      $range = max($parts) - min($parts);
      $defaultDate = $parts[0];
      $settings['yearRange'] = '-' . $range . ':' . '+' . $range;
    }
    $settings += array(
      'defaultDate' => (string) $defaultDate . 'y',
    );
  }
  if (!$js_added) {
    drupal_add_js(drupal_get_path('module', 'date_popup') . '/date_popup.js');
    $js_added = TRUE;
  }

  // We use a static array to account for possible multiple form_builder()
  // calls in the same request (form instance on 'Preview').
  if (!isset($id_count[$id])) {
    $id_count[$id] = 0;
  }

  // It looks like we need the additional id_count for this to
  // work correctly when there are multiple values.
  //  $return_id = "$id-$func-popup";
  $return_id = "{$id}-{$func}-popup-" . $id_count[$id]++;
  $js_settings['datePopup'][$return_id] = array(
    'func' => $func,
    'settings' => $settings,
  );
  drupal_add_js($js_settings, 'setting');
  return $return_id;
}