User:Evad37/XFDcloserCommon.js

//This page contains common functions to be used in various XFD close scripts

//************ Global vars ************//

var isSysop;

var iconimages = []; iconimages.noimage = ""; iconimages.working = " "; iconimages.confirm = " "; iconimages.is_done = " "; iconimages.deleted = " "; iconimages.error  = " Error: "; iconimages.warning = " Warning: "; iconimages.question = " ";

//**** Execute When Document Ready ****//

$(document).ready( function {

// Show/hide closed discussions // This section adapted from User:Czar/hideClosedFFD.js, based on User:Mr.Z-man/hideClosedAFD mw.util.addPortletLink( 'p-cactions', '#', 'Hide closed', 'ca-hideClosed', "Hide closed discussions" ); mw.util.addPortletLink( 'p-cactions', '#', 'Show closed', 'ca-showClosed', "Show closed discussions" ); $('#ca-showClosed').hide; $('#ca-hideClosed').on('click', function {		$(".xfd-closed, .tfd-closed").hide;		$('#ca-showClosed').show;		$('#ca-hideClosed').hide;		return false;	}); $('#ca-showClosed').on('click', function {		$(".xfd-closed, .tfd-closed").show;		$('#ca-showClosed').hide;		$('#ca-hideClosed').show;		return false;	}); } );

//********* Common Functions **********//

/* == Check if user is an admin == */ function setSysopStatus { if ( -1 !== $.inArray( 'sysop', mw.config.get( 'wgUserGroups' ) ) ) { isSysop = true; } else { isSysop = false; }	console.log("isSysop: " + isSysop); }

/* == Mark discussions which have been closed == */ function markClosed( anchor, closed_as, mf ) { var closed_note = "Closed as " + closed_as + " (reload page to see the actual close)."; deliverMsg( anchor, closed_note, null, true, true, null, mf ); }

/* == Deliver messages to page == */ function deliverMsg( anchor, msg_txt, msg_img, clear_last_msg, strike_prev, make_penultimate, make_first ) {

var messages_object = {}; var messages, popped_msg;

var anchor_js = "#XFDClosing_" + anchor.replace( /(:|\.|\[|\]|,|\(|\)|%|\!|\/|@)/g, "\\$1" ); //jquery requires these to be escaped

var close_id = "XFDClosing_" + anchor; var $output_span = $(anchor_js);

//messages are stored as data in output span

messages = $output_span.data("array");

if ( messages === undefined ) { messages = []; }

if ( messages && messages.length > 0 && (clear_last_msg || make_penultimate) ) { popped_msg = messages.pop; }	if (!msg_img) { msg_img = "noimage"; }	if (msg_txt) {                    // msg_text can be empty if the last message is to be removed without adding anything if (make_first) { messages.unshift(iconimages[msg_img] + msg_txt); } else { messages.push(iconimages[msg_img] + msg_txt); }	}	if (make_penultimate) { messages.push(popped_msg); }

//save messages to data in output span $output_span.data("array", messages);

var msg_string = messages.join(" ");

$output_span.html(msg_string);

if (strike_prev) { $output_span.prev.attr("style", "text-decoration: line-through"); }

}

/* == Api failure messages == */ function apifailed( fcode, fresult ) { if ( fcode === "http" ) { return " Error details: HTTP error: " + fresult.textStatus + " "; // result.xhr contains the jqXHR object } else if ( fcode === "ok-but-empty" ) { return " Error details: Error: Got an empty response from the server "; } else { return " Error details: API error: " + fcode + " "; } }

/* == Check namespace == */ function checkNS(pagetitle, expectedNamespaceId) { var t = new mw.Title( pagetitle ); return (t.getNamespaceId === expectedNamespaceId);

}

/* == Get month name == */ function getMonthName(number) { //d.getUTCMonth

switch (number) { case 0: return "January"; case 1: return "February"; case 2: return "March"; case 3: return "April"; case 4: return "May"; case 5: return "June"; case 6: return "July"; case 7: return "August"; case 8: return "September"; case 9: return "October"; case 10: return "November"; case 11: return "December"; }

}

/* == Adjust bolding in comment == */ function adjustCommentBolding(comment) { // Figure out what if/where bold markup should be added var includes_bold = comment.match(/'''/g); if ( !includes_bold ) { bold1 = "'''"; bold2 = "'''"; } else if ( includes_bold.length > 1 ) { bold1 = ""; bold2 = ""; } else if ( comment.match(/^'''/g) ) { //includes start-of-bold markup at start of comment //see if it is possible to find an appropriate place to add end-of-bold markup var dot_dash_or_colon = comment.match(/(?:\.|-|–|—|:(?![^\]\[\}\{]*(?:\}|\])))/ ); if ( dot_dash_or_colon ) { //if so, add the missing end-of-bold markup comment = comment.replace(dot_dash_or_colon, "'''" + dot_dash_or_colon); bold1 = ""; bold2 = ""; } else { //remove unpaired start-of-bold, and set bold1/bold2 to bold whole comment comment = comment.replace("'''", ""); bold1 = "'''"; bold2 = "'''"; }	} else { //includes end-of-bold markup at midway through comment f- only add markup at start bold1 = "'''"; bold2 = ""; }

return [bold1, bold2, comment]; }