Sometimes there is the need to load the component header in several places, for example using a plugin or a module (or many modules), it's done callng the function:
headerDeclarations
you'll find it in the helper.php file.
Everything works, BUT if you take a look on the source code of the generated page, you'll find out that all the script declarations (for example those for the jquery validationengine) are loaded each time the headerDeclaration is called, so for example if you have 1 plugin and 4 modules, it's loaded 5 times (+1 if you are in a view of your component).
my workaround is to add a static variable $loaded to the function in this way:
public static function headerDeclarations()
{
static $loaded = false;
if(!$loaded){
$app = JFactory::getApplication();
$doc = JFactory::getDocument();
$siteUrl = JURI::root(true);
$baseSite = 'components' .DS. COM_ESTIMATOR;
$baseAdmin = 'administrator' .DS. 'components' .DS. COM_ESTIMATOR;
$componentUrl = $siteUrl . '/' . str_replace(DS, '/', $baseSite);
$componentUrlAdmin = $siteUrl . '/' . str_replace(DS, '/', $baseAdmin);
//Javascript
//jQuery Loading : Abstraction to handle cross versions of Joomla
JDom::_('framework.jquery');
JDom::_('framework.jquery.chosen');
JDom::_('framework.bootstrap');
$doc->addScript($siteUrl . '/media/system/js/core.js');
//Load the jQuery-Validation-Engine (MIT License, Copyright(c) 2011 Cedric Dugas http://www.position-absolute.com)
self::addScript($doc, $baseAdmin, 'js' .DS. 'jquery.validationEngine.js');
self::addStyleSheet($doc, $baseAdmin, 'css' .DS. 'validationEngine.jquery.css');
EstimatorHelperHtmlValidator::loadLanguageScript();
//CSS
if ($app->isAdmin())
{
// Blue stork override
$styles = "fieldset td.key label{display: block;}fieldset input, fieldset textarea, fieldset select, fieldset img, fieldset button{float: none;}fieldset label, fieldset span.faux-label{float: none;display: inline;min-width: inherit;}";
$doc->addStyleDeclaration($styles);
self::addStyleSheet($doc, $baseAdmin, 'css' .DS. 'estimator.css');
self::addStyleSheet($doc, $baseAdmin, 'css' .DS. 'toolbar.css');
$doc->addStyleSheet($componentUrlAdmin . '/css/jquery-ui-1.9.1.min.css');
}
else if ($app->isSite())
{
self::addStyleSheet($doc, $baseSite, 'css' .DS. 'estimator.css');
self::addStyleSheet($doc, $baseSite, 'css' .DS. 'toolbar.css');
$doc->addStyleSheet($componentUrl . '/css/jquery-ui-1.9.1.min.css');
}
$loaded = true;
}
}
this will solve the problem, but if you have custom js/css desclarations for a plugin or module, it's better if you move them out of the function headerDeclarations()