You are here

function labjs_preprocess_page in LABjs 6

Implements hook_preprocess_page().

1 string reference to 'labjs_preprocess_page'
labjs_theme_registry_alter in ./labjs.module
Implementation of hook_theme_registry_alter().

File

./labjs.module, line 78
LABjs module

Code

function labjs_preprocess_page(&$variables) {
  if (labjs_suppress() || empty($variables['scripts'])) {
    return;
  }

  // Init some variables.
  $javascript = '';
  $out = '';
  $exception_mode = variable_get('labjs_exception_mode', 'whitelist') == 'whitelist' ? 'whitelist' : 'blacklist';
  $list = explode("\n", str_replace(array(
    "\r\n",
    "\r",
  ), "\n", variable_get('labjs_exception_' . $exception_mode, '')));
  $list = array_flip(array_filter(array_map('trim', $list)));

  // If advagg is enabled, this happens in hook_advagg_js_pre_alter().
  if (!module_exists('advagg') || !variable_get('advagg_enabled', ADVAGG_ENABLED)) {

    // Get an array of all the JavaScript files loaded by Drupal on this page.
    $types = drupal_add_js(NULL, NULL, NULL);
    _labjs_patch_core($types['header']);
    _labjs_patch_core($types['footer']);
    $variables['scripts'] = drupal_get_js('header', $types['header']);
  }

  // If advagg is enabled, this happens in labjs_advagg_js_builder().
  if (!module_exists('advagg') || !variable_get('advagg_enabled', ADVAGG_ENABLED) || isset($_GET['advagg']) && $_GET['advagg'] == -1) {

    // Now everything is ok, wrap JS with LABjs.
    $javascript = labjs_rewrite_js($variables['scripts']);
    $closure = drupal_get_js('footer', $types['footer']);
    $javascript_closure = labjs_rewrite_js($closure);
    $out = _labjs_prepare_required_js();
    $variables['scripts'] = '';
  }
  else {
    $javascript_closure = '';
  }

  // Replace any inline scripts.
  foreach ($variables as $key => &$value) {
    if (!empty($value) && is_string($value) && ($exception_mode == 'whitelist') === isset($list[$key])) {
      labjs_rewrite_js($value);
    }
  }

  // Modify scripts in the header.
  $scripts = _labjs_rewrite_js();
  if (!empty($scripts)) {
    $out .= '<script type="text/javascript">' . "\n" . '<!--//--><![CDATA[//><!--' . "\n";
    $out .= "\$L = \$L.script([" . implode(",\n", $scripts) . "]);\n//--><!]]>\n</script>\n";
  }
  $variables['scripts'] .= $out . $javascript;

  // Add triger to end of closure.
  $variables['closure'] .= $javascript_closure . '<script type="text/javascript">' . "\n" . '<!--//--><![CDATA[//><!--' . "\n" . LABJS_EXCLUDE . "\n" . '$L = $L.wait(function() {Drupal.scriptsready=true;jQuery(document).trigger(\'scripts-ready\');});' . "\n" . "//--><!]]>\n" . "</script>\n";
}