User:Euphoria/common.js: Difference between revisions

From Test Wiki
Content deleted Content added
fix
fix
Line 10: Line 10:
const heading = this;
const heading = this;


// Skip headings already inside a .vfd container
// Skip headings already inside a .vfd container or without links
if ($(heading).closest('.vfd').length) return;
if ($(heading).closest('.vfd').length) return;

// Skip headings without links
if (!$(heading).find('a').length) return;
if (!$(heading).find('a').length) return;


Line 20: Line 18:


const actions = [
const actions = [
{name: 'delete', color: '#e74c3c'}, // red
{name: 'delete', color: '#e74c3c'},
{name: 'keep', color: '#27ae60'}, // green
{name: 'keep', color: '#27ae60'},
{name: 'no consensus', color: '#f1c40f'} // yellow
{name: 'no consensus', color: '#f1c40f'}
];
];


actions.forEach(function(actionObj) {
actions.forEach(function(actionObj) {
const btn = document.createElement('button');
const btn = document.createElement('button');
btn.textContent = actionObj.name.charAt(0).toUpperCase(); // D/K/N
btn.textContent = actionObj.name.charAt(0).toUpperCase();
btn.title = 'Close as ' + actionObj.name;
btn.title = 'Close as ' + actionObj.name;


// Small inline button styling
// Button styling
btn.style.width = '16px';
btn.style.width = '16px';
btn.style.height = '16px';
btn.style.height = '16px';
Line 51: Line 49:
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; // prevent double-clicks
btn.disabled = true;
const api = new mw.Api();
const api = new mw.Api();


Line 67: Line 65:
let content = pages[pageId].revisions[0].slots.main['*'];
let content = pages[pageId].revisions[0].slots.main['*'];


const topText = '{{subst:vt|' + actionObj.name + '. --~~~~}}\n';
const discussionNewContent = '{{subst:vt|' + actionObj.name + '. --~~~~}}\n' + content.trim() + '\n{{subst:vb}}';
const bottomText = '\n{{subst:vb}}';
const discussionNewContent = topText + content.trim() + bottomText;


// Edit discussion page
// Edit discussion page
Line 81: Line 77:
const link = $(heading).find('a').first();
const link = $(heading).find('a').first();
if (!link.length) {
if (!link.length) {
mw.notify('Cannot find target page link!', {title: 'VfDcloser', type: 'error', timeout: 2000});
mw.notify('Cannot find target page link!', {title: 'VfDcloser', type: 'error', timeout: 1500});
setTimeout(() => location.reload(), 2000);
setTimeout(() => location.reload(), 1500);
return;
return;
}
}
Line 88: Line 84:
const targetPage = link.attr('title') || link.text().trim();
const targetPage = link.attr('title') || link.text().trim();
if (!targetPage) {
if (!targetPage) {
mw.notify('Cannot determine target page title!', {title: 'VfDcloser', type: 'error', timeout: 2000});
mw.notify('Cannot determine target page title!', {title: 'VfDcloser', type: 'error', timeout: 1500});
setTimeout(() => location.reload(), 2000);
setTimeout(() => location.reload(), 1500);
return;
return;
}
}
Line 98: Line 94:
.done(() => {
.done(() => {
const talkPage = 'Talk:' + targetPage;
const talkPage = 'Talk:' + targetPage;

// Check if talk page exists
api.get({action: 'query', titles: talkPage, format: 'json'}).done(data => {
api.get({action: 'query', titles: talkPage, format: 'json'}).done(data => {
const talkPages = data.query.pages;
const talkPages = data.query.pages;
Line 105: Line 99:


if (talkId !== "-1") {
if (talkId !== "-1") {
// Delete talk page if exists
api.postWithToken('csrf', {action: 'delete', title: talkPage, reason: 'Parent page deleted via VfD'})
api.postWithToken('csrf', {action: 'delete', title: talkPage, reason: 'Parent page deleted via VfD'})
.done(() => {
.done(() => {
mw.notify('Discussion closed. Page and talk page deleted.', {title: 'VfDcloser', type: 'success', timeout: 2000});
mw.notify('Discussion closed. Page and talk page deleted.', {title: 'VfDcloser', type: 'success', timeout: 1500});
setTimeout(() => location.reload(), 2000);
setTimeout(() => location.reload(), 1500);
}).fail(err => mw.notify('Error deleting talk page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: 5000}));
}).fail(err => mw.notify('Error deleting talk page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: 1500}));
} else {
} else {
mw.notify('Discussion closed. Page deleted.', {title: 'VfDcloser', type: 'success', timeout: 2000});
mw.notify('Discussion closed. Page deleted.', {title: 'VfDcloser', type: 'success', timeout: 1500});
setTimeout(() => location.reload(), 2000);
setTimeout(() => location.reload(), 1500);
}
}
});
});
}).fail(err => mw.notify('Error deleting page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: 5000}));
}).fail(err => mw.notify('Error deleting page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: 1500}));
} else {
} else {
// Keep / No consensus: update target and talk page
// Keep / No consensus: update page and talk page
api.get({
api.get({
action: 'query',
action: 'query',
Line 164: Line 159:
minor: true
minor: true
}).done(() => {
}).done(() => {
mw.notify('Discussion closed. Page and talk page updated.', {title: 'VfDcloser', type: 'success', timeout: 2000});
mw.notify('Discussion closed. Page and talk page updated.', {title: 'VfDcloser', type: 'success', timeout: 1500});
setTimeout(() => location.reload(), 2000);
setTimeout(() => location.reload(), 1500);
}).fail(err => mw.notify('Error editing talk page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: 5000}));
}).fail(err => mw.notify('Error editing talk page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: 1500}));
});
});
}).fail(err => mw.notify('Error editing page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: 5000}));
}).fail(err => mw.notify('Error editing page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: 1500}));
});
});
}
}
}).fail(err => mw.notify('Error editing discussion page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: 5000}));
}).fail(err => mw.notify('Error editing discussion page: ' + JSON.stringify(err), {title: 'VfDcloser', type: 'error', timeout: 1500}));
});
});
});
});