1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-04 05:37:32 +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 * 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 $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 $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 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) if(self::$_js_enabled === false)
{ {
@@ -2462,7 +2462,11 @@ class e107
} }
$jshandler = self::getJs(); $jshandler = self::getJs();
$jshandler->setDependency($dep); if(is_string($parm))
{
$jshandler->setDependency($parm);
$parm = null;
}
switch ($type) switch ($type)
{ {
@@ -2534,11 +2538,11 @@ class e107
// data is e.g. 'http://cdn.somesite.com/some.js' // data is e.g. 'http://cdn.somesite.com/some.js'
if($zone !== null) if($zone !== null)
{ {
$jshandler->headerFile($data, $zone, $pre, $post); $jshandler->headerFile($data, $zone, $pre, $post, $parm);
} }
else else
{ {
$jshandler->headerFile($data, 5, $pre, $post); $jshandler->headerFile($data, 5, $pre, $post, $parm);
} }
break; break;
@@ -2547,11 +2551,11 @@ class e107
// data is e.g. '{e_PLUGIN}myplugin/jslib/myplug.js' // data is e.g. '{e_PLUGIN}myplugin/jslib/myplug.js'
if($zone !== null) if($zone !== null)
{ {
$jshandler->footerFile($data, $zone, $pre, $post); $jshandler->footerFile($data, $zone, $pre, $post, $parm);
} }
else else
{ {
$jshandler->footerFile($data, 5, $pre, $post); $jshandler->footerFile($data, 5, $pre, $post, $parm);
} }
break; break;

View File

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

View File

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

View File

@@ -173,34 +173,87 @@
0 => array( 0 => array(
'file' => '{e_PLUGIN}forum/js/forum.js', 'file' => '{e_PLUGIN}forum/js/forum.js',
'zone' => 5, 'zone' => 5,
'opts' => []
), ),
1 => array( 1 => array(
'file' => 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js', 'file' => 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js',
'zone' => 1, 'zone' => 1,
'opts' => []
), ),
2 => array( 2 => array(
'file' => '{e_WEB}js/bootstrap-notify/js/bootstrap-notify.js', '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) 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); $result = $this->js->renderJs('header', 1, true, true);
$expected = ' $this->assertStringContainsString('<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>', $result);
<!-- [JSManager] Header JS include - zone #1 --> $this->assertStringContainsString('zone #1', $result);
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
';
$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);
} }
/* /*