User:Abelmoschus Esculentus/cv-revdel.js

$( function {    var SUMMARY = "{" + "{copyvio-revde" + "l}}";    var pageName;    var saved = false; // has the submit button been clicked?

/**    * Make a link to a given oldid of the current page. */   function makeOldidLink( oldid ) { var href = "/w/index.php?title=" + encodeURIComponent( pageName ) + "&oldid=" + oldid; var link = document.createElement( "a" ); link.href = href; link.textContent = oldid; return link; }

/**    * Add the appropriate copyvio-revdel template to the current page. */   function editPage {

var template = "{" + "{copyvio-revdel|url=" + document.getElementById( "cv-rd-url" ).value; var rows = document.querySelectorAll( "#cv-revdel tr" ); var num; for( var i = 1, n = rows.length; i < n; i++ ) { template += "|start" + i + "=" + rows[i].childNodes[0].textContent + ( rows[i].childNodes[2].childNodes[0].checked ? ( "|end" + i + "=" + rows[i].childNodes[1].textContent ) : "" ); }       template += "}}";

( new mw.Api ).postWithToken( "csrf", {           action: "edit",            title: pageName,            summary: SUMMARY,            prependtext: template + "\n"        } ).done( function ( d ) {            if( d && d.edit && d.edit.result && d.edit.result == "Success" ) {                document.querySelector( "#ca-view a" ).click;            } else {                console.log( d );            }        } ).fail( function ( code, result ) {            console.log( code, result );        } ); }

/**    * Load the main cv-revdel panel and add buttons/other stuff to     * the history UI     */ function load( evt ) { if( evt ) evt.preventDefault;

// Don't load the panel for a second time if it's already there if( document.getElementById( "cv-revdel" ) ) return;

// Style for the panel mw.util.addCSS(           "#cv-revdel { border: thin solid rgb(197, 197, 197); " +            "box-shadow: 0px 3px 8px rgba(0, 0, 0, 0.25); border-radius: 3px;" +            "padding: 2em; }" +            "#cv-revdel table { margin: 1em 0 }" +            "#cv-revdel td { padding: 0 0.5em }" +            "#cv-revdel button { padding: 0.2em }" +            "#cv-rd-close {position: absolute; top: 35px; right: 5px;}" +            "#cv-revdel a.disabled {color: gray;text-decoration: line-through;font-style: italic;}" +            "#cv-rd-url { width: 50%; min-width: 30em; }" +

// Low-budget mw-ui "#cv-revdel #cv-rd-submit { color: #fff;background-color: #36c;border-color: #36c;" + "transition: background-color 100ms,color 100ms,border-color 100ms,box-shadow 100ms;" + "padding: 0.625em 0.94em 0.55em; border-style: solid;border-width: 1px;border-radius: 2px;" + "cursor: pointer; font-weight: bold; font-family: sans-serif; }" + "#cv-rd-submit:hover {background-color: #447ff5;border-color: #447ff5;}" + "#cv-rd-submit:active {color: #fff;background-color: #2a4b8d;border-color: #2a4b8d;box-shadow: none;}" + "#cv-rd-submit:focus {border-color: #36c;box-shadow: inset 0 0 0 1px #36c,inset 0 0 0 2px #fff;outline: 0;}" );

// Add the panel itself var panel = document.createElement( "div" ); panel.id = "cv-revdel"; panel.innerHTML = "URL: " + "" + " " +           "Submit " + "Close "; document.getElementById( "bodyContent" ).insertBefore( panel,           document.getElementById( "mw-content-text" ) );

// Add range-add buttons before each of the buttons // that say "Compare selected revisions" var cmpSelRevsBtns = document.getElementsByClassName( "historysubmit" ); for( var i = 0, n = cmpSelRevsBtns.length; i < n; i++ ) { var rangeBtn = document.createElement( "button" ); rangeBtn.textContent = "Add range to revdel template"; rangeBtn.className = "cv-rd-add-range"; cmpSelRevsBtns[i].parentNode.insertBefore( rangeBtn, null ); rangeBtn.addEventListener( "click", function ( evt ) {               evt.preventDefault;

var oldidStart = document.querySelector( "li.selected.after" ).dataset.mwRevid; var oldidEnd = document.querySelector( "li.selected.before" ).dataset.mwRevid;

// Add new row to ranges table var rangesTable = document.getElementById( "cv-rd-ranges" ).getElementsByTagName( "tbody" )[0]; var newRow = rangesTable.insertRow( rangesTable.rows.length ); newRow.insertCell( 0 ).appendChild( makeOldidLink( oldidStart ) ); newRow.insertCell( 1 ).appendChild( makeOldidLink( oldidEnd ) ); newRow.insertCell( 2 ).innerHTML = ""; newRow.cells[2].childNodes[0].checked = true; newRow.cells[2].childNodes[0].addEventListener( "click", function {                    this.parentNode.previousElementSibling.childNodes[0].className = this.checked ? "" : "disabled";                } ); var deleteBtn = document.createElement( "button" ); deleteBtn.textContent = "Delete"; deleteBtn.className = "delete"; deleteBtn.addEventListener( "click", function {                    this.parentNode.parentNode.parentNode.removeChild( this.parentNode.parentNode );               } ); newRow.insertCell( 3 ).appendChild( deleteBtn ); } );       }

// Panel submission handler document.getElementById( "cv-rd-submit" ).addEventListener( "click", function {            if( saved ) return;            saved = true;            editPage;        } );

// Close handler document.getElementById( "cv-rd-close" ).addEventListener( "click", function {            $( "#cv-revdel" ).remove;            $( ".cv-rd-add-range" ).remove;        } ); }

mw.loader.using( [ "mediawiki.api", "mediawiki.util", "mediawiki.ui.input" ], function {        pageName = mw.config.get( "wgPageName" );

if( mw.config.get( "wgAction" ) == "history" ) { var link = mw.util.addPortletLink( "p-cactions", "#", "Request CV revdel", "pt-cv-revdel" ); link.addEventListener( "click", load ); if( window.location.href.indexOf( "open_cv_revdel=true" ) >= 0 ) { load; }       } else { var historyPage = mw.util.getUrl( pageName, { "action": "history", "open_cv_revdel": "true" } ); var link = mw.util.addPortletLink( "p-cactions", historyPage, "Request CV revdel", "pt-cv-revdel" ); }   } ); } );