From 8d2a3f4ac733359be11b6bc87630f6f8b27dca1e Mon Sep 17 00:00:00 2001 From: Prasanna LMSACE Date: Sat, 16 Sep 2023 11:24:53 +0530 Subject: [PATCH] Smart menu JS included to the page based on menus existence. - Fix #357, UN-231 --- CHANGES.md | 1 + classes/output/navigation/primary.php | 6 ++++++ layout/includes/smartmenus.php | 4 ++++ templates/theme_boost/columns2.mustache | 4 ++-- templates/theme_boost/drawers.mustache | 4 ++-- 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index dcb2012e4ac..8e2c926f0ed 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,7 @@ Changes ### Unreleased +* 2023-09-22 - Improvement: Smartmenus.js is only be added to the page if smart menus are really used, solves #357 * 2023-09-22 - Bugfix: Smart menu language restriction did not respect switching language with the language switcher, solves #358. * 2023-09-26 - Bugfix: The smart menu third level arrow was broken, solves #402. * 2023-09-22 - Make codechecker happier diff --git a/classes/output/navigation/primary.php b/classes/output/navigation/primary.php index 12e6d29cdda..71ab45d6cc0 100644 --- a/classes/output/navigation/primary.php +++ b/classes/output/navigation/primary.php @@ -136,6 +136,11 @@ public function export_for_template(?renderer_base $output = null): array { $usermenu = $this->get_user_menu($output); $this->build_usermenus($usermenu, $locationusermenus); + // Check if any of the smartmenus are going to be included on the page. + // This is used as flag to include the smart menu's JS file in mustache templates later + // as well as for controlling the smart menu SCSS. + $includesmartmenu = (!empty($mainmenu) || !empty($menubarmenus) || !empty($locationusermenus) || !empty($locationbottom)); + return [ 'mobileprimarynav' => $mobileprimarynav, 'moremenu' => $moremenu->export_for_template($output), @@ -143,6 +148,7 @@ public function export_for_template(?renderer_base $output = null): array { 'lang' => !isloggedin() || isguestuser() ? $languagemenu->export_for_template($output) : [], 'user' => $usermenu ?? [], 'bottombar' => $bottombardata ?? false, + 'includesmartmenu' => $includesmartmenu ? true : false ]; } diff --git a/layout/includes/smartmenus.php b/layout/includes/smartmenus.php index e24c1808822..1a265be8938 100644 --- a/layout/includes/smartmenus.php +++ b/layout/includes/smartmenus.php @@ -27,3 +27,7 @@ // Add smart menu elements to template context. $templatecontext['menubar'] = $primarymenu['menubar'] ?? []; $templatecontext['bottombar'] = $primarymenu['bottombar'] ?? []; + +// Add smart menu flag if the smart menu contains any menus to show. +$includesmartmenu = (isset($primarymenu['includesmartmenu']) && !empty($primarymenu['includesmartmenu'])); +$templatecontext['includesmartmenu'] = $includesmartmenu ? true : false; diff --git a/templates/theme_boost/columns2.mustache b/templates/theme_boost/columns2.mustache index c56c8af744b..5759182dbf4 100644 --- a/templates/theme_boost/columns2.mustache +++ b/templates/theme_boost/columns2.mustache @@ -122,9 +122,9 @@ {{#js}} M.util.js_pending('theme_boost/loader'); -require(['theme_boost/loader', 'theme_boost/drawer', 'theme_boost_union/smartmenu'], function(Loader, Drawer, SmartMenu) { +require(['theme_boost/loader', 'theme_boost/drawer'{{#includesmartmenu}}, 'theme_boost_union/smartmenu'{{/includesmartmenu}}], function(Loader, Drawer{{#includesmartmenu}}, SmartMenu{{/includesmartmenu}}) { Drawer.init(); M.util.js_complete('theme_boost/loader'); - SmartMenu.init(); + {{#includesmartmenu}}SmartMenu.init();{{/includesmartmenu}} }); {{/js}} diff --git a/templates/theme_boost/drawers.mustache b/templates/theme_boost/drawers.mustache index c6af6606f01..7c551938c4c 100644 --- a/templates/theme_boost/drawers.mustache +++ b/templates/theme_boost/drawers.mustache @@ -297,10 +297,10 @@ {{#js}} M.util.js_pending('theme_boost/loader'); -require(['theme_boost/loader', 'theme_boost/drawer'{{#regions.offcanvas.hasblocks}},'theme_boost_union/offcanvas'{{/regions.offcanvas.hasblocks}}, 'theme_boost_union/smartmenu'], function(Loader, Drawer{{#regions.offcanvas.hasblocks}},OffCanvas{{/regions.offcanvas.hasblocks}}, SmartMenu) { +require(['theme_boost/loader', 'theme_boost/drawer'{{#regions.offcanvas.hasblocks}},'theme_boost_union/offcanvas'{{/regions.offcanvas.hasblocks}}{{#includesmartmenu}}, 'theme_boost_union/smartmenu'{{/includesmartmenu}}], function(Loader, Drawer{{#regions.offcanvas.hasblocks}},OffCanvas{{/regions.offcanvas.hasblocks}}{{#includesmartmenu}}, SmartMenu{{/includesmartmenu}}) { Drawer.init(); M.util.js_complete('theme_boost/loader'); {{#regions.offcanvas.hasblocks}}OffCanvas.init();{{/regions.offcanvas.hasblocks}} - SmartMenu.init(); + {{#includesmartmenu}}SmartMenu.init();{{/includesmartmenu}} }); {{/js}}