You are here

function date_popup_js_settings_id in Date 7.2

Same name and namespace in other branches
  1. 6.2 date_popup/date_popup.module \date_popup_js_settings_id()
  2. 7.3 date_popup/date_popup.module \date_popup_js_settings_id()
  3. 7 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

string $id: The CSS class prefix to search the DOM for. @todo unused ?

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

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

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

File

date_popup/date_popup.module, line 123
A module to enable jQuery calendar and time entry popups.

Code

function date_popup_js_settings_id($id, $func, array $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.
    $default_date = $parts[0] > 0 || 0 > $parts[1] ? $parts[0] : 0;
    $settings += array(
      'defaultDate' => (string) $default_date . '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-" . $id_count[$id]++;
  $js_settings['datePopup'][$return_id] = array(
    'func' => $func,
    'settings' => $settings,
  );
  drupal_add_js($js_settings, 'setting');
  return $return_id;
}