Skip to content

Commit

Permalink
Attach Assets to Document
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik committed Sep 25, 2018
1 parent f63ecf9 commit 742e1ca
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 7 deletions.
24 changes: 17 additions & 7 deletions libraries/src/WebAsset/WebAssetFactory.php
Expand Up @@ -10,7 +10,7 @@

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Document\HtmlDocument;
use Joomla\CMS\Document\Document;
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\Path;

Expand Down Expand Up @@ -249,26 +249,36 @@ public function disableAsset($name)
/**
* Attach an active assets to the Document
*
* @param HtmlDocument $doc Document for attach StyleSheet/JavaScript
* @param Document $doc Document for attach StyleSheet/JavaScript
*
* @return self
*
* @since __DEPLOY_VERSION__
*/
public function attach(HtmlDocument $doc)
public function attach(Document $doc)
{
//$app = Factory::getApplication();
$app = Factory::getApplication();

// Resolve Dependency
$this->resolveDependency();

// Trigger the event
//$app->triggerEvent('onBeforeAttachWebAsset', array($this));
$app->triggerEvent('onBeforeAttachWebAssets', array($this, $doc));

// Attach an active assets do the document
$assets = $this->getActiveAssets();

var_dump($assets);
// Presave existing Scripts, and attach them after requested assets.
$jsBackup = $doc->_scripts;
$doc->_scripts = array();

// Attach an active assets do the document
foreach ($assets as $asset)
{
$asset->attach($doc);
}

// Merge with previously added scripts
$doc->_scripts = array_replace($doc->_scripts, $jsBackup);

return $this;
}
Expand Down
125 changes: 125 additions & 0 deletions libraries/src/WebAsset/WebAssetItem.php
Expand Up @@ -8,6 +8,9 @@

namespace Joomla\CMS\WebAsset;

use Joomla\CMS\Document\Document;
use Joomla\CMS\HTML\HTMLHelper;

defined('JPATH_PLATFORM') or die;

/**
Expand Down Expand Up @@ -253,4 +256,126 @@ public function getWeight()
{
return $this->weight;
}

/**
* Attach active asset to the Document
*
* @param Document $doc Document for attach StyleSheet/JavaScript
*
* @return self
*
* @throws \RuntimeException If try attach inactive asset
*
* @since __DEPLOY_VERSION__
*/
public function attach(Document $doc)
{
if (!$this->isActive())
{
throw new RuntimeException('Incative Asset cannot be attached');
}

return $this->attachCSS($doc)->attachJS($doc);
}

/**
* Attach StyleSheet files to the document
*
* @param Document $doc Document for attach StyleSheet/JavaScript
*
* @return self
*
* @since __DEPLOY_VERSION__
*/
protected function attachCSS(Document $doc)
{
foreach ($this->css as $path)
{
$file = $path;
$version = false;

if (!$this->isPathExternal($path))
{
// Get the file path
$file = HTMLHelper::_('stylesheet', $path, [
'pathOnly' => true,
'relative' => !$this->isPathAbsolute($path)
]
);
$version = 'auto';
}

if ($file)
{
$doc->addStyleSheet($file, ['version' => $version]);
}
}

return $this;
}

/**
* Attach JavaScript files to the document
*
* @param Document $doc Document for attach StyleSheet/JavaScript
*
* @return self
*
* @since __DEPLOY_VERSION__
*/
protected function attachJS(Document $doc)
{
foreach ($this->js as $path)
{
$file = $path;
$version = false;

if (!$this->isPathExternal($path))
{
// Get the file path
$file = HTMLHelper::_('script', $path, [
'pathOnly' => true,
'relative' => !$this->isPathAbsolute($path)
]
);
$version = 'auto';
}

if ($file)
{
$doc->addScript($file, ['version' => $version]);
}
}

return $this;
}

/**
* Check if the Path is External
*
* @param string $path Path to test
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
protected function isPathExternal($path)
{
return strpos($path, 'http') === 0 || strpos($path, '//') === 0;
}

/**
* Check if the Path is relative to /media folder or absolute
*
* @param string $path Path to test
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
protected function isPathAbsolute($path)
{
// We have a full path or not
return is_file(JPATH_ROOT . '/' . $path);
}
}

0 comments on commit 742e1ca

Please sign in to comment.