Skip to content

Commit

Permalink
Adds remembering of active admin tab on page reload.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwehr committed Jul 17, 2023
1 parent b674193 commit b138778
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
10 changes: 10 additions & 0 deletions amd/build/admintabs.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions amd/build/admintabs.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions amd/src/admintabs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Theme Boost Union - JS code current admin tab selector
*
* @module theme_boost_union/admintab
* @copyright 2023 Mario Wehr <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define(['jquery'], function($) {
"use strict";

/**
* Initialising.
*/
function initAdminTabs() {
// Wait for bootstrap
whenBootstrapAvailable(() => {
const sessionKey = "boost_union_active_admin_tab";
// Register for all boost union tabs
$('a[href^="#theme_boost_union_"]').parent().on("shown.bs.tab", function() {
// Store active tab in session
sessionStorage.setItem(sessionKey, $(this).children("a").eq(0).attr("href"));
});
// Get active tab from session
const activeTab = sessionStorage.getItem(sessionKey);
if (activeTab) {
// Show active tab from session
$('a[href="' + activeTab + '"]').tab("show");
}
});
}

/**
* Wait for Bootstrap is finally loaded
* @param {function} callback
*/
function whenBootstrapAvailable(callback) {
window.setTimeout(() => {
if (typeof $().tab == "function") {
callback();
} else {
whenBootstrapAvailable(callback);
}
}, 100);
}

return {
init: function() {
initAdminTabs();
}
};
});
4 changes: 4 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
defined('MOODLE_INTERNAL') || die();

if ($hassiteconfig || has_capability('theme/boost_union:configure', context_system::instance())) {
global $PAGE;

// How this file works:
// This theme's settings are divided into multiple settings pages.
Expand All @@ -44,6 +45,9 @@
// Avoid that the theme settings page is auto-created.
$settings = null;

// Load admin tab remember js
$PAGE->requires->js_call_amd('theme_boost_union/admintabs', 'init');

// Create custom admin settings category.
$ADMIN->add('themes', new admin_category('theme_boost_union',
get_string('pluginname', 'theme_boost_union', null, true)));
Expand Down

0 comments on commit b138778

Please sign in to comment.