You are here

function _async_js_build_urls in Asynchronous JavaScript 8

Same name and namespace in other branches
  1. 7 async_js.module \_async_js_build_urls()

Prepares script data for actual requests.

Parameters

array $scripts: An array of scripts as returned by async_js_add_js().

See also

async_js_js_alter()

async_js_add_js()

1 call to _async_js_build_urls()
async_js_js_alter in ./async_js.module
Implements hook_js_alter().

File

./async_js.module, line 127
Queue JavaScript files to be loaded asynchronously.

Code

function _async_js_build_urls(array $scripts = array()) {

  // Initialize variables necessary for building the file url.
  $default_query_string = variable_get('css_js_query_string', '0');
  $js_version_string = variable_get('drupal_js_version_query_string', 'v=');

  // Keep track of any changes we make so we can reference them later when
  // dealing with dependencies and dependents.
  $changes = array();

  // Build the urls.
  foreach ($scripts as &$script) {
    if ($script['type'] != 'external') {
      $old_data = $script['data'];
      $query_string = empty($script['version']) ? $default_query_string : $js_version_string . $script['version'];
      $query_string_separator = strpos($script['data'], '?') !== FALSE ? '&' : '?';
      $script['data'] = file_create_url($script['data']) . $query_string_separator . ($script['cache'] ? $query_string : REQUEST_TIME);
      $changes[$old_data] = $script['data'];
    }
  }

  // Handle potential dependencies and dependents.
  foreach ($scripts as &$script) {

    // Handle potential dependencies.
    if (!empty($script['async_dependencies'])) {
      foreach ($script['async_dependencies'] as $key => $dependency) {
        if (!empty($changes[$dependency])) {
          $script['async_dependencies'][$key] = $changes[$dependency];
        }
      }
    }

    // Handle potential dependents.
    if (!empty($script['async_dependents'])) {
      foreach ($script['async_dependents'] as $key => $dependent) {
        if (!empty($changes[$dependent])) {
          $script['async_dependents'][$key] = $changes[$dependent];
        }
      }
    }
  }
  return $scripts;
}