| Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/ |
| Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/TResource.php |
<?php
/*
* @package Tech Fry Library
* @license https://www.gnu.org/licenses/gpl-3.0.en.html
*/
namespace TechFry\Library;
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
// Class for Managing Web Assets - style, script
// Documentation - https://manual.joomla.org/docs/general-concepts/web-asset-manager/
/*
Resources are css styles and js scripts
3 Ways to load - registry files, direct url, inline
Registry Files:
media/vendor/joomla.asset.json
media/system/joomla.asset.json
media/legacy/joomla.asset.json
Where to add?
media/{com_active_component}/joomla.asset.json
templates/{active_template}/joomla.asset.json
*/
/*
Web Assets: css and js
Stages
- 1. Registering: WebAssetManager knows about the existence of these assets, but will not attach them to a document while rendering.
- 2. Using: Asset is enabled via $wa->useAsset() (->useScript(), ->useStyle(), ->registerAndUseX())
- 3. Rendering: <jdoc:include type="styles" />, <jdoc:include type="scripts" />
*/
class TResource
{
public $wa; // WebAssetManager
public $wr; // WebAssetRegistry
public function __construct()
{
$this->wa = Factory::getApplication()->getDocument()->getWebAssetManager();
$this->wr = $this->wa->getRegistry();
}
// 01. Add assets using Joomla WAM
public function add_resources($resources = [])
{
// Add and Use Resources from TF Registry File
if (!\is_array($resources))
{
return;
}
// 1. Register assets
$this->wr->addRegistryFile('libraries/techfry/json/joomla.asset.json');
// 2. Enable assets
foreach ($resources as $resource)
{
switch ($resource)
{
case 'fa' : // Font Awesome from CDN
$this->wa->useScript('tf.lazyme');
$this->wa->getAsset('style', 'tf.fontawesome')->setAttribute('rel', 'lazy-stylesheet');
$this->wa->useStyle('tf.fontawesome');
break;
case 'jfa' : // Font Awesome from Joomla
$this->wa->useScript('tf.lazyme');
$this->wa->getAsset('style', 'j.fontawesome')->setAttribute('rel', 'lazy-stylesheet');
$this->wa->useStyle('j.fontawesome');
break;
case 'bs_css' : // Bootstrap from CDN
$this->wa->useStyle('tf.bs.css');
break;
case 'jbs_css' : // Bootstrap from Joomla
$this->wa->useStyle('bootstrap.css');
break;
case 'bs_js' :
$this->wa->useScript('tf.bs.js');
break;
case 'jquery' :
$this->wa->useScript('tf.jquery');
break;
case 'air_animation_css' :
$this->wa->useStyle('air.animation');
break;
case 'air_template' :
$this->wa->useStyle('air.template');
break;
}
}
}
// 02. Add inline css
public function add_style($css_code = '')
{
if (empty($css_code))
{
return;
}
$this->wa->addInlineStyle($css_code);
}
// 03. Add inline script
public function add_script($js_code = '')
{
if (empty($js_code))
{
return;
}
$this->wa->addInlineScript($js_code);
}
// 04. Use asset already rgistered - script, style
public function use_asset($type, $name)
{
$this->wa->useAsset($type, $name);
}
// 05. Remove or disable asset - script, style
public function remove_asset($type, $name)
{
$this->wa->disableAsset($type, $name);
}
// 06. Get asset registry json files
public function get_registry_files()
{
return $this->wa->getManagerState()['registryFiles']; // Array
}
// 07. Get assets of specific types
public function get_assets($type)
{
// $type - script, style
return $this->wa->getAssets($type);
}
// 08. Add any single resource directly from Url
public function add_resource($type, $name, $resource_url, $options = [], $attribs = [], $depends = [])
{
// attribs like ['defer' => true]
$this->wa->registerAsset($type, $name, $resource_url, $options, $attribs, $depends);
$this->wa->useAsset($type, $name);
}
}