Tpl::add_js_file(): add possibility to specify a prioroty as last parameter and fix including translation JS files before app.js

This commit is contained in:
Benjamin Renard 2024-12-03 01:31:59 +01:00
parent 4f2259f118
commit f85e7392b9
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
3 changed files with 41 additions and 29 deletions

View file

@ -128,8 +128,8 @@ class I18n {
&& Tpl :: initialized() && Tpl :: initialized()
) { ) {
Tpl :: register_static_directory(self :: $root_path, null, 'locales/'); Tpl :: register_static_directory(self :: $root_path, null, 'locales/');
Tpl :: add_js_file("lib/babel.js", "js/translation.js"); Tpl :: add_js_file("lib/babel.js", "js/translation.js", 10);
Tpl :: add_js_file("locales/", "$locale.js"); Tpl :: add_js_file("locales/", "$locale.js", 11);
} }
if (php_sapi_name() == 'cli') { if (php_sapi_name() == 'cli') {

View file

@ -31,7 +31,7 @@ class Tpl {
* Smarty templates directories path with their priority * Smarty templates directories path with their priority
* @var array<string,int> * @var array<string,int>
*/ */
public static $templates_directories = array(); public static $templates_directories = [];
/** /**
* Enable/disable AJAX returned data debugging in logs * Enable/disable AJAX returned data debugging in logs
@ -49,19 +49,19 @@ class Tpl {
* Static directories path with their priority * Static directories path with their priority
* @var array<string,array> * @var array<string,array>
*/ */
private static $static_directories = array(); private static $static_directories = [];
/** /**
* CSS files to load in next displayed page * CSS files to load in next displayed page
* @var array<string> * @var array<string>
*/ */
private static $css_files = array(); private static $css_files = [];
/** /**
* JavaScript files to load in next displayed page * JavaScript files to load in next displayed page
* @var array<string> * @var array<string>
*/ */
private static $js_files = array(); private static $js_files = [];
/** /**
* MIME type detector object * MIME type detector object
@ -104,9 +104,9 @@ class Tpl {
'cache_directory' => null, 'cache_directory' => null,
'main_pagetitle' => null, 'main_pagetitle' => null,
'static_root_url' => 'static/', 'static_root_url' => 'static/',
'static_directories' => array(), 'static_directories' => [],
'included_css_files' => array(), 'included_css_files' => ["css/app.css"],
'included_js_files' => array(), 'included_js_files' => ["js/app.js"],
'webstats_js_code' => null, 'webstats_js_code' => null,
'upload_max_filesize' => null, 'upload_max_filesize' => null,
'debug_ajax' => App::get('debug_ajax', false, 'bool'), 'debug_ajax' => App::get('debug_ajax', false, 'bool'),
@ -218,9 +218,9 @@ class Tpl {
// Initialize errors & messages session variables // Initialize errors & messages session variables
if (!isset($_SESSION['errors'])) if (!isset($_SESSION['errors']))
$_SESSION['errors'] = array(); $_SESSION['errors'] = [];
if (!isset($_SESSION['messages'])) if (!isset($_SESSION['messages']))
$_SESSION['messages'] = array(); $_SESSION['messages'] = [];
} }
/** /**
@ -404,7 +404,7 @@ class Tpl {
public static function get_errors() { public static function get_errors() {
if(isset($_SESSION['errors']) && is_array($_SESSION['errors'])) if(isset($_SESSION['errors']) && is_array($_SESSION['errors']))
return $_SESSION['errors']; return $_SESSION['errors'];
return array(); return [];
} }
/** /**
@ -414,7 +414,7 @@ class Tpl {
public static function get_messages() { public static function get_messages() {
if(isset($_SESSION['messages']) && is_array($_SESSION['messages'])) if(isset($_SESSION['messages']) && is_array($_SESSION['messages']))
return $_SESSION['messages']; return $_SESSION['messages'];
return array(); return [];
} }
/** /**
@ -462,7 +462,11 @@ class Tpl {
/** /**
* Register JS file(s) to load on next displayed page * Register JS file(s) to load on next displayed page
* @param string|array<string> $args JS files to load *
* Notes:
* - if first argument matched with one declared static directories, used it as static root URL
* - if last argument is an integer, used it as JS files inclusion priority
* @param string|int|array<string|int> $args JS files to load
* @return void * @return void
*/ */
public static function add_js_file(...$args) { public static function add_js_file(...$args) {
@ -475,13 +479,23 @@ class Tpl {
) )
) )
$root_url = self :: clean_static_root_url(array_shift($args)); $root_url = self :: clean_static_root_url(array_shift($args));
foreach ($args as $files) { $base_priority = (
if (!is_array($files)) $files = array($files); count($args) > 1 && is_int($args[count($args)-1])?
foreach ($files as $file) { intval(array_pop($args)):
$path = $root_url.$file; ceil(max(max(self :: $js_files), 1000))+1
if (!in_array($path, self :: $js_files)) );
self :: $js_files[] = $path; $files = call_user_func_array(
} "array_merge",
array_map('\EesyPHP\ensure_is_array', $args)
);
foreach ($files as $idx => $file) {
$path = $root_url.$file;
$file_priority = $base_priority + 0.001 * $idx;
self :: $js_files[$path] = (
array_key_exists($path, self :: $js_files)?
min(self :: $js_files[$path], $file_priority):
$file_priority
);
} }
} }
@ -510,7 +524,7 @@ class Tpl {
// Handle CSS & JS files included // Handle CSS & JS files included
self :: add_css_file(App::get('templates.included_css_files', null, 'array')); self :: add_css_file(App::get('templates.included_css_files', null, 'array'));
self :: add_js_file(App::get('templates.included_js_files', null, 'array')); self :: add_js_file(App::get('templates.included_js_files', null, 'array'), 500);
// Messages // Messages
self :: assign('errors', self :: get_errors()); self :: assign('errors', self :: get_errors());
@ -518,7 +532,8 @@ class Tpl {
// Files inclusions // Files inclusions
self :: assign('css', self :: $css_files); self :: assign('css', self :: $css_files);
self :: assign('js', self :: $js_files); asort(self :: $js_files, SORT_NUMERIC);
self :: assign('js', array_keys(self :: $js_files));
// I18n text domains // I18n text domains
self :: assign('CORE_TEXT_DOMAIN', I18n :: CORE_TEXT_DOMAIN); self :: assign('CORE_TEXT_DOMAIN', I18n :: CORE_TEXT_DOMAIN);
@ -647,7 +662,7 @@ class Tpl {
$data=null, $error_code=null, $pretty=null, $keep_messages_on_success=false $data=null, $error_code=null, $pretty=null, $keep_messages_on_success=false
) { ) {
if (!is_array($data)) if (!is_array($data))
$data = array(); $data = [];
// Adjust HTTP error code on unsuccessful request (or if custom error code is provided) // Adjust HTTP error code on unsuccessful request (or if custom error code is provided)
if ( if (
@ -876,7 +891,7 @@ class Tpl {
public static function static_directories($details=false) { public static function static_directories($details=false) {
if ($details) if ($details)
return self :: $static_directories; return self :: $static_directories;
$result = array(); $result = [];
foreach(self :: $static_directories as $root_url => $dirs) foreach(self :: $static_directories as $root_url => $dirs)
foreach(array_keys($dirs) as $dir) foreach(array_keys($dirs) as $dir)
if (!in_array($dir, $result)) if (!in_array($dir, $result))
@ -901,7 +916,7 @@ class Tpl {
} }
if (!array_key_exists($root_url, self :: $static_directories)) { if (!array_key_exists($root_url, self :: $static_directories)) {
self :: $static_directories[$root_url] = array(); self :: $static_directories[$root_url] = [];
if (is_null($priority)) $priority = 100; if (is_null($priority)) $priority = 100;
$pattern = "#^(?P<root_url>$root_url)(?P<path>.*)#"; $pattern = "#^(?P<root_url>$root_url)(?P<path>.*)#";
Log :: trace( Log :: trace(

View file

@ -24,7 +24,6 @@
<link href="{static_url path="lib/alertify-1.13.1/css/alertify.min.css"}" rel="stylesheet"/> <link href="{static_url path="lib/alertify-1.13.1/css/alertify.min.css"}" rel="stylesheet"/>
<link href="{static_url path="lib/alertify-1.13.1/css/themes/bootstrap.min.css"}" rel="stylesheet"/> <link href="{static_url path="lib/alertify-1.13.1/css/themes/bootstrap.min.css"}" rel="stylesheet"/>
<link href="{static_url path="css/app.css"}" rel="stylesheet"/>
{foreach $css as $path} {foreach $css as $path}
<link href="{$path|escape:"quotes"}" rel="stylesheet"/> <link href="{$path|escape:"quotes"}" rel="stylesheet"/>
{/foreach} {/foreach}
@ -143,8 +142,6 @@
<script src="{static_url path="lib/bootstrap-5.2.3/js/bootstrap.bundle.min.js"}"></script> <script src="{static_url path="lib/bootstrap-5.2.3/js/bootstrap.bundle.min.js"}"></script>
<script src="{static_url path="lib/alertify-1.13.1/alertify.min.js"}"></script> <script src="{static_url path="lib/alertify-1.13.1/alertify.min.js"}"></script>
<script src="{static_url path="js/app.js"}"></script>
<!-- Other libs & JavaScript scripts --> <!-- Other libs & JavaScript scripts -->
{foreach $js as $path} {foreach $js as $path}
<script language="javascript" src="{$path|escape:"quotes"}"></script> <script language="javascript" src="{$path|escape:"quotes"}"></script>