MediaWiki:Gadget-ICU.js
Jump to navigation
Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes.
- Internet Explorer: hold down the Ctrl key and click the Refresh or Reload button, or press Ctrl+F5.
- Firefox: hold down the Shift key while clicking Reload; alternatively press Ctrl+F5 or Ctrl-Shift-R.
- Opera, Konqueror and Safari users can just click the Reload button.
- Chrome: press Ctrl+F5 or Shift+F5
// <nowiki>
// ICU Admin Tool - Adds "Mark for ICU" button to page actions menu
// Only loads for users with editsemiprotected right (handled by gadget definition)
if (mw.config.get('wgNamespaceNumber') != -1) { // Don't show on special pages
mw.loader.using(['oojs-ui-core', 'oojs-ui-windows']).done(function () {
// Prevent duplicate buttons
if (document.getElementById("ca-icu")) return;
$(function () {
var timestamp = '~~~~~'; // Timestamp only (no signature)
// Create window manager for dialogs
var windowManager = new OO.ui.WindowManager();
$(document.body).append(windowManager.$element);
// Create radio select widget with ICU reasons
var radioSelect = new OO.ui.RadioSelectWidget({
items: [
new OO.ui.RadioOptionWidget({
data: "random",
label: "The truth is funnier than nonsense",
}),
new OO.ui.RadioOptionWidget({
data: "format",
label: "Very poorly formatted",
}),
new OO.ui.RadioOptionWidget({
data: "short",
label: "Very short",
}),
new OO.ui.RadioOptionWidget({
data: "notfunny",
label: "Not funny",
}),
new OO.ui.RadioOptionWidget({
data: "list",
label: "Random lists are not funny",
}),
new OO.ui.RadioOptionWidget({
data: "encyclopedic",
label: "This does not look like an article parodying Wikipedia",
}),
]
});
// Create selection dialog
function ICUDialog(config) {
ICUDialog.super.call(this, config);
}
OO.inheritClass(ICUDialog, OO.ui.ProcessDialog);
ICUDialog.static.name = 'icuDialog';
ICUDialog.static.title = 'Mark for ICU';
ICUDialog.static.actions = [
{ action: 'submit', label: 'Add ICU tag', flags: ['primary', 'progressive'] },
{ label: 'Cancel', flags: 'safe' }
];
ICUDialog.prototype.initialize = function () {
ICUDialog.super.prototype.initialize.apply(this, arguments);
var content = new OO.ui.PanelLayout({
padded: true,
expanded: false
});
content.$element.append(
$('<p>').text('Select a reason to add an ICU tag:'),
radioSelect.$element
);
this.content = content;
this.$body.append(content.$element);
};
ICUDialog.prototype.getActionProcess = function (action) {
var dialog = this;
if (action === 'submit') {
return new OO.ui.Process(function () {
var selectedItem = radioSelect.findSelectedItem();
if (!selectedItem) {
OO.ui.alert('Please select a reason before submitting.');
return;
}
// Show loading message
dialog.pushPending();
// Edit page to add ICU template
new mw.Api().postWithToken('csrf', {
action: 'edit',
title: mw.config.get('wgPageName'),
appendtext: '\n{{ICU|' + timestamp + '|sub=' + selectedItem.getData() + '}}',
summary: 'Adding an [[ICU]] tag',
minor: true
}).done(function () {
dialog.popPending();
dialog.close({ action: 'submit' });
// Show success dialog
OO.ui.confirm('Done. Would you like to refresh the page?').done(function (confirmed) {
if (confirmed) {
location.reload();
}
});
}).fail(function (error) {
dialog.popPending();
OO.ui.alert('Failed to add ICU tag: ' + error);
});
});
}
return ICUDialog.super.prototype.getActionProcess.call(this, action);
};
ICUDialog.prototype.getBodyHeight = function () {
return this.content.$element.outerHeight(true);
};
// Create and add dialog to window manager
var icuDialog = new ICUDialog({
size: 'medium'
});
windowManager.addWindows([icuDialog]);
// Add ICU button to page actions menu
mw.util.addPortletLink(
"p-cactions",
"#",
"Mark for ICU",
"ca-icu",
"Mark this page for Intensive Care Unit review"
);
// Attach click handler
document.getElementById("ca-icu").addEventListener("click", function (e) {
e.preventDefault();
windowManager.openWindow(icuDialog);
});
});
});
}
//</nowiki>