MediaWiki:DonationForm mctest.js: Difference between revisions

Content deleted Content added
Created page with "→‎jshint strict:false: →‎* MediaWiki:DonationForm.js - loaded on all donation forms * TODO: lots of cleanup: var donationForm = {}; donationForm.loadedTime = Date.now(); donationForm.extraData = { 'vw' : window.innerWidth, 'vh' : window.innerHeight }; if ( navigator.brave !== undefined ) { // T283367 donationForm.extraData.brave = '1'; } // Don't offer recurring at all in these countries donationForm.noRecurringCountries = [ 'AR', 'CL', 'CO', 'MX',..."
 
add utility function donationForm.getLocale
 
(4 intermediate revisions by the same user not shown)
Line 673:
/* -- Moved from Template:2012FR/Form-section/Processing/Default -- */
/**
* Validate form, and ifprep itmost looksof goodthe submit to paymentsparameters
*
* @param {string} paymentMethod - method e.g. 'cc', 'paypal'
Line 683:
if ( donationForm.validate( skipAmountValidation ) ) {
 
var uri = new mw.Uri('https://payments.wikimedia.org/index.php/Special:GatewayFormChooser');
var params = {};
 
Line 712 ⟶ 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 751 ⟶ 750:
}
 
// Check for monthly convert. TODO: make it possible to test JS variants
donationForm.extraData.time = Math.round( (Date.now() - donationForm.loadedTime)/1000 );
if ( mw.util.getParamValue('monthlyconvert') ) {
 
mc.main( params, donationForm.finalStep );
// Tracking data
} else {
params.utm_medium = mw.util.getParamValue( 'utm_medium' );
donationForm.finalStep( params );
params.utm_campaign = mw.util.getParamValue( 'utm_campaign' );
params.utm_source = donationForm.buildUtmSource( params );
params.utm_key = donationForm.buildUtmKey( donationForm.extraData );
if ( document.referrer ) { // TODO: do we need this?
// Strip protocol to stop firewall complaining
params.referrer = document.referrer.replace(/https?:\/\//i, '');
}
 
uri.extend( params );
donationForm.goToPayments( uri );
 
} else {
Line 773 ⟶ 764:
};
 
/**
* Build final tracking parameters, and submit to payments
* @param {Object} params
*/
donationForm.finalStep = function( params ) {
 
var uri = new mw.Uri('https://payments.wikimedia.org/index.php/Special:GatewayFormChooser');
 
donationForm.extraData.time = Math.round( (Date.now() - donationForm.loadedTime)/1000 );
 
// Tracking data
params.utm_medium = mw.util.getParamValue( 'utm_medium' );
params.utm_campaign = mw.util.getParamValue( 'utm_campaign' );
params.utm_source = donationForm.buildUtmSource( params );
params.utm_key = donationForm.buildUtmKey( donationForm.extraData );
if ( document.referrer ) { // TODO: do we need this?
// Strip protocol to stop firewall complaining
params.referrer = document.referrer.replace(/https?:\/\//i, '');
}
 
uri.extend( params );
 
donationForm.goToPayments = function( uri ) {
if ( window.top !== window.self ) {
// In a frame, open payments in a new tab
Line 875 ⟶ 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 886 ⟶ 897:
value = value.replace(/[\$£€¥,.]/g, '');
value = value.replace(/:/, '.');
 
amount = parseFloat( value );
if ( isNaN( amount ) ) {
Line 1,037 ⟶ 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 ) {
inputElement.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,045 ⟶ 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,056 ⟶ 1,109:
donationForm.country = mw.util.getParamValue('country');
 
// Block typing symbols in inputOther field, otherwise Safari allows them and then chokes
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