You are here

function magic_group_js in Magic 7.2

Same name and namespace in other branches
  1. 7 includes/scripts-experimental.inc \magic_group_js()

Default callback to group JavaScript items.

This function arranges the JavaScript items that are in the #items property of the scripts element into groups. When aggregation is enabled, files within a group are aggregated into a single file, significantly improving page loading performance by minimizing network traffic overhead.

This function puts multiple items into the same group if they are groupable and if they are for the same browsers. Items of the 'file' type are groupable if their 'preprocess' flag is TRUE. Items of the 'inline', 'settings', or 'external' type are not groupable.

This function also ensures that the process of grouping items does not change their relative order. This requirement may result in multiple groups for the same type and browsers, if needed to accommodate other items in between.

Parameters

$javascript: An array of JavaScript items, as returned by drupal_add_js(), but after alteration performed by drupal_get_js().

Return value

An array of JavaScript groups. Each group contains the same keys (e.g., 'data', etc.) as a JavaScript item from the $javascript parameter, with the value of each key applying to the group as a whole. Each group also contains an 'items' key, which is the subset of items from $javascript that are in the group.

See also

drupal_pre_render_scripts()

File

includes/scripts-experimental.inc, line 441
A file to contain functions for the magic module to abuse.

Code

function magic_group_js($javascript) {
  $groups = array();

  // If a group can contain multiple items, we track the information that must
  // be the same for each item in the group, so that when we iterate the next
  // item, we can determine if it can be put into the current group, or if a
  // new group needs to be made for it.
  $current_group_keys = NULL;
  $index = -1;
  $ordering_array = array();
  foreach ($javascript as &$item) {
    $item['browsers'] = isset($item['browsers']) ? $item['browsers'] : array();
    $item['browsers'] += array(
      'IE',
    );

    // As these are not native in Drupal, we add in the default values.
    $item += array(
      'defer' => FALSE,
      'async' => FALSE,
      'type' => '',
    );

    // The browsers for which the JavaScript item needs to be loaded is part of
    // the information that determines when a new group is needed, but the order
    // of keys in the array doesn't matter, and we don't want a new group if all
    // that's different is that order.
    ksort($item['browsers']);

    // We are first going to ensure that all the javascript is in the right
    // order.
    $group = $item['group'];
    $weight = $item['weight'];

    // We are chaging the weight of the files, so that ones flagged with
    // every_page are put first.
    $weight += $item['every_page'] ? -10000 : 10000;
    $grouped_items[$group][] = $item;
    $ordering_array[$group][] = $weight;
  }

  // This will sort all the groups to ensure we are in the right order.
  ksort($grouped_items);
  ksort($ordering_array);
  foreach ($grouped_items as $group => $items) {

    // Now we go into each group, and sort by the individual weight. This will
    // also take into effect the every_page tag.
    array_multisort($ordering_array[$group], SORT_NUMERIC, $items);
    foreach ($items as $item) {
      switch ($item['type']) {
        case 'file':

          // Group file items if their 'preprocess' flag is TRUE.
          // Help ensure maximum reuse of aggregate files by only grouping
          // together items that share the same 'group' value and 'every_page'
          // flag. See drupal_add_js() for details about that.
          $group_keys = !empty($item['preprocess']) ? array(
            $item['every_page'],
            $item['browsers'],
            $item['defer'],
          ) : FALSE;
          break;
        case 'external':
        case 'inline':
        case 'setting':
        default:

          // Do not group external, settings, and inline items.
          $group_keys = FALSE;
          break;
      }

      // If the group keys don't match the most recent group we're working with,
      // then a new group must be made. Also, each inline, external, or setting
      // script will also be in their own group.
      if (!empty($group_keys) && $group_keys !== $current_group_keys || $group_keys == FALSE) {
        $index++;

        // We set the base settings to the first object, for reference later on.
        $groups[$index] = $item;
        $current_group_keys = $group_keys;
      }
      $groups[$index]['items'][] = $item;
    }
  }
  return $groups;
}