You are here

function labjs_js_alter in LABjs 7

Implements hook_js_alter().

File

./labjs.module, line 82
LABjs module

Code

function labjs_js_alter(&$javascript) {
  if (labjs_suppress()) {
    return;
  }

  // Replaces the original misc/drupal.js with a new one, without using
  // $(document).ready(...);
  $javascript['misc/drupal.js']['data'] = drupal_get_path('module', 'labjs') . '/replace/drupal.js';

  // Overlay thinks that DOM is ready when it is loaded.
  // With LABjs enabled, it is no longer correct. We need to patch it, too.
  if (isset($javascript['modules/overlay/overlay-parent.js'])) {
    $javascript['modules/overlay/overlay-parent.js']['data'] = drupal_get_path('module', 'labjs') . '/replace/overlay-parent.js';
  }
  if (module_exists('advagg')) {
    return;
  }
  $scripts = array();
  $files = array();
  $preprocess_js = variable_get('preprocess_js', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update');

  // The index counter is used to keep aggregated and non-aggregated files in
  // order by weight.
  $index = 1;

  // Sorts the scripts into correct order
  // Drupal assigns different weight for each scripts, thus we can't determine
  // if two scripts can be executed in parallel. However, they are all loaded in
  // parallel.
  uasort($javascript, 'drupal_sort_css_js');

  // Provide the page with information about the individual JavaScript files
  // used, information not otherwise available when aggregation is enabled.
  $setting['ajaxPageState']['js'] = array_fill_keys(array_keys($javascript), 1);
  unset($setting['ajaxPageState']['js']['settings']);
  drupal_add_js($setting, 'setting');
  foreach ($javascript as $key => $item) {
    if (empty($item['inline'])) {
      if (!isset($item['type']) || $item['type'] == 'file' || $item['type'] == 'external') {
        if ($item['type'] == 'external' || !$item['preprocess'] || !$preprocess_js) {
          $src = $item['type'] == 'external' ? $item['data'] : file_create_url($item['data']);
          $scripts[$item['scope']][$index++] = $src;
        }
        else {
          $filekey = 'aggregate_' . $item['group'] . '_' . $item['every_page'] . '_' . $index;
          $scripts[$item['scope']][$filekey] = '';
          $files[$item['scope']][$filekey][$item['data']] = $item;
        }
        unset($javascript[$key]);
      }
    }
    elseif ($item['type'] == 'inline') {
      $javascript[$key]['data'] = LABJS_EXCLUDE . "\n" . $javascript[$key]['data'];
    }
  }

  // Aggregates any remaining JS files
  if ($preprocess_js && count($files) > 0) {
    foreach ($files as $scope => $items) {
      foreach ($items as $key => $file_set) {
        $uri = drupal_build_js_cache($file_set);

        // Only include the file if was written successfully. Errors are logged
        // using watchdog.
        if ($uri) {
          $scripts[$scope][$key] = file_create_url($uri);
        }
      }
    }
  }

  // Adds the JS function call
  $base = array(
    'defer' => FALSE,
    'type' => 'inline',
    'group' => JS_LIBRARY,
    'every_page' => 1,
  );
  $javascript['labjs--init'] = $base + array(
    'scope' => 'header',
    'data' => LABJS_EXCLUDE . "\nvar \$L = \$LAB.setGlobalDefaults({AlwaysPreserveOrder:true});",
    'weight' => -20,
  );

  // Makes Google Analytics work.
  $javascript['labjs--init']['group']--;
  $javascript['labjs--execute'] = $base + array(
    'scope' => 'footer',
    'data' => LABJS_EXCLUDE . "\n\$L = \$L.wait(function() {Drupal.scriptsready=true;jQuery(document).trigger('scripts-ready');});",
    'weight' => 20,
  );
  foreach ($scripts as $scope => $items) {
    if (!$items) {
      continue;
    }
    $javascript['labjs-' . $scope] = $base + array(
      'scope' => $scope,
      'data' => LABJS_EXCLUDE . "\n\$L = \$L.script([\"" . implode("\",\n\"", $items) . "\"]);",
      'weight' => 10,
    );
  }
}