You are here

function labjs_advagg_modify_js_pre_render_alter in LABjs 7

Implements hook_advagg_modify_js_pre_render_alter().

File

./labjs.module, line 233
LABjs module

Code

function labjs_advagg_modify_js_pre_render_alter(&$children, &$elements) {
  if (labjs_suppress()) {
    return;
  }

  // Detect if $LAB.setGlobalDefaults is used in this scope. If it is, then set
  // $init_encountered to FALSE so the LABjs loader code doesn't get LAB-ed. If
  // it is not detected, assume the LABjs loader code will be found in another
  // scope.
  foreach ($children as $key => &$values) {

    // Do not LAB.js anything until "$LAB.setGlobalDefaults" has been seen.
    if (!empty($values['#value']) && strpos($values['#value'], '$LAB.setGlobalDefaults')) {
      $init_encountered = TRUE;
      break;
    }
  }
  if (!empty($init_encountered)) {
    $init_encountered = FALSE;
  }
  else {
    $init_encountered = TRUE;
  }
  $labjs_ready_key = FALSE;
  $drupal_settings_key = FALSE;
  foreach ($children as $key => &$values) {

    // Do not LAB.js anything until "$LAB.setGlobalDefaults" has been seen.
    if (!$init_encountered && !empty($values['#value']) && strpos($values['#value'], '$LAB.setGlobalDefaults')) {
      $init_encountered = TRUE;
    }
    if (!$init_encountered) {
      continue;
    }

    // Inline JS.
    if (!empty($values['#value']) && strpos($values['#value'], LABJS_EXCLUDE) === FALSE) {
      if (!_labjs_fix_inline($values['#value'])) {
        continue;
      }
    }

    // JS src files.
    if (!empty($values['#attributes']['src'])) {
      $values['#value'] = "\n" . LABJS_EXCLUDE . "\n" . '$L = $L.script(["' . $values['#attributes']['src'] . '"]);';
      $values['#value_prefix'] = "\n" . '<!--//--><![CDATA[//><!--';
      $values['#value_suffix'] = "\n" . '//--><!]]>';
      $values['#attributes']['src'] = NULL;
      unset($values['#attributes']['src']);
    }
    if (!empty($values['#value'])) {

      // LAB.js ready script key value.
      if (strpos($values['#value'], 'Drupal.scriptsready=true;jQuery(document).trigger("scripts-ready")') !== FALSE) {
        $labjs_ready_key = $key;
      }

      // Drupal.settings script key value.
      if (strpos($values['#value'], 'jQuery.extend(Drupal.settings') !== FALSE) {
        $drupal_settings_key = $key;
      }
    }
  }

  // If settings is in the footer, make sure trigger("scripts-ready") happens
  // after Drupal.settings has been loaded.
  if ($labjs_ready_key !== FALSE && $drupal_settings_key !== FALSE && $labjs_ready_key < $drupal_settings_key) {
    $settings = $children[$drupal_settings_key];
    $children[$drupal_settings_key] = $children[$labjs_ready_key];
    $children[$labjs_ready_key] = $settings;
  }
}