MediaWiki:MonthlyConvert.js: Difference between revisions

From Donate
Jump to navigation Jump to search
Content deleted Content added
don't show mc immediately
No edit summary
Line 1: Line 1:
var mc = {};

// If one-time amount <= left amount, suggest right amount for monthly
// If changing these, please update spreadsheet
// https://docs.google.com/spreadsheets/d/1z36zi8EegPLAvR5FYAgwz8ywKZ50QNB82SpwpTdk-xQ/edit#gid=1258723967
mc.amounts = {
'USD' : [ // also CAD, AUD, NZD
[ 2.74, 0 ],
[ 9, 1.75 ],
[ 12, 2 ],
[ 15, 2.5 ],
[ 18, 3 ],
[ 21, 3.5 ],
[ 24, 4 ],
[ 27, 4.5 ],
[ 30, 5 ],
[ 33, 5.5 ],
[ 36, 6 ],
[ 39, 6.5 ],
[ 42, 7 ],
[ 45, 7.5 ],
[ 48, 8 ],
[ 51, 8.5 ],
[ 54, 9 ],
[ 57, 9.5 ],
[ 60, 10 ],
[ 63, 10.5 ],
[ 66, 11 ],
[ 69, 11.5 ],
[ 72, 12 ],
[ 75, 12.5 ],
[ 102, 17 ],
[ 250, 25 ],
[ 499, 50 ],
[ Infinity, 0 ]
],
'GBP' : [ // also EUR
[ 1.99, 0 ],
[ 9, 1.75 ],
[ 12, 2 ],
[ 15, 2.5 ],
[ 18, 3 ],
[ 21, 3.5 ],
[ 24, 4 ],
[ 27, 4.5 ],
[ 30, 5 ],
[ 33, 5.5 ],
[ 36, 6 ],
[ 39, 6.5 ],
[ 42, 7 ],
[ 45, 7.5 ],
[ 48, 8 ],
[ 51, 8.5 ],
[ 54, 9 ],
[ 57, 9.5 ],
[ 60, 10 ],
[ 63, 10.5 ],
[ 66, 11 ],
[ 69, 11.5 ],
[ 72, 12 ],
[ 75, 12.5 ],
[ 102, 17 ],
[ 250, 25 ],
[ 499, 50 ],
[ Infinity, 0 ]
],
'INR' : [
[ 300, 150 ],
[ 500, 175 ],
[ 1000, 250 ],
[ 1500, 350 ],
[ 3000, 500 ],
[ 5000, 750 ],
[ 10000, 1000 ],
[ 25000, 2500 ],
[ Infinity, 0 ]
],
'JPY' : [
[ 299, 0 ],
[ 900, 150 ],
[ 1200, 200 ],
[ 1500, 250 ],
[ 1800, 300 ],
[ 2100, 350 ],
[ 2400, 400 ],
[ 2700, 450 ],
[ 3000, 500 ],
[ 3300, 550 ],
[ 3600, 600 ],
[ 3900, 650 ],
[ 4200, 700 ],
[ 4500, 750 ],
[ 4800, 800 ],
[ 5100, 850 ],
[ 5400, 900 ],
[ 5700, 950 ],
[ 6000, 1000 ],
[ 6300, 1050 ],
[ 6600, 1100 ],
[ 6900, 1150 ],
[ 7200, 1200 ],
[ 7500, 1250 ],
[ 10800, 1800 ],
[ 18000, 3000 ],
[ 50000, 6000 ],
[ Infinity, 0 ]
],
'SEK' : [
[ 50, 25 ],
[ 100, 30 ],
[ 200, 50 ],
[ 300, 70 ],
[ 500, 90 ],
[ 1000, 110 ],
[ 2500, 250 ],
[ 5000, 500 ],
[ Infinity, 0 ]
]
};
mc.amounts.EUR = mc.amounts.GBP;
mc.amounts.CAD = mc.amounts.USD;
mc.amounts.AUD = mc.amounts.USD;
mc.amounts.NZD = mc.amounts.USD;

mc.getConvertAsk = function ( amount, currency ) {
var i,
amountsForCurrency = mc.amounts[ currency ],
numAmounts;
if ( !amountsForCurrency ) {
return 0;
}
numAmounts = amountsForCurrency.length;
for ( i = 0; i < numAmounts; i++ ) {
if ( amount <= amountsForCurrency[ i ][ 0 ] ) {
return amountsForCurrency[ i ][ 1 ];
}
}
return 0;
};

mc.formatAmount = function ( amount, currency, locale ) {
var formattedAmount;

try {
formattedAmount = amount.toLocaleString( locale, { currency: currency, style: 'currency' } );
} catch ( e ) {
// Assume a two decimal place currency for fallback
formattedAmount = currency + ' ' + amount.toFixed( 2 );
}
return formattedAmount;
};

mc.main = function( amount, currency ) {

var suggestedAmount = mc.getConvertAsk( amount, currency ),
locale = 'en-US';
if ( suggestedAmount === 0 ) {
return false;
}
$('.mc-currencycode').text( currency );

$('.mc-amt-initial').text( mc.formatAmount( amount, currency, locale) );
$('.mc-amt-suggest').text( mc.formatAmount( suggestedAmount, currency, locale ) );
$('.mc-modal-screen').show();

}

$(function() {
$(function() {
$( '.mc-diff-amount-link' ).on( 'click keypress', function ( e ) {
$( '.mc-diff-amount-link' ).on( 'click keypress', function ( e ) {
if ( e.which === 13 || e.type === 'click' ) {
if ( e.which === 13 || e.type === 'click' ) {
$( '.mc-choice' ).fadeOut( function () {
$( '.mc-choice' ).fadeOut( function () {
$( '.mc-edit-amount' ).fadeIn();
$( '.mc-edit-amount' ).fadeIn();
$( '.mc-back' ).fadeIn();
$( '.mc-back' ).fadeIn();
$( '.mc-other-amount-input' ).focus();
$( '.mc-other-amount-input' ).focus();
} );
} );
}
}
} );
} );
$( '.mc-back' ).on( 'click keypress', function ( e ) {
$( '.mc-back' ).on( 'click keypress', function ( e ) {
if ( e.which === 13 || e.type === 'click' ) {
if ( e.which === 13 || e.type === 'click' ) {
$( '.mc-back' ).fadeOut();
$( '.mc-back' ).fadeOut();
$( '.mc-edit-amount' ).fadeOut( function () {
$( '.mc-edit-amount' ).fadeOut( function () {
$( '.mc-choice' ).fadeIn();
$( '.mc-choice' ).fadeIn();
} );
} );
}
}
} );
} );
});
});

Revision as of 19:01, 20 August 2021

var mc = {};

// If one-time amount <= left amount, suggest right amount for monthly
// If changing these, please update spreadsheet
// https://docs.google.com/spreadsheets/d/1z36zi8EegPLAvR5FYAgwz8ywKZ50QNB82SpwpTdk-xQ/edit#gid=1258723967
mc.amounts = {
    'USD' : [ // also CAD, AUD, NZD
        [ 2.74, 0 ],
        [ 9, 1.75 ],
        [ 12, 2 ],
        [ 15, 2.5 ],
        [ 18, 3 ],
        [ 21, 3.5 ],
        [ 24, 4 ],
        [ 27, 4.5 ],
        [ 30, 5 ],
        [ 33, 5.5 ],
        [ 36, 6 ],
        [ 39, 6.5 ],
        [ 42, 7 ],
        [ 45, 7.5 ],
        [ 48, 8 ],
        [ 51, 8.5 ],
        [ 54, 9 ],
        [ 57, 9.5 ],
        [ 60, 10 ],
        [ 63, 10.5 ],
        [ 66, 11 ],
        [ 69, 11.5 ],
        [ 72, 12 ],
        [ 75, 12.5 ],
        [ 102, 17 ],
        [ 250, 25 ],
        [ 499, 50 ],
        [ Infinity, 0 ]
    ],
    'GBP' : [ // also EUR
        [ 1.99, 0 ],
        [ 9, 1.75 ],
        [ 12, 2 ],
        [ 15, 2.5 ],
        [ 18, 3 ],
        [ 21, 3.5 ],
        [ 24, 4 ],
        [ 27, 4.5 ],
        [ 30, 5 ],
        [ 33, 5.5 ],
        [ 36, 6 ],
        [ 39, 6.5 ],
        [ 42, 7 ],
        [ 45, 7.5 ],
        [ 48, 8 ],
        [ 51, 8.5 ],
        [ 54, 9 ],
        [ 57, 9.5 ],
        [ 60, 10 ],
        [ 63, 10.5 ],
        [ 66, 11 ],
        [ 69, 11.5 ],
        [ 72, 12 ],
        [ 75, 12.5 ],
        [ 102, 17 ],
        [ 250, 25 ],
        [ 499, 50 ],
        [ Infinity, 0 ]
    ],
    'INR' : [
        [ 300, 150 ],
        [ 500, 175 ],
        [ 1000, 250 ],
        [ 1500, 350 ],
        [ 3000, 500 ],
        [ 5000, 750 ],
        [ 10000, 1000 ],
        [ 25000, 2500 ],
        [ Infinity, 0 ]
    ],
    'JPY' : [
        [ 299, 0 ],
        [ 900, 150 ],
        [ 1200, 200 ],
        [ 1500, 250 ],
        [ 1800, 300 ],
        [ 2100, 350 ],
        [ 2400, 400 ],
        [ 2700, 450 ],
        [ 3000, 500 ],
        [ 3300, 550 ],
        [ 3600, 600 ],
        [ 3900, 650 ],
        [ 4200, 700 ],
        [ 4500, 750 ],
        [ 4800, 800 ],
        [ 5100, 850 ],
        [ 5400, 900 ],
        [ 5700, 950 ],
        [ 6000, 1000 ],
        [ 6300, 1050 ],
        [ 6600, 1100 ],
        [ 6900, 1150 ],
        [ 7200, 1200 ],
        [ 7500, 1250 ],
        [ 10800, 1800 ],
        [ 18000, 3000 ],
        [ 50000, 6000 ],
        [ Infinity, 0 ]
    ],
    'SEK' : [
        [ 50, 25 ],
        [ 100, 30 ],
        [ 200, 50 ],
        [ 300, 70 ],
        [ 500, 90 ],
        [ 1000, 110 ],
        [ 2500, 250 ],
        [ 5000, 500 ],
        [ Infinity, 0 ]
    ]
};
mc.amounts.EUR = mc.amounts.GBP;
mc.amounts.CAD = mc.amounts.USD;
mc.amounts.AUD = mc.amounts.USD;
mc.amounts.NZD = mc.amounts.USD;

mc.getConvertAsk = function ( amount, currency ) {
    var i,
        amountsForCurrency = mc.amounts[ currency ],
        numAmounts;
    if ( !amountsForCurrency ) {
        return 0;
    }
    numAmounts = amountsForCurrency.length;
    for ( i = 0; i < numAmounts; i++ ) {
        if ( amount <= amountsForCurrency[ i ][ 0 ] ) {
            return amountsForCurrency[ i ][ 1 ];
        }
    }
    return 0;
};

mc.formatAmount = function ( amount, currency, locale ) {
    var formattedAmount;

    try {
        formattedAmount = amount.toLocaleString( locale, { currency: currency, style: 'currency' } );
    } catch ( e ) {
        // Assume a two decimal place currency for fallback
        formattedAmount = currency + ' ' + amount.toFixed( 2 );
    }
    return formattedAmount;
};

mc.main = function( amount, currency ) {

    var suggestedAmount = mc.getConvertAsk( amount, currency ),
        locale = 'en-US';
    if ( suggestedAmount === 0 ) {
        return false;
    }
    
    $('.mc-currencycode').text( currency );

    $('.mc-amt-initial').text( mc.formatAmount( amount, currency, locale) );
    $('.mc-amt-suggest').text( mc.formatAmount( suggestedAmount, currency, locale ) );
    
    $('.mc-modal-screen').show();

}

$(function() {
    
    $( '.mc-diff-amount-link' ).on( 'click keypress', function ( e ) {
        if ( e.which === 13 || e.type === 'click' ) {
            $( '.mc-choice' ).fadeOut( function () {
                $( '.mc-edit-amount' ).fadeIn();
                $( '.mc-back' ).fadeIn();
                $( '.mc-other-amount-input' ).focus();
            } );
        }
    } );
    $( '.mc-back' ).on( 'click keypress', function ( e ) {
        if ( e.which === 13 || e.type === 'click' ) {
            $( '.mc-back' ).fadeOut();
            $( '.mc-edit-amount' ).fadeOut( function () {
                $( '.mc-choice' ).fadeIn();
            } );
        }
    } );
});