MediaWiki:Gadget-Twinkle-slettnom-body.js
Utseende
Merk: Etter publisering vil det kanskje være nødvendig å slette mellomlageret i nettleseren din for å se endringene.
- Firefox / Safari: Hold Shift mens du klikker på Oppdater, eller trykk enten Ctrl+F5 eller Ctrl+R (⌘+R på Mac)
- Google Chrome: Trykk Ctrl+Shift+R (⌘+Shift+R på Mac)
- Edge: Hold Ctrl mens du trykker på Oppdater eller trykk Ctrl+F5
/*jslint browser: true, sloppy: true, plusplus: true, indent: 4, es5: true */
/*global mediaWiki, jQuery */
/* TODO:
- Legge til nomineringssiden på overvåkningslisten (det skjer antakelig ikke automatisk?)
*/
(function () {
"use strict"; // enable strict mode within this function
var api = new mw.Api(),
numTasks = 0,
numErrors = 0,
articleCreator = '',
listPageTitle = 'Wikipedia:Sletting',
listSectionId = 3,
listEntryPoint = '<!-- Legg inn nye {{sletteforslag|<navn på side>}} rett under denne linjen, øverst, ikke nederst -->',
nominatedPage = mw.config.get('wgPageName').replace(/_/g, ' '),
nominationPage = listPageTitle + '/' + nominatedPage,
listItem = '{{Sletteforslag|' + nominatedPage + '}}',
topTemplate = '{{Sletting}}',
reason = '',
notifyUser = false,
nominatedBefore = false,
oldNomText = '',
tasks = {
'createNomPage': { 'info': 'Oppretter slettediskusjonsside', 'summary': '[[Hjelp:Slettnom|Slettnom]]: Nominerer %s for sletting.' },
'insertTemplate': { 'info': 'Setter inn <tt>{{Sletting}}</tt> i artikkelen', 'summary': '[[Hjelp:Slettnom|Slettnom]]: [[%s|Nominerer]] for sletting.' },
'notifyCreator': { 'info': 'Informerer artikkeloppretter', 'summary': '[[Hjelp:Slettnom|Slettnom]]: Nominerer [[:%s]] for sletting.' },
'addToList': { 'info': 'Oppføring på WP:S', 'summary': '[[Hjelp:Slettnom|Slettnom]]: Nominerer [[:%s]].' }
};
function disableForm() {
$('#mw-slettnom-button-send').button('option', 'disabled', true);
$('#mw-slettnom-button-cancel').button('option', 'disabled', true);
$('#mw-slettnom-message').attr("disabled", true);
}
function enableForm() {
$('#mw-slettnom-button-send').button('option', 'disabled', false);
$('#mw-slettnom-button-cancel').button('option', 'disabled', false);
$('#mw-slettnom-message').attr("disabled", false);
$('#mw-slettnom-working').hide();
}
function debug(txt) {
mw.log('SLETTNOM> ' + txt);
}
function addTask(t) {
numTasks = numTasks + 1;
}
/* ************ CreateNomPage task ************ */
function CreateNomPage() {
debug("CreateNomPage");
this.name = 'createNomPage';
var template = '',
statusId = '#mw-slettnom-status-' + this.name;
if (nominatedBefore) {
debug("Nominated before");
template = oldNomText + '\n{{Ny slettenominering}}\n' + reason + ' ~~' + '~~';
} else {
debug("Not nominated before");
template = '<noinclude>\n\
===[[:' + nominatedPage + ']]===\n\
[[Kategori:Slettediskusjoner|' + nominatedPage + ']]\n\
</noinclude>\n' + reason + ' ~~' + '~~';
}
api.post({
'action': 'edit',
'title': nominationPage,
'text': template,
'summary': tasks[this.name].summary.replace('%s', nominatedPage),
'token': mw.user.tokens.get('csrfToken')
}).done(function (result) {
if (result.edit.result.toLowerCase() === 'success') {
$(statusId).trigger('slettnomTaskDone', 'OK');
} else {
$(statusId).trigger('slettnomTaskFailed', 'ukjent feil');
}
}).fail(function (code, result) {
$(statusId).trigger('slettnomTaskFailed', code);
});
}
/* ************ InsertTemplate task ************ */
function InsertTemplate() {
this.name = 'insertTemplate';
var statusId = '#mw-slettnom-status-' + this.name,
template = topTemplate + '\n';
$(statusId).html(tasks[this.name].info + '…');
// Wrap in <noinclude> if we are in the Template namespace:
if (mw.config.get('wgNamespaceNumber') === 10) {
template = '<noinclude>' + template + '</noinclude>';
}
api.post({
'action': 'edit',
'title': nominatedPage,
'section': 0,
'prependtext': template,
'summary': tasks[this.name].summary.replace('%s', nominationPage),
'token': mw.user.tokens.get('csrfToken')
}).done(function (result) {
if (result.edit.result.toLowerCase() === 'success') {
$(statusId).trigger('slettnomTaskDone', 'OK');
} else {
$(statusId).trigger('slettnomTaskFailed', 'ukjent feil');
}
}).fail(function (code, result) {
$(statusId).trigger('slettnomTaskFailed', code);
});
}
/* ************ NotifyCreator task ************ */
function NotifyCreator() {
this.name = 'notifyCreator';
var statusId = '#mw-slettnom-status-' + this.name,
talkPage = 'Brukerdiskusjon:' + articleCreator;
$(statusId).html(tasks[this.name].info + '…');
api.post({
'action': 'edit',
'title': talkPage,
'appendtext': '\n' + $('#mw-slettnom-notify-txt').val(),
'summary': tasks[this.name].summary.replace('%s', nominatedPage),
'token': mw.user.tokens.get('csrfToken')
}).done(function (result) {
if (result.edit.result.toLowerCase() === 'success') {
$(statusId).trigger('slettnomTaskDone', talkPage + ' lagret');
} else {
$(statusId).trigger('slettnomTaskFailed', 'ukjent feil');
}
}).fail(function (code, result) {
$(statusId).trigger('slettnomTaskFailed', code);
});
}
/* ************ AddToList task ************ */
function AddToList() {
this.name = 'addToList';
var summary = tasks[this.name].summary,
statusId = '#mw-slettnom-status-' + this.name;
$(statusId).html(tasks[this.name].info + '…');
api.get({
'action': 'parse',
'page': listPageTitle,
'section': listSectionId,
'prop': 'wikitext'
}).done(function (response) {
var old_text = response.parse.wikitext['*'],
text = old_text.replace(listEntryPoint, listEntryPoint + '\n' + listItem);
if (text === old_text) {
$(statusId).trigger('slettnomTaskFailed', 'Fant ikke innsettingsmerket!');
} else {
api.post({
'action': 'edit',
'title': listPageTitle,
'section': listSectionId,
'text': text,
'summary': summary.replace('%s', nominatedPage),
'token': mw.user.tokens.get('csrfToken')
}).done(function (result) {
if (result.edit.result.toLowerCase() === 'success') {
$(statusId).trigger('slettnomTaskDone', 'OK');
} else {
$(statusId).trigger('slettnomTaskFailed', 'ukjent feil');
}
}).fail(function (code, result) {
$(statusId).trigger('slettnomTaskFailed', code);
});
}
});
}
/************* GetUserNotificationMsg task *************/
function GetUserNotificationMsg() {
api.get({
'action': 'parse',
'prop': 'text',
'title': 'Brukerdiskusjon:' + articleCreator,
'text': '{{subst' + ':Slettnom|1=' + nominatedPage + '|2=' + mw.user.getName() + '}}',
'pst': 1, // substitute
'onlypst': 1 // but don't parse wikitext into html
}).done(function (response) {
var txt = response.parse.text['*'];
$('#mw-slettnom-notify-txt').val(txt + ' ~~' + '~~');
$('#mw-slettnom-working').trigger('slettnomTaskDone', 'GetUserNotificationMsg ok');
}).fail(function (code, respo) {
$('#mw-slettnom-working').trigger('slettnomTaskFailed', code);
//$('#mw-slettnom-notify-username').html("Det oppsto en feil: "+code);
// and leave the form disabled
});
}
/************* FindArticleCreator task *************/
function FindArticleCreator() {
api.get({
'action': 'query',
'prop': 'revisions',
'titles': nominatedPage,
'rvlimit': 1,
'rvprop': 'user',
'rvdir': 'newer'
}).done(function (response) {
var pageId = mw.config.get('wgArticleId');
if (response.query.pages[pageId] === undefined) {
$('#mw-slettnom-notify-username').html("n/a");
} else {
articleCreator = response.query.pages[pageId].revisions[0].user;
api.get({
'action': 'parse',
'prop': 'text',
'text': '[[Bruker:' + articleCreator + '|' + articleCreator + ']]'
}).done(function (response) {
var txt = $(response.parse.text['*']).children();
$('#mw-slettnom-notify-username').html(txt);
});
addTask(new GetUserNotificationMsg());
$('#mw-slettnom-working').trigger('slettnomTaskDone', 'FindArticleCreator ok');
}
}).fail(function (code, respo) {
$('#mw-slettnom-notify-username').html("Det oppsto en feil: " + code);
// and leave the form disabled
});
}
/************* CheckIfNominatedBefore task *************/
function CheckIfNominatedBefore() {
debug("Checking " + nominationPage);
api.get({
'action': 'parse',
'prop': 'wikitext',
'page': nominationPage
}).done(function (result) {
debug("-> Previous nomination page found");
if (result.error !== undefined && result.error.code === 'missingtitle') {
nominatedBefore = false;
} else if (result.parse.wikitext['*'] === false) {
nominatedBefore = false;
} else {
nominatedBefore = true;
oldNomText = result.parse.wikitext['*'];
$('#mw-nominatedbefore-info').addClass('mw-slettnom-err');
$('#mw-nominatedbefore-info').html('NB! Denne siden har vært slettenominert før. Du bør lese <a href="' + mw.util.getUrl(nominationPage) + '">den forrige slettediskusjonen</a> før du nominerer siden på nytt.');
}
$('#mw-slettnom-working').trigger('slettnomTaskDone', 'CheckIfNominatedBefore ok');
}).fail(function (result) {
debug("-> No previous nomination page found");
nominatedBefore = false;
$('#mw-slettnom-working').trigger('slettnomTaskDone', 'CheckIfNominatedBefore ok');
});
}
/************* Submit nomination *************/
function cancel(event) {
event.preventDefault();
$('#mw-slettnom-dialog').dialog('close');
}
function closeAndReload(event) {
event.preventDefault();
$('#mw-slettnom-dialog').dialog('close');
location.reload();
}
function nominationComplete(event) {
$('#mw-slettnom-dialog').off('slettnomAllTasksDone');
if (numErrors > 0) {
$('#mw-slettnom-status h3').html('Ferdig! Det oppsto ' + numErrors + ' feil.');
} else {
$('#mw-slettnom-status h3').html('Ferdig!');
}
$('#mw-slettnom-button-close').button('option', 'disabled', false);
}
function nominationPageCreated(event) {
// Nomination page created. Do remaining tasks
$('#mw-slettnom-dialog').off('slettnomAllTasksDone');
$('#mw-slettnom-dialog').on('slettnomAllTasksDone', nominationComplete);
addTask(new AddToList());
addTask(new InsertTemplate());
if (notifyUser) {
addTask(new NotifyCreator());
}
}
function submitNomination(event, status) {
event.preventDefault();
disableForm();
numErrors = 0;
reason = $('#mw-slettnom-reason').val();
notifyUser = $('#mw-slettnom-notify-checkbox').is(':checked');
$('#mw-slettnom-form').fadeOut(200, function () {
$('#mw-slettnom-status').fadeIn(200);
$('#mw-slettnom-button-close').button('option', 'disabled', true);
});
if (notifyUser) {
debug("notify user");
$('#mw-slettnom-status-notifyCreator').show();
} else {
debug("don't notify user");
$('#mw-slettnom-status-notifyCreator').hide();
}
// Create nomination page first
$('#mw-slettnom-dialog').on('slettnomAllTasksDone', nominationPageCreated);
addTask(new CreateNomPage());
}
function taskComplete(event, msg) {
event.stopPropagation();
//debug(event);
if (event.type === 'slettnomTaskFailed' || event.type === 'slettnomTaskDone') {
var taskName = event.target.attributes.getNamedItem('id').value.substr(19),
taskSelector = '#mw-slettnom-status-' + taskName;
if (event.type === 'slettnomTaskFailed') {
numErrors = numErrors + 1;
debug('Task failed: ' + msg + ' name: ' + taskName + ' sel: ' + taskSelector);
} else {
debug('Task complete: ' + msg + ' name: ' + taskName + ' sel: ' + taskSelector);
}
if (taskName !== '') {
$(taskSelector).removeClass('mw-slettnom-spinner');
if (event.type === 'slettnomTaskFailed') {
$(taskSelector).addClass('mw-slettnom-err');
$(taskSelector).html(tasks[taskName].info + ' : ' + msg);
} else {
$(taskSelector).addClass('mw-slettnom-ok');
$(taskSelector).html(tasks[taskName].info + ' : ' + msg);
}
}
numTasks = numTasks - 1;
//delete tasks[taskName];
debug('-> Remaining tasks: ' + numTasks);
if (numTasks === 0) {
debug("Trigger slettnomAllTasksDone");
$('#mw-slettnom-dialog').trigger('slettnomAllTasksDone');
}
}
}
/************* User Interface *************/
function initalizeDialog() {
var dialog = $(
'<div id="mw-slettnom-dialog"> \
<div id="mw-slettnom-working" class="mw-slettnom-status mw-slettnom-spinner" style="display:inline-block; width:16px;height:16px;float:right; padding: 4px;"></div> \
<form id="mw-slettnom-form"> \
<label for="mw-slettnom-reason" id="mw-slettnom-reason-label"><!--<html:msg key="slettnom-enter-message"/>-->Begrunnelse:</label><br /> \
<textarea id="mw-slettnom-reason" style="width:520px;height:60px;"></textarea> \
<div id="mw-slettnom-notify"> \
<input type="checkbox" id="mw-slettnom-notify-checkbox" name="notify"/> \
<label for="mw-slettnom-notify-checkbox"><!--<html:msg key="slettnom-notify"/>--> Varsle artikkeloppretter: <span id="mw-slettnom-notify-username" style="font-weight:bold;">…</span></label><span style="color:#666;"> med <tt>{{Slettnom}}</tt></span> (du kan redigere teksten) \
<div id="mw-slettnom-notify-msg" style="display:none;"> \
<textarea id="mw-slettnom-notify-txt" style="width:550px;height:150px;"></textarea> \
</div> \
</div> \
<div id="mw-nominatedbefore-info" class="mw-slettnom-status"></div> \
<button class="submit" id="mw-slettnom-button-send" type="submit">Submit</button> \
<button id="mw-slettnom-button-cancel" type="button">Cancel</button> \
</form> \
<div id="mw-slettnom-status">\
<h3>Et øyeblikk…</h3>\
<div id="mw-slettnom-status-createNomPage" class="mw-slettnom-status mw-slettnom-spinner"></div>\
<div id="mw-slettnom-status-notifyCreator" class="mw-slettnom-status mw-slettnom-spinner"></div>\
<div id="mw-slettnom-status-insertTemplate" class="mw-slettnom-status mw-slettnom-spinner"></div>\
<div id="mw-slettnom-status-addToList" class="mw-slettnom-status mw-slettnom-spinner"></div>\
<button class="button" id="mw-slettnom-button-close" type="button">Close</button> \
</div>\
</div>'
);
//dialog.localize();
dialog.dialog({
width: 600,
position: ['center', 80],
autoOpen: true,
title: "Slettenominering", //mw.msg( 'slettnom-dialog-title' ),
modal: true,
resizable: false
});
$('#mw-slettnom-button-cancel').button({
disabled: true,
label: "Avbryt" // mw.msg( 'slettnom-button-cancel' )
});
$('#mw-slettnom-button-send').button({
disabled: true,
label: "Send" // mw.msg( 'slettnom-button-send' )
});
$('#mw-slettnom-button-close').button({
disabled: true,
label: "Lukk og last siden inn på nytt" // mw.msg( 'slettnom-button-close' )
});
$('#mw-slettnom-status').hide();
$('#mw-slettnom-status').on('slettnomTaskDone slettnomTaskFailed', taskComplete);
$('#mw-slettnom-working').on('slettnomTaskDone slettnomTaskFailed', taskComplete);
$('#mw-slettnom-button-send').on('click', submitNomination);
$('#mw-slettnom-button-cancel').on('click', cancel);
$('#mw-slettnom-button-close').on('click', closeAndReload);
$('#mw-slettnom-notify-checkbox').on('change', function () {
if ($('#mw-slettnom-notify-checkbox').is(':checked')) {
$('#mw-slettnom-notify-msg').slideDown();
} else {
$('#mw-slettnom-notify-msg').slideUp();
}
});
}
function dialogReady(event) {
$('#mw-slettnom-dialog').off('slettnomAllTasksDone');
enableForm();
}
function makeDialog() {
mw.util.addCSS(
'.mw-slettnom-status {\
background-repeat: no-repeat;\
background-position: left center;\
padding: 3px 3px 3px 20px;\
margin: 1px 1px 1px 6px;\
}\
.mw-slettnom-spinner {\
background-image: url("data:image/gif;base64,R0lGODlhEAAQAPQAAPL19wAAAOTn6YOFhtXY2UJDRHR1dgAAAFNUVSIiI6Olp7W3uBMTE5SWlwMDAzM0NGNkZQAAAAAAAAAAAAAAAAAA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CgAAACwAAAAAEAAQAAAFeSAgAgIpnOSoLgxxvqgKLEcCC65KEAByKK8cSpA4DAiHQ/DkKhGKh4ZCtCyZGo6F6iYYPAqFgYy02xkSaLEMV34tELyRYNEsCQyHlvWkGCzsPgMCEAY7Cg04Uk48LAsDhRA8MVQPEF0GAgqYYwSRlycNcWskCkApIyEAOwAAAAAAAAAAAA==");\
}\
.mw-slettnom-err {\
background-image: url("//upload.wikimedia.org/wikipedia/commons/thumb/0/04/Process-stop.svg/16px-Process-stop.svg.png");\
}\
.mw-slettnom-ok {\
background-image: url("//upload.wikimedia.org/wikipedia/commons/thumb/0/0e/Dialog-apply.svg/16px-Dialog-apply.svg.png");\
}'
);
mw.loader.using(['jquery.ui'], function () {
initalizeDialog();
$('#mw-slettnom-dialog').dialog('open');
$('#mw-slettnom-dialog').on('slettnomAllTasksDone', dialogReady);
addTask(new FindArticleCreator());
addTask(new CheckIfNominatedBefore());
});
}
$(makeDialog);
}());