MediaWiki:Common.js: Difference between revisions
From Future Of Mankind
No edit summary Tags: Manual revert Reverted |
No edit summary Tag: Reverted |
||
Line 37: | Line 37: | ||
console.log('btnHideGerman does not exist'); | console.log('btnHideGerman does not exist'); | ||
} | } | ||
(function () { | |||
'use strict'; | |||
// Define the Tooltip class | |||
class Tooltip { | |||
constructor(element) { | |||
this.element = element; | |||
this.setup(); | |||
} | |||
setup() { | |||
this.placeholder = this.element.querySelector('[data-tooltip-placeholder]'); | |||
this.dropdown = this.element.querySelector('[data-tooltip-dropdown]'); | |||
this.placeholder.addEventListener('mouseover', () => this.handleDropdownPosition()); | |||
this.placeholder.addEventListener('touchstart', () => this.toggle()); | |||
} | |||
handleDropdownPosition() { | |||
const screenPadding = 16; | |||
const placeholderRect = this.placeholder.getBoundingClientRect(); | |||
const dropdownRect = this.dropdown.getBoundingClientRect(); | |||
const dropdownRightX = dropdownRect.x + dropdownRect.width; | |||
const placeholderRightX = placeholderRect.x + placeholderRect.width; | |||
if (dropdownRect.x < 0) { | |||
this.dropdown.style.left = '0'; | |||
this.dropdown.style.right = 'auto'; | |||
this.dropdown.style.transform = | |||
translateX(${-placeholderRect.x + screenPadding}px) | |||
; | |||
} else if (dropdownRightX > window.outerWidth) { | |||
this.dropdown.style.left = 'auto'; | |||
this.dropdown.style.right = '0'; | |||
this.dropdown.style.transform = | |||
translateX(${(window.outerWidth - placeholderRightX) - screenPadding}px) | |||
; | |||
} | |||
} | |||
toggle() { | |||
if (this.element.classList.contains('tooltip--open')) { | |||
this.close(); | |||
} else { | |||
this.open(); | |||
} | |||
} | |||
open() { | |||
this.element.classList.add('tooltip--open'); | |||
this.handleDropdownPosition(); | |||
} | |||
close() { | |||
this.element.classList.remove('tooltip--open'); | |||
} | |||
} | |||
// Function to dismiss all tooltips | |||
function dismissAllTooltips(event) { | |||
if (typeof event.target.closest !== 'function') return; | |||
const currentTooltip = event.target.closest('.tooltip'); | |||
document.querySelectorAll('.tooltip--open').forEach(tooltip => { | |||
if (tooltip === currentTooltip) return; | |||
tooltip.classList.remove('tooltip--open'); | |||
}); | |||
} | |||
// Initialize tooltips on the page | |||
document.querySelectorAll('.tooltip').forEach(tooltipElement => { | |||
new Tooltip(tooltipElement); | |||
}); | |||
// Add event listener to dismiss tooltips on touchstart | |||
document.addEventListener('touchstart', e => dismissAllTooltips(e)); | |||
})(); |
Revision as of 14:22, 10 September 2024
/* Any JavaScript here will be loaded for all users on every page load. */ //collapsible table column test, based on: https://codepen.io/feger/pen/eDybC $(".btnHideEnglish").click(function() { console.log('Hiding first column of table with an id of report'); $("table#collapsible_report > tbody:last-child > tr > th:first-child, table#collapsible_report > tbody:last-child > tr > td:first-child").toggle(); $(this).text($(this).text() == "Hide English" ? "Show English" : "Hide English"); $("table#collapsible_report").css({ "width": $(this).text() == "Show English" ? "80%" : "100%" }); }); $(".btnHideHigh-German").click(function() { console.log('Hiding first column of table with an id of report'); $("table#collapsible_report > tbody:last-child > tr > th:last-child, table#collapsible_report > tbody:last-child > tr > td:last-child").toggle(); $(this).text($(this).text() == "Hide High German" ? "Show High German" : "Hide High German"); $("table#collapsible_report").css({ "width": $(this).text() == "Show High German" ? "80%" : "100%" }); }); $(".btnHideGerman").click(function() { console.log('Hiding first column of table with an id of report'); $("table#collapsible_report > tbody:last-child > tr > th:last-child, table#collapsible_report > tbody:last-child > tr > td:last-child").toggle(); $(this).text($(this).text() == "Hide German" ? "Show German" : "Hide German"); $("table#collapsible_report").css({ "width": $(this).text() == "Show German" ? "80%" : "100%" }); }); // hide the german by default, on page load, but only if the btnHideGerman element exists on the page if (document.getElementById("btnHideGerman")) { console.log('btnHideGerman exists'); //$("#btnHideGerman").click(); } else { // do stuff console.log('btnHideGerman does not exist'); } (function () { 'use strict'; // Define the Tooltip class class Tooltip { constructor(element) { this.element = element; this.setup(); } setup() { this.placeholder = this.element.querySelector('[data-tooltip-placeholder]'); this.dropdown = this.element.querySelector('[data-tooltip-dropdown]'); this.placeholder.addEventListener('mouseover', () => this.handleDropdownPosition()); this.placeholder.addEventListener('touchstart', () => this.toggle()); } handleDropdownPosition() { const screenPadding = 16; const placeholderRect = this.placeholder.getBoundingClientRect(); const dropdownRect = this.dropdown.getBoundingClientRect(); const dropdownRightX = dropdownRect.x + dropdownRect.width; const placeholderRightX = placeholderRect.x + placeholderRect.width; if (dropdownRect.x < 0) { this.dropdown.style.left = '0'; this.dropdown.style.right = 'auto'; this.dropdown.style.transform = translateX(${-placeholderRect.x + screenPadding}px) ; } else if (dropdownRightX > window.outerWidth) { this.dropdown.style.left = 'auto'; this.dropdown.style.right = '0'; this.dropdown.style.transform = translateX(${(window.outerWidth - placeholderRightX) - screenPadding}px) ; } } toggle() { if (this.element.classList.contains('tooltip--open')) { this.close(); } else { this.open(); } } open() { this.element.classList.add('tooltip--open'); this.handleDropdownPosition(); } close() { this.element.classList.remove('tooltip--open'); } } // Function to dismiss all tooltips function dismissAllTooltips(event) { if (typeof event.target.closest !== 'function') return; const currentTooltip = event.target.closest('.tooltip'); document.querySelectorAll('.tooltip--open').forEach(tooltip => { if (tooltip === currentTooltip) return; tooltip.classList.remove('tooltip--open'); }); } // Initialize tooltips on the page document.querySelectorAll('.tooltip').forEach(tooltipElement => { new Tooltip(tooltipElement); }); // Add event listener to dismiss tooltips on touchstart document.addEventListener('touchstart', e => dismissAllTooltips(e)); })();