1
0
mirror of https://github.com/e107inc/e107.git synced 2025-04-20 04:32:01 +02:00

Added 'defer' to bootstrap5 and fontawesome5 <script> tags.

This commit is contained in:
Cameron 2021-10-12 08:00:41 -07:00
parent 0442ba138a
commit 78d72fa139
4 changed files with 135 additions and 95 deletions

View File

@ -2451,10 +2451,10 @@ class e107
* JS Common Public Function. Prefered is shortcode script path
* @param string $type core|theme|footer|inline|footer-inline|url or any existing plugin_name
* @param string|array $data depends on the type - path/url or inline js source
* @param string|array $parm parameters. BC string dependence : null | prototype | jquery OR array of parameters. tbd
* @param integer $zone [optional] leave it null for default zone
* @param string $dep dependence : null | prototype | jquery
*/
public static function js($type, $data, $dep = null, $zone = null, $pre = '', $post = '')
public static function js($type, $data, $parm = null, $zone = null, $pre = '', $post = '')
{
if(self::$_js_enabled === false)
{
@ -2462,7 +2462,11 @@ class e107
}
$jshandler = self::getJs();
$jshandler->setDependency($dep);
if(is_string($parm))
{
$jshandler->setDependency($parm);
$parm = null;
}
switch ($type)
{
@ -2534,11 +2538,11 @@ class e107
// data is e.g. 'http://cdn.somesite.com/some.js'
if($zone !== null)
{
$jshandler->headerFile($data, $zone, $pre, $post);
$jshandler->headerFile($data, $zone, $pre, $post, $parm);
}
else
{
$jshandler->headerFile($data, 5, $pre, $post);
$jshandler->headerFile($data, 5, $pre, $post, $parm);
}
break;
@ -2547,11 +2551,11 @@ class e107
// data is e.g. '{e_PLUGIN}myplugin/jslib/myplug.js'
if($zone !== null)
{
$jshandler->footerFile($data, $zone, $pre, $post);
$jshandler->footerFile($data, $zone, $pre, $post, $parm);
}
else
{
$jshandler->footerFile($data, 5, $pre, $post);
$jshandler->footerFile($data, 5, $pre, $post, $parm);
}
break;

View File

@ -534,12 +534,17 @@ class e_jsmanager
* @param integer $zone 1-5 (see header.php)
* @return e_jsmanager
*/
public function headerFile($file_path, $zone = 5, $pre = '', $post = '')
public function headerFile($file_path, $zone = 5, $pre = '', $post = '', $opts = array())
{
$opts = [
'pre' => $pre,
'post' => $post,
];
if(!empty($pre))
{
$opts['pre'] = $pre;
}
if(!empty($post))
{
$opts['post'] = $post;
}
$this->addJs('header', $file_path, $zone, $opts);
return $this;
@ -626,12 +631,18 @@ class e_jsmanager
* @param integer $priority 1-5 (see footer.php)
* @return e_jsmanager
*/
public function footerFile($file_path, $priority = 5, $pre = '', $post = '')
public function footerFile($file_path, $priority = 5, $pre = '', $post = '', $opts=array())
{
$opts = [
'pre' => $pre,
'post' => $post,
];
if(!empty($pre))
{
$opts['pre'] = $pre;
}
if(!empty($post))
{
$opts['post'] = $post;
}
$this->addJs('footer', $file_path, $priority, $opts);
return $this;
}
@ -1088,23 +1099,50 @@ class e_jsmanager
case 'header':
$file_path = $tp->createConstants($file_path, 'mix').$this->_sep.$pre.$this->_sep.$post;
$zone = intval($runtime_location);
$info = [
$tp->createConstants($file_path, 'mix'),
$pre,
$post
];
if(!empty($opts['defer']))
{
$info[] = 'defer';
}
$file_path = implode($this->_sep, $info);
$zone = (int) $runtime_location;
if($zone > 5 || $zone < 1)
{
$zone = 5;
}
if(!isset($this->_runtime_header[$zone]))
{
$this->_runtime_header[$zone] = array();
}
$registry = &$this->_runtime_header[$zone];
$runtime = true;
break;
case 'footer':
$file_path = $tp->createConstants($file_path, 'mix').$this->_sep.$pre.$this->_sep.$post;
$zone = intval($runtime_location);
$info = [
$tp->createConstants($file_path, 'mix'),
$pre,
$post
];
if(!empty($opts['defer']))
{
$info[] = 'defer';
}
$file_path = implode($this->_sep, $info);
$zone = (int) $runtime_location;
if($zone > 5 || $zone < 1)
{
$zone = 5;

View File

@ -366,6 +366,7 @@ class core_library
'dist/js/bootstrap.bundle.min.js' => array(
'zone' => 2,
'type' => 'footer',
'defer' => true,
),
),
'css' => array(
@ -374,26 +375,7 @@ class core_library
),
),
),
'variants' => array(
// 'unminified' version for debugging.
/*'dev' => array(
'files' => array(
'js' => array(
'js/bootstrap.js' => array(
'zone' => 2,
'type' => 'footer',
),
),
'css' => array(
'css/bootstrap.css' => array(
'zone' => 2,
),
),
),
),*/
),
'variants' => array(),
// Override library path to CDN.
//https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css
'library_path' => 'https://cdn.jsdelivr.net/npm/bootstrap@5.1.1',
@ -416,6 +398,7 @@ class core_library
'js/bootstrap.bundle.min.js' => array(
'zone' => 2,
'type' => 'footer',
'defer' => true,
),
),
'css' => array(
@ -424,24 +407,7 @@ class core_library
),
),
),
/* 'variants' => array(
// 'unminified' version for debugging.
'dev' => array(
'files' => array(
'js' => array(
'js/bootstrap.bundle.js' => array(
'zone' => 2,
'type' => 'footer',
),
),
'css' => array(
'css/bootstrap.css' => array(
'zone' => 2,
),
),
),
),
),*/
'variants' => array(),
'library_path' => '{e_WEB}lib/bootstrap',
'path' => '5',
'version' => '5.1.1',
@ -944,10 +910,12 @@ class core_library
'js/all.min.js' => array(
'zone' => 2,
'type' => 'footer',
'defer' => true,
),
'js/v4-shims.min.js' => array(
'zone' => 2,
'type' => 'footer',
'defer' => true,
),
),
'css' => array(
@ -959,18 +927,7 @@ class core_library
),
),
),
/* 'variants' => array(
// 'unminified' version for debugging.
'dev' => array(
'files' => array(
'css' => array(
'css/font-awesome.css' => array(
'zone' => 2,
),
),
),
),
),*/
'variants' => array(),
// Override library path to CDN.
'library_path' => 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0',
'path' => '',
@ -991,10 +948,12 @@ class core_library
'js/all.min.js' => array(
'zone' => 2,
'type' => 'footer',
'defer' => true,
),
'js/v4-shims.min.js' => array(
'zone' => 2,
'type' => 'footer',
'defer' => true,
),
),
'css' => array(
@ -1006,22 +965,8 @@ class core_library
),
),
),
'variants' => array(
// 'unminified' version for debugging.
'dev' => array(
'files' => array(
'css' => array(
'css/all.css' => array(
'zone' => 2,
),
'css/v4-shims.css' => array(
'zone' => 2,
),
),
),
),
),
// Override library path.
'variants' => array(),
'library_path' => '{e_WEB}lib/font-awesome',
'path' => '5',
'version' => '5.15.2',
@ -2101,7 +2046,7 @@ class e_library_manager
}
if($type == 'js')
{
e107::js($options['type'], $data, null, $options['zone']);
e107::js($options['type'], $data, $options, $options['zone']);
}
elseif($type == 'css')
{

View File

@ -173,34 +173,87 @@
0 => array(
'file' => '{e_PLUGIN}forum/js/forum.js',
'zone' => 5,
'opts' => []
),
1 => array(
'file' => 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js',
'zone' => 1,
'opts' => []
),
2 => array(
'file' => '{e_WEB}js/bootstrap-notify/js/bootstrap-notify.js',
'zone' => 2
'zone' => 2,
'opts' => []
),
3 => array(
'file' => 'https://somewhere/something.min.js',
'zone' => 3,
'opts' => array('defer'=>true)
),
);
foreach($load as $t)
{
$this->js->headerFile($t['file'], $t['zone']);
$this->js->headerFile($t['file'], $t['zone'], null, null, $t['opts']);
}
// Test loaded files.
$result = $this->js->renderJs('header', 1, true, true);
$expected = '
<!-- [JSManager] Header JS include - zone #1 -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
';
$this->assertStringContainsString('<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>', $result);
$this->assertStringContainsString('zone #1', $result);
$this->assertSame($expected, $result);
$result = $this->js->renderJs('header', 3, true, true);
$this->assertStringContainsString('<script type="text/javascript" src="https://somewhere/something.min.js" defer></script>', $result);
$this->assertStringContainsString('zone #3', $result);
}
public function testFooterFile()
{
$load = array(
0 => array(
'file' => '{e_PLUGIN}forum/js/forum.js',
'zone' => 5,
'opts' => []
),
1 => array(
'file' => 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js',
'zone' => 1,
'opts' => []
),
2 => array(
'file' => '{e_WEB}js/bootstrap-notify/js/bootstrap-notify.js',
'zone' => 2,
'opts' => []
),
3 => array(
'file' => 'https://somewhere/something.min.js',
'zone' => 3,
'opts' => array('defer'=>true)
),
);
foreach($load as $t)
{
$this->js->footerFile($t['file'], $t['zone'], null, null, $t['opts']);
}
// Test loaded files.
$result = $this->js->renderJs('footer', 1, true, true);
$this->assertStringContainsString('<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>', $result);
$this->assertStringContainsString('priority #1', $result);
$result = $this->js->renderJs('footer', 3, true, true);
$this->assertStringContainsString('<script type="text/javascript" src="https://somewhere/something.min.js" defer></script>', $result);
$this->assertStringContainsString('priority #3', $result);
}
/*