Skip to content

Commit

Permalink
Joomla.JText without inline JavaScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik committed Aug 20, 2016
1 parent c1b0295 commit fce59c5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 28 deletions.
34 changes: 10 additions & 24 deletions libraries/joomla/document/renderer/html/head.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public function fetchHead($document)
$document->_metaTags['name']['tags'] = implode(', ', $tagsHelper->getTagNames($document->_metaTags['name']['tags']));
}

if (JText::script())
{
JHtml::_('behavior.core');
}

// Trigger the onBeforeCompileHead event
$app = JFactory::getApplication();
$app->triggerEvent('onBeforeCompileHead');
Expand Down Expand Up @@ -279,32 +284,13 @@ public function fetchHead($document)
}

// Generate script language declarations.
if (count(JText::script()))
if (JText::script())
{
$buffer .= $tab . '<script';
$prettyPrint = (JDEBUG && defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : false);

if (!$document->isHtml5())
{
$buffer .= ' type="text/javascript"';
}

$buffer .= '>' . $lnEnd;

if ($document->_mime != 'text/html')
{
$buffer .= $tab . $tab . '//<![CDATA[' . $lnEnd;
}

$buffer .= $tab . $tab . '(function() {' . $lnEnd;
$buffer .= $tab . $tab . $tab . 'Joomla.JText.load(' . json_encode(JText::script()) . ');' . $lnEnd;
$buffer .= $tab . $tab . '})();' . $lnEnd;

if ($document->_mime != 'text/html')
{
$buffer .= $tab . $tab . '//]]>' . $lnEnd;
}

$buffer .= $tab . '</script>' . $lnEnd;
$buffer .= $tab . '<script type="application/json" id="joomla-text-strings">';
$buffer .= json_encode(JText::script(), $prettyPrint);
$buffer .= '</script>' . $lnEnd;
}

// Output the custom tags - array_unique makes sure that we don't output the same tags twice
Expand Down
47 changes: 44 additions & 3 deletions media/system/js/core-uncompressed.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,56 @@ Joomla.editors.instances = Joomla.editors.instances || {};
* Allows you to call Joomla.JText._() to get a translated JavaScript string pushed in with JText::script() in Joomla.
*/
Joomla.JText = {
strings: {},
strings: {},
preloaded: false,

/**
* Translates a string into the current language.
*
* @param {String} key The string to translate
* @param {String} def Default string
*
* @returns {String}
*/
'_': function( key, def ) {
return typeof this.strings[ key.toUpperCase() ] !== 'undefined' ? this.strings[ key.toUpperCase() ] : def;

if (!this.preloaded) {
this.preload();
}

def = def === undefined ? '' : def;
return this.strings[ key.toUpperCase() ] !== undefined ? this.strings[ key.toUpperCase() ] : def;
},

/**
* Load new strings in to Joomla.JText
*
* @param {Object} object Object with new strings
* @returns {Joomla.JText}
*/
load: function( object ) {
for ( var key in object ) {
if (!object.hasOwnProperty(key)) continue;
this.strings[ key.toUpperCase() ] = object[ key ];
}
return this;
},

/**
* Preload strings from <script type="application/json" id="joomla-text-strings"> container
*
* @param {String} id Id of the container
* @returns {Joomla.JText}
*/
preload: function( id ) {
id = id || 'joomla-text-strings';
var element = document.getElementById(id),
str = element ? (element.text || element.textContent) : null,
options = str ? JSON.parse(str) : null;

this.load(options || {});
this.preloaded = true;

return this;
}
};
Expand Down Expand Up @@ -614,7 +655,7 @@ Joomla.editors.instances = Joomla.editors.instances || {};
parentElement.appendChild(loadingDiv);
}
// Show or hide the layer.
else
else
{
if (!document.getElementById('loading-logo'))
{
Expand Down
2 changes: 1 addition & 1 deletion media/system/js/core.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fce59c5

Please sign in to comment.