View source
<?php
define('LABJS_EXCLUDE', '// LABjs exclusion');
function labjs_init() {
drupal_add_js(labjs_get_path(), array(
'group' => JS_LIBRARY - 10,
'weight' => -50,
'every_page' => 1,
'preprocess' => FALSE,
'inline' => TRUE,
));
if (module_exists('advagg')) {
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,
));
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,
));
}
}
function labjs_menu() {
$items = array();
$file_path = drupal_get_path('module', 'labjs') . '/includes';
$items['admin/config/development/labjs'] = array(
'title' => 'LABjs',
'description' => 'Configure the settings used to wrap JS blocks.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'labjs_admin_settings_form',
),
'access arguments' => array(
'administer site configuration',
),
'file path' => $file_path,
'file' => 'labjs.admin.inc',
);
return $items;
}
function labjs_module_implements_alter(&$implementations, $hook) {
if ($hook === 'advagg_modify_js_pre_render_alter' && array_key_exists('labjs', $implementations)) {
$item = array(
'labjs' => $implementations['labjs'],
);
unset($implementations['labjs']);
$implementations = array_merge($item, $implementations);
}
}
function labjs_js_alter(&$javascript) {
if (labjs_suppress()) {
return;
}
$javascript['misc/drupal.js']['data'] = drupal_get_path('module', 'labjs') . '/replace/drupal.js';
if (isset($javascript['modules/overlay/overlay-parent.js'])) {
$javascript['modules/overlay/overlay-parent.js']['data'] = drupal_get_path('module', 'labjs') . '/replace/overlay-parent.js';
}
if (module_exists('advagg')) {
return;
}
$scripts = array();
$files = array();
$preprocess_js = variable_get('preprocess_js', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update');
$index = 1;
uasort($javascript, 'drupal_sort_css_js');
$setting['ajaxPageState']['js'] = array_fill_keys(array_keys($javascript), 1);
unset($setting['ajaxPageState']['js']['settings']);
drupal_add_js($setting, 'setting');
foreach ($javascript as $key => $item) {
if (empty($item['inline'])) {
if (!isset($item['type']) || $item['type'] == 'file' || $item['type'] == 'external') {
if ($item['type'] == 'external' || !$item['preprocess'] || !$preprocess_js) {
$src = $item['type'] == 'external' ? $item['data'] : file_create_url($item['data']);
$scripts[$item['scope']][$index++] = $src;
}
else {
$filekey = 'aggregate_' . $item['group'] . '_' . $item['every_page'] . '_' . $index;
$scripts[$item['scope']][$filekey] = '';
$files[$item['scope']][$filekey][$item['data']] = $item;
}
unset($javascript[$key]);
}
}
elseif ($item['type'] == 'inline') {
$javascript[$key]['data'] = LABJS_EXCLUDE . "\n" . $javascript[$key]['data'];
}
}
if ($preprocess_js && count($files) > 0) {
foreach ($files as $scope => $items) {
foreach ($items as $key => $file_set) {
$uri = drupal_build_js_cache($file_set);
if ($uri) {
$scripts[$scope][$key] = file_create_url($uri);
}
}
}
}
$base = array(
'defer' => FALSE,
'type' => 'inline',
'group' => JS_LIBRARY,
'every_page' => 1,
);
$javascript['labjs--init'] = $base + array(
'scope' => 'header',
'data' => LABJS_EXCLUDE . "\nvar \$L = \$LAB.setGlobalDefaults({AlwaysPreserveOrder:true});",
'weight' => -20,
);
$javascript['labjs--init']['group']--;
$javascript['labjs--execute'] = $base + array(
'scope' => 'footer',
'data' => LABJS_EXCLUDE . "\n\$L = \$L.wait(function() {Drupal.scriptsready=true;jQuery(document).trigger('scripts-ready');});",
'weight' => 20,
);
foreach ($scripts as $scope => $items) {
if (!$items) {
continue;
}
$javascript['labjs-' . $scope] = $base + array(
'scope' => $scope,
'data' => LABJS_EXCLUDE . "\n\$L = \$L.script([\"" . implode("\",\n\"", $items) . "\"]);",
'weight' => 10,
);
}
}
function _labjs_fix_inline(&$value) {
if (strpos($value, 'rpxnow.com') !== FALSE || strpos($value, 'fbcdn.net') !== FALSE) {
return FALSE;
}
$value = str_replace('var _gaq = _gaq || [];', 'if (typeof(_gaq)=="undefined") _gaq=[];', $value);
$value = str_replace('var _paq = _paq || [];', 'if (typeof(_paq)=="undefined") _paq=[];', $value);
$value = "\$L = \$L.wait(function() {\n" . $value . "\n});";
return TRUE;
}
function labjs_preprocess_html_tag(&$variables) {
if (labjs_suppress()) {
return;
}
if (module_exists('advagg')) {
return;
}
if ($variables['element']['#tag'] == 'script' && !empty($variables['element']['#value']) && strpos($variables['element']['#value'], LABJS_EXCLUDE) !== 0) {
_labjs_fix_inline($variables['element']['#value']);
}
}
function labjs_advagg_modify_js_pre_render_alter(&$children, &$elements) {
if (labjs_suppress()) {
return;
}
foreach ($children as $key => &$values) {
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) {
if (!$init_encountered && !empty($values['#value']) && strpos($values['#value'], '$LAB.setGlobalDefaults')) {
$init_encountered = TRUE;
}
if (!$init_encountered) {
continue;
}
if (!empty($values['#value']) && strpos($values['#value'], LABJS_EXCLUDE) === FALSE) {
if (!_labjs_fix_inline($values['#value'])) {
continue;
}
}
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'])) {
if (strpos($values['#value'], 'Drupal.scriptsready=true;jQuery(document).trigger("scripts-ready")') !== FALSE) {
$labjs_ready_key = $key;
}
if (strpos($values['#value'], 'jQuery.extend(Drupal.settings') !== FALSE) {
$drupal_settings_key = $key;
}
}
}
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;
}
}
function labjs_advagg_js_groups_alter(&$js_groups, $preprocess_js) {
if (!$preprocess_js) {
return;
}
$labjs_location = labjs_get_path();
foreach ($js_groups as &$data) {
foreach ($data['items'] as &$values) {
if ($values['data'] == $labjs_location) {
$values['preprocess'] = FALSE;
$data['preprocess'] = FALSE;
break 2;
}
}
}
}
function labjs_get_path() {
return drupal_get_path('module', 'labjs') . '/labjs.min.js';
}
function labjs_suppress($set = FALSE) {
static $suppress;
if ($set) {
$suppress = TRUE;
}
elseif (!isset($suppress)) {
$suppress = !variable_get('labjs_enabled', TRUE) || defined('MAINTENANCE_MODE');
if (!$suppress && ($pages = variable_get('labjs_pages_list', ''))) {
$path = drupal_get_path_alias($_GET['q']);
$page_match = drupal_match_path($path, $pages);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $pages);
}
$suppress = (variable_get('labjs_pages_choice', 0) xor $page_match);
}
}
return $suppress;
}
function labjs_page_delivery_callback_alter(&$callback) {
if (module_exists('overlay') && overlay_display_empty_page() && !labjs_suppress()) {
$callback = 'labjs_deliver_empty_page';
}
}
function labjs_deliver_empty_page() {
$empty_page = '<html><head><title></title>' . drupal_get_css() . drupal_get_js() . '</head><body class="overlay"><script>$L = $L.wait(function() {Drupal.scriptsready=true;jQuery(document).trigger("scripts-ready");});</script></body></html>';
print $empty_page;
drupal_exit();
}