function advagg_mod_js_alter

nittany7 advagg_mod.module advagg_mod_js_alter(&$js)
cis7 advagg_mod.module advagg_mod_js_alter(&$js)
mooc7 advagg_mod.module advagg_mod_js_alter(&$js)

Implements hook_js_alter().

File

modules/contrib/advagg/advagg_mod/advagg_mod.module, line 104
Advanced aggregation modifier module.

Code

function advagg_mod_js_alter(&$js) {
  if (module_exists('advagg') && !advagg_enabled()) {
    return;
  }

  // Only add JS if it's actually needed.
  if (variable_get('advagg_mod_js_remove_unused', ADVAGG_MOD_JS_REMOVE_UNUSED)) {
    advagg_remove_js_if_not_used($js);
  }

  // Move all JS to the footer.
  $move_js_to_footer = variable_get('advagg_mod_js_footer', ADVAGG_MOD_JS_FOOTER);
  if (!empty($move_js_to_footer)) {
    foreach ($js as $name => &$values) {
      if ($move_js_to_footer == 1 && $values['group'] <= JS_LIBRARY) {
        continue;
      }
      // If JS is not in the header increase group by 1000.
      if ($values['scope'] != 'header') {
        $values['group'] += 1000;
      }
      // If JS is already in the footer increase group by 1000.
      if ($values['scope'] == 'footer') {
        $values['group'] += 1000;
      }
      $values['scope'] = 'footer';
    }
  }

  // Do not use preprocessing if JS is inlined.
  // Do not use defer if JS is inlined.
  if (advagg_mod_inline_page()) {
    advagg_mod_inline_js($js);
    return;
  }

  // Force all JS to be preprocessed.
  if (variable_get('advagg_mod_js_preprocess', ADVAGG_MOD_JS_PREPROCESS)) {
    foreach ($js as $name => &$values) {
      $values['preprocess'] = TRUE;
    }
  }

  // Add the defer tag to all JS.
  if (variable_get('advagg_mod_js_defer', ADVAGG_MOD_JS_DEFER)) {
    foreach ($js as $name => &$values) {
      // Everything is defer.
      $values['defer'] = TRUE;

      if (strpos($name, 'jquery.js') !== FALSE || strpos($name, 'jquery.min.js') !== FALSE) {
        // Do not defer the loading of jquery.js
        $values['defer'] = FALSE;

        // jquery_update fallback.
        if (module_exists('jquery_update')) {
          $values['onload'] = "if (typeof window.init_drupal_jquery_update_fallback === 'function') {init_drupal_jquery_update_fallback();}";
        }
      }
      if (strpos($name, 'jquery-ui.js') !== FALSE || strpos($name, 'jquery-ui.min.js') !== FALSE) {
        // Do not defer the loading of jquery-ui.js
        $values['defer'] = FALSE;

        // jquery_update fallback.
        if (module_exists('jquery_update')) {
          $values['onload'] = "if (typeof window.init_drupal_jquery_ui_update_fallback === 'function') {init_drupal_jquery_ui_update_fallback();}";
        }
      }

      // Drupal settings.
      if ($name == 'misc/drupal.js') {
        $values['onload'] = "if (typeof window.init_drupal_core_settings === 'function') {init_drupal_core_settings();}";
      }

    }
  }
}