MediaWiki:Common.js: Difference between revisions

From Blue Archive Wiki
Jump to navigation Jump to search
Content added Content deleted
(Re-format JS)
Tag: Reverted
m (Reverted edits by LeTrashman (talk) to last revision by Electricsheep)
Tag: Rollback
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */


$(function () {
$(function() {
if (window.location.host === "bluearchive.miraheze.org") {
if (window.location.host === "bluearchive.miraheze.org") {
window.location.replace("https://bluearchive.wiki" + window.location.pathname);
window.location.replace("https://bluearchive.wiki" + window.location.pathname);
}
}
if (mw.config.get("skin") === "minerva") {

if (mw.config.get("skin") === "minerva") {
// This hack is required to get custom css to load in mobile view. See https://phabricator.wikimedia.org/T270845
// This hack is required to get custom css to load in mobile view. See https://phabricator.wikimedia.org/T270845
loadMobileStylesheet();
loadMobileStylesheet();
}
}

function initCountdown() {
function initCountdown() {
var reset = new Date();
var reset = new Date();
reset.setUTCHours(19, 0, 0, 0);
reset.setUTCHours(19, 0, 0, 0);

function pad(n) {
function pad(n) {
return ("0" + (n | 0)).slice(-2);
return ("0" + (n | 0)).slice(-2);
Line 22: Line 20:
var now = new Date();
var now = new Date();
if (now > reset) {
if (now > reset) {
reset.setUTCDate(reset.getUTCDate() + 1);
reset.setUTCDate(reset.getUTCDate() + 1);
}
}
var remaining = ((reset - now) / 1000);
var remaining = ((reset - now) / 1000);
var hours = (remaining / 3600) % 60;
var hours = (remaining / 3600) % 60;
var minutes = (remaining / 60) % 60;
var minutes = (remaining / 60) % 60;
Line 34: Line 32:
tick();
tick();
}
}

function initCharacterTable() {
function initCharacterTable() {
mw.loader.using("jquery.tablesorter", function () {
mw.loader.using("jquery.tablesorter", function() {
$("table.sortable").tablesorter({ sortList: [{ 2: "desc" }, { 1: "asc" }] });
$("table.sortable").tablesorter({sortList: [{2: "desc"}, {1: "asc"}]});
});
});
}
}

function loadMobileStylesheet() {
function loadMobileStylesheet() {
var head = document.getElementsByTagName("head")[0];
var head = document.getElementsByTagName("head")[0];
var common = document.createElement("link");
var common = document.createElement("link");
var mobile = document.createElement("link");
var mobile = document.createElement("link");
common.rel = mobile.rel = "stylesheet";
common.rel = mobile.rel = "stylesheet";
common.type = mobile.type = "text/css";
common.type = mobile.type = "text/css";
common.href = "https://bluearchive.miraheze.org/w/load.php?lang=en&modules=site.styles&only=styles&skin=minerva";
common.href = "https://bluearchive.miraheze.org/w/load.php?lang=en&modules=site.styles&only=styles&skin=minerva";
mobile.href = "https://bluearchive.miraheze.org/wiki/MediaWiki:Mobile.css?action=raw&ctype=text/css";
mobile.href = "https://bluearchive.miraheze.org/wiki/MediaWiki:Mobile.css?action=raw&ctype=text/css";
head.appendChild(common);
head.appendChild(common);
head.appendChild(mobile);
head.appendChild(mobile);
}
}
if (mw.config.get("wgIsArticle")) {
if (mw.config.get("wgIsArticle")) {
// These won't run in Edit mode
// These won't run in Edit mode
if (mw.config.get("wgPageName") === "Main_Page") {
if (mw.config.get("wgPageName") === "Main_Page") {
initCountdown();
initCountdown();
initBirthdays();
initBirthdays();
}
}
if (mw.config.get("wgPageName") === "Characters") {
if (mw.config.get("wgPageName") === "Characters") {
initCharacterTable();
initCharacterTable();
}
}
}
}
});


/* Character stat calc*/
mw.loader.load('/w/index.php?title=MediaWiki:StatCalc.js&action=raw&ctype=text/javascript');


/* Character stat calc*/
mw.loader.load( '/w/index.php?title=MediaWiki:StatCalc.js&action=raw&ctype=text/javascript' );


/* Character birthdays - start */
function initBirthdays() {
const separator = ' <span></span> ';
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];


/* Character birthdays - start */
var birthdays = [];
function initBirthdays(){
var data_out = [];
const separator = ' <span></span> ';
var html_out = '';
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];


var birthdays = [];
var raw_data = $('#character-birthdays').attr('data-birthdays').split('&');
var data_out = [];
var html_out = '';


var raw_data = $('#character-birthdays').attr('data-birthdays').split('&');
raw_data.forEach(function (character) {
var char_data = character.split(' |');
raw_data.forEach(function (character) {
char_data.push(char_data[0].split('(')[0].trim()); //normalized name
var char_data = character.split(' |');
if (!birthdays.some(function (data) { return data[2] == char_data[2]; })) birthdays.push(char_data);
char_data.push(char_data[0].split('(')[0].trim()); //normalized name
});
if (!birthdays.some(function (data) {return data[2] == char_data[2];})) birthdays.push(char_data);
});


for (var index = -1; index <= 30; index++) {
for (var index = -1; index <= 30; index++) {
var date = new Date();
var date = new Date();
date.setDate(date.getDate() + index);
date.setDate(date.getDate() + index);
data_out = data_out.concat(birthdays.filter(function (data) { return data[1] == months[date.getMonth()] + ' ' + date.getDate(); }));
data_out = data_out.concat(birthdays.filter(function (data) { return data[1] == months[date.getMonth()]+' '+date.getDate();}));


if (data_out.length >= 5) { break; }
if (data_out.length >= 5) { break; }
}
}


data_out.forEach(
data_out.forEach(
function (character) { html_out += '<a href="/wiki/' + character[0] + '">' + character[2] + '&nbsp;(' + character[1].replace(' ', '&nbsp;') + ')</a>' + separator; }
function (character) { html_out += '<a href="/wiki/'+character[0]+'">'+character[2]+'&nbsp;('+character[1].replace(' ', '&nbsp;')+')</a>'+separator; }
);
);


$('#character-birthdays').append(html_out.substring(0, html_out.length - separator.length)).css("display", "");
$('#character-birthdays').append(html_out.substring(0,html_out.length-separator.length)).css( "display", "" );
}
}
/* Character birthdays - end */
/* Character birthdays - end */




/* Character affection table - start */
/* Character affection table - start */
const affection_start = 50;
const affection_start = 50;
const affection_cap = 50;
const affection_cap = 50;
var affection_data = {};
var affectionTableCounter = 0;


var affection_data = {};
$( document ).ready(function() {
var affectionTableCounter = 0;
initAffectionTable();
$(".affection-level input").on("change mouseup keyup click", function(){affectionChange($(this).closest("table"), $(this).val());});
$(".affection-data").children("div").on("click", function(){affectionChange($(this).closest("table"),$(this).attr('data-level'));});
});


function initAffectionTable(){
$(".character-affectiontable").each(function(){
var id = 'affectionTable-'+(++affectionTableCounter);
$(this).attr('id',id);
var data = {};


$(this).find(".affection-data > div").each(function(){
$(document).ready(function () {
var level = $(this).attr('data-level');
initAffectionTable();
data[level] = {};
$(".affection-level input").on("change mouseup keyup click", function () { affectionChange($(this).closest("table"), $(this).val()); });
var bonus = $(this).attr('data-stats').split(' ');
$(".affection-data").children("div").on("click", function () { affectionChange($(this).closest("table"), $(this).attr('data-level')); });
});


$.each( bonus, function( index ) {
bonus[index] = bonus[index].split('+');
data[level][bonus[index][0]] = parseInt(bonus[index][1]);
});
});
affection_data[id] = data;


$(this).find(".affection-level").html('<input type="number" value="'+affection_start+'" step="1" min="1" max="'+affection_cap+'" />');
function initAffectionTable() {
$(".character-affectiontable").each(function () {
affectionChange($(this), affection_start);
var id = 'affectionTable-' + (++affectionTableCounter);
if (typeof affection !== 'undefined') {
$(this).attr('id', id);
affectionGet($(".character-stattable"));
statTableRecalc($(".character-stattable"));
}
});
}


function affectionChange (affectionTable, level){
var data = {};
var effective_bonus = {};
var html_out = '';


level = (typeof level !== 'undefined' && !isNaN(level)) ? level : 1 ;
$(this).find(".affection-data > div").each(function () {
var level = $(this).attr('data-level');
if (level < 1) { affectionTable.find(".affection-level input").val(1); level = 1; }
data[level] = {};
if (level > affection_cap) { affectionTable.find(".affection-level input").val(affection_cap); level = affection_cap; }
var bonus = $(this).attr('data-stats').split(' ');


$.each(bonus, function (index) {
for (var index = 2; index <= level; index++) {
$.each( affection_data[affectionTable.attr('id')][index], function(stat_name, stat_value){
bonus[index] = bonus[index].split('+');
if (typeof effective_bonus[stat_name] == 'undefined') effective_bonus[stat_name] = 0;
data[level][bonus[index][0]] = parseInt(bonus[index][1]);
effective_bonus[stat_name] += stat_value;
});
});
});
}
affection_data[id] = data;
$.each( effective_bonus, function(stat_name, stat_value){
html_out += '<b>' + stat_name + '</b>' + ' +' + stat_value + ', ';
});


affection_data[affectionTable.attr('id')].current = effective_bonus;
$(this).find(".affection-level").html('<input type="number" value="' + affection_start + '" step="1" min="1" max="' + affection_cap + '" />');


if (affectionTable.find(".affection-level input").val() !== level) affectionTable.find(".affection-level input").val(level);
affectionChange($(this), affection_start);
affectionTable.find(".affection-total").html(html_out.substring(0,html_out.length-2));
if (typeof affection !== 'undefined') {
affectionGet($(".character-stattable"));
statTableRecalc($(".character-stattable"));
}
});
}


//update StatCalc if present
if (typeof affection !== 'undefined') {
affectionGet($(".character-stattable"));
statTableRecalc($(".character-stattable"));
}
}
/* Character affection table - end */


function affectionChange(affectionTable, level) {
var effective_bonus = {};
var html_out = '';


/* Character voice preview - start */
level = (typeof level !== 'undefined' && !isNaN(level)) ? level : 1;
$( document ).ready(function() {
initCharacterVoice();
});


function initCharacterVoice(){
if (level < 1) { affectionTable.find(".affection-level input").val(1); level = 1; }
var voice = $(".character td.character-voice");
if (level > affection_cap) { affectionTable.find(".affection-level input").val(affection_cap); level = affection_cap; }
if (voice.length && voice.attr('data-voice').length) {

voice.wrapInner('<span>');
for (var index = 2; index <= level; index++) {
if (voice.find("span").width() > voice.width()-36 ) voice.css('padding-right', '16px');
$.each(affection_data[affectionTable.attr('id')][index], function (stat_name, stat_value) {
voice.find("span").children().unwrap();
if (typeof effective_bonus[stat_name] == 'undefined') effective_bonus[stat_name] = 0;
voice.append('<audio class="voice-clip" src="'+voice.attr('data-voice')+'"></audio>').addClass('character-voice-preview');
effective_bonus[stat_name] += stat_value;
});
voice.find('audio')[0].volume=0.6;
$(".character td.character-voice-preview").on("click", function(){ voice.find('audio')[0].play(); })
}

$.each(effective_bonus, function (stat_name, stat_value) {
html_out += '<b>' + stat_name + '</b>' + ' +' + stat_value + ', ';
});

affection_data[affectionTable.attr('id')].current = effective_bonus;

if (affectionTable.find(".affection-level input").val() !== level) affectionTable.find(".affection-level input").val(level);
affectionTable.find(".affection-total").html(html_out.substring(0, html_out.length - 2));

//update StatCalc if present
if (typeof affection !== 'undefined') {
affectionGet($(".character-stattable"));
statTableRecalc($(".character-stattable"));
}
}
}
}
/* Character affection table - end */
/* Character voice preview - end */


/* Character voice preview - start */
$(document).ready(function () {
initCharacterVoice();
});

function initCharacterVoice() {
var voice = $(".character td.character-voice");
if (voice.length && voice.attr('data-voice').length) {
voice.wrapInner('<span>');
if (voice.find("span").width() > voice.width() - 36) voice.css('padding-right', '16px');
voice.find("span").children().unwrap();
voice.append('<audio class="voice-clip" src="' + voice.attr('data-voice') + '"></audio>').addClass('character-voice-preview');
voice.find('audio')[0].volume = 0.6;
$(".character td.character-voice-preview").on("click", function () { voice.find('audio')[0].play(); })
}
}
/* Character voice preview - end */
});

Revision as of 21:58, 9 July 2022

/* Any JavaScript here will be loaded for all users on every page load. */

$(function() {
	if (window.location.host === "bluearchive.miraheze.org") {
		window.location.replace("https://bluearchive.wiki" + window.location.pathname);
	}
	if (mw.config.get("skin") === "minerva") {
        // This hack is required to get custom css to load in mobile view. See https://phabricator.wikimedia.org/T270845
        loadMobileStylesheet();
    }
    function initCountdown() {
        var reset = new Date();
        reset.setUTCHours(19, 0, 0, 0);
        
        function pad(n) {
            return ("0" + (n | 0)).slice(-2);
        }

        function tick() {
            var now = new Date();
            if (now > reset) {
            reset.setUTCDate(reset.getUTCDate() + 1);
            }
            var remaining = ((reset - now) /  1000);
            var hours = (remaining / 3600) % 60;
            var minutes = (remaining / 60) % 60;
            var seconds = remaining % 60;
            document.getElementById("resetCountdown").innerHTML = pad(hours) + ":" + pad(minutes) + ":" + pad(seconds);
            setTimeout(tick, 1000);
        }

        tick();
    }
    function initCharacterTable() {
        mw.loader.using("jquery.tablesorter", function() {
            $("table.sortable").tablesorter({sortList: [{2: "desc"}, {1: "asc"}]});
        });
    }
    function loadMobileStylesheet() {
    	var head = document.getElementsByTagName("head")[0];
        var common = document.createElement("link");
		var mobile = document.createElement("link");
        common.rel = mobile.rel = "stylesheet";
        common.type = mobile.type = "text/css";
        common.href = "https://bluearchive.miraheze.org/w/load.php?lang=en&modules=site.styles&only=styles&skin=minerva";
		mobile.href = "https://bluearchive.miraheze.org/wiki/MediaWiki:Mobile.css?action=raw&ctype=text/css";
        head.appendChild(common);
        head.appendChild(mobile);
    }
    if (mw.config.get("wgIsArticle")) {
    	// These won't run in Edit mode
	    if (mw.config.get("wgPageName") === "Main_Page") {
	        initCountdown();
	        initBirthdays();
	    }
	    if (mw.config.get("wgPageName") === "Characters") {
	        initCharacterTable();
	    }
    }
});


/* Character stat calc*/
mw.loader.load( '/w/index.php?title=MediaWiki:StatCalc.js&action=raw&ctype=text/javascript' );


/* Character birthdays - start */
function initBirthdays(){
	const separator = ' <span></span> ';
	const months = [
		'January',
		'February',
		'March',
		'April',
		'May',
		'June',
		'July',
		'August',
		'September',
		'October',
		'November',
		'December'
	];

	var birthdays = [];
	var data_out = [];
	var html_out = '';

	var raw_data = $('#character-birthdays').attr('data-birthdays').split('&');
	
	raw_data.forEach(function (character) {
		var char_data = character.split(' |');
		char_data.push(char_data[0].split('(')[0].trim()); //normalized name
		if (!birthdays.some(function (data) {return data[2] == char_data[2];})) birthdays.push(char_data);
	});

	for (var index = -1; index <= 30; index++) {
		var date = new Date();
		date.setDate(date.getDate() + index);
		data_out = data_out.concat(birthdays.filter(function (data) { return data[1] == months[date.getMonth()]+' '+date.getDate();}));

		if (data_out.length >= 5) { break; }
	}

	data_out.forEach(
		function (character) { html_out += '<a href="/wiki/'+character[0]+'">'+character[2]+'&nbsp;('+character[1].replace(' ', '&nbsp;')+')</a>'+separator; }
	);

	$('#character-birthdays').append(html_out.substring(0,html_out.length-separator.length)).css( "display", "" );
}
/* Character birthdays - end */


/* Character affection table - start */
const affection_start = 50;
const affection_cap = 50;
	
var affection_data = {};
var affectionTableCounter = 0;

	
$( document ).ready(function() {
	initAffectionTable();
	$(".affection-level input").on("change mouseup keyup click", function(){affectionChange($(this).closest("table"), $(this).val());});
	$(".affection-data").children("div").on("click", function(){affectionChange($(this).closest("table"),$(this).attr('data-level'));});
});
	

function initAffectionTable(){
	$(".character-affectiontable").each(function(){
		var id = 'affectionTable-'+(++affectionTableCounter);
		$(this).attr('id',id);
		
		var data = {};

		$(this).find(".affection-data > div").each(function(){
			var level = $(this).attr('data-level');
			data[level] = {};
			var bonus = $(this).attr('data-stats').split(' ');

			$.each( bonus, function( index ) {
				bonus[index] = bonus[index].split('+');
				data[level][bonus[index][0]] =  parseInt(bonus[index][1]);
			});
		});
		affection_data[id] = data;

		$(this).find(".affection-level").html('<input type="number" value="'+affection_start+'" step="1" min="1" max="'+affection_cap+'" />'); 
		
		affectionChange($(this), affection_start);
		if (typeof affection !== 'undefined') {
			affectionGet($(".character-stattable"));
			statTableRecalc($(".character-stattable"));
		}
	});
}
	

function affectionChange (affectionTable, level){
	var effective_bonus = {};
	var html_out = '';

	level = (typeof level !== 'undefined' && !isNaN(level)) ? level : 1 ;
	
	if (level < 1) 	 			{ affectionTable.find(".affection-level input").val(1);	level = 1; }
	if (level > affection_cap) 	{ affectionTable.find(".affection-level input").val(affection_cap); level = affection_cap; }

	for (var index = 2; index <= level; index++) {
		$.each( affection_data[affectionTable.attr('id')][index], function(stat_name, stat_value){
			if (typeof effective_bonus[stat_name] == 'undefined') effective_bonus[stat_name] = 0;
			effective_bonus[stat_name] += stat_value;
		});
	}
	
	$.each( effective_bonus, function(stat_name, stat_value){
		html_out += '<b>' + stat_name + '</b>' + ' +' + stat_value + ', ';
	});

	affection_data[affectionTable.attr('id')].current = effective_bonus;

	if (affectionTable.find(".affection-level input").val() !== level) affectionTable.find(".affection-level input").val(level);
	affectionTable.find(".affection-total").html(html_out.substring(0,html_out.length-2));

	//update StatCalc if present
	if (typeof affection !== 'undefined') {
		affectionGet($(".character-stattable"));
		statTableRecalc($(".character-stattable"));
	}
}
/* Character affection table - end */


/* Character voice preview - start */
$( document ).ready(function() {
	initCharacterVoice();
});

function initCharacterVoice(){
    var voice = $(".character td.character-voice");
    if (voice.length && voice.attr('data-voice').length) {
        voice.wrapInner('<span>');
        if (voice.find("span").width() > voice.width()-36 ) voice.css('padding-right', '16px');
        voice.find("span").children().unwrap();
        voice.append('<audio class="voice-clip" src="'+voice.attr('data-voice')+'"></audio>').addClass('character-voice-preview');
        voice.find('audio')[0].volume=0.6;
        $(".character td.character-voice-preview").on("click", function(){ voice.find('audio')[0].play(); })
    }
}
/* Character voice preview - end */