User:BZPN/RfD.js: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
// <nowiki> |
// <nowiki> |
||
$(function () { |
$(function () { |
||
// Sprawdzamy, czy strona należy do przestrzeni nazw "Project:Requests for Deletion" |
|||
var QC = {}; |
|||
var pageName = mw.config.get('wgPageName'); |
|||
QC.summary = ' - with QuickClose'; |
|||
if (!pageName.startsWith("Project:Requests for Deletion")) { |
|||
return; |
|||
} |
|||
// |
// Funkcja dodająca przycisk "Quick Close" w każdej podsekcji |
||
function addQuickCloseButtons() { |
|||
var $title = $('#firstHeading'); |
|||
$('span.mw-editsection-bracket').each(function () { |
|||
if ($title.length) { |
|||
try { |
|||
var $this = $(this); |
|||
var sectionLink = $this.find('a').first(); |
|||
if (!sectionLink.length) return; |
|||
// Wyciągnięcie numeru sekcji z URL |
|||
css: { marginLeft: '10px', cursor: 'pointer', color: '#0645AD', textDecoration: 'underline' } |
|||
var href = sectionLink.attr('href'); |
|||
}); |
|||
var m = href.match(/action=edit§ion=(\d+)/); |
|||
$title.append($btn); |
|||
if (!m) return; |
|||
var sectionNumber = m[1]; |
|||
$btn.click(function () { |
|||
// Dodajemy separator i przycisk "Quick Close" |
|||
openQuickCloseDialog(); |
|||
$this.append(' | '); |
|||
var $btn = $('<a>', { |
|||
text: 'Quick Close', |
|||
href: 'javascript:void(0)', |
|||
'data-section': sectionNumber, |
|||
css: { cursor: 'pointer', color: '#0645AD', textDecoration: 'underline' } |
|||
}); |
|||
$this.append($btn); |
|||
// Obsługa kliknięcia przycisku |
|||
$btn.click(function () { |
|||
openQuickCloseDialog(sectionNumber); |
|||
}); |
|||
} catch (e) { |
|||
console.error(e); |
|||
} |
|||
}); |
}); |
||
} |
} |
||
// Funkcja tworząca prosty popup dialogowy |
|||
function openQuickCloseDialog() { |
|||
function openQuickCloseDialog(sectionNumber) { |
|||
// Definicja okna dialogowego z użyciem OO.ui |
|||
// Usuwamy ewentualny poprzedni dialog |
|||
function QuickCloseDialog(config) { |
|||
$('#quickclose-dialog').remove(); |
|||
// Tworzymy element dialogu |
|||
var $dialog = $( '<div id="quickclose-dialog" style="position: fixed; top: 30%; left: 50%; transform: translate(-50%, -30%); background: #fff; border: 1px solid #aaa; padding: 20px; z-index: 10000; width: 400px; box-shadow: 0 0 10px rgba(0,0,0,0.5);"></div>' ); |
|||
// Pole komentarza |
|||
var $commentLabel = $('<div style="margin-bottom:5px;">Wynik (komentarz):</div>'); |
|||
var $commentInput = $('<textarea rows="4" style="width: 100%;">the outcome...</textarea>'); |
|||
// Wybór szablonu (radio) |
|||
var $templateLabel = $('<div style="margin:10px 0 5px;">Wybierz szablon:</div>'); |
|||
var $radioDelete = $('<label style="margin-right:10px;"><input type="radio" name="qc-template" value="delete" checked> {{delete}}</label>'); |
|||
var $radioKeep = $('<label><input type="radio" name="qc-template" value="keep"> {{keep}}</label>'); |
|||
// Checkbox dla sysopa |
|||
var isSysop = (mw.config.get('wgUserGroups').indexOf('sysop') !== -1); |
|||
var $deleteCheckbox = ''; |
|||
if (isSysop) { |
|||
$deleteCheckbox = $('<div style="margin-top:10px;"><label><input type="checkbox" id="qc-delete-page"> Delete this page</label></div>'); |
|||
} |
} |
||
// Przyciski akcji |
|||
OO.inheritClass(QuickCloseDialog, OO.ui.ProcessDialog); |
|||
var $btnContainer = $('<div style="margin-top:15px; text-align: right;"></div>'); |
|||
QuickCloseDialog.static.name = 'quickCloseDialog'; |
|||
var $btnCancel = $('<button style="margin-right:10px;">Anuluj</button>'); |
|||
QuickCloseDialog.static.title = 'Quick Close RfD'; |
|||
var $btnSubmit = $('<button>Zatwierdź</button>'); |
|||
QuickCloseDialog.static.actions = [ |
|||
{ action: 'cancel', label: 'Anuluj', flags: 'safe' }, |
|||
{ action: 'submit', label: 'Zatwierdź', flags: ['primary', 'progressive'] } |
|||
]; |
|||
$btnContainer.append($btnCancel, $btnSubmit); |
|||
QuickCloseDialog.prototype.initialize = function () { |
|||
$dialog.append($commentLabel, $commentInput, $templateLabel, $radioDelete, $radioKeep, $deleteCheckbox, $btnContainer); |
|||
QuickCloseDialog.super.prototype.initialize.call(this); |
|||
$('body').append($dialog); |
|||
// Obsługa przycisku "Anuluj" |
|||
$btnCancel.click(function () { |
|||
$dialog.remove(); |
|||
}); |
|||
// Obsługa przycisku "Zatwierdź" |
|||
$btnSubmit.click(function () { |
|||
var comment = $commentInput.val().trim(); |
|||
var outcome = $('input[name="qc-template"]:checked').val(); // "delete" lub "keep" |
|||
var templateText = (outcome === 'delete') ? '{{delete}}' : '{{keep}}'; |
|||
// Łączymy szablon z komentarzem |
|||
var fullComment = templateText + ' ' + comment; |
|||
// Pobieramy tytuł strony RfD, a także wyciągamy tytuł docelowy (po ostatnim ukośniku) |
|||
var pageTitle = mw.config.get('wgPageName'); |
|||
var targetPage = pageTitle.split('/').pop().trim(); |
|||
// |
// Pobieramy bieżący wikitext strony |
||
new mw.Api().get({ |
|||
action: 'parse', |
|||
page: pageTitle, |
|||
prop: 'wikitext' |
|||
}).done(function (result) { |
|||
var wikitext = result.parse.wikitext['*']; |
|||
// Usuwamy szablon dyskusji z sekcji "Current deletion request discussions" |
|||
this.outcomeSelect = new OO.ui.RadioSelectInputWidget({ |
|||
// Przykład: {{Wikipedia:Requests for deletion/Requests/2025/Sticker Mule}} |
|||
options: [ |
|||
var regex = /\{\{\s*Wikipedia:Requests for deletion\/Requests\/[^\}]+\}\}\s*\n?/g; |
|||
var newText = wikitext.replace(regex, ''); |
|||
// Dodajemy komentarz na końcu sekcji |
|||
newText += '\n:' + fullComment; |
|||
this.outcomeSelect.selectItemByData('delete'); |
|||
// Jeśli użytkownik ma uprawnienia sysopa – dodatkowy checkbox |
|||
this.sysopCheckbox = null; |
|||
if (mw.config.get('wgUserGroups').indexOf('sysop') !== -1) { |
|||
this.sysopCheckbox = new OO.ui.CheckboxInputWidget(); |
|||
this.sysopCheckboxField = new OO.ui.FieldLayout(this.sysopCheckbox, { |
|||
label: 'Delete this page' |
|||
}); |
|||
} |
|||
// Komponowanie formularza |
|||
var $form = $('<div>'); |
|||
$form.append($('<div>').css('margin-bottom', '10px').append( |
|||
$('<label>').text('Wynik zamknięcia:'), |
|||
this.commentInput.$element |
|||
)); |
|||
$form.append($('<div>').css('margin-bottom', '10px').append( |
|||
$('<label>').text('Wybierz szablon: '), |
|||
this.outcomeSelect.$element |
|||
)); |
|||
if (this.sysopCheckboxField) { |
|||
$form.append($('<div>').css('margin-bottom', '10px').append( |
|||
this.sysopCheckboxField.$element |
|||
)); |
|||
} |
|||
this.content = new OO.ui.PanelLayout({ |
|||
padded: true, |
|||
expanded: false |
|||
}); |
|||
this.content.$element.append($form); |
|||
this.$body.append(this.content.$element); |
|||
}; |
|||
QuickCloseDialog.prototype.getActionProcess = function (action) { |
|||
var dialog = this; |
|||
if (action === 'submit') { |
|||
// Pobranie danych z formularza |
|||
var comment = this.commentInput.getValue().trim(); |
|||
var outcome = this.outcomeSelect.getSelectedItem().getData(); // 'delete' lub 'keep' |
|||
var templateText = (outcome === 'delete') ? '{{delete}}' : '{{keep}}'; |
|||
comment = templateText + ' ' + comment; |
|||
var pageTitle = mw.config.get('wgPageName'); |
|||
// Edytujemy stronę RfD |
|||
// Pobierz bieżący wikitext strony i usuń szablon z sekcji "Current deletion request discussions" |
|||
new mw.Api(). |
new mw.Api().postWithEditToken({ |
||
action: ' |
action: 'edit', |
||
title: pageTitle, |
|||
text: newText, |
|||
summary: 'Quick close (' + outcome + '): ' + comment, |
|||
minor: true |
|||
}).done(function () { |
|||
// Jeśli użytkownik sysop i checkbox zaznaczony – usuwamy stronę docelową |
|||
// {{Wikipedia:Requests for deletion/Requests/2025/Sticker Mule}} |
|||
if (isSysop && $('#qc-delete-page').is(':checked')) { |
|||
new mw.Api().postWithEditToken({ |
|||
action: 'delete', |
|||
title: targetPage, |
|||
reason: 'Quick close deletion: ' + outcome + ' – ' + comment |
|||
}).done(function () { |
|||
title: pageTitle, |
|||
text: newText, |
|||
summary: 'Quick close: ' + outcome + '. ' + comment + QC.summary, |
|||
minor: true |
|||
}).done(function () { |
|||
// Jeśli użytkownik sysop i checkbox "Delete this page" został zaznaczony – usuń stronę |
|||
if (dialog.sysopCheckbox && dialog.sysopCheckbox.isSelected()) { |
|||
new mw.Api().postWithEditToken({ |
|||
action: 'delete', |
|||
title: pageTitle, |
|||
reason: 'Quick close deletion: ' + outcome + '. ' + comment |
|||
}).done(function () { |
|||
location.reload(); |
|||
}); |
|||
} else { |
|||
location.reload(); |
location.reload(); |
||
} |
}); |
||
} |
} else { |
||
location.reload(); |
|||
} |
|||
}); |
}); |
||
}); |
|||
$dialog.remove(); |
|||
}); |
|||
return QuickCloseDialog.super.prototype.getActionProcess.call(this, action); |
|||
}; |
|||
var quickDialog = new QuickCloseDialog({ size: 'large' }); |
|||
var windowManager = new OO.ui.WindowManager(); |
|||
$(document.body).append(windowManager.$element); |
|||
windowManager.addWindows([quickDialog]); |
|||
windowManager.openWindow(quickDialog); |
|||
} |
} |
||
// Inicjujemy przyciski po załadowaniu strony |
|||
addQuickCloseButtons(); |
|||
}); |
}); |
||
// </nowiki> |
// </nowiki> |
Revision as of 20:05, 25 March 2025
// <nowiki>
$(function () {
// Sprawdzamy, czy strona należy do przestrzeni nazw "Project:Requests for Deletion"
var pageName = mw.config.get('wgPageName');
if (!pageName.startsWith("Project:Requests for Deletion")) {
return;
}
// Funkcja dodająca przycisk "Quick Close" w każdej podsekcji
function addQuickCloseButtons() {
$('span.mw-editsection-bracket').each(function () {
try {
var $this = $(this);
var sectionLink = $this.find('a').first();
if (!sectionLink.length) return;
// Wyciągnięcie numeru sekcji z URL
var href = sectionLink.attr('href');
var m = href.match(/action=edit§ion=(\d+)/);
if (!m) return;
var sectionNumber = m[1];
// Dodajemy separator i przycisk "Quick Close"
$this.append(' | ');
var $btn = $('<a>', {
text: 'Quick Close',
href: 'javascript:void(0)',
'data-section': sectionNumber,
css: { cursor: 'pointer', color: '#0645AD', textDecoration: 'underline' }
});
$this.append($btn);
// Obsługa kliknięcia przycisku
$btn.click(function () {
openQuickCloseDialog(sectionNumber);
});
} catch (e) {
console.error(e);
}
});
}
// Funkcja tworząca prosty popup dialogowy
function openQuickCloseDialog(sectionNumber) {
// Usuwamy ewentualny poprzedni dialog
$('#quickclose-dialog').remove();
// Tworzymy element dialogu
var $dialog = $( '<div id="quickclose-dialog" style="position: fixed; top: 30%; left: 50%; transform: translate(-50%, -30%); background: #fff; border: 1px solid #aaa; padding: 20px; z-index: 10000; width: 400px; box-shadow: 0 0 10px rgba(0,0,0,0.5);"></div>' );
// Pole komentarza
var $commentLabel = $('<div style="margin-bottom:5px;">Wynik (komentarz):</div>');
var $commentInput = $('<textarea rows="4" style="width: 100%;">the outcome...</textarea>');
// Wybór szablonu (radio)
var $templateLabel = $('<div style="margin:10px 0 5px;">Wybierz szablon:</div>');
var $radioDelete = $('<label style="margin-right:10px;"><input type="radio" name="qc-template" value="delete" checked> {{delete}}</label>');
var $radioKeep = $('<label><input type="radio" name="qc-template" value="keep"> {{keep}}</label>');
// Checkbox dla sysopa
var isSysop = (mw.config.get('wgUserGroups').indexOf('sysop') !== -1);
var $deleteCheckbox = '';
if (isSysop) {
$deleteCheckbox = $('<div style="margin-top:10px;"><label><input type="checkbox" id="qc-delete-page"> Delete this page</label></div>');
}
// Przyciski akcji
var $btnContainer = $('<div style="margin-top:15px; text-align: right;"></div>');
var $btnCancel = $('<button style="margin-right:10px;">Anuluj</button>');
var $btnSubmit = $('<button>Zatwierdź</button>');
$btnContainer.append($btnCancel, $btnSubmit);
$dialog.append($commentLabel, $commentInput, $templateLabel, $radioDelete, $radioKeep, $deleteCheckbox, $btnContainer);
$('body').append($dialog);
// Obsługa przycisku "Anuluj"
$btnCancel.click(function () {
$dialog.remove();
});
// Obsługa przycisku "Zatwierdź"
$btnSubmit.click(function () {
var comment = $commentInput.val().trim();
var outcome = $('input[name="qc-template"]:checked').val(); // "delete" lub "keep"
var templateText = (outcome === 'delete') ? '{{delete}}' : '{{keep}}';
// Łączymy szablon z komentarzem
var fullComment = templateText + ' ' + comment;
// Pobieramy tytuł strony RfD, a także wyciągamy tytuł docelowy (po ostatnim ukośniku)
var pageTitle = mw.config.get('wgPageName');
var targetPage = pageTitle.split('/').pop().trim();
// Pobieramy bieżący wikitext strony
new mw.Api().get({
action: 'parse',
page: pageTitle,
prop: 'wikitext'
}).done(function (result) {
var wikitext = result.parse.wikitext['*'];
// Usuwamy szablon dyskusji z sekcji "Current deletion request discussions"
// Przykład: {{Wikipedia:Requests for deletion/Requests/2025/Sticker Mule}}
var regex = /\{\{\s*Wikipedia:Requests for deletion\/Requests\/[^\}]+\}\}\s*\n?/g;
var newText = wikitext.replace(regex, '');
// Dodajemy komentarz na końcu sekcji
newText += '\n:' + fullComment;
// Edytujemy stronę RfD
new mw.Api().postWithEditToken({
action: 'edit',
title: pageTitle,
text: newText,
summary: 'Quick close (' + outcome + '): ' + comment,
minor: true
}).done(function () {
// Jeśli użytkownik sysop i checkbox zaznaczony – usuwamy stronę docelową
if (isSysop && $('#qc-delete-page').is(':checked')) {
new mw.Api().postWithEditToken({
action: 'delete',
title: targetPage,
reason: 'Quick close deletion: ' + outcome + ' – ' + comment
}).done(function () {
location.reload();
});
} else {
location.reload();
}
});
});
$dialog.remove();
});
}
// Inicjujemy przyciski po załadowaniu strony
addQuickCloseButtons();
});
// </nowiki>