User:Guillaume (WMF)/vediffs.js

mw.loader.using( ['mediawiki.util', 'mediawiki.ui.button', 'mediawiki.api.edit'], 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/Feedback/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 = ' '; diff_buttons += ''; diff_buttons += 'OK '; diff_buttons += 'User error '; diff_buttons += 'User error: improvable UX '; diff_buttons += 'VE/Parsoid bug '; diff_buttons += 'unknown  '; diff_buttons += '   '; diff_buttons += 'Submit review '; diff_buttons += ' '; var tally_buttons = ''; tally_buttons += 'Discard the set of reviews '; tally_buttons += ' [0 reviews in this set] ' tally_buttons += 'Save the set of reviews ' tally_buttons += ' '; // 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 + ' '; 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 +=  + oldid + ; if (comment) { wikitext += ' (' + comment + ') '; };                       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 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 = ' Saved reviewed successfully at ' + tally_page + '' success += ' ' $( "#save_tallies" ).after(success); } ).fail( function (code, result) { $( "#tally_buttons" ).after( ' An error occurred:\n ' + code + '\n' + result + ' '); } );       }        function escape_html(str) {            return $( "" ).text( str ).html        };

};

} );