/** * Method to load the JavaScript headers * * @param array $headers * @return null */ public function loadJs($headers) { // Dot not load if this is not the right document-class $document = JFactory::getDocument(); if ($document->getType() != 'html') { return false; } // Check whether all scripts are disabled $disable_js = MagebridgeModelConfig::load('disable_js_mage'); if ($disable_js == 'all') { return false; } // Check whether the bridge is offline $offline = MageBridge::getBridge()->isOffline(); if ($offline == true) { return false; } // Initialize the internal array $this->_scripts = array(); // Get system variables $bridge = MageBridge::getBridge(); $html = "<script type=\"text/javascript\">\n" . "//<![CDATA[\n" . "var BLANK_URL = '" . $this->getBaseJsUrl() . "blank.html';\n" . "var BLANK_IMG = '" . $this->getBaseJsUrl() . "spacer.gif';\n" . "//]]>\n" . "</script>\n"; $document->addCustomTag($html); // Load Prototype if ($this->loadPrototype() == true) { $this->_has_prototype = true; } // Loop through all the header-items fetched from Magento if (!empty($headers['items'])) { $jslist = array(); $jstags = array(); foreach ($headers['items'] as $item) { if ($item['type'] == 'skin_js' || $item['type'] == 'js') { if (MageBridgeHelper::jsIsDisabled($item['name']) == true) { continue; } $this->_stylesheets[] = $item['name']; $this->_scripts[] = $item['name']; if (empty($item['name'])) { continue; } // If this is a skin-script, construct the tag but add it later to the HTML-header if ($item['type'] == 'skin_js') { if (!preg_match('/^http/', $item['path'])) { $item['path'] = $bridge->getMagentoUrl() . $item['path']; } $tag = '<script type="text/javascript" src="' . $item['path'] . '"></script>' . "\n"; $jstags[] = $tag; continue; } // If this is a conditional script, construct the tag but add it later to the HTML-header if (!empty($item['if'])) { if (!preg_match('/^http/', $item['path'])) { $item['path'] = $bridge->getMagentoUrl() . $item['path']; } $tag = '<script type="text/javascript" src="' . $item['path'] . '"></script>' . "\n"; $tag = '<!--[if ' . $item['if'] . ' ]>' . "\n" . $tag . '<![endif]-->' . "\n"; $jstags[] = $tag; continue; } // Detect Prototype if (strstr($item['path'], 'prototype') || strstr($item['path'], 'scriptaculous')) { $this->_has_prototype = true; // Load an optimized Prototype/script.acul.us version if (MagebridgeModelConfig::load('use_protoaculous') == 1 || MagebridgeModelConfig::load('use_protoculous') == 1) { $skip_scripts = array('prototype/prototype.js', 'scriptaculous/builder.js', 'scriptaculous/effects.js', 'scriptaculous/dragdrop.js', 'scriptaculous/controls.js', 'scriptaculous/slider.js'); if (in_array($item['name'], $skip_scripts)) { continue; } } // Skip these, if the Google API is already loaded if (MagebridgeModelConfig::load('use_google_api') == 1) { if (preg_match('/prototype.js$/', $item['name'])) { continue; } if (preg_match('/scriptaculous.js$/', $item['name'])) { continue; } } } // Detect jQuery and replace it if (preg_match('/jquery-([0-9]+)\\.([0-9]+)\\.([0-9]+)/', $item['path']) || preg_match('/jquery.js$/', $item['path']) || preg_match('/jquery.min.js$/', $item['path'])) { if (MagebridgeModelConfig::load('replace_jquery') == 1) { MageBridgeTemplateHelper::load('jquery'); continue; } } // Detect the translation script if (strstr($item['name'], 'translate.js')) { $translate = true; } // Load this script through JS merging or not if (MagebridgeModelConfig::load('merge_js') == 1) { $jslist[] = $item['name']; } else { if (MagebridgeModelConfig::load('merge_js') == 2 && !empty($headers['merge_js'])) { // Don't do anything here yet } else { if (!preg_match('/^http/', $item['path'])) { $item['path'] = $bridge->getMagentoUrl() . $item['path']; } $item['path'] = $this->convertUrl($item['path']); $tag = '<script type="text/javascript" src="' . $item['path'] . '"></script>' . "\n"; $jstags[] = $tag; } } } } if (MagebridgeModelConfig::load('merge_js') == 2 && !empty($headers['merge_js'])) { $this->addScript($headers['merge_js']); } else { if (!empty($jslist)) { $this->addScript($this->getBaseJsUrl() . 'index.php?c=auto&f=,' . implode(',', $jslist)); } } if (!empty($jstags)) { foreach ($jstags as $tag) { if (!empty($tag)) { $document->addCustomTag($tag); } } } } // Load some extra JavaScript tags if (isset($headers['custom'])) { foreach ($headers['custom'] as $custom) { $custom = MageBridgeEncryptionHelper::base64_decode($custom); $custom = preg_replace('/Mage.Cookies.domain([^;]+)\\;/m', 'Mage.Cookies.domain = null;', $custom); $document->addCustomTag($custom); } } else { if (isset($translate) && $translate == true) { $html = '<script type="text/javascript">var Translator = new Translate([]);</script>'; $document->addCustomTag($html); } } return; }