مدیاویکی:WomenInRedScoring.js
ظاهر
نکته: برای دیدن تغییرات، ممکن است نیاز باشد که حافظهٔ نهانی مرورگر خود را پس از انتشار پاکسازی کنید. گوگل کروم، فایرفاکس، مایکروسافت اج و سافاری: کلید ⇧ Shift را نگه دارید و روی دکمهٔ Reload در نوار ابزار مرورگر کلیک کنید. برای آگاهی از جزئیات و نحوهٔ پاکسازی حافظهٔ نهانی سایر مرورگرها، صفحهٔ ویکیپدیا:میانگیر مرورگرتان را خالی کنید را ببینید.
//<nowiki>
$(function() {
var currDate = Date.now(),
startDate = new Date('04/07/2023 23:59:59').valueOf(),
endDate = new Date('04/14/2023 23:59:59').valueOf(),
timeIsValid = currDate > startDate && startDate <= endDate;
// Only sysops, eliminators and extended confirmed users in a valid time range
if (
(
mw.config.get('wgPageName') !== 'ویکیپدیا:همایه_بانوان_سرخپوش/۲۰۲۳/فهرست_مقالات' ||
(
mw.config.get('wgUserGroups').indexOf('extendedconfirmed') < 0 &&
mw.config.get('wgUserGroups').indexOf('sysop') < 0 &&
mw.config.get('wgUserGroups').indexOf('eliminator') < 0
)
) && !timeIsValid
) {
return;
}
var infoText = $('<div><p>مهلت ثبت رأی از <b style="color:#006400">۰۷ آوریل ۲۰۲۳، ساعت ۲۳:۵۹:۵۹</b>' +
' تا <b style="color:#006400">۱۴ آوریل ۲۰۲۳، ساعت ۲۳:۵۹:۵۹</b> است</p>' +
'<b>تنها ' +
'<a href="/wiki/ویکیپدیا:مدیران">مدیران</a>، ' +
'<a href="/wiki/ویکیپدیا:ویکیبان">ویکیبانان</a>، و ' +
'<a href="/wiki/ویکیپدیا:تأییدشده_پایدار">کاربران تأییدشدهٔ پایدار</a> ' +
'قادر به رأی دادن در این نظرخواهی هستند.</b><br/>' +
'برای ثبت نظر، ابتدا از هر مقاله بازدید کنید و سپس امتیاز خود از ۱ تا ۱۰ ' +
'را با استفاده از دکمههای + و - روبروی نام مقاله، وارد کنید.<br/>' +
'پس از آن که برای تمام مقالههای مورد نظر خود امتیازی ثبت کردید، ' +
'در آخرین سطر جدول روی دکمهٔ «ثبت امتیاز(ها)» کلیک کنید تا ' +
'امتیاز شما در ' +
'<a href="/wiki/ویکیپدیا:همایه_بانوان_سرخپوش/۲۰۲۳/فهرست_مقالات/امتیازها">فایل امتیازها</a> ' +
'ذخیره شود.<br/>' +
'پس از ثبت امتیاز، در صورت تمایل به تغییر امتیاز، ' +
'میتوانید صفحه را تازهسازی کنید، امتیاز خود را تغییر دهید و ' +
'مجدداً روی دکمهٔ ثبت کلیک کنید تا امتیازهای شما بهروز شوند.<br/>' +
'میانگین کل امتیازهای ثبتشده توسط کاربران برای هر مقاله در ' +
'ستون آخر جدول نمایش یافته است.<br/><br/>' +
'برای کسب اطلاعات بیشتر، ' +
'<a href="/wiki/کاربر:Jeeputer/womenInRedScoring"> مستندات ابزار امتیازدهی</a> ' +
'را ببینید.</div>');
var api = new mw.Api(),
trs = $('#mw-content-text > div.mw-parser-output > table:nth-child(3) > tbody > tr'),
table = $('#mw-content-text > div.mw-parser-output > table:nth-child(3)'),
listPage = 'ویکیپدیا:همایه_بانوان_سرخپوش/۲۰۲۳/فهرست_مقالات/امتیازها',
thisUser = mw.config.get('wgUserName'),
tr, a, title;
// Create submit button
table.append('<tr><td colspan="6"><button type="submit"; class="submit-wir-form"><b>ثبت امتیاز(ها)</b></button></td></tr>');
$('.submit-wir-form').css('width', '100%');
function getUrl(page) {
return mw.util.getUrl(page);
}
function savePage(title, text, summary) {
return api.post({
action: 'edit',
title: title,
text: text,
summary: summary,
nocreate: '',
minor: true,
token: mw.user.tokens.get('csrfToken')
}).done(function(result) {
if (result.edit.result === 'Success') {
mw.notify('ثبت رأی با موفقیت انجام شد!', {
title: 'موفق!',
type: 'success'
});
} else {
mw.notify('هنگام ثبت رأی خطایی رخ داد!', {
title: 'ناموفق!',
type: 'error'
});
}
});
}
function getJson(page) {
mw.notify('در حال دریافت امتیازها و بارگیری ابزار امتیازدهی', {
title: 'کمی صبر کنید …',
tag: 'get-wir-json'
});
return $.getJSON('/w/index.php', {
action: 'raw',
ctype: 'application/json',
title: page
}).then(function(data) {
mw.notify('امتیازها دریافت شدند!', {
title: 'انجام شد!',
tag: 'get-wir-json',
type: 'success'
});
return data;
});
}
// Function code from https://stackoverflow.com/questions/1359761/sorting-a-javascript-object-by-property-name
function sortObject(o) {
var sorted = {},
key, a = [];
// from [[مدیاویکی:Gadget-AddNewUserToJSON.js#L-162]]
var persianSort = (window.persianTools || {}).persianSort || function(x) {
return x.sort();
};
for (key in o) {
if (o.hasOwnProperty(key)) {
a.push(key);
}
}
persianSort(a);
for (key = 0; key < a.length; key++) {
sorted[a[key]] = o[a[key]];
}
return sorted;
}
function showCreator(title, cell) {
api.get({
action: 'query',
titles: title,
prop: 'revisions',
rvprop: 'user',
rvlimit: 1,
rvdir: 'newer'
}).done(function(data) {
var pages = data.query.pages;
var id = Object.keys(pages)[0];
var creator = pages[id].revisions[0].user;
$(cell).html('<a href="' + getUrl('کاربر:' + creator) + '">' + creator + '</a>');
});
}
function addNumberInput(cell, index, title, preScore) {
var t = title
.replace(/\s\([^\)]*\)/g, '')
.replace(/\s/g, '-');
var id = t + '-score-' + index;
var deleteButton = new OO.ui.ButtonWidget({
framed: false,
icon: 'close',
id: 'clear-' + index,
title: 'حذف امتیاز',
invisibleLabel: true
});
var numberInput = new OO.ui.NumberInputWidget({
min: 1,
max: 10,
id: id,
inputId: 'num-' + index,
title: title,
plusButton: {
flags: 'progressive'
},
minusButton: {
flags: 'destructive'
}
});
var layout = new OO.ui.HorizontalLayout({
id: t + '-layout-' + index,
items: [
numberInput
]
});
if (preScore) {
//layout.addItems([deleteButton]);
numberInput.setValue(preScore);
$(cell).css('background-color', '#d5fdf4');
}
$(cell).append(layout.$element);
$('#' + t + '-layout-' + index).css('width', '8em');
}
function showAverage(row, scores) {
var content;
if (!scores) {
content = 'فاقد امتیاز';
} else {
var len = Object.keys(scores).length;
var values = Object.values(scores);
var sum = 0;
for (var v = 0; v < len; v++) {
sum += parseInt(values[v], 10);
}
var avg = sum / len;
avg = Math.round(avg * 10) / 10; // round to 1 decimal place
content = mw.language.convertNumber(avg);
}
row.append($('<td style="text-align: center; white-space: nowrap">' + content + '</td>'));
}
function handleScores(inputs, file) {
var inp, j, parentID, article, score,
scores = [];
for (j = 1; j <= inputs.length; j++) {
inp = $('#num-' + j),
article = inp[0].title;
score = inp[0].value;
if (!score || score === '') {
continue;
}
scores.push([article, score]);
}
if (!scores.length) {
mw.notify('برای ثبت امتیاز، باید دست کم به یک مقاله امتیاز دهید!', {
type: 'warn'
});
return;
} else {
for (j = 0; j < scores.length; j++) {
// if article's entry already exists
if (file[scores[j][0]]) {
// If this user has voted to this article before
if (file[scores[j][0]][thisUser]) {
// If the new score is the same as the old one, return
if (file[scores[j][0]][thisUser] === scores[j][1]) {
continue;
// If the new score is not the same, then update
} else {
file[scores[j][0]][thisUser] = parseInt(scores[j][1]);
}
// If user has not voted to this article before, then add their score
} else {
file[scores[j][0]][thisUser] = parseInt(scores[j][1]);
}
// if article does not hav an entry, then add a new entry and the score
} else {
file[scores[j][0]] = {
[thisUser]: parseInt(scores[j][1])
};
}
}
sortObject(file);
savePage(
listPage,
JSON.stringify(file),
'ثبت رأی (با [[کاربر:Jeeputer/womenInRedScoring|ابزار]])'
);
}
}
getJson(listPage).then(function(data) {
for (var i = 1; i <= trs.length; i++) {
tr = $('#mw-content-text > div.mw-parser-output > table:nth-child(3) > tbody > tr:nth-child(' + i + ')');
a = tr.find('a');
title = a.attr('title');
var creatorCell = $(tr[0]).children('td:nth-child(4)')[0];
scoreCell = $(tr[0]).children('td:nth-child(5)')[0];
showCreator(title, creatorCell);
addNumberInput(scoreCell, i, title, (data[title] && data[title][thisUser]) ? data[title][thisUser] : false);
showAverage(tr, data[title]);
}
$('table:nth-child(3) .oo-ui-textInputWidget-type-number').css('width', '8em');
var inputs = $('.oo-ui-numberInputWidget-buttoned');
$('#mw-content-text > div.mw-parser-output > table:nth-child(3) > thead > tr:nth-child(1)').append($('<th class="headerSort role= "columnheader button" tabindex="0">میانگین</th>'));
// Show information text on top of the table
infoText.insertBefore(table);
$('.submit-wir-form').on('click', function() {
handleScores(inputs, data);
});
});
});
//</nowiki>