'Auto',
'DeferedCSS' => 'loadCSS',
'DeferedJS' => 'loadJS',
];
}
public static function Auto($class = false)
{
$config = Config::inst()->get(self::class);
$themeName = WebpackTemplateProvider::themeName();
// Initialization
Requirements::block(THIRDPARTY_DIR.'/jquery/jquery.js');
/*if (defined('FONT_AWESOME_DIR')) {
Requirements::block(FONT_AWESOME_DIR.'/css/lib/font-awesome.min.css');
}*/
Requirements::set_force_js_to_bottom(true);
// Main libs
if (!$config['nojquery']) {
DeferedRequirements::loadJS('//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js');
}
// App libs
if (!$config['nofontawesome']) {
DeferedRequirements::loadCSS('//use.fontawesome.com/releases/v5.4.0/css/all.css');
}
DeferedRequirements::loadCSS($themeName.'.css');
DeferedRequirements::loadJS($themeName.'.js');
// Class libs
$class = get_class(Controller::curr());
if (isset($config['custom_requirements'][$class])) {
foreach ($config['custom_requirements'][$class] as $file) {
if (strpos($file, '.css')) {
DeferedRequirements::loadCSS($file);
}
if (strpos($file, '.js')) {
DeferedRequirements::loadJS($file);
}
}
}
$class = str_replace('\\', '.', $class);
$dir = Path::join(
Director::publicFolder(),
ManifestFileFinder::RESOURCES_DIR,
$themeName,
'client',
'dist'
);
if (file_exists(Path::join($dir, 'css', $themeName.'_'.$class . '.css'))) {
DeferedRequirements::loadCSS($themeName.'_'.$class . '.css');
}
if (file_exists(Path::join($dir, 'js', $themeName.'_'.$class . '.js'))) {
DeferedRequirements::loadJS($themeName.'_'.$class . '.js');
}
return self::forTemplate();
}
public static function loadCSS($css)
{
$external = (mb_substr($css, 0, 2) === '//' || mb_substr($css, 0, 4) === 'http');
if ($external || (self::$defered && !self::_webpackActive())) {
self::$css[] = $css;
} else {
WebpackTemplateProvider::loadCSS($css);
}
}
public static function loadJS($js)
{
/*$external = (mb_substr($js, 0, 2) === '//' || mb_substr($js, 0, 4) === 'http');
if ($external || (self::$defered && !self::_webpackActive())) {*/
// webpack supposed to load external JS
if (self::$defered && !self::_webpackActive()) {
self::$js[] = $js;
} else {
WebpackTemplateProvider::loadJS($js);
}
}
protected static function _webpackActive()
{
return class_exists('WebpackTemplateProvider') && WebpackTemplateProvider::isActive();
}
public static function setDefered($bool)
{
self::$defered = $bool;
}
public static function forTemplate()
{
$result = '';
foreach (self::$css as $css) {
$result .= '';
}
foreach (self::$js as $js) {
$result .= '';
}
$result .=
'';
return $result;
}
private static function get_url($url)
{
$config = Config::inst()->get(self::class);
// external URL
if (strpos($url, '//') !== false) {
return $url;
}
$version = $config['version'];
$version = $version
? strpos($url, '?') // inner URL
? '&'.$version // add param
: '?'.$version // new param
: ''; // no version defined
$static_domain = $config['static_domain'];
$static_domain = $static_domain ? $static_domain : '';
return $url.$version;
}
}