diff --git a/labjs.module b/labjs.module index 4000a2d..b742fe9 100644 --- a/labjs.module +++ b/labjs.module @@ -14,14 +14,32 @@ function labjs_init() { // Loads our loader first // Normally we use JS_LIBRARY, but Google Analytics module uses JS_LIBRARY-1, // then we use JS_LIBRARY-10. We should look for a more proper approach. - drupal_add_js(labjs_get_path(), array('group' => JS_LIBRARY-10, 'weight' => -50, 'every_page' => 1, 'inline' => TRUE, 'preprocess' => FALSE)); + drupal_add_js(labjs_get_path(), array( + 'group' => JS_LIBRARY-10, + 'weight' => -50, + 'every_page' => 1, + 'preprocess' => FALSE, + )); if (module_exists('advagg')) { // Add in LAB.js init function. - drupal_add_js(LABJS_EXCLUDE . "\n" . 'var $L = $LAB.setGlobalDefaults({AlwaysPreserveOrder:true});', array('group' => JS_LIBRARY-10, 'weight' => -49, 'every_page' => 1, 'type' => 'inline', 'preprocess' => TRUE)); + drupal_add_js(LABJS_EXCLUDE . "\n" . 'var $L = $LAB.setGlobalDefaults({AlwaysPreserveOrder:true});', array( + 'group' => JS_LIBRARY-10, + 'weight' => -49, + 'every_page' => 1, + 'type' => 'inline', + 'movable' => FALSE, + )); // Makes Google Analytics work. - drupal_add_js(LABJS_EXCLUDE . "\n" . '$L = $L.wait(function() {Drupal.scriptsready=true;jQuery(document).trigger("scripts-ready");});', array('group' => JS_THEME+101, 'weight' => 20, 'every_page' => 1, 'type' => 'inline', 'preprocess' => TRUE, 'scope' => 'footer')); + drupal_add_js(LABJS_EXCLUDE . "\n" . '$L = $L.wait(function() {Drupal.scriptsready=true;jQuery(document).trigger("scripts-ready");});', array( + 'group' => JS_THEME+101, + 'weight' => 20, + 'every_page' => 1, + 'type' => 'inline', + 'scope' => 'footer', + 'movable' => FALSE, + )); } } @@ -216,7 +234,24 @@ function labjs_advagg_modify_js_pre_render_alter(&$children, &$elements) { return; } - static $init_encountered = FALSE; + // 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) {