MediaWiki:Common.js: Difference between revisions

From Donate
Jump to navigation Jump to search
Content deleted Content added
load MediaWiki:EditTemplates.js by default for logged in users
No edit summary
 
(37 intermediate revisions by the same user not shown)
Line 1: Line 1:
/**
/**
* Any JavaScript here will be loaded for all users on every page load.
* MediaWiki:Common.js - Any JavaScript here will be loaded for all users on every page load.
**/
**/


function getQuerystring( key ) {
$(function() {
// TODO: Replace with mw.util.getParamValue() everywhere, and remove
key = key.replace( /[\[]/, '\\\[' ).replace( /[\]]/, '\\\]' );
var regex = new RegExp( '[\\?&]' + key + '=([a-zA-Z0-9\.\_\-]*)' );
var qs = regex.exec( window.location.search );
return qs === null ? '' : qs[1];
}


mw.loader.using( ['mediawiki.util'] ).done( function() {
function adjustHPC() {
/* Adjust amounts based on highest previous contribution (hpc)
or most recent contribution (mrc) parameter. Used for emails.
TODO: split data out? */


// If Jimmy tweets a bare link to https://donate.wikimedia.org/w/index.php?title=Special:LandingPage
// Look for 'hpc' parameter, then 'mrc'. If neither, then bail out.
// we are gonna have a bad time as geolocation doesn't happen. Do a redirect to send them round again
var hpc = parseFloat( mw.util.getParamValue('hpc') );
try {
if( isNaN(hpc) ) {
var url = new URL( location.href );
hpc = parseFloat( mw.util.getParamValue('mrc') );
if (
if( isNaN(hpc) ) {
url.searchParams.get( 'title' ) === 'Special:LandingPage' &&
return;
url.searchParams.get( 'country' ) === null &&
}
url.searchParams.get( 'retried' ) === null
}
) {
console.log( 'redirecting to pick up country parameter' );
url.pathname = '/';
url.searchParams.set( 'retried', 1 ); // only do this once
url.searchParams.delete( 'title' );
window.location.href = url.toString();
}
} catch (error) {
console.warn('Error parsing URL. Possibly due to unreplaced % signs?');
}


// Disable logo link
var hpcSet = mw.util.getParamValue('hpcSet');
$('#p-logo a').attr( { href: '#', title: '' } );


// Code for donation forms - https://donate.wikimedia.org/wiki/MediaWiki:DonationForm.js
var currency = $("input[name='currency_code']").val();
if ( document.forms['donateForm'] ) {
var language = mw.config.get('wgUserLanguage');
var formJS = mw.util.getParamValue( 'formJS' );
if ( formJS && formJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
mw.loader.load( '/w/index.php?title=' + formJS + '&action=raw&ctype=text/javascript' );
} else {
mw.loader.load( '/w/index.php?title=MediaWiki:DonationForm.js&action=raw&ctype=text/javascript' );
}


// Load monthly convert JS
var radioAmountsData = {
// TODO: allow loading different JS variants, either here or defined in the Template
'USD' : {
mw.loader.load( '/w/index.php?title=MediaWiki:MonthlyConvert.js&action=raw&ctype=text/javascript' );
'default' : [
}
[ 0, [ 5, 10, 20, 25, 35, 50, 100 ] ],
[ 5, [ 10, 15, 20, 35, 50, 100, 150 ] ],
[ 10, [ 15, 20, 25, 35, 50, 100, 150 ] ],
[ 15, [ 20, 25, 35, 50, 75, 100, 250 ] ],
[ 20, [ 25, 35, 50, 75, 100, 150, 250 ] ],
[ 25, [ 30, 40, 50, 75, 100, 150, 250 ] ],
[ 35, [ 25, 50, 75, 100, 200, 250, 300 ] ],
[ 75, [ 25, 50, 75, 100, 200, 300, 500 ] ],
[ 100, [ 25, 50, 100, 150, 250, 500, 1000 ] ],
[ 150, [ 50, 100, 150, 200, 350, 500, 1000 ] ],
[ 200, [ 100, 150, 200, 300, 400, 500, 1000 ] ],
[ 500, [ 100, 250, 500, 750, 1000, 1500, 2000 ] ],
[ 1000, [ 500, 1000, 2000, 2500, 3000, 4000, 5000 ] ]
],
'LTLA' : [
[ 0, [ 3, 10, 15, 20, 35, 50, 100 ] ],
[ 5, [ 5, 10, 20, 35, 50, 100, 150 ] ],
[ 10, [ 10, 20, 35, 50, 75, 100, 150 ] ],
[ 15, [ 15, 25, 35, 50, 75, 100, 250 ] ],
[ 20, [ 20, 30, 50, 75, 100, 150, 250 ] ],
[ 25, [ 25, 35, 50, 75, 100, 150, 250 ] ],
[ 35, [ 25, 50, 75, 100, 200, 250, 300 ] ],
[ 75, [ 25, 50, 75, 100, 200, 300, 500 ] ],
[ 100, [ 25, 50, 100, 150, 250, 500, 1000 ] ],
[ 150, [ 50, 100, 150, 200, 350, 500, 1000 ] ],
[ 200, [ 100, 150, 200, 300, 400, 500, 1000 ] ],
[ 500, [ 100, 250, 500, 750, 1000, 1500, 2000 ] ],
[ 1000, [ 500, 1000, 2000, 2500, 3000, 4000, 5000 ] ]
],
'midtier2018' : [
[ 0, [ 3, 5, 10, 20, 30, 50, 100 ] ],
[ 5, [ 5, 10, 20, 35, 50, 100, 150 ] ],
[ 10, [ 10, 15, 20, 35, 50, 100, 150 ] ],
[ 15, [ 15, 20, 25, 35, 50, 100, 150 ] ],
[ 20, [ 20, 25, 35, 50, 75, 100, 150 ] ],
[ 25, [ 35, 50, 75, 100, 150, 250, 500 ] ],
[ 35, [ 50, 75, 100, 150, 250, 350, 500 ] ],
[ 50, [ 75, 100, 150, 200, 250, 350, 500 ] ],
[ 75, [ 100, 150, 200, 300, 400, 500, 1000 ] ],
[ 100, [ 150, 200, 250, 300, 400, 500, 1000 ] ],
[ 150, [ 200, 250, 300, 400, 500, 1000, 2500 ] ],
[ 200, [ 500, 750, 1000, 2000, 3500, 5000, 7500 ] ],
[ 500, [ 750, 1000, 1500, 2500, 5000, 7500, 10000 ] ],
[ 1000, [ 1000, 2000, 3000, 4000, 5000, 7500, 10000 ] ]
],
'LT2018' : [
[ 0, [ 5, 10, 20.18, 25, 35, 50, 100 ] ],
[ 5, [ 10, 15, 20.18, 35, 50, 100, 150 ] ],
[ 10, [ 15, 20.18, 25, 35, 50, 100, 150 ] ],
[ 15, [ 20.18, 25, 35, 50, 75, 100, 250 ] ],
[ 20, [ 25, 35, 50, 75, 100, 150, 250 ] ],
[ 25, [ 30, 40, 50, 75, 100, 150, 250 ] ],
[ 35, [ 25, 50, 75, 100, 200, 250, 300 ] ],
[ 75, [ 25, 50, 75, 100, 200, 300, 500 ] ],
[ 100, [ 25, 50, 100, 150, 250, 500, 1000 ] ],
[ 150, [ 50, 100, 150, 200, 350, 500, 1000 ] ],
[ 200, [ 100, 150, 200, 300, 400, 500, 1000 ] ],
[ 500, [ 100, 250, 500, 750, 1000, 1500, 2018 ] ],
[ 1000, [ 500, 1000, 2018, 2500, 3000, 4000, 5000 ] ]
]
},
'BRL' : [
[ 0, [ 15, 25, 50, 75, 100, 150, 300 ] ],
[ 5, [ 25, 35, 50, 75, 100, 200, 300 ] ],
[ 10, [ 30, 50, 75, 100, 150, 300, 500 ] ],
[ 15, [ 50, 75, 100, 150, 225, 300, 500 ] ],
[ 20, [ 50, 100, 150, 225, 300, 400, 500 ] ],
[ 25, [ 75, 125, 200, 250, 300, 400, 500 ] ],
[ 35, [ 100, 150, 225, 300, 500, 750, 1000 ] ],
[ 75, [ 100, 150, 225, 300, 500, 1000, 1500 ] ],
[ 100, [ 100, 150, 300, 500, 1000, 1500, 3000 ] ],
[ 150, [ 250, 500, 750, 1000, 1500, 3000, 5000 ] ],
[ 200, [ 500, 1000, 2000, 3000, 4000, 5000, 10000 ] ],
[ 500, [ 1000, 2500, 5000, 7500, 10000, 12500, 15000 ] ],
[ 1000, [ 1000, 2500, 5000, 7500, 10000, 12500, 15000 ] ]
],
'JPY' : [
[ 0, [ 500, 1000, 2000, 2500, 4000, 5000, 10000 ] ],
[ 5, [ 1000, 1500, 2500, 4000, 5000, 10000, 15000 ] ],
[ 10, [ 1500, 2000, 3000, 4000, 5000, 10000, 15000 ] ],
[ 15, [ 2000, 2500, 3500, 5000, 7500, 10000, 25000 ] ],
[ 20, [ 2500, 3500, 5000, 7500, 10000, 15000, 25000 ] ],
[ 25, [ 3000, 4000, 5000, 7500, 10000, 15000, 25000 ] ],
[ 35, [ 2500, 5000, 7500, 10000, 20000, 30000, 50000 ] ],
[ 75, [ 2500, 5000, 7500, 10000, 20000, 50000, 100000 ] ],
[ 100, [ 5000, 10000, 15000, 20000, 35000, 50000, 100000 ] ],
[ 500, [ 10000, 25000, 50000, 75000, 100000, 150000, 200000 ] ]
],
'SEK' : [
[ 0, [ 20, 50, 100, 200, 300, 500, 1000 ] ],
[ 3, [ 30, 50, 100, 200, 300, 500, 1000 ] ],
[ 5, [ 50, 100, 150, 200, 300, 500, 1000 ] ],
[ 23, [ 50, 100, 200, 300, 500, 750, 1000 ] ]
]
};
radioAmountsData.AUD = radioAmountsData.USD;
radioAmountsData.CAD = radioAmountsData.USD;
radioAmountsData.GBP = radioAmountsData.USD;
radioAmountsData.NZD = radioAmountsData.USD;
radioAmountsData.EUR = radioAmountsData.USD;


// Allow manual country selection and reload
var appealAmountsData = {
// Run this only when needed
'USD' : [
var enableCountrySelect = function() {
[ 0, [ 5, 10, 20 ] ],
var language = mw.config.get('wgPageContentLanguage'),
[ 10, [ 10, 20, 50 ] ],
countrySelect = document.getElementById('country-select');
[ 20, [ 20, 30, 50 ] ],
[ 35, [ 20, 30, 50 ] ],
[ 50, [ 20, 50, 100 ] ],
[ 75, [ 50, 75, 100 ] ],
[ 100, [ 75, 100, 150 ] ],
[ 150, [ 75, 100, 200 ] ],
[ 200, [ 100, 200, 300 ] ]
],
'JPY' : [
[ 0, [ 300, 500, 1000 ] ],
[ 3, [ 500, 1000, 1500 ] ],
[ 5, [ 1000, 1500, 2000 ] ],
[ 10, [ 1500, 2000, 5000 ] ],
[ 20, [ 2000, 3000, 5000 ] ],
[ 50, [ 2000, 5000, 10000 ] ],
[ 100, [ 5000, 10000, 15000 ] ]
],
'SEK' : [
[ 0, [ 20, 50, 100 ] ],
[ 3, [ 30, 50, 100 ] ],
[ 5, [ 50, 100, 150 ] ],
[ 15, [ 100, 150, 200 ] ],
[ 23, [ 100, 200, 300 ] ],
[ 38, [ 100, 200, 500 ] ],
[ 75, [ 100, 500, 750 ] ],
[ 112, [ 100, 500, 1000 ] ]
]
};
appealAmountsData.AUD = appealAmountsData.USD;
appealAmountsData.CAD = appealAmountsData.USD;
appealAmountsData.GBP = appealAmountsData.USD;
appealAmountsData.NZD = appealAmountsData.USD;
appealAmountsData.EUR = appealAmountsData.USD;


// Translations
var formats = {
if ( language !== 'en' && language !== 'en-gb' ) {
"USD" : "$\t",
const translationsUrl = 'https://donate.wikimedia.org/wiki/Module:Country_links/data.json?action=raw';
"EUR" : {
$.getJSON( translationsUrl, function( translations ) {
"en" : "€\t",
if ( translations[language] ) {
"cy" : "€\t",
var list = Array.from( countrySelect.options );
"ga" : "€\t",
list.forEach( option => {
"mt" : "€\t",
option.innerText = translations[language][option.value] || option.innerText;
"nl" : "€ \t",
});
"lv" : "€ \t",
// Sort for new language
"tr" : "€ \t",
list.sort( function(a, b) {
"default" : "\t €"
return a.innerText.localeCompare( b.innerText );
},
});
"AUD" : "$\t",
"CAD" : {
for ( var i = 0; i < list.length; i++ ) {
list[i].parentNode.appendChild( list[i] );
"fr" : "\t $",
}
"default" : "$\t"
}
},
});
"GBP" : "£\t",
}
"NZD" : "$\t",
"JPY" : "¥\t",
"SEK" : "\t kr",
"BRL" : "R$\t"
};


document.getElementById('country-select-go').addEventListener( 'click', function(e) {
var format = formats[currency][language] || formats[currency]["default"] || formats[currency] || '\t';
if ( countrySelect.value ) {
// Chapter countries, use the built-in donatewiki redirects
if ( countrySelect.value === 'DE' ) {
window.location = 'https://donate.wikimedia.org/?country=DE';
} else if ( countrySelect.value === 'CH' ) {
window.location = 'https://donate.wikimedia.org/?country=CH';
} else {
var url = new URL( location.href );
url.searchParams.set( 'country', countrySelect.value );
window.location = url.toString();
}
}
});
};


if ( document.getElementById('country-change') ) {
// Radio button amounts
document.getElementById('country-change').addEventListener( 'click', function(e) {
var radioAmounts = pickAmountArray( radioAmountsData, currency, hpc, hpcSet );
document.getElementById('country-change-select').style.display = 'block';
if ( radioAmounts.length ) {
enableCountrySelect();
// Change buttons
});
for (var j = 0; j < radioAmounts.length; j++) {
}
var $radio = $("#input_amount_" + j);
var $label = $("label[for='input_amount_" + j + "']");
$radio.val( radioAmounts[j] );
$label.text( format.replace('\t', radioAmounts[j]) );
}
}


if ( document.getElementById('geolocate-error') ) {
// Appeal amounts
enableCountrySelect();
var appealAmounts = pickAmountArray( appealAmountsData, currency, hpc, hpcSet );
}
if ( appealAmounts.length ) {
// Build string
// Fundraise Up privacy footer
var appealAmountString = '';
if ( mw.util.getParamValue('fundraiseupScript') ) {
for( var k = 0; k < appealAmounts.length; k++ ) {
document.getElementById('donate-privacy-third-party').style.display = 'block';
appealAmountString += format.replace('\t', appealAmounts[k]) + ', ';
}
}
appealAmountString = appealAmountString.trim();


// Code for Thank You pages - https://donate.wikimedia.org/wiki/MediaWiki:ThankYouPage.js
$('.consider-amounts').html(appealAmountString);
if ( mw.config.get( 'wgTitle' ).indexOf('Thank You') !== -1 ) {
}
mw.loader.load( '/w/index.php?title=MediaWiki:ThankYouPage.js&action=raw&ctype=text/javascript' );
}


// Code for support pages - https://donate.wikimedia.org/wiki/MediaWiki:SupportPage.js
}
// TODO: have a better way to trigger this
var supportPages = [ 'Ways to Give', 'Problems donating', 'Cancel or change recurring giving',
'Matching Gifts', 'Support Page', 'FAQ', 'Tax deductibility',
'US State Solicitation Disclosures', 'Draft:FAQ', 'Workplace giving' ];
if ( supportPages.indexOf( mw.config.get( 'wgTitle' ).split('/')[0] ) !== -1 || document.querySelector( '.language-switcher' ) ) {
mw.loader.load( '/w/index.php?title=MediaWiki:SupportPage.js&action=raw&ctype=text/javascript' );
}


// Load some tools for editors - https://donate.wikimedia.org/wiki/MediaWiki:EditTemplates.js
function pickAmountArray( data, currency, hpc, hpcSet ) {
if ( mw.config.get( 'wgUserName' ) !== null ) {
/**
mw.loader.load( '/w/index.php?title=MediaWiki:EditTemplates.js&action=raw&ctype=text/javascript' );
* Choose the amounts for radio buttons / appeal based on hpc
}
* @param {Object} data
* @param {String} currency
* @param {Number} hpc
* @param {String} hpcSet
* @return {Array} Array of amounts (as numbers)
*/


/**
var set, amounts;
* @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
* @rev 6
*/
var extraCSS = mw.util.getParamValue( 'withCSS' ),
extraJS = mw.util.getParamValue( 'withJS' );


if ( !(currency in data) ) {
if ( extraCSS ) {
if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
return [];
mw.loader.load( '/w/index.php?title=' + extraCSS + '&action=raw&ctype=text/css', 'text/css' );
}
} else {
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
}
}


if ( $.isArray(data[currency]) ) {
if ( extraJS ) {
if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
// No variant sets
mw.loader.load( '/w/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript' );
set = data[currency];
} else {
} else {
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
// We need to go deeper. Check the variants.
}
if ( hpcSet in data[currency] ) {
}
set = data[currency][hpcSet];
} else {
set = data[currency]['default'];
}
}


/* HACK to run certain inline scripts when document ready (e.g. so jquery is avaliable)
// Find correct amount array for this hpc
TODO: Make suitable for multiple scripts. Or figure out a better way to do this.
for (var i = 0; i < set.length; i++) {
*/
if ( set[i][0] > hpc ) {
if ( typeof inlineScriptWhenReady !== 'undefined') {
break;
inlineScriptWhenReady();
}
}
amounts = set[i][1];
}


} );
return amounts;

}

function preSelect() {
/* Check for a 'preSelect' url parameter, and select that option.
If there isn't an option, add it to the "Other" box and select that */
var preSelectAmount = mw.util.getParamValue('preSelect');
if ( preSelectAmount > 0 ) {
$preSelectOption = $('input[name="amount"][value="' + preSelectAmount + '"]');
if ( $preSelectOption.length ) {
// Select existing input
$preSelectOption.prop('checked', true);
} else {
$('#input_amount_other_box').val( preSelectAmount );
$('#input_amount_other').prop('checked', true);
}
}
}

function addCardTypesClass(country) {
/**
* Add card types class to credit card button, so we can show correct logos
* Banner equivalent: https://meta.wikimedia.org/wiki/MediaWiki:FundraisingBanners/LocalizeJS-2017.js
* @param {String} country ISO code
*/
var cardTypes = {
// Big 6
'US' : 'vmad',
'CA' : 'vma',
'GB' : 'vmaj',
'IE' : 'vmaj',
'AU' : 'vmaj',
'NZ' : 'vma',
// Euro countries
'AT' : 'vmaj',
'BE' : 'vmaj',
'ES' : 'vmaj',
'FR' : 'CBvma', // Adyen
'IT' : 'vmaj',
'LU' : 'vmaj',
'LV' : 'vma',
'NL' : 'vmaj',
'PT' : 'vmaj',
'SK' : 'vmaj',
// Others
'DK' : 'vma',
'HU' : 'vma',
'IL' : 'vmad', // Adyen
'JP' : 'vmaj',
'MY' : 'vmaj',
'NO' : 'vma',
'PL' : 'vma',
'RO' : 'vma',
'SE' : 'vma',
'UA' : 'vma', // Adyen
'ZA' : 'vm'
};
if ( cardTypes[country] ) {
$('.paymentmethod-cc').addClass('cctypes-' + cardTypes[country] );
}
}

/* Form functions */
function clearOther(box) {
document.getElementById("input_amount_other").checked = true;
box.value = "";
}

function selectOther() {
document.getElementById("input_amount_other").checked = true;
}

function resetOther(box) {
box.value = "</html>{{int:donate_interface-other}}<html>";
}

function selectAmount() {
$('#input_amount_other_box').val('');
$('input[name="amountGiven"]').val('');
}
/* End form functions */


$(document).ready(function() {

mw.loader.using( ['mediawiki.util'] ).done( function() {

// Block typing symbols in input field, otherwise Safari allows them and then chokes
// https://phabricator.wikimedia.org/T118741, https://phabricator.wikimedia.org/T173431
var amountOtherInput = document.getElementById('input_amount_other_box');
if ( amountOtherInput ) {
amountOtherInput.onkeypress = function(e) {
// Allow special keys in Firefox
if ((e.code == 'ArrowLeft') || (e.code == 'ArrowRight') ||
(e.code == 'ArrowUp') || (e.code == 'ArrowDown') ||
(e.code == 'Delete') || (e.code == 'Backspace')) {
return;
}
var chr = String.fromCharCode(e.which);
if ("0123456789., ".indexOf(chr) === -1) {
return false;
}
};
}

// Disable submitting form with Enter key
$('form[name="paypalcontribution"]').on('keypress', function(e) {
var code = ( e.keyCode ? e.keyCode : e.which );
if ( code == 13 ) {
e.preventDefault();
}
});

// But allow Enter on buttons
$('.payment-method-button').keyup(function(e) {
if (event.keyCode === 13) {
e.target.click();
}
});

if ( document.paypalcontribution ) {
document.paypalcontribution.utm_medium.value = mw.util.getParamValue( 'utm_medium' );
document.paypalcontribution.utm_campaign.value = mw.util.getParamValue( 'utm_campaign' );
document.paypalcontribution.utm_key.value = mw.util.getParamValue( 'utm_key' );

// Strip protocol to stop firewall throwing fits
document.paypalcontribution.referrer.value = document.referrer.replace(/https?:\/\//i, "");

// hide frequency options in India, where we can only handle one-time donations
if (document.paypalcontribution.country.value === 'IN') {
$("#frequency_onetime").prop('checked', true);
$(".frequency-options").hide();
$("#cancel-monthly").hide();
}

addCardTypesClass(document.paypalcontribution.country.value);
}

// Links open in new tab
$('.links-in-new-tab a').attr('target', '_blank');

// Disable logo link
$("#p-logo a").attr("href", "#");
$("#p-logo a").attr("title", "");

// These don't need to be tabbable on the landing page
$('#searchInput, .mw-jump-link').attr('tabindex', '-1');

$(".input_amount_other").click(function() {
$("#input_amount_other_box").focus();
});

// Allow preselecting monthly
if( mw.util.getParamValue('monthly') ) {
$('#frequency_monthly').click();
}

// If the optin section has some javascript to run, do that
if ( typeof initOptin === 'function' ) {
initOptin();
}

try {
adjustHPC();
preSelect(); // Make sure to do this *after* other fiddling with values
}
finally {
$('.optin-options').insertAfter('.amount-options');
$('.consider-amounts').show();
$('#actual-form').show();
$('#actual-form-loading').hide();
}

});

/**
* Code for fundraising Thank You pages
* e.g. https://donate.wikimedia.org/wiki/Thank_You
*/
if ( mw.config.get( 'wgTitle' ).split('/')[0] === 'Thank You' ) {

mw.loader.using( 'mediawiki.util', function() {

var paymentMethod = mw.util.getParamValue('payment_method'),
country = mw.util.getParamValue('country');

if ( paymentMethod === 'bt' ) {
document.getElementById('ty-banktransfer').style.display = 'block';
}
if ( paymentMethod === 'bitcoin' ) {
document.getElementById('ty-bitcoin').style.display = 'block';
}
if ( paymentMethod === 'bpay' ) {
document.getElementById('ty-bpay').style.display = 'block';
}

if ( country === 'US' || country === 'CA' ) {
document.getElementById('ty-store').style.display = 'block';
}

});

}

/**
* Code for fundraising support pages
* e.g. https://donate.wikimedia.org/wiki/Problems_donating
*
* Edit at https://donate.wikimedia.org/wiki/MediaWiki:SupportPage.js
*/
var supportPages = [
'Ways to Give',
'Problems donating',
'Cancel or change recurring giving',
'Matching Gifts',
'Support Page',
'FAQ',
'Tax deductibility'
]
if ( supportPages.indexOf( mw.config.get( 'wgTitle' ).split('/')[0] ) !== -1 ) {
mw.loader.load( '/w/index.php?title=MediaWiki:SupportPage.js&action=raw&ctype=text/javascript' );
}

/**
* Load some tools for editors
*
* Edit at https://donate.wikimedia.org/wiki/MediaWiki:EditTemplates.js
*/
if ( mw.config.get( 'wgUserName' ) !== null ) {
mw.loader.load( '/w/index.php?title=MediaWiki:EditTemplates.js&action=raw&ctype=text/javascript' );
}

});


$(document).ready(function() {
/* HACK to run certain inline scripts when document ready (e.g. so jquery is avaliable)
TODO: Make suitable for multiple scripts. Or figure out a better way to do this.
*/
if ( typeof inlineScriptWhenReady !== 'undefined') {
inlineScriptWhenReady();
}
});
});

Latest revision as of 17:19, 2 December 2024

/**
 * MediaWiki:Common.js - Any JavaScript here will be loaded for all users on every page load.
 **/

$(function() {

	mw.loader.using( ['mediawiki.util'] ).done( function() {

		// If Jimmy tweets a bare link to https://donate.wikimedia.org/w/index.php?title=Special:LandingPage
		// we are gonna have a bad time as geolocation doesn't happen. Do a redirect to send them round again
		try {
			var url = new URL( location.href );
			if ( 
				url.searchParams.get( 'title' ) === 'Special:LandingPage' &&
				url.searchParams.get( 'country' ) === null &&
				url.searchParams.get( 'retried' ) === null
			) {
				console.log( 'redirecting to pick up country parameter' );
				url.pathname = '/';
				url.searchParams.set( 'retried', 1 ); // only do this once
				url.searchParams.delete( 'title' );
				window.location.href = url.toString();
			}
		} catch (error) {
			console.warn('Error parsing URL. Possibly due to unreplaced % signs?');
		}

		// Disable logo link
		$('#p-logo a').attr( { href: '#', title: '' } );

		// Code for donation forms - https://donate.wikimedia.org/wiki/MediaWiki:DonationForm.js
		if ( document.forms['donateForm'] ) {
			var formJS = mw.util.getParamValue( 'formJS' );
			if ( formJS && formJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
				mw.loader.load( '/w/index.php?title=' + formJS + '&action=raw&ctype=text/javascript' );
			} else {
				mw.loader.load( '/w/index.php?title=MediaWiki:DonationForm.js&action=raw&ctype=text/javascript' );
			}

			// Load monthly convert JS
			// TODO: allow loading different JS variants, either here or defined in the Template
			mw.loader.load( '/w/index.php?title=MediaWiki:MonthlyConvert.js&action=raw&ctype=text/javascript' );
		}

		// Allow manual country selection and reload
		// Run this only when needed
		var enableCountrySelect = function() {
			var language = mw.config.get('wgPageContentLanguage'),
				countrySelect = document.getElementById('country-select');

			// Translations
			if ( language !== 'en' && language !== 'en-gb' ) {
				const translationsUrl = 'https://donate.wikimedia.org/wiki/Module:Country_links/data.json?action=raw';
				$.getJSON( translationsUrl, function( translations ) {
					if ( translations[language] ) {
						var list = Array.from( countrySelect.options );
						list.forEach( option => {
							option.innerText = translations[language][option.value] || option.innerText;
						});
						// Sort for new language
						list.sort( function(a, b) {
							return a.innerText.localeCompare( b.innerText );
						});
						for ( var i = 0; i < list.length; i++ ) {
							list[i].parentNode.appendChild( list[i] );
						}
					}
				});
			}

			document.getElementById('country-select-go').addEventListener( 'click', function(e) {
				if ( countrySelect.value ) {
					// Chapter countries, use the built-in donatewiki redirects
					if ( countrySelect.value === 'DE' ) {
						window.location = 'https://donate.wikimedia.org/?country=DE';
					} else if ( countrySelect.value === 'CH' ) {
						window.location = 'https://donate.wikimedia.org/?country=CH';
					} else {
						var url = new URL( location.href );
						url.searchParams.set( 'country', countrySelect.value );
						window.location = url.toString();
					}
				}
			});
		};

		if ( document.getElementById('country-change') ) {
			document.getElementById('country-change').addEventListener( 'click', function(e) {
				document.getElementById('country-change-select').style.display = 'block';
				enableCountrySelect();
			});
		}

		if ( document.getElementById('geolocate-error') ) {
			enableCountrySelect();
		}
		
		// Fundraise Up privacy footer
		if ( mw.util.getParamValue('fundraiseupScript') ) {
			document.getElementById('donate-privacy-third-party').style.display = 'block';
		}

		// Code for Thank You pages - https://donate.wikimedia.org/wiki/MediaWiki:ThankYouPage.js
		if ( mw.config.get( 'wgTitle' ).indexOf('Thank You') !== -1 ) {
			mw.loader.load( '/w/index.php?title=MediaWiki:ThankYouPage.js&action=raw&ctype=text/javascript' );
		}

		// Code for support pages - https://donate.wikimedia.org/wiki/MediaWiki:SupportPage.js
		// TODO: have a better way to trigger this
		var supportPages = [ 'Ways to Give', 'Problems donating', 'Cancel or change recurring giving',
							 'Matching Gifts', 'Support Page', 'FAQ', 'Tax deductibility',
							 'US State Solicitation Disclosures', 'Draft:FAQ', 'Workplace giving' ];
		if ( supportPages.indexOf( mw.config.get( 'wgTitle' ).split('/')[0] ) !== -1 || document.querySelector( '.language-switcher' ) ) {
			mw.loader.load( '/w/index.php?title=MediaWiki:SupportPage.js&action=raw&ctype=text/javascript' );
		}

		// Load some tools for editors - https://donate.wikimedia.org/wiki/MediaWiki:EditTemplates.js
		if ( mw.config.get( 'wgUserName' ) !== null ) {
			mw.loader.load( '/w/index.php?title=MediaWiki:EditTemplates.js&action=raw&ctype=text/javascript' );
		}

		/**
		 * @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
		 * @rev 6
		 */
		var extraCSS = mw.util.getParamValue( 'withCSS' ),
			extraJS = mw.util.getParamValue( 'withJS' );

		if ( extraCSS ) {
			if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
				mw.loader.load( '/w/index.php?title=' + extraCSS + '&action=raw&ctype=text/css', 'text/css' );
			} else {
				mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
			}
		}

		if ( extraJS ) {
			if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
				mw.loader.load( '/w/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript' );
			} else {
				mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
			}
		}

	   /* HACK to run certain inline scripts when document ready (e.g. so jquery is avaliable)
			TODO: Make suitable for multiple scripts. Or figure out a better way to do this.
		*/
		if ( typeof inlineScriptWhenReady !== 'undefined') {
			inlineScriptWhenReady();
		}

	} );
});