No edit summary |
No edit summary |
||
Line 177: | Line 177: | ||
})(jQuery); | })(jQuery); | ||
$(document).ready(function() { | |||
// Function to save the current state of the menu | |||
function saveMenuState() { | |||
var menuState = { | |||
openSections: [] | |||
}; | |||
// Find all open menu sections and save their IDs | |||
$('.menu-section .submenu').each(function() { | |||
if ($(this).css('display') !== 'none') { | |||
menuState.openSections.push($(this).data('menu-id')); | |||
} | |||
}); | |||
// Save the state to local storage | |||
localStorage.setItem('menuState', JSON.stringify(menuState)); | |||
} | |||
// Function to restore the state of the menu | |||
function restoreMenuState() { | |||
var savedState = localStorage.getItem('menuState'); | |||
if (savedState) { | |||
var menuState = JSON.parse(savedState); | |||
// Open the sections that were previously open | |||
$.each(menuState.openSections, function(index, sectionId) { | |||
var section = $('[data-menu-id="' + sectionId + '"]'); | |||
if (section.length) { | |||
section.show(); | |||
} | |||
}); | |||
} | |||
} | |||
// Function to toggle a section open or closed | |||
function toggleSection(element) { | |||
var submenu = $(element).next('.submenu'); | |||
submenu.toggle(); | |||
// Save the updated state | |||
saveMenuState(); | |||
} | |||
// Event listeners for the menu items | |||
$('.menu-header').click(function() { | |||
toggleSection(this); | |||
}); | |||
// Restore the menu state when the page loads | |||
restoreMenuState(); | |||
}); |
Revision as of 09:49, 25 November 2023
/* Any JavaScript here will be loaded for all users on every page load. */
$(document).ready(function () {
$.get(mw.util.wikiScript('api'), {
action: 'query',
meta: 'userinfo',
format: 'json'
}).done(function (data) {
if (data.query.userinfo.id !== 0) {
var username = data.query.userinfo.name;
var userLink = mw.util.getUrl('User:' + username);
var logoutLink = mw.util.getUrl('Special:Logout');
$('#user-info').html('<a href="' + userLink + '" class="text-white">' + username + '</a>' +
' | <a href="' + logoutLink + '" class="text-white">Logout</a>');
}
});
});
document.getElementById('offcanvas-toggler').addEventListener('click', function() {
var sidebar = document.getElementById('offcanvas-menu');
if (sidebar.classList.contains('show')) {
sidebar.classList.remove('show');
} else {
sidebar.classList.add('show');
}
});
(function() {
function toggleSection(header) {
var submenu = header.nextElementSibling;
var menuState = JSON.parse(localStorage.getItem('menuState') || '{}');
var menuKey = header.innerText.trim();
if (submenu.style.display === "none" || submenu.style.display === "") {
submenu.style.display = "block";
menuState[menuKey] = 'block';
} else {
submenu.style.display = "none";
menuState[menuKey] = 'none';
}
localStorage.setItem('menuState', JSON.stringify(menuState));
}
window.toggleSection = toggleSection;
window.onload = function() {
var menuState = JSON.parse(localStorage.getItem('menuState') || '{}');
var headers = document.querySelectorAll('.menu-header');
headers.forEach(function(header, index) {
var menuKey = header.innerText.trim();
var submenu = header.nextElementSibling;
if (menuState.hasOwnProperty(menuKey)) {
submenu.style.display = menuState[menuKey];
} else {
submenu.style.display = 'none';
}
});
};
})();
$(document).ready(function() {
$('#offcanvas-close').on('click', function() {
$('#offcanvas-menu').removeClass('show');
});
});
document.addEventListener('DOMContentLoaded', function() {
var form = document.querySelector('.namespace-search-form');
if (form) {
form.addEventListener('submit', function(e) {
var input = form.querySelector('#bs-extendedsearch-input');
if (input) {
var namespace = mw.config.get('wgCanonicalNamespace');
if (namespace && namespace.length > 0) {
input.value = namespace + ": " + input.value;
}
}
});
}
});
(function($) {
'use strict';
var css = [
'#suggestion-container {',
' position: relative;',
' width: 100%;',
'}',
'#suggestion-box {',
' position: absolute;',
' top: 100%;',
' left: 0;',
' width: 100%;',
' margin-top: 5px;',
' border: 1px solid #ccc;',
' background-color: #fff;',
' z-index: 1000;',
'}',
'.suggestion-item {',
' padding: 8px;',
' cursor: pointer;',
'}',
'.suggestion-item:hover {',
' background-color: #e0e0e0;',
'}'
].join('\n');
$('head').append('<style type="text/css">' + css + '</style>');
$('#suggestion-container').append('<div id="suggestion-box"></div>');
function showSuggestions() {
var query = $(this).val();
if (query.length > 0) {
var apiUrl = "https://newwiki.mdriven.net/api.php";
var requestData = {
action: "bs-extendedsearch-autocomplete",
format: "json",
q: JSON.stringify({
query: {
bool: {
must: {
match: {
ac_ngram: {
query: query
}
}
}
}
},
size: 8
}),
searchData: JSON.stringify({
namespace: 0,
value: query,
mainpage: ""
})
};
$.ajax({
url: apiUrl,
data: requestData,
dataType: "json",
method: "GET",
success: function(data) {
var suggestions = data.suggestions || [];
$('#suggestion-box').empty();
$.each(suggestions, function(index, suggestion) {
var item = $('<div class="suggestion-item"></div>').text(suggestion.basename);
$('#suggestion-box').append(item);
});
},
error: function(jqxhr, textStatus, error) {
console.error('Error fetching suggestions:', error);
}
});
}
}
function hideSuggestions(event) {
if (!$(event.target).closest('#suggestion-container').length) {
$('#suggestion-box').empty();
}
}
function selectSuggestion(event) {
event.preventDefault(); // Prevent the mousedown event from triggering blur on the search input
var selectedText = $(this).text();
window.location.href = '/index.php?title=' + encodeURIComponent(selectedText);
}
$('.search-input').on('input', showSuggestions);
$(document).on('click', hideSuggestions);
$('#suggestion-box').on('mousedown', '.suggestion-item', selectSuggestion);
})(jQuery);
$(document).ready(function() {
// Function to save the current state of the menu
function saveMenuState() {
var menuState = {
openSections: []
};
// Find all open menu sections and save their IDs
$('.menu-section .submenu').each(function() {
if ($(this).css('display') !== 'none') {
menuState.openSections.push($(this).data('menu-id'));
}
});
// Save the state to local storage
localStorage.setItem('menuState', JSON.stringify(menuState));
}
// Function to restore the state of the menu
function restoreMenuState() {
var savedState = localStorage.getItem('menuState');
if (savedState) {
var menuState = JSON.parse(savedState);
// Open the sections that were previously open
$.each(menuState.openSections, function(index, sectionId) {
var section = $('[data-menu-id="' + sectionId + '"]');
if (section.length) {
section.show();
}
});
}
}
// Function to toggle a section open or closed
function toggleSection(element) {
var submenu = $(element).next('.submenu');
submenu.toggle();
// Save the updated state
saveMenuState();
}
// Event listeners for the menu items
$('.menu-header').click(function() {
toggleSection(this);
});
// Restore the menu state when the page loads
restoreMenuState();
});
This page was edited 146 days ago on 08/26/2024. What links here