MediaWiki:Common.js: Difference between revisions

From Donate
Jump to navigation Jump to search
Content deleted Content added
add utm parameter handling (from form)
showing country confirm by default now
 
(114 intermediate revisions by 3 users 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 parseParams( url ) {
$(function() {
// Helper function to get querystring parameters as a JS object
// Located here as MediaWiki messes up the ampersand when included inline using rawHTML
// TODO: Replace with mw.Uri or mw.util.getParamValue() everywhere, and remove
var ret={};
if ( url.indexOf( '?' ) === -1 ) {
return ret;
}
var s = url.split( '?' ).slice( 1 ).join();
var t = s.split( '&' );
for ( var i = 0; i < t.length; ++i ) {
var z = t[ i ].split( '=' );
z.push( null );
ret[ z[ 0 ] ] = z[ 1 ];
}
return ret;
}


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


// If Jimmy tweets a bare link to https://donate.wikimedia.org/w/index.php?title=Special:LandingPage
function getQuerystring( key ) {
// we are gonna have a bad time as geolocation doesn't happen. Do a redirect to send them round again
// TODO: Replace with mw.util.getParamValue() everywhere, and remove
try {
key = key.replace( /[\[]/, '\\\[' ).replace( /[\]]/, '\\\]' );
var regex = new RegExp( '[\\?&]' + key + '=([a-zA-Z0-9\.\_\-]*)' );
var uri = new mw.Uri( window.location.href );
if ( uri.query.title === 'Special:LandingPage' && uri.query.country === undefined && uri.query.retried === undefined ) {
var qs = regex.exec( window.location.search );
console.log( 'redirecting to pick up country parameter' );
return qs == null ? '' : qs[1];
uri.path = '';
}
uri.query.retried = 1; // only do this once
delete uri.query.title;
window.location.href = uri.toString();
}
} catch (error) {
console.warn('Error parsing URL. Possibly due to unreplaced % signs?');
}


// Disable logo link
function checkPaymentOutages() {
$('#p-logo a').attr( { href: '#', title: '' } );
/* Check against the scheduled payment method outages
* in global variable "outages" (loaded cross-wiki from meta)
* and hide the relevant button for any which are ongoing.
*/


// Code for donation forms - https://donate.wikimedia.org/wiki/MediaWiki:DonationForm.js
// var outages = outages || []; // don't die if it hasn't loaded...
if ( document.forms['donateForm'] ) {
var now = new Date();
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
for (var i = outages.length - 1; i >= 0; i--) {
// TODO: allow loading different JS variants, either here or defined in the Template
if ( now > outages[i]['start'] ) {
mw.loader.load( '/w/index.php?title=MediaWiki:MonthlyConvert.js&action=raw&ctype=text/javascript' );
if ( now < outages[i]['end'] ) {
}


// Allow manual country selection and reload
if ( (typeof outages[i]['country'] === 'undefined') || (outages[i]['country'] == country) ) {
// Run this only when needed
$('.paymentmethod-' + outages[i]['method']).hide();
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) {
function adjustHPC() {
if ( countrySelect.value ) {
/* Adjust amounts based on highest previous contribution (hpc)
mw.loader.using( ['mediawiki.Uri'] ).done( function() {
or most recent contribution (mrc) parameter. Used for emails.
var uri = new mw.Uri( document.location.href );
TODO: split data out? */
uri.query.country = countrySelect.value;
document.location = uri.toString();
});
}
});
};


if ( document.getElementById('country-change') ) {
// Look for 'hpc' parameter, then 'mrc'. If neither, then bail out.
document.getElementById('country-change').addEventListener( 'click', function(e) {
var hpc = parseFloat( mw.util.getParamValue('hpc') );
document.getElementById('country-change-select').style.display = 'block';
if( isNaN(hpc) ) {
enableCountrySelect();
hpc = parseFloat( mw.util.getParamValue('mrc') );
});
if( isNaN(hpc) ) {
}
return;
}
}


if ( document.getElementById('geolocate-error') ) {
var hpcSet = mw.util.getParamValue('hpcSet');
enableCountrySelect();
}


// Code for Thank You pages - https://donate.wikimedia.org/wiki/MediaWiki:ThankYouPage.js
var currency = $("input[name='currency_code']").val();
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
var radioAmountsData = {
// TODO: have a better way to trigger this
'USD' : {
var supportPages = [ 'Ways to Give', 'Problems donating', 'Cancel or change recurring giving',
'default' : [ // Control - low bottom ask
'Matching Gifts', 'Support Page', 'FAQ', 'Tax deductibility',
[ 0, [ 5, 10, 20, 25, 35, 50, 100 ] ],
'US State Solicitation Disclosures', 'Draft:FAQ' ];
[ 10, [ 10, 20, 25, 35, 50, 100, 150 ] ],
if ( supportPages.indexOf( mw.config.get( 'wgTitle' ).split('/')[0] ) !== -1 ) {
[ 20, [ 20, 25, 35, 50, 75, 100, 250 ] ],
mw.loader.load( '/w/index.php?title=MediaWiki:SupportPage.js&action=raw&ctype=text/javascript' );
[ 35, [ 20, 35, 50, 100, 150, 200, 250 ] ],
}
[ 50, [ 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 ] ]
],
'v2' : [ // High ask, more groups on low end
[ 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 ] ],
[ 50, [ 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 ] ]
],
'v3' : [ // Med ask for lowest # in string
[ 0, [ 5, 10, 20, 25, 35, 50, 100 ] ],
[ 10, [ 15, 20, 25, 35, 50, 100, 150 ] ],
[ 20, [ 25, 35, 50, 75, 100, 150, 250 ] ],
[ 35, [ 25, 40, 50, 100, 150, 200, 250 ] ],
[ 50, [ 35, 50, 75, 125, 200, 250, 300 ] ],
[ 75, [ 35, 50, 75, 100, 200, 300, 500 ] ],
[ 100, [ 50, 75, 100, 150, 250, 500, 1000 ] ],
[ 150, [ 100, 150, 200, 250, 350, 500, 1000 ] ],
[ 200, [ 150, 200, 250, 300, 400, 500, 1000 ] ],
[ 500, [ 250, 500, 750, 1000, 1500, 2000, 2500 ] ],
[ 1000, [ 1000, 2000, 2500, 3000, 3500, 4000, 5000 ] ]
]
},
'EUR' : [
[ 0, [ 3, 5, 10, 20, 30, 50, 100] ],
[ 4, [ 5, 10, 15, 20, 30, 50, 100] ],
[ 27, [ 5, 10, 20, 30, 50, 100, 150] ],
[ 136, [ 5, 10, 20, 50, 100, 150, 200] ]
],
'JPY' : [
[ 0, [ 700, 1000, 2000, 3000, 4000, 5000, 10000 ] ],
[ 5, [ 1000, 1500, 2500, 4000, 5000, 10000, 15000 ] ],
[ 10, [ 1500, 2000, 3000, 4000, 5000, 10000, 15000 ] ],
[ 15, [ 2000, 3000, 4000, 5000, 10000, 12500, 25000 ] ],
[ 20, [ 3000, 4000, 5000, 10000, 12500, 15000, 25000 ] ],
[ 25, [ 4000, 5000, 7500, 10000, 12500, 15000, 25000 ] ],
[ 35, [ 5000, 7500, 10000, 12500, 25000, 30000, 35000 ] ],
[ 75, [ 5000, 7500, 10000, 12500, 25000, 35000, 50000 ] ],
[ 100, [ 5000, 7500, 12500, 20000, 30000, 50000, 100000 ] ],
[ 500, [ 10000, 25000, 50000, 100000, 125000, 150000, 250000 ] ]
],
'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; // TODO: change this
radioAmountsData.NZD = radioAmountsData.USD;


// Load some tools for editors - https://donate.wikimedia.org/wiki/MediaWiki:EditTemplates.js
var appealAmountsData = {
if ( mw.config.get( 'wgUserName' ) !== null ) {
'USD' : [
mw.loader.load( '/w/index.php?title=MediaWiki:EditTemplates.js&action=raw&ctype=text/javascript' );
[ 0, [ 5, 10, 20 ] ],
}
[ 10, [ 10, 20, 50 ] ],
[ 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 ] ]
],
'EUR' : [
[ 0, [ 3, 5, 10 ] ],
[ 4, [ 5, 10, 20 ] ],
[ 7, [ 10, 20, 50 ] ],
[ 14, [ 15, 20, 50 ] ],
[ 20, [ 20, 50, 100 ] ],
[ 40, [ 30, 50, 100 ] ],
[ 68, [ 50, 100, 150 ] ]
],
'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;


/**
var formats = {
* @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
'USD' : '$\t',
* @rev 6
'AUD' : '$\t',
*/
'CAD' : '$\t',
var extraCSS = mw.util.getParamValue( 'withCSS' ),
'EUR' : '€\t',
extraJS = mw.util.getParamValue( 'withJS' );
'GBP' : '£\t',
'NZD' : '$\t',
'JPY' : '¥\t',
'SEK' : '\t kr'
};
var format = formats[currency] || '\t';


if ( extraCSS ) {
// Radio button amounts
if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
var radioAmounts = pickAmountArray( radioAmountsData, currency, hpc, hpcSet );
mw.loader.load( '/w/index.php?title=' + extraCSS + '&action=raw&ctype=text/css', 'text/css' );
if ( radioAmounts.length ) {
} else {
// Change buttons
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
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 ( extraJS ) {
// Appeal amounts
if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
var appealAmounts = pickAmountArray( appealAmountsData, currency, hpc, hpcSet );
mw.loader.load( '/w/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript' );
if ( appealAmounts.length ) {
} else {
// Build string
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
var appealAmountString = '';
}
for( var k = 0; k < appealAmounts.length; k++ ) {
}
appealAmountString += format.replace('\t', appealAmounts[k]) + ', ';
}
appealAmountString = appealAmountString.trim();


/* HACK to run certain inline scripts when document ready (e.g. so jquery is avaliable)
$('.consider-amounts').html(appealAmountString);
TODO: Make suitable for multiple scripts. Or figure out a better way to do this.
}
*/

if ( typeof inlineScriptWhenReady !== 'undefined') {
}
inlineScriptWhenReady();

}
function pickAmountArray( data, currency, hpc, hpcSet ) {
/**
* 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;

if ( !(currency in data) ) {
return [];
}

if ( $.isArray(data[currency]) ) {
// No variant sets
set = data[currency];
} else {
// We need to go deeper. Check the variants.
if ( hpcSet in data[currency] ) {
set = data[currency][hpcSet];
} else {
set = data[currency]['default'];
}
}

// Find correct amount array for this hpc
for (var i = 0; i < set.length; i++) {
if ( set[i][0] > hpc ) {
break;
}
amounts = set[i][1];
}

return amounts;

}

function adjustOtherVal() {
// prefill other amount box if param provided
var otherVal = mw.util.getParamValue( 'otherVal' );
if( isNaN( parseFloat( otherVal ) ) ) {
return;
}
otherVal = parseFloat( otherVal );
$("#input_amount_other").attr( 'checked', true );
$("#input_amount_other_box").val( otherVal );
}


$(document).ready(function() {

// Load list of payment outages from meta, and hide any which are current
$.getScript('//meta.wikimedia.org/w/index.php?' + $.param( { title:'MediaWiki:FR2013/Resources/PaymentOutages.js', action:'raw', ctype:'text/javascript' } ),
function() {
setTimeout(checkPaymentOutages(), 250); // allow time for script to have _executed_ (not just loaded)
});

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

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' );
document.paypalcontribution.referrer.value = document.referrer;

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

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

adjustHPC();
adjustOtherVal();


} );
});
});

Latest revision as of 10:25, 21 May 2024

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

$(function() {

	mw.loader.using( ['mediawiki.util', 'mediawiki.Uri'] ).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 uri = new mw.Uri( window.location.href );
			if ( uri.query.title === 'Special:LandingPage' && uri.query.country === undefined && uri.query.retried === undefined ) {
				console.log( 'redirecting to pick up country parameter' );
				uri.path = '';
				uri.query.retried = 1; // only do this once
				delete uri.query.title;
				window.location.href = uri.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 ) {
					mw.loader.using( ['mediawiki.Uri'] ).done( function() {
						var uri = new mw.Uri( document.location.href );
						uri.query.country = countrySelect.value;
						document.location = uri.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();
		}

		// 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' ];
		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 - 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();
		}

	} );
});