MediaWiki:Resources/2015/landingpage.js: Difference between revisions

From Donate
Jump to navigation Jump to search
Content deleted Content added
No edit summary
refactor adjustHPC - avoid issues with object ordering, dedupe code
Line 28: Line 28:
}
}


function adjustHPC() {
function adjustHPC(hpc, currency) {
/* Adjust amounts based on highest previous contribution (hpc)
or most recent contribution (mrc) parameter. Used for emails. */


var radioAmountsData = {
// Look for 'hpc' parameter, then 'mrc'. If neither, then bail out.
'USD' : [
var hpc = parseFloat( mw.util.getParamValue('hpc') );
[ 0, [ 5, 10, 20, 25, 35, 50, 100 ] ],
if( isNaN(hpc) ) {
[ 10, [ 10, 20, 25, 35, 50, 100, 150 ] ],
hpc = parseFloat( mw.util.getParamValue('mrc') );
[ 20, [ 20, 25, 35, 50, 75, 100, 250 ] ],
if( isNaN(hpc) ) {
return;
[ 35, [ 20, 35, 50, 100, 150, 200, 250 ] ],
[ 50, [ 20, 35, 50, 100, 150, 200, 250 ] ],
}
[ 75, [ 25, 50, 75, 100, 200, 300, 500 ] ],
}
[ 100, [ 25, 50, 100, 150, 250, 500, 1000 ] ],

[ 150, [ 50, 100, 150, 200, 350, 500, 1000 ] ],
var hpcSet = mw.util.getParamValue('hpcset');
[ 200, [ 50, 100, 200, 300, 400, 500, 1000 ] ]

],
var currency = $("input[name='currency_code']").val();
'EUR' : [

[ 0, [ 3, 5, 10, 20, 30, 50, 100] ],
/* Radio buttons */
[ 4, [ 5, 10, 15, 20, 30, 50, 100] ],
var asks = {
[ 27, [ 5, 10, 20, 30, 50, 100, 150] ],
'USD' : {
0 : [5, 10, 20, 25, 35, 50, 100],
[ 136, [ 5, 10, 20, 50, 100, 150, 200] ]
10 : [10, 20, 25, 35, 50, 100, 150],
],
20 : [20, 25, 35, 50, 75, 100, 250],
'JPY' : [
35 : [20, 35, 50, 100, 150, 200, 250],
[ 0, [ 300, 500, 1000, 2000, 3000, 5000, 10000 ] ],
50 : [20, 35, 50, 100, 150, 200, 250],
[ 3, [ 500, 1000, 1500, 2000, 3000, 5000, 10000 ] ],
75 : [25, 50, 75, 100, 200, 300, 500],
[ 5, [ 1000, 1500, 2000, 3000, 5000, 10000, 15000 ] ]
],
100 : [25, 50, 100, 150, 250, 500, 1000],
150 : [50, 100, 150, 200, 350, 500, 1000],
'SEK' : [
200 : [50, 100, 200, 300, 400, 500, 1000]
[ 0, [ 20, 50, 100, 200, 300, 500, 1000 ] ],
[ 3, [ 30, 50, 100, 200, 300, 500, 1000 ] ],
},
[ 5, [ 50, 100, 150, 200, 300, 500, 1000 ] ],
'EUR' : {
0 : [3, 5, 10, 20, 30, 50, 100],
[ 23, [ 50, 100, 200, 300, 500, 750, 1000 ] ]
]
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 : [300, 500, 1000, 2000, 3000, 5000, 10000],
3 : [500, 1000, 1500, 2000, 3000, 5000, 10000],
5 : [1000, 1500, 2000, 3000, 5000, 10000, 15000]
},
'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]
}
};
};
asks.AUD = asks.USD;
radioAmountsData.AUD = radioAmountsData.USD;
asks.CAD = asks.USD;
radioAmountsData.CAD = radioAmountsData.USD;
asks.GBP = asks.USD;
radioAmountsData.GBP = radioAmountsData.USD;
asks.NZD = asks.USD;
radioAmountsData.NZD = radioAmountsData.USD;


var appealAmountsData = {
/* Amounts in appeal */
var appealasks = {
'USD' : [
'USD' : {
[ 0, [ 5, 10, 20 ] ],
0 : [5, 10, 20],
[ 10, [ 10, 20, 50 ] ],
10 : [10, 20, 50],
[ 20, [ 20, 30, 50 ] ],
20 : [20, 30, 50],
[ 35, [ 20, 30, 50 ] ],
35 : [20, 30, 50],
[ 50, [ 20, 50, 100 ] ],
50 : [20, 50, 100],
[ 75, [ 50, 75, 100 ] ],
75 : [50, 75, 100],
[ 100, [ 75, 100, 150 ] ],
100 : [75, 100, 150],
[ 150, [ 75, 100, 200 ] ],
150 : [75, 100, 200],
[ 200, [ 100, 200, 300 ] ]
200 : [100, 200, 300]
],
},
'EUR' : [
'EUR' : {
[ 0, [ 3, 5, 10 ] ],
0: [3, 5, 10],
[ 4, [ 5, 10, 20 ] ],
4: [5, 10, 20],
[ 7, [ 10, 20, 50 ] ],
7: [10, 20, 50],
[ 14, [ 15, 20, 50 ] ],
14: [15, 20, 50],
[ 20, [ 20, 50, 100 ] ],
20: [20, 50, 100],
[ 40, [ 30, 50, 100 ] ],
40: [30, 50, 100],
[ 68, [ 50, 100, 150 ] ]
68: [50, 100, 150]
],
},
'JPY' : [
[ 0, [ 300, 500, 1000 ] ],
'JPY' : {
0: [300, 500, 1000],
[ 3, [ 500, 1000, 1500 ] ],
3: [500, 1000, 1500],
[ 5, [ 1000, 1500, 2000 ] ],
5: [1000, 1500, 2000],
[ 10, [ 1500, 2000, 5000 ] ],
10: [1500, 2000, 5000],
[ 20, [ 2000, 3000, 5000 ] ],
20: [2000, 3000, 5000],
[ 50, [ 2000, 5000, 10000 ] ],
50: [2000, 5000, 10000],
[ 100, [ 5000, 10000, 15000 ] ]
100: [5000, 10000, 15000]
],
},
'SEK' : [
'SEK' : {
[ 0, [ 20, 50, 100 ] ],
0: [20, 50, 100],
[ 3, [ 30, 50, 100 ] ],
3: [30, 50, 100],
[ 5, [ 50, 100, 150 ] ],
5: [50, 100, 150],
[ 15, [ 100, 150, 200 ] ],
15: [100, 150, 200],
[ 23, [ 100, 200, 300 ] ],
23: [100, 200, 300],
[ 38, [ 100, 200, 500 ] ],
38: [100, 200, 500],
[ 75, [ 100, 500, 750 ] ],
75: [100, 500, 750],
[ 112, [ 100, 500, 1000 ] ]
112: [100, 500, 1000]
]
}
};
};
appealasks.AUD = appealasks.USD;
appealAmountsData.AUD = appealAmountsData.USD;
appealasks.CAD = appealasks.USD;
appealAmountsData.CAD = appealAmountsData.USD;
appealasks.GBP = appealasks.USD;
appealAmountsData.GBP = appealAmountsData.USD;
appealasks.NZD = appealasks.USD;
appealAmountsData.NZD = appealAmountsData.USD;


var symbols = {
var formats = {
'USD' : '$\t',
'USD' : '$\t',
'AUD' : '$\t',
'AUD' : '$\t',
Line 138: Line 121:
'SEK' : '\t kr'
'SEK' : '\t kr'
};
};
var format = formats[currency] || '\t';


var ask = null;
// Radio button amounts
var radioAmounts = pickAmountArray( radioAmountsData, currency, hpc );
var symbol = '';
if ( radioAmounts.length ) {
// Change buttons
for (var j = 0; j < radioAmounts.length; j++) {
var $radio = $("#input_amount_" + j);
var $label = $("label[for='input_amount_" + j + "']");
if( $radio.length && $label.length ) {
$radio.val( radioAmounts[j] );
$label.text( format.replace('\t', radioAmounts[j]) );
}
}
}

// Appeal amounts
var appealAmounts = pickAmountArray( appealAmountsData, currency, hpc );
if ( appealAmounts.length ) {
// Build string
var appealAmountString = '';
for( var k = 0; k < appealAmounts.length; k++ ) {
appealAmountString += format.replace('\t', appealAmounts[k]) + ', ';
}
appealAmountString = appealAmountString.trim();


$('.consider-amounts').html(appealAmountString);
if( currency in symbols ){
symbol = symbols[currency];
}
}


}
if( currency in asks ){


function pickAmountArray( data, currency, hpc ) {
for( var prev in asks[currency] ){
/**
// this assumes that JS returns keys in sorted order, most do
if( prev > hpc ){
* Choose the amounts for radio buttons / appeal based on hpc
break;
* @param {Object} data
}
* @param {String} currency
* @param {Number} hpc
ask = asks[currency][prev];
* @return {Array} Array of amounts (as numbers)
}
*/
if( ask != null ){

for( var j = 0; j < ask.length; j++ ){
if ( !(currency in data) ) {
var radio = $("#input_amount_" + j);
return [];
var label = $("label[for='input_amount_" + j + "']");
if( radio != null ){
if( label != null ){
// I know an && could be used here, but they are getting escaped
radio.val( ask[j] );
label.text( symbol.replace('\t', ask[j]) );
}
}
}
}
}
}


if( currency in appealasks ){
var set = data[currency]; // Want to expand this to allow multiple sets
var amounts = [];


for( var prev in appealasks[currency] ){
for (var i = 0; i < set.length; i++) {
if ( set[i][0] > hpc ) {
// this assumes that JS returns keys in sorted order, most do
if( prev > hpc ){
break;
break;
}
appealask = appealasks[currency][prev];
}
}
var considerAmountString = "";
amounts = set[i][1];
for( var k = 0; k < appealask.length; k++ ) {
considerAmountString += symbol.replace('\t', appealask[k]) + ', ';
}
considerAmountString = considerAmountString.trim();
$('.consider-amounts').html(considerAmountString);
}
}

return amounts;

}
}



Revision as of 14:48, 6 July 2015

function getQuerystring( key ) {
  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];
}

function checkPaymentOutages() {
    /*  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.
     */

    // var outages = outages || []; // don't die if it hasn't loaded...
    var now = new Date();

    for (var i = outages.length - 1; i >= 0; i--) {
        if ( now > outages[i]['start'] ) {
            if ( now < outages[i]['end'] ) {

                if ( (typeof outages[i]['country'] === 'undefined') || (outages[i]['country'] == country) ) {
                    $('.paymentmethod-' + outages[i]['method']).hide();
                }

            }
        }
    }
}

function adjustHPC(hpc, currency) {

    var radioAmountsData = {
        'USD' : [
            [   0, [  5,  10,  20,  25,  35,  50,  100 ] ],
            [  10, [ 10,  20,  25,  35,  50, 100,  150 ] ],
            [  20, [ 20,  25,  35,  50,  75, 100,  250 ] ],
            [  35, [ 20,  35,  50, 100, 150, 200,  250 ] ],
            [  50, [ 20,  35,  50, 100, 150, 200,  250 ] ],
            [  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, [ 50, 100, 200, 300, 400, 500, 1000 ] ]
        ],
        '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, [  300,  500, 1000, 2000, 3000,  5000, 10000 ] ],
            [   3, [  500, 1000, 1500, 2000, 3000,  5000, 10000 ] ],
            [   5, [ 1000, 1500, 2000, 3000, 5000, 10000, 15000 ] ]
        ],
        '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;

    var appealAmountsData = {
        'USD' : [
            [   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 = {
        'USD' : '$\t',
        'AUD' : '$\t',
        'CAD' : '$\t',
        'EUR' : '€\t',
        'GBP' : '£\t',
        'NZD' : '$\t',
        'JPY' : '¥\t',
        'SEK' : '\t kr'
    };
    var format = formats[currency] || '\t';

    // Radio button amounts
    var radioAmounts = pickAmountArray( radioAmountsData, currency, hpc );
    if ( radioAmounts.length ) {
        // Change buttons
        for (var j = 0; j < radioAmounts.length; j++) {
            var $radio = $("#input_amount_" + j);
            var $label = $("label[for='input_amount_" + j + "']");
            if( $radio.length && $label.length ) {
                $radio.val( radioAmounts[j] );
                $label.text( format.replace('\t', radioAmounts[j]) );
            }
        }
    }

    // Appeal amounts
    var appealAmounts = pickAmountArray( appealAmountsData, currency, hpc );
    if ( appealAmounts.length ) {
        // Build string
        var appealAmountString = '';
        for( var k = 0; k < appealAmounts.length; k++ ) {
            appealAmountString += format.replace('\t', appealAmounts[k]) + ', ';
        }
        appealAmountString = appealAmountString.trim();

        $('.consider-amounts').html(appealAmountString);
    }

}

function pickAmountArray( data, currency, hpc ) {
    /**
     * Choose the amounts for radio buttons / appeal based on hpc
     * @param {Object} data
     * @param {String} currency
     * @param {Number} hpc
     * @return {Array} Array of amounts (as numbers)
     */

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

    var set = data[currency]; // Want to expand this to allow multiple sets
    var amounts = [];

    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;
    });

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

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

    adjustHPC();
    adjustOtherVal();

});