MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 77: | Line 77: | ||
function togglePreferredExpansion() { | function togglePreferredExpansion() { | ||
togglePreference('switchExpansion', 'preferredExpansion', 'hota', 'sod', 'Enable HotA', 'Disable HotA'); | togglePreference('switchExpansion', 'preferredExpansion', 'hota', 'sod', 'Enable HotA', 'Disable HotA', 'Horn of the Abyss', ); | ||
} | } | ||
function togglePreferredView() { | function togglePreferredView() { | ||
togglePreference('switchView', 'preferredView', 'modern', 'legacy', 'Enable Modern', 'Enable Legacy'); | togglePreference('switchView', 'preferredView', 'modern', 'legacy', 'Enable Modern', 'Enable Legacy', 'Legacy/Modern'); | ||
} | } | ||
function toggleDoR() { | function toggleDoR() { | ||
togglePreference('switchDoR', 'preferredDoR', 'dor', 'nodor', 'Enable DoR', 'Disable DoR'); | togglePreference('switchDoR', 'preferredDoR', 'dor', 'nodor', 'Enable DoR', 'Disable DoR', 'Day of Reckoning'); | ||
} | } | ||
function initPreference(linkSelector, cookieName, cookie1, cookie2, link1, link2, toggleFunc) { | function initPreference(linkSelector, cookieName, cookie1, cookie2, link1, link2, prefName, toggleFunc) { | ||
var switchTo = ''; | var switchTo = ''; | ||
var userlink = document.querySelector('#pt-anonuserpage'); | var userlink = document.querySelector('#pt-anonuserpage'); | ||
Line 100: | Line 100: | ||
switchView.style = 'cursor: pointer; color: rgb(102, 177, 250);'; | switchView.style = 'cursor: pointer; color: rgb(102, 177, 250);'; | ||
switchView.id = linkSelector; | switchView.id = linkSelector; | ||
switchView.title = 'Toggle ' + prefName | |||
switchView.addEventListener('click', toggleFunc); | switchView.addEventListener('click', toggleFunc); | ||
userlink.parentElement.insertBefore(switchView, null); | userlink.parentElement.insertBefore(switchView, null); | ||
Line 125: | Line 126: | ||
switchView2.style = 'cursor: pointer; color: rgb(102, 177, 250);'; | switchView2.style = 'cursor: pointer; color: rgb(102, 177, 250);'; | ||
switchView2.id = linkSelector + '2'; | switchView2.id = linkSelector + '2'; | ||
switchView2.title = 'Toggle ' + prefName | |||
switchView2.addEventListener('click', toggleFunc); | switchView2.addEventListener('click', toggleFunc); | ||
mwpanelul.insertBefore(switchView2, null); | mwpanelul.insertBefore(switchView2, null); | ||
Line 155: | Line 157: | ||
function initCommon() { | function initCommon() { | ||
initPreference('switchExpansion', 'preferredExpansion', 'hota', 'sod', 'Enable HotA', 'Disable HotA', togglePreferredExpansion); | initPreference('switchExpansion', 'preferredExpansion', 'hota', 'sod', 'Enable HotA', 'Disable HotA', 'Horn of the Abyss', togglePreferredExpansion); | ||
initPreference('switchDoR', 'preferredDoR', 'dor', 'nodor', 'Enable DoR', 'Disable DoR', toggleDoR); | initPreference('switchDoR', 'preferredDoR', 'dor', 'nodor', 'Enable DoR', 'Disable DoR', 'Day of Reckoning', toggleDoR); | ||
var mainpage = document.querySelector('.mainpage.onlysod.onlyhota'); | var mainpage = document.querySelector('.mainpage.onlysod.onlyhota'); | ||
if (mainpage) { | if (mainpage) { | ||
initPreference('switchView', 'preferredView', 'modern', 'legacy', 'Enable Modern', 'Enable Legacy', togglePreferredView); | initPreference('switchView', 'preferredView', 'modern', 'legacy', 'Enable Modern', 'Enable Legacy', 'Legacy/Modern', togglePreferredView); | ||
removeTooltips(); | removeTooltips(); | ||
} | } |
Revision as of 06:38, 18 August 2024
/* Any JavaScript here will be loaded for all users on every page load. */
(function() {
function getCookie(cname) {
var name = cname + '=';
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return '';
}
function showElements(desc, linkSelector) {
var elems = document.querySelectorAll(desc);
for (var i = 0; i < elems.length; i++) {
elems[i].style = '';
}
elems = document.querySelectorAll('.' + linkSelector + desc);
for (var i = 0; i < elems.length; i++) {
elems[i].style = 'cursor: pointer; color: rgb(102, 177, 250);';
}
/*
if (desc == '.onlyhota' || desc == '.onlysod') {
var main_page_tab = '';
if (desc == '.onlyhota') {
main_page_tab = document.querySelector('.mainpage.onlysod.onlyhota #tabs-input-1-2');
} else {
main_page_tab = document.querySelector('.mainpage.onlysod.onlyhota #tabs-input-1-1');
}
if (main_page_tab) {
main_page_tab.click();
}
}*/
}
function hideElements(desc) {
var elems = document.querySelectorAll(desc);
for (var i = 0; i < elems.length; i++) {
elems[i].style = 'display:none;';
}
elems = document.querySelectorAll('.initialOnly');
for (var i = 0; i < elems.length; i++) {
elems[i].style = 'display:none;';
}
}
function togglePreference(linkSelector, cookieName, cookie1, cookie2, link1, link2) {
var switchTo = '';
var preference= getCookie(cookieName);
var switchView = document.querySelector('#' + linkSelector);
var switchView2 = document.querySelector('#' + linkSelector + '2');
if (preference == cookie2) {
preference = cookie1;
switchView.textContent = switchTo + link2;
switchView2.textContent = switchTo + link2;
hideElements('.only' + cookie2);
showElements('.only' + cookie1, linkSelector);
} else {
preference = cookie2;
switchView.textContent = switchTo + link1;
switchView2.textContent = switchTo + link1;
hideElements('.only' + cookie1);
showElements('.only' + cookie2, linkSelector);
}
var CookieDate = new Date();
CookieDate.setFullYear(CookieDate.getFullYear() + 1);
document.cookie = cookieName + '=' + preference + '; expires=' + CookieDate.toUTCString() + ';';
}
function togglePreferredExpansion() {
togglePreference('switchExpansion', 'preferredExpansion', 'hota', 'sod', 'Enable HotA', 'Disable HotA', 'Horn of the Abyss', );
}
function togglePreferredView() {
togglePreference('switchView', 'preferredView', 'modern', 'legacy', 'Enable Modern', 'Enable Legacy', 'Legacy/Modern');
}
function toggleDoR() {
togglePreference('switchDoR', 'preferredDoR', 'dor', 'nodor', 'Enable DoR', 'Disable DoR', 'Day of Reckoning');
}
function initPreference(linkSelector, cookieName, cookie1, cookie2, link1, link2, prefName, toggleFunc) {
var switchTo = '';
var userlink = document.querySelector('#pt-anonuserpage');
if (!userlink) {
userlink = document.querySelector('#pt-userpage');
}
if (userlink) {
var switchView = document.querySelector('#' + linkSelector);
if (!switchView) {
switchView = document.createElement('li');
switchView.style = 'cursor: pointer; color: rgb(102, 177, 250);';
switchView.id = linkSelector;
switchView.title = 'Toggle ' + prefName
switchView.addEventListener('click', toggleFunc);
userlink.parentElement.insertBefore(switchView, null);
var elems = document.querySelectorAll('.' + linkSelector);
for (var i = 0; i < elems.length; i++) {
elems[i].addEventListener('click', toggleFunc);
elems[i].style = 'cursor: pointer; color: rgb(102, 177, 250);';
}
}
if (getCookie(cookieName) == cookie2) {
switchView.textContent = switchTo + link1;
hideElements('.only' + cookie1);
showElements('.only' + cookie2);
} else {
switchView.textContent = switchTo + link2;
hideElements('.only' + cookie2);
showElements('.only' + cookie1);
}
}
var mwpanelul = document.querySelector('#mw-panel ul');
if (mwpanelul) {
var switchView2 = document.querySelector('#' + linkSelector + '2');
if (!switchView2) {
switchView2 = document.createElement('li');
switchView2.style = 'cursor: pointer; color: rgb(102, 177, 250);';
switchView2.id = linkSelector + '2';
switchView2.title = 'Toggle ' + prefName
switchView2.addEventListener('click', toggleFunc);
mwpanelul.insertBefore(switchView2, null);
}
if (getCookie(cookieName) == cookie2) {
switchView2.textContent = switchTo + link1;
hideElements('.only' + cookie1);
showElements('.only' + cookie2);
} else {
switchView2.textContent = switchTo + link2;
hideElements('.only' + cookie2);
showElements('.only' + cookie1);
}
}
}
function removeTooltips() {
var elems = document.querySelectorAll('.mainpage .tabs-content div div a');
for (var i = 0; i < elems.length; i++) {
elems[i].title = '';
}
}
function removeUnwantedTitles() {
var elems = document.querySelectorAll('#p-logo a');
for (var i = 0; i < elems.length; i++) {
elems[i].title = '';
}
}
function initCommon() {
initPreference('switchExpansion', 'preferredExpansion', 'hota', 'sod', 'Enable HotA', 'Disable HotA', 'Horn of the Abyss', togglePreferredExpansion);
initPreference('switchDoR', 'preferredDoR', 'dor', 'nodor', 'Enable DoR', 'Disable DoR', 'Day of Reckoning', toggleDoR);
var mainpage = document.querySelector('.mainpage.onlysod.onlyhota');
if (mainpage) {
initPreference('switchView', 'preferredView', 'modern', 'legacy', 'Enable Modern', 'Enable Legacy', 'Legacy/Modern', togglePreferredView);
removeTooltips();
}
removeUnwantedTitles();
}
window.addEventListener('readystatechange', function() {
initCommon();
});
window.addEventListener('DOMContentLoaded', function() {
initCommon();
});
window.addEventListener('load', function() {
initCommon();
});
initCommon();
})();