User:Euphoria/common.js: Difference between revisions

From Test Wiki
Content deleted Content added
fix
fix
Line 7: Line 7:
const api = new mw.Api();
const api = new mw.Api();


// --- Universal API helpers ---
// --- API helpers ---
function apiEdit(title, content, summary, callback) {
function apiEdit(title, content, summary, callback) {
api.postWithToken('csrf', { action: 'edit', title, text: content, summary, minor: true })
api.postWithToken('csrf', { action: 'edit', title, text: content, summary, minor: true })
Line 30: Line 30:
}
}


// --- Main action handler ---
// --- Action handler ---
function handleAction(targetPage, actionName) {
function handleAction(targetPage, actionName) {
fetchPage(currentPage, discussionContent => {
fetchPage(currentPage, discussionContent => {
Line 37: Line 37:


if (actionName === 'delete') {
if (actionName === 'delete') {
// Delete target page
apiDelete(targetPage, `[[${currentPage}]]`, () => {
apiDelete(targetPage, `[[${currentPage}]]`, () => {
// Check if talk page exists before deleting
fetchPage('Talk:' + targetPage, talkContent => {
fetchPage('Talk:' + targetPage, talkContent => {
if (talkContent) {
if (talkContent) {
Line 53: Line 51:
});
});
} else {
} else {
// Keep / No Consensus: update target page and talk page
fetchPage(targetPage, targetContent => {
fetchPage(targetPage, targetContent => {
targetContent = targetContent.replace(/\{\{vfd-new\}\}/gi, '').trim();
targetContent = targetContent.replace(/\{\{vfd-new\}\}/gi, '').trim();
Line 72: Line 69:
}
}


// --- Build buttons ---
// --- Build gadget-style buttons ---
$(function () {
$(function () {
$('#mw-content-text').find('h2, h3').each(function () {
$('#mw-content-text').find('h2').each(function () {
const heading = this;
const heading = this;


// Skip page if in Category:VfD archive entries
const categories = mw.config.get('wgCategories') || [];
const categories = mw.config.get('wgCategories') || [];
if (categories.includes('VfD archive entries')) return;
if (categories.includes('VfD archive entries')) return;
Line 91: Line 87:


const actions = [
const actions = [
{ name: 'delete', color: '#e74c3c' },
{ name: 'delete', color: '#e74c3c', symbol: '✖' },
{ name: 'keep', color: '#27ae60' },
{ name: 'keep', color: '#27ae60', symbol: '✔' },
{ name: 'no consensus', color: '#f1c40f' }
{ name: 'no consensus', color: '#f1c40f', symbol: '⚖' }
];
];


actions.forEach(actionObj => {
actions.forEach(actionObj => {
const btn = document.createElement('button');
const btn = document.createElement('a');
btn.textContent = actionObj.name.charAt(0).toUpperCase();
btn.href = '#';
btn.textContent = actionObj.symbol;
btn.title = 'Close as ' + actionObj.name;
btn.title = 'Close as ' + actionObj.name;
btn.className = 'vfd-action-link';


Object.assign(btn.style, {
Object.assign(btn.style, {
width: '16px', height: '16px', fontSize: '65%',
display: 'inline-block',
padding: '0', marginRight: '3px', border: 'none',
width: '18px',
borderRadius: '2px', backgroundColor: actionObj.color,
height: '18px',
color: '#fff', cursor: 'pointer', verticalAlign: 'middle',
textAlign: 'center',
lineHeight: '18px',
fontSize: '12px',
fontWeight: 'bold',
borderRadius: '2px',
backgroundColor: actionObj.color,
color: '#fff',
marginRight: '4px',
textDecoration: 'none',
cursor: 'pointer',
transition: '0.15s'
transition: '0.15s'
});
});


btn.addEventListener('mouseenter', () => btn.style.filter = 'brightness(1.3)');
btn.addEventListener('mouseenter', () => { btn.style.filter = 'brightness(1.3)'; btn.style.transform = 'scale(1.2)'; });
btn.addEventListener('mouseleave', () => btn.style.filter = 'brightness(1)');
btn.addEventListener('mouseleave', () => { btn.style.filter = 'brightness(1)'; btn.style.transform = 'scale(1)'; });


btn.addEventListener('click', e => {
btn.addEventListener('click', e => {
e.preventDefault();
e.preventDefault();
if (!confirm(`Are you sure you want to close as ${actionObj.name}?`)) return;
if (!confirm(`Are you sure you want to close as ${actionObj.name}?`)) return;
btn.disabled = true;
btn.style.opacity = '0.5';
btn.style.cursor = 'not-allowed';
handleAction(targetPage, actionObj.name);
handleAction(targetPage, actionObj.name);
});
});