MediaWiki:Gadget-checkBox.js: Difference between revisions
Jump to navigation
Jump to search
m Claycorp moved page MediaWiki:Gadgets-checkBox.js to MediaWiki:Gadget-checkBox.js without leaving a redirect: Misspelled title |
replace don't warp. |
||
| Line 3: | Line 3: | ||
$(function () { | $(function () { | ||
const $checklists = $("div.checklist"); | const $checklists = $("div.checklist"); | ||
if (!$checklists.length) return; | if (!$checklists.length) return; | ||
const pageKey = mw.config.get('wgPageName') || 'defaultChecklist'; | const pageKey = mw.config.get('wgPageName') || 'defaultChecklist'; | ||
function hashString(str) { | function hashString(str) { | ||
let hash = 0; | let hash = 0; | ||
| Line 17: | Line 16: | ||
} | } | ||
$checklists.each(function (listIndex) { | $checklists.each(function (listIndex) { | ||
const $checklist = $(this); | const $checklist = $(this); | ||
| Line 29: | Line 27: | ||
} | } | ||
// Find and replace each <li> with a <label> | |||
$checklist.find("li").each(function () { | $checklist.find("li").each(function () { | ||
const $li = $(this); | const $li = $(this); | ||
| Line 42: | Line 41: | ||
}); | }); | ||
const | const $label = $('<label>') | ||
$li.empty(). | .css({ display: 'block', margin: '5px 0' }) | ||
.append($checkbox) | |||
.append(" ") | |||
.append($li.contents()); | |||
$li.replaceWith($label); | |||
}); | |||
// Remove the <ul> wrapper if empty | |||
$checklist.find("ul").each(function () { | |||
if (!$(this).children().length) { | |||
$(this).remove(); | |||
} | |||
}); | }); | ||
// Add Reset button | |||
const resetButton = $('<button>') | const resetButton = $('<button>') | ||
.text('Reset This Checklist') | .text('Reset This Checklist') | ||
.css({ margin: '10px 0', display: 'block' }) | .css({ margin: '10px 0', display: 'block' }) | ||
.on('click', function () { | .on('click', function () { | ||
$checklist.find(" | $checklist.find("input[type=checkbox]").each(function () { | ||
$(this).prop("checked", false); | $(this).prop("checked", false); | ||
}); | }); | ||
| Line 56: | Line 68: | ||
}); | }); | ||
$checklist. | $checklist.prepend(resetButton); | ||
}); | }); | ||
}); | }); | ||
Revision as of 09:58, 15 October 2025
//This code was generated with Copilot.
$(function () {
const $checklists = $("div.checklist");
if (!$checklists.length) return;
const pageKey = mw.config.get('wgPageName') || 'defaultChecklist';
function hashString(str) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = ((hash << 5) - hash) + str.charCodeAt(i);
hash |= 0;
}
return hash.toString();
}
$checklists.each(function (listIndex) {
const $checklist = $(this);
const checklistKey = `${pageKey}-checklist-${listIndex}`;
let savedState = {};
try {
savedState = JSON.parse(localStorage.getItem(checklistKey)) || {};
} catch (e) {
savedState = {};
}
// Find and replace each <li> with a <label>
$checklist.find("li").each(function () {
const $li = $(this);
const rawText = $li.text().trim();
const itemId = hashString(rawText);
const isChecked = savedState[itemId] === true;
const $checkbox = $('<input type="checkbox">')
.prop("checked", isChecked)
.on("change", function () {
savedState[itemId] = $(this).prop("checked");
localStorage.setItem(checklistKey, JSON.stringify(savedState));
});
const $label = $('<label>')
.css({ display: 'block', margin: '5px 0' })
.append($checkbox)
.append(" ")
.append($li.contents());
$li.replaceWith($label);
});
// Remove the <ul> wrapper if empty
$checklist.find("ul").each(function () {
if (!$(this).children().length) {
$(this).remove();
}
});
// Add Reset button
const resetButton = $('<button>')
.text('Reset This Checklist')
.css({ margin: '10px 0', display: 'block' })
.on('click', function () {
$checklist.find("input[type=checkbox]").each(function () {
$(this).prop("checked", false);
});
localStorage.removeItem(checklistKey);
});
$checklist.prepend(resetButton);
});
});