User:Enterprisey/cv-revdel.js: Difference between revisions
Appearance
Content deleted Content added
Enterprisey (talk | contribs) auto-open when portlet link is clicked on other page |
Enterprisey (talk | contribs) fix bug |
||
Line 52: | Line 52: | ||
*/ |
*/ |
||
function load( evt ) { |
function load( evt ) { |
||
evt.preventDefault(); |
if( evt ) evt.preventDefault(); |
||
// Style for the panel |
// Style for the panel |
Revision as of 04:19, 21 September 2018
$( function () {
var SUMMARY = "Requesting copyvio revdel ([[User:Enterprisey/cv-revdel|cv-revdel]])";
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
+ "|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();
// 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 }" +
// Low-budget mw-ui
"#cv-revdel button { 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-revdel button:hover {background-color: #447ff5;border-color: #447ff5;}" +
"#cv-revdel button:active {color: #fff;background-color: #2a4b8d;border-color: #2a4b8d;box-shadow: none;}" +
"#cv-revdel button: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 = "<label for='cv-rd-url'>URL: </label>" +
"<input type='text' id='cv-rd-url' />" +
"<table id='cv-rd-ranges'><tr><th>Start</th><th>End</th>" +
"<th>Remove</th></table>" +
"<button id='cv-rd-submit' class='mw-ui-button mw-ui-constructive'>Submit</button>";
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";
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 ) );
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( 2 ).appendChild( deleteBtn );
} );
}
// Panel submission handler
document.getElementById( "cv-rd-submit" ).addEventListener( "click", function () {
if( saved ) return;
saved = true;
editPage();
} );
}
mw.loader.using( [ "mediawiki.api", "mediawiki.util" ], 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" );
}
} );
} );