MediaWiki:DonationForm mctest.js: Difference between revisions

Content deleted Content added
split finalStep from redirectPayments, so we can call it back from monthlyconvert if needed
add utility function donationForm.getLocale
 
(3 intermediate revisions by the same user not shown)
Line 711:
params.country = donationForm.country;
params.uselang = mw.config.get('wgPageContentLanguage'); // see T281285 for why not wgUserLanguage
 
if ( params.uselang === 'pt' && params.country === 'BR' ) {
params.uselang = 'pt-br';
Line 750:
}
 
// Check for monthly convert. TODO: make it possible to test JS variants
donationForm.finalStep( params );
if ( mw.util.getParamValue('monthlyconvert') ) {
mc.main( params, donationForm.finalStep );
} else {
donationForm.finalStep( params );
}
 
} else {
Line 787 ⟶ 792:
window.location.href = uri.toString();
}
};
 
/**
Line 881 ⟶ 886:
* Does some awful regex stuff to rm symbols and turn the string into a number
* Remember some locales flip . & , for decimal point/thousands separator
*
* @param {string} value Value of "Other" field
* @return {float} Float with amount, or 0 if NaN
Line 892 ⟶ 897:
value = value.replace(/[\$£€¥,.]/g, '');
value = value.replace(/:/, '.');
 
amount = parseFloat( value );
if ( isNaN( amount ) ) {
Line 1,043 ⟶ 1,048:
});
};
 
/**
* Block typing letters and symbols in given input. Used for Other amount inputs
*
* If we don't do this, Safari allows typing them and then chokes on submit
// * https://phabricator.wikimedia.org/T118741, https://phabricator.wikimedia.org/T173431
*
* @param {Element} inputElement The element to block typing on
*/
donationForm.otherInputControl = function( inputElement ) {
if ( inputElement ) {
amountOtherInputinputElement.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;
}
};
}
};
 
/**
* Make language and country into a standard javascript Intl locale identifier
*
* Currently only used in monthlyconvert, but this could be useful
*
* @param {string} language
* @param {string} country
* @return {string} locale identifier
*/
donationForm.getLocale = function( language, country ) {
if ( language === 'en-gb' ) {
language = 'en';
}
if ( language === 'es-419' ) {
language = 'es';
}
if ( language === 'pt-br' ) {
language = 'pt';
}
return language + '-' + country;
};
 
 
/* End form functions */
Line 1,051 ⟶ 1,104:
 
var form = document.forms['donateForm'];
// Load monthly convert JS if needed
// TODO: allow loading different JS variants, either here or defined in the Template
if ( mw.util.getParamValue('monthlyconvert') ) {
mw.loader.load( '/w/index.php?title=MediaWiki:MonthlyConvert.js&action=raw&ctype=text/javascript' );
}
 
// These get used in quite a few places
Line 1,062 ⟶ 1,109:
donationForm.country = mw.util.getParamValue('country');
 
// Block typing symbols in inputOther field, otherwise Safari allows them and then chokes
var amountOtherInput =donationForm.otherInputControl( document.getElementById('input_amount_other_box') );
// 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;
}
};
}
 
// Validate amount and update fee when selected/entered