User:LordVoldemort728/common.js

//

( function ( $, mw ) {

mw.loader.load( "jquery.chosen" );

mw.loader.load( "mediawiki.ui.input", "text/css" );

var afdcCategories = { "m": "Media and music", "o": "Organization, corporation, or product", "b": "Biographical", "s": "Society topics", "w": "Web or Internet", "g": "Games or sports", "t": "Science and technology", "f": "Fiction and the arts", "p": "Places and transportation", "i": "Indiscernible or unclassifiable topic", "u": "Not sorted yet" };

var ADVERTISEMENT = " (assisted)";

var currentAfdcCat = "";

var currentDelsortCategories = [];

if ( mw.config.get( "wgPageName" ).indexOf("Wikipedia:Articles_for_deletion/") != -1 &&

mw.config.get( "wgPageName" ).indexOf("Wikipedia:Articles_for_deletion/Log/") == -1) {

var portletLink = mw.util.addPortletLink("p-cactions", "#", "Delsort", "pt-delsort", "Perform deletion sorting");

// Load list of delsort categories

var delsortCategoriesPromise = $.ajax( {

url: "https://en.wikipedia.org/w/index.php?action=raw&title=" + encodeURIComponent( "Wikipedia:WikiProject Deletion sorting/Computer-readable.json" ) + "&maxage=86400&smaxage=86400",

dataType: "json"

} )

$( portletLink ).click( function ( e ) {

e.preventDefault();

// Validation for new custom fields

var validateCustomCat = function ( container ) {

var categoryName = container.children( "input" ).first().val();

$.getJSON(

mw.util.wikiScript("api"),

{

format: "json",

action: "query",

prop: "pageprops",

titles: "Wikipedia:WikiProject Deletion sorting/" + categoryName

}

).done( function ( data ) {

var setStatus = function ( status ) {

var text = "Not sure";

var imageSrc = "https://upload.wikimedia.org/wikipedia/commons/a/ad/Question_mark_grey.png";

switch( status ) {

case "d":

text = "Doesn't exist";

imageSrc = "https://upload.wikimedia.org/wikipedia/commons/5/5f/Red_X.svg";

break;

case "e":

text = "Exists";

imageSrc = "https://upload.wikimedia.org/wikipedia/commons/1/16/Allowed.svg";

break;

}

container.children( ".category-status" ).empty()

.append( $( "", { "src": imageSrc,

"style": "padding: 0 5px; width: 20px; height: 20px" } ) )

.append( text );

};

if( data && data.query && data.query.pages ) {

if( data.query.pages.hasOwnProperty( "-1" ) ) {

setStatus( "d" );

} else {

setStatus( "e" );

}

} else {

setStatus( "n" );

}

} );

};

// Define a function to add a new custom field, used below

var addCustomField = function ( e ) {

$( "

" )

.appendTo( "#delsort-td" )

.css( "width", "100%" )

.css( "margin", "0.25em auto" )

.append( $( "" )

.attr( "type", "text" )

.addClass( "mw-ui-input mw-ui-input-inline custom-delsort-field" )

.change( function ( e ) {

validateCustomCat( $( this ).parent() );

} ) )

.append( $( "" ).addClass( "category-status" ) )

.append( " (" )

.append( $( "", { "src": "https://upload.wikimedia.org/wikipedia/commons/a/a2/Crystal_128_reload.svg",

"style": "width: 15px; height: 15px; cursor: pointer" } )

.click( function ( e ) {

validateCustomCat( $( this ).parent() );

} ) )

.append( ")" )

.append( $( "' +

'

' );

$( "#add-custom-button" ).click( addCustomField );

$( "#close-button" ).click( function () { $( "#delsort" ).remove(); } );

var afdcHtml = "";

Object.keys( afdcCategories ).forEach( function ( code, i ) {

if ( i % 2 === 0 ) afdcHtml += "";

afdcHtml += "";

if ( i % 2 !== 0 ) afdcHtml += "";

} );

// If there are an odd number of AFDC cats, we need to close off the last row

if ( Object.keys( afdcCategories ).length % 2 !== 0 ) afdcHtml += "";

$( "#afdc" ).html( afdcHtml );

// Build the deletion sorting categories

delsortCategoriesPromise.done( function ( delsortCategories ) {

$.each( delsortCategories, function ( groupName, categories ) {

var group = $( "" )

.appendTo( "#delsort select" )

.attr( "label", groupName );

$.each( categories, function ( index, category ) {

group.append( $( "

.val( category )

.text( category )

.addClass( "delsort-category" ) );

} );

} );

getWikitext( mw.config.get( "wgPageName" ) ).then( function ( wikitext ) {

autofillAfdc( wikitext );

// Autofill the delsort box

var DELSORT_RE = /:(.+?)<\/small>/g;

var DELSORT_LIST_RE = /\[\[Wikipedia:WikiProject Deletion sorting\/(.+?)\|.+?\]\]/;

var delsortMatch;

var delsortListMatch;

do {

delsortMatch = DELSORT_RE.exec( wikitext );

if( delsortMatch !== null ) {

delsortListMatch = DELSORT_LIST_RE.exec( delsortMatch[1] );

if( delsortListMatch !== null ) {

currentDelsortCategories.push( delsortListMatch[1] );

var delsortOption = document.querySelector( "option.delsort-category[value='" + delsortListMatch[1] + "']" );

if( delsortOption ) {

delsortOption.selected = true;

}

}

}

} while( delsortMatch );

// Now that we've updated the underlying