Utente:Guillaume (WMF)/vediffs.js
Questa pagina definisce alcuni parametri di aspetto e comportamento generale di tutte le pagine. Per personalizzarli vedi Aiuto:Stile utente.
Nota: dopo aver salvato è necessario pulire la cache del proprio browser per vedere i cambiamenti (per le pagine globali è comunque necessario attendere qualche minuto). Per Mozilla / Firefox / Safari: fare clic su Ricarica tenendo premuto il tasto delle maiuscole, oppure premere Ctrl-F5 o Ctrl-R (Command-R su Mac); per Chrome: premere Ctrl-Shift-R (Command-Shift-R su un Mac); per Konqueror: premere il pulsante Ricarica o il tasto F5; per Opera può essere necessario svuotare completamente la cache dal menù Strumenti → Preferenze; per Internet Explorer: mantenere premuto il tasto Ctrl mentre si preme il pulsante Aggiorna o premere Ctrl-F5.
mw.loader.using( ['mediawiki.util', 'mediawiki.ui.button', 'mediawiki.api'], function () {
if (($('table.diff').length != 0) && ($('td.diff-ntitle span.mw-tag-marker-visualeditor').length != 0)) {
// Globals
var number_of_pending_tallies = 0;
var current_diff = ''
var oldid = mw.config.get('wgRevisionId');
var user = mw.config.get('wgUserName');
var diff_has_been_reviewed = false;
// Script parameters
var tally_page = 'Wikipedia:VisualEditor/Commenti/diffs'
var diff_types = [
'diff_ok',
'diff_user_error',
'diff_borderline',
'diff_bug',
'diff_unknown'
];
// Wait for the page to be parsed
$(document).ready( function () {
// Retrieve pending tallies
if (localStorage.vediffs) {
var pending_tallies = JSON.parse(localStorage['vediffs']);
for (category in pending_tallies) {
number_of_pending_tallies += pending_tallies[category].length;
};
};
// Prepare the tagging bar
var diff_buttons = '<div id="tagbar">';
diff_buttons += '<div class="mw-ui-button-group" id="diff_buttons">';
diff_buttons += '<div class="mw-ui-button diff_button" id="diff_ok">OK</div>';
diff_buttons += '<div class="mw-ui-button diff_button" id="diff_user_error">User error</div>';
diff_buttons += '<div class="mw-ui-button diff_button" id="diff_borderline">User error: improvable UX</div>';
diff_buttons += '<div class="mw-ui-button diff_button" id="diff_bug">VE/Parsoid bug</div>';
diff_buttons += '<div class="mw-ui-button diff_button" id="diff_unknown">unknown</div></div><div style="clear:both"></div>';
diff_buttons += ' <input type="text" placeholder="optional comment" id="diff_comment" maxlength="140" class="mw-ui-input mw-ui-input-inline" /> ';
diff_buttons += '<button class="mw-ui-button mw-ui-constructive" id="submit_review">Submit review</button>';
diff_buttons += '</div><div style="clear:both"></div>';
var tally_buttons = '<div id="tally_buttons">';
tally_buttons += '<button class="mw-ui-button mw-ui-destructive tally_button" id="discard_tallies" disabled>Discard the set of reviews</button>';
tally_buttons += ' [<span id="number_pending">0</span> reviews in this set] '
tally_buttons += '<button class="mw-ui-button mw-ui-constructive tally_button" id="save_tallies" disabled>Save the set of reviews</button>'
tally_buttons += '</div><div style="clear:both"></div>';
// Add the buttons to the page
$('#siteSub').before( tally_buttons );
$('#mw-content-text').before( diff_buttons );
$('#tally_buttons').css('float', 'right');
$('#tagbar').css('display', 'table');
$('#tagbar').css('margin', '0 auto');
$('#tagbar').css('text-align', 'center');
// Enable the tally buttons if we have pending tallies
if (number_of_pending_tallies) {
$( ".tally_button" ).prop( "disabled", false );
$('#number_pending').html(number_of_pending_tallies);
} else {
$( ".tally_button" ).prop( "disabled", true );
};
// We can't review until we've tagged
$( "#submit_review" ).prop( "disabled", true );
// Attach events to diff buttons
$('.diff_button').click( function() {
tag_tally(this.id);
});
$( "#submit_review" ).click( function() {
record_tally();
} );
// Attach events to tally buttons
$( "#discard_tallies" ).click( function() {
discard_tallies();
} );
$( "#save_tallies" ).click( function() {
save_tallies(diff_types, tally_page);
} );
} );
function tag_tally(diff_type) {
if (!diff_has_been_reviewed){
if (current_diff) {
$('#' + current_diff).removeClass('mw-ui-progressive');
};
$('#' + diff_type).addClass('mw-ui-progressive');
current_diff = diff_type;
$( "#submit_review" ).prop( "disabled", false );
};
}
function record_tally() {
var date = Date();
// Retrieve the review comment, clean it up
var comment = escape_html($('#diff_comment').val());
console.log('recorded: ' + oldid + ' // ' + current_diff + ' // ' + user + ' // ' + date + ' // ' + comment)
// Get or initialize the pending tallies.
var pending_tallies = [];
if (localStorage.vediffs)
{
pending_tallies = JSON.parse(localStorage['vediffs']);
}
else
{
pending_tallies = { "diff_ok": [], "diff_user_error": [], "diff_borderline": [], "diff_bug": [], "diff_unknown": []};
};
// Add the current diff to the appropriate sublist.
pending_tallies[current_diff].push({"oldid": oldid, "user": user, "date": date, "comment": comment});
// Save to localStorage
localStorage["vediffs"] = JSON.stringify(pending_tallies);
// Feedback and cleanup
$( "#submit_review" ).text('Submitted!');
$('.tally_button').prop( "disabled", false );
number_of_pending_tallies++;
$('#number_pending').html(number_of_pending_tallies);
// Disable the tool
$( "#submit_review" ).prop( "disabled", true );
$('.diff_button').removeClass('mw-ui-progressive');
diff_has_been_reviewed = true;
}
function discard_tallies(){
if (localStorage.vediffs)
{
localStorage.removeItem('vediffs');
}
$('.tally_button').prop( "disabled", true );
number_of_pending_tallies = 0;
$('#number_pending').html(number_of_pending_tallies);
$('.diff_button').removeClass('mw-ui-progressive');
$('#submit_review').prop('disabled', true);
// Re-enable the tool since we've discarded the tallies
diff_has_been_reviewed = false;
current_diff = ''
}
function save_tallies(diff_types, tally_page){
if (localStorage.vediffs)
{
var pending_tallies = JSON.parse(localStorage['vediffs']);
// Make a few calculations
var total_diffs = 0;
for (category in pending_tallies) {
total_diffs += pending_tallies[category].length;
};
// prepare the wikitext to be posted on the wiki
var wikitext = '\n== ' + Date();
wikitext += ', [[user:' + user + '|' + user + ']] ';
wikitext += '==\n\n{| class="wikitable sortable"\n|-\n';
wikitext += '! Category !! Tally !! Percentage !! Diffs\n|-\n';
for (i = 0; i < diff_types.length; i++) {
var type = diff_types[i];
wikitext += '| ' + type + ' || ' + pending_tallies[type].length + ' || ';
wikitext += Math.round(pending_tallies[type].length * 100 / total_diffs);
wikitext += '% || ';
for (j = 0; j < pending_tallies[type].length; j++) {
var oldid = pending_tallies[type][j]['oldid'];
var comment = pending_tallies[type][j]['comment'];
wikitext += '[[Special:Diff/' + oldid + '|' + oldid + ']]';
if (comment) {
wikitext += ' <small>(' + comment + ')</small>';
};
wikitext += ' · '
};
wikitext += '\n|-\n';
};
wikitext += '|}';
console.log(wikitext);
publish_wikitext(tally_page, wikitext);
}
}
function publish_wikitext (tally_page, wikitext) {
new mw.Api().postWithEditToken( {
action: 'edit',
minor: 'false',
title: tally_page,
appendtext: wikitext,
summary: 'Saving tallies using [[User:Guillaume (WMF)/vediffs.js|vediffs.js]]'
} ).done( function() {
// Clean up a bit
localStorage.removeItem('vediffs');
$('.tally_button').prop( "disabled", true );
number_of_pending_tallies = 0;
var current_diff = ''
// Give feedback
var success = '<div class="successbox">Saved reviewed successfully at <a href="';
success += mw.config.get('wgArticlePath').replace('$1', mw.util.wikiUrlencode(tally_page));
success += '" title="' + tally_page + '">' + tally_page + '</a>'
success += '</div>'
$( "#save_tallies" ).after(success);
} ).fail( function (code, result) {
$( "#tally_buttons" ).after( '<div class="errorbox">An error occurred:\n<br>' + code + '\n' + result + '</div>');
} );
}
function escape_html(str) {
return $( "<a>" ).text( str ).html()
};
};
} );