MediaWiki:Common.js: Difference between revisions
From Hidden Mickey Wiki
No edit summary Tag: Reverted |
No edit summary Tag: Reverted |
||
| Line 91: | Line 91: | ||
}); | }); | ||
$('. | ( function ( $, mw ) { | ||
var ts = | 'use strict'; | ||
if (!ts) | |||
if ( | // Helpers | ||
function formatTime(d) { return d.toLocaleTimeString('en-US'); } | |||
}); | function storageKey(id) { return 'mw-cb-' + id; } | ||
// Ensure element has an id (create a stable one if missing) | |||
function ensureId($el, prefix) { | |||
var id = $el.attr('id'); | |||
if (!id) { | |||
id = prefix + '-' + Math.floor(Date.now() / 1000) + '-' + Math.floor(Math.random() * 10000); | |||
$el.attr('id', id); | |||
} | |||
return id; | |||
} | |||
// Locate or create the timestamp box for a given checkbox id | |||
function findTsBox(id, $context) { | |||
var $box = $('#' + id + '-ts'); | |||
if ($box.length) return $box; | |||
// try common places: sibling .timestamp-box, next cell, etc. | |||
$box = $('#' + id).siblings('.timestamp-box').first(); | |||
if ($box.length) return $box; | |||
$box = $('#' + id).closest('td,th').next().find('.timestamp-box').first(); | |||
if ($box.length) return $box; | |||
// fallback: create after the checkbox | |||
return $('<span>', { id: id + '-ts', 'class': 'timestamp-box' }).insertAfter($('#' + id)); | |||
} | |||
// Initialize a single checkbox element | |||
function setupCheckbox($cb) { | |||
if ($cb.data('mwCbInit')) return; // avoid double-init | |||
$cb.data('mwCbInit', true); | |||
var id = ensureId($cb, 'mwcb'); | |||
var $ts = findTsBox(id); | |||
// Restore saved state if present | |||
try { | |||
var raw = localStorage.getItem(storageKey(id)); | |||
if (raw) { | |||
var data = JSON.parse(raw); | |||
$cb.prop('checked', !!data.checked); | |||
$ts.text(data.timestamp || ''); | |||
} | |||
} catch (e) { | |||
console && console.warn && console.warn('mw-cb restore error', e); | |||
} | |||
// Change handler | |||
$cb.on('change.mwCb', function () { | |||
if ($cb.prop('checked')) { | |||
var ts = formatTime(new Date()); | |||
$ts.text(ts); | |||
try { | |||
localStorage.setItem(storageKey(id), JSON.stringify({ checked: true, timestamp: ts })); | |||
} catch (e) { /* ignore */ } | |||
} else { | |||
$ts.text(''); | |||
try { localStorage.removeItem(storageKey(id)); } catch (e) { /* ignore */ } | |||
} | |||
}); | |||
} | |||
// Convert any legacy span.clickable-checkbox into real input (optional) | |||
function convertSpans($context) { | |||
$context.find('span.clickable-checkbox').each(function () { | |||
var $s = $(this); | |||
if ($s.data('mwConverted')) return; | |||
$s.data('mwConverted', true); | |||
var id = $s.data('cb-id') || $s.attr('id') || null; | |||
var $input = $('<input>', { type: 'checkbox', class: 'clickable-checkbox' }); | |||
if (id) $input.attr('id', id); | |||
$s.empty().append($input); | |||
}); | |||
} | |||
// Initialize all checkboxes within a context (document or a portion) | |||
function init(context) { | |||
var $ctx = context ? $(context) : $(document); | |||
convertSpans($ctx); // optional; harmless if you already use HTMLTAG | |||
$ctx.find('input.clickable-checkbox').each(function () { | |||
setupCheckbox($(this)); | |||
}); | |||
} | |||
// Run on initial load and on dynamic content loads (so it works with AJAX content) | |||
$(function () { | |||
init(document); | |||
mw.hook('wikipage.content').add(function ( $content ) { | |||
init($content); | |||
}); | |||
}); | |||
}( jQuery, mw )); | |||
Revision as of 07:33, 30 September 2025
/* Any JavaScript here will be loaded for all users on every page load. */
// JavaScript code to save checkbox state and restore it when the page loads
$(document).ready(function() {
// Function to save the state of checkboxes to localStorage
function saveCheckboxState() {
$('input[type="checkbox"]').each(function() {
localStorage.setItem($(this).attr('id'), $(this).prop('checked'));
});
}
// Function to load the state of checkboxes from localStorage
function loadCheckboxState() {
$('input[type="checkbox"]').each(function() {
const savedState = localStorage.getItem($(this).attr('id'));
if (savedState !== null) {
$(this).prop('checked', savedState === 'true');
}
});
}
// Load the saved checkbox state when the page is loaded
loadCheckboxState();
// Save the checkbox state whenever a checkbox is changed
$('input[type="checkbox"]').change(function() {
saveCheckboxState();
});
});
// Adjust the search box width
$(document).ready(function () {
$('#searchInput').css('width', '600px'); // Adjust width as needed
});
// Add Edit Source to user dropdown
mw.loader.using('mediawiki.util', function () {
mw.util.addPortletLink( 'p-personal', mw.util.getUrl( mw.config.get('wgPageName'), { action: 'edit' } ), 'Edit Source', 'pt-editsource' );
mw.util.addPortletLink( 'p-personal', mw.util.getUrl( mw.config.get('wgPageName'), { action: 'history' } ), 'View History', 'pt-history' );
mw.util.addPortletLink( 'p-personal', mw.util.getUrl( mw.config.get('wgPageName'), { action: 'delete' } ), 'Delete', 'pt-delete' );
var moveLink = document.getElementById('ca-move');
if (moveLink) {
var a = moveLink.querySelector('a');
mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-move', a.title || 'Move this page' );
moveLink.remove();
}
mw.util.addPortletLink( 'p-personal', mw.util.getUrl( mw.config.get('wgPageName'), { action: 'protect' } ), 'Protect', 'pt-protect' );
mw.util.addPortletLink( 'p-personal', mw.util.getUrl( mw.config.get('wgPageName'), { action: 'unwatch' } ), 'Unwatch', 'pt-unwatch' );
var talkLink = document.getElementById('pt-mytalk');
talkLink.remove();
var whatLinksHereLink = document.getElementById('t-whatlinkshere');
if (whatLinksHereLink) {
var a = whatLinksHereLink.querySelector('a');
mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-whatlinkshere', a.title || 'What Links Here' );
whatLinksHereLink.remove();
}
var relatedChangesLink = document.getElementById('t-recentchangeslinked');
if (relatedChangesLink) {
var a = relatedChangesLink.querySelector('a');
mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-recentchanges', a.title || 'Recent Changes' );
relatedChangesLink.remove();
}
var uploadLink = document.getElementById('t-upload');
if (uploadLink) {
var a = uploadLink.querySelector('a');
mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-upload', a.title || 'Upload File' );
uploadLink.remove();
}
var specialPagesLink = document.getElementById('t-specialpages');
if (specialPagesLink) {
var a = specialPagesLink.querySelector('a');
mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-specialpages', a.title || 'Special Pages' );
specialPagesLink.remove();
}
var permanentLink = document.getElementById('t-permalink');
if (permanentLink) {
var a = permanentLink.querySelector('a');
mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-permalink', a.title || 'Permanent Link' );
permanentLink.remove();
}
var pageInfoLink = document.getElementById('t-info');
if (pageInfoLink) {
var a = pageInfoLink.querySelector('a');
mw.util.addPortletLink( 'p-personal', a.href, a.textContent.trim(), 'pt-info', a.title || 'Page Info' );
pageInfoLink.remove();
}
var printLink = document.getElementById('t-print');
if (printLink) {
var a = printLink.querySelector('a');
printLink.remove();
}
});
( function ( $, mw ) {
'use strict';
// Helpers
function formatTime(d) { return d.toLocaleTimeString('en-US'); }
function storageKey(id) { return 'mw-cb-' + id; }
// Ensure element has an id (create a stable one if missing)
function ensureId($el, prefix) {
var id = $el.attr('id');
if (!id) {
id = prefix + '-' + Math.floor(Date.now() / 1000) + '-' + Math.floor(Math.random() * 10000);
$el.attr('id', id);
}
return id;
}
// Locate or create the timestamp box for a given checkbox id
function findTsBox(id, $context) {
var $box = $('#' + id + '-ts');
if ($box.length) return $box;
// try common places: sibling .timestamp-box, next cell, etc.
$box = $('#' + id).siblings('.timestamp-box').first();
if ($box.length) return $box;
$box = $('#' + id).closest('td,th').next().find('.timestamp-box').first();
if ($box.length) return $box;
// fallback: create after the checkbox
return $('<span>', { id: id + '-ts', 'class': 'timestamp-box' }).insertAfter($('#' + id));
}
// Initialize a single checkbox element
function setupCheckbox($cb) {
if ($cb.data('mwCbInit')) return; // avoid double-init
$cb.data('mwCbInit', true);
var id = ensureId($cb, 'mwcb');
var $ts = findTsBox(id);
// Restore saved state if present
try {
var raw = localStorage.getItem(storageKey(id));
if (raw) {
var data = JSON.parse(raw);
$cb.prop('checked', !!data.checked);
$ts.text(data.timestamp || '');
}
} catch (e) {
console && console.warn && console.warn('mw-cb restore error', e);
}
// Change handler
$cb.on('change.mwCb', function () {
if ($cb.prop('checked')) {
var ts = formatTime(new Date());
$ts.text(ts);
try {
localStorage.setItem(storageKey(id), JSON.stringify({ checked: true, timestamp: ts }));
} catch (e) { /* ignore */ }
} else {
$ts.text('');
try { localStorage.removeItem(storageKey(id)); } catch (e) { /* ignore */ }
}
});
}
// Convert any legacy span.clickable-checkbox into real input (optional)
function convertSpans($context) {
$context.find('span.clickable-checkbox').each(function () {
var $s = $(this);
if ($s.data('mwConverted')) return;
$s.data('mwConverted', true);
var id = $s.data('cb-id') || $s.attr('id') || null;
var $input = $('<input>', { type: 'checkbox', class: 'clickable-checkbox' });
if (id) $input.attr('id', id);
$s.empty().append($input);
});
}
// Initialize all checkboxes within a context (document or a portion)
function init(context) {
var $ctx = context ? $(context) : $(document);
convertSpans($ctx); // optional; harmless if you already use HTMLTAG
$ctx.find('input.clickable-checkbox').each(function () {
setupCheckbox($(this));
});
}
// Run on initial load and on dynamic content loads (so it works with AJAX content)
$(function () {
init(document);
mw.hook('wikipage.content').add(function ( $content ) {
init($content);
});
});
}( jQuery, mw ));