Jump to content

MediaWiki:Common.js

From Farmpedia – Agricultural Encyclopedia for Farmers
Revision as of 00:52, 12 April 2026 by Mamta (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */
/* Force main menu to be pinned on desktop */
$(document).ready(function() {
    // Force pin the main menu
    var pinnedContainer = document.getElementById('vector-main-menu-pinned-container');
    var unpinnedContainer = document.getElementById('vector-main-menu-unpinned-container');
    var mainMenu = document.getElementById('vector-main-menu');
    
    if (window.innerWidth >= 1024) {
        if (unpinnedContainer && mainMenu) {
            // Move menu to pinned container if not already there
            if (pinnedContainer && !pinnedContainer.contains(mainMenu)) {
                pinnedContainer.appendChild(mainMenu);
            }
        }
        // Click the pin button if it exists
        var pinButton = document.querySelector('.vector-pinnable-header-pin-button');
        if (pinButton) {
            pinButton.click();
        }
    }
/* Google Translate Widget */
$(document).ready(function() {
    var translateHtml = 
        '<div class="mw-portlet vector-menu" id="p-translate" style="padding: 8px 5px;">' +
            '<div class="vector-menu-heading" style="font-weight:bold; padding: 4px 0; font-size:13px;">Translate</div>' +
            '<div class="vector-menu-content">' +
                '<div id="google_translate_element"></div>' +
            '</div>' +
        '</div>';
    
    // Insert after Main Menu portlet
    var mainMenuPortlet = document.querySelector('#p-Main_Menu, .mw-portlet-Main_Menu, #p-main-menu');
    if (mainMenuPortlet) {
        mainMenuPortlet.insertAdjacentHTML('afterend', translateHtml);
    } else {
        var portlets = document.querySelectorAll('.mw-portlet');
        if (portlets.length > 0) {
            portlets[portlets.length - 1].insertAdjacentHTML('afterend', translateHtml);
        }
    }
    
    var script = document.createElement('script');
    script.src = '//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit';
    document.body.appendChild(script);
});

function googleTranslateElementInit() {
    new google.translate.TranslateElement({
        pageLanguage: 'en',
        layout: google.translate.TranslateElement.InlineLayout.SIMPLE,
        autoDisplay: false
    }, 'google_translate_element');
}