Skip to content

Commit

Permalink
Adding 'behavior.jquery' to Joomla Framework with deep non-conflict mode
Browse files Browse the repository at this point in the history
Method to load jQuery frameworks and non-conflicting javascript code
into the document head
  • Loading branch information
beat committed Jan 11, 2012
1 parent dcbb3ed commit 73eeffd
Show file tree
Hide file tree
Showing 5 changed files with 372 additions and 28 deletions.
33 changes: 27 additions & 6 deletions libraries/joomla/document/document.php
Expand Up @@ -449,20 +449,40 @@ public function setMetaData($name, $content, $http_equiv = false, $sync = true)
/**
* Adds a linked script to the page
*
* @param string $url URL to the linked script
* @param string $type Type of script. Defaults to 'text/javascript'
* @param boolean $defer Adds the defer attribute.
* @param boolean $async Adds the async attribute.
* @param string $url URL to the linked script
* @param string $type Type of script. Defaults to 'text/javascript'
* @param boolean $defer Adds the defer attribute.
* @param boolean $async Adds the async attribute.
* @param string $preScript Javascript code that must be just before the file inclusion [since 11.5]
* @param string $postScript Javascript code that must be just after the file [since 11.5]
*
* @return JDocument instance of $this to allow chaining
*
* @since 11.1
*/
public function addScript($url, $type = "text/javascript", $defer = false, $async = false)
public function addScript($url, $type = "text/javascript", $defer = false, $async = false, $preScript = null, $postScript = null)
{
$this->_scripts[$url]['mime'] = $type;
$this->_scripts[$url]['defer'] = $defer;
$this->_scripts[$url]['async'] = $async;
$this->_scripts[$url]['preScript'] = $preScript;
$this->_scripts[$url]['postScript'] = $postScript;

return $this;
}

/**
* Removes a linked script from the page (useful for replacing it by e.g. a newer version)
*
* @param string $url URL to the linked script to remove
*
* @return JDocument instance of $this to allow chaining
*
* @since 11.5
*/
public function removeScript($url)
{
unset($this->_scripts[$url]);

return $this;
}
Expand Down Expand Up @@ -963,7 +983,8 @@ public function parse($params = array())
*/
public function render($cache = false, $params = array())
{
if ($mdate = $this->getModifiedDate())
$mdate = $this->getModifiedDate();
if ($mdate)
{
JResponse::setHeader('Last-Modified', $mdate /* gmdate('D, d M Y H:i:s', time() + 900) . ' GMT' */);
}
Expand Down
5 changes: 3 additions & 2 deletions libraries/joomla/document/html/html.php
Expand Up @@ -478,7 +478,7 @@ public function countModules($condition)
*/
public function countMenuChildren()
{
static $children;
static $children = null;

if (!isset($children))
{
Expand All @@ -488,7 +488,8 @@ public function countMenuChildren()
$active = $menu->getActive();
if ($active)
{
$query->getQuery(true);
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('COUNT(*)');
$query->from('#__menu');
$query->where('parent_id = ' . $active->id);
Expand Down
78 changes: 60 additions & 18 deletions libraries/joomla/document/html/renderer/head.php
Expand Up @@ -40,7 +40,39 @@ public function render($head, $params = array(), $content = null)

return $buffer;
}
/**
*
*
* @param JDocument $document The document for which the head will be created
* @param string $content The script content
* @param string $type The type of script (default is 'text/javascript')
*
* @return string The head hTML
*/
private function renderInlineScript( $document, $content, $type = 'text/javascript' ) {
// Get line endings
$lnEnd = $document->_getLineEnd();
$tab = $document->_getTab();

$buffer = $tab . '<script type="' . $type . '">' . $lnEnd;

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

$buffer .= $content . $lnEnd;

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

return $buffer;
}
/**
* Generates the head HTML and return the results as a string
*
Expand Down Expand Up @@ -105,7 +137,8 @@ public function fetchHead(&$document)
foreach ($document->_links as $link => $linkAtrr)
{
$buffer .= $tab . '<link href="' . $link . '" ' . $linkAtrr['relType'] . '="' . $linkAtrr['relation'] . '"';
if ($temp = JArrayHelper::toString($linkAtrr['attribs']))
$temp = JArrayHelper::toString($linkAtrr['attribs']);
if ($temp)
{
$buffer .= ' ' . $temp;
}
Expand All @@ -120,7 +153,8 @@ public function fetchHead(&$document)
{
$buffer .= ' media="' . $strAttr['media'] . '" ';
}
if ($temp = JArrayHelper::toString($strAttr['attribs']))
$temp = JArrayHelper::toString($strAttr['attribs']);
if ($temp)
{
$buffer .= ' ' . $temp;
}
Expand Down Expand Up @@ -149,8 +183,20 @@ public function fetchHead(&$document)
}

// Generate script file links
$previousPreScript = null;
$pendingPostScript = null;
foreach ($document->_scripts as $strSrc => $strAttr)
{
// Javascript that must be just before the file inclusion:
if (($strAttr['preScript'] && ($strAttr['preScript'] != $previousPreScript))
||($pendingPostScript && ($strAttr['postScript'] != $pendingPostScript)))
{
$preScript = ( $pendingPostScript ? $pendingPostScript . $lnEnd : '' ) . $strAttr['preScript'];
$pendingPostScript = null;
$buffer .= $this->renderInlineScript($document, $preScript);
}
$previousPreScript = $strAttr['preScript'];

$buffer .= $tab . '<script src="' . $strSrc . '"';
if (!is_null($strAttr['mime']))
{
Expand All @@ -165,27 +211,23 @@ public function fetchHead(&$document)
$buffer .= ' async="async"';
}
$buffer .= '></script>' . $lnEnd;

// Javascript that must be just after the file:
if($strAttr['postScript'])
{
// delaying: $buffer .= $this->renderInlineScript($document, $strAttr['postScript']);
$pendingPostScript = $strAttr['postScript'];
}
}
if ($pendingPostScript)
{
$buffer .= $this->renderInlineScript($document, $pendingPostScript);
}

// Generate script declarations
foreach ($document->_script as $type => $content)
{
$buffer .= $tab . '<script type="' . $type . '">' . $lnEnd;

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

$buffer .= $content . $lnEnd;

// See above note
if ($document->_mime != 'text/html')
{
$buffer .= $tab . $tab . ']]>' . $lnEnd;
}
$buffer .= $tab . '</script>' . $lnEnd;
$buffer .= $this->renderInlineScript($document, $content, $type);
}

// Generate script language declarations.
Expand Down
42 changes: 40 additions & 2 deletions libraries/joomla/html/html.php
Expand Up @@ -108,7 +108,8 @@ public static function _($key)
if (!class_exists($className))
{
jimport('joomla.filesystem.path');
if ($path = JPath::find(JHtml::$includePaths, strtolower($file) . '.php'))
$path = JPath::find(JHtml::$includePaths, strtolower($file) . '.php');
if ($path)
{
require_once $path;

Expand Down Expand Up @@ -141,6 +142,43 @@ public static function _($key)
}
}

/**
* Checks if a JHtml::_($key) call can be made.
* If the function is not registered, tries to load the corresponding class without registering the function.
*
* @param string $key The name of helper method to load, (prefix).(class).function
* prefix and class are optional and can be used to load custom
* html helpers.
*
* @return boolean True: $key function is implemented, False: is not.
*/
public static function isCallable_($key)
{
// Do same as the function _($key) above but just checking without calling nor registering nor raising errors:

list($key, $prefix, $file, $func) = self::extract($key);
if (array_key_exists($key, self::$registry))
{
return true;
}

$className = $prefix . ucfirst($file);

if (!class_exists($className))
{
jimport('joomla.filesystem.path');
$path = JPath::find(JHtml::$includePaths, strtolower($file) . '.php');
if ($path)
{
require_once $path;
}
}

$toCall = array($className, $func);

return is_callable($toCall);
}

/**
* Registers a function to be called with a specific key
*
Expand Down Expand Up @@ -850,7 +888,7 @@ public static function tooltip($tooltip, $title = '', $image = 'tooltip.png', $t
*/
public static function calendar($value, $name, $id, $format = '%Y-%m-%d', $attribs = null)
{
static $done;
static $done = null;

if ($done === null)
{
Expand Down

0 comments on commit 73eeffd

Please sign in to comment.