You are here

function better_statistics_add_js_api in Better Statistics 7

Add the Better Statistics JS API file to the current request.

Return value

TRUE if the BS JS API file was added successfully to the request, or FALSE if the API file was not added to the request.

1 call to better_statistics_add_js_api()
better_statistics_preprocess_html in ./better_statistics.module
Implements hook_preprocess_html().

File

./better_statistics.module, line 438
Drupal hook implementations for the Better Statistics module.

Code

function better_statistics_add_js_api() {
  static $js_added = FALSE;
  $file =& drupal_static(__FUNCTION__, FALSE);
  $methods = variable_get('better_statistics_methods', array());

  // No need to run this if no methods have been added.
  if (!empty($methods)) {

    // If the file URI isn't yet known, attempt to build the file.
    if (!$file) {

      // Prepend the BS JS API init file to the methods array.
      $bs_path = drupal_get_path('module', 'better_statistics');
      array_unshift($methods, $bs_path . '/js/better_statistics.init.js');

      // Append the BS JS API exec file to the methods array.
      $methods[] = $bs_path . '/js/better_statistics.exec.js';

      // Prepare all files for use in drupal_build_js_cache().
      $js_files = array();
      foreach ($methods as $file) {
        $js_files[$file] = array(
          'preprocess' => TRUE,
          'data' => $file,
        );
      }

      // Build the JS cache file.
      $uri = drupal_build_js_cache($js_files);

      // If the file was created successfully, generate the URL.
      if ($uri) {
        $file = file_create_url($uri);
      }
    }

    // If the JS hasn't been added yet, add it to the request.
    if (!$js_added) {
      $bs = variable_get('better_statistics_bs_var', 'bs');
      $js = preg_replace('!\\s+!', ' ', "(function(w,d,s,u,v,a,m) {w['BetterStatsObj'] = v;\n        w[v] = w[v] ||\n        function() {(w[v].q = w[v].q || []).push(arguments)};\n        w[v]('set', 'i', 1 * new Date());\n        w[v]('set', 'basePath', '" . base_path() . "');\n        a=d.createElement(s),\n        m=d.getElementsByTagName(s)[0];\n        a.src=u;\n        a.async=1;\n        m.parentNode.insertBefore(a,m);})(window,document,'script','" . $file . "','" . $bs . "');");
      drupal_add_js($js, array(
        'type' => 'inline',
        'scope' => 'header',
        'group' => JS_LIBRARY,
        'weight' => -20,
      ));
      $js_added = TRUE;
    }
  }
  return $js_added;
}