From af856a135f74d9ecf9dd3be71974438f7298bf89 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 1 Aug 2014 12:58:54 +0200 Subject: [PATCH] Piggy [skip ci] --- app/assets/javascripts/firefly/piggybanks.js | 87 ++++++++++++++------ app/controllers/PiggybankController.php | 5 +- app/views/piggybanks/index.blade.php | 11 ++- 3 files changed, 73 insertions(+), 30 deletions(-) diff --git a/app/assets/javascripts/firefly/piggybanks.js b/app/assets/javascripts/firefly/piggybanks.js index 060ebf011b..598b6480bd 100644 --- a/app/assets/javascripts/firefly/piggybanks.js +++ b/app/assets/javascripts/firefly/piggybanks.js @@ -1,7 +1,8 @@ $(function () { - $('input[type="range"]').on('input', inputAmount); - $('input[type="range"]').on('change', changeAmount); + $('input[type="range"]').on('input', inputRange); + $('input[type="range"]').on('change', updateAmount); + $('input[type="number"]').on('input',inputNumber); }); @@ -10,40 +11,80 @@ $(function () { * @param e * @returns {boolean} */ -function inputAmount(e) { -// var target = $(e.target); -// var piggyBankId = target.attr('name').substring(6); -// var accountId = target.data('account'); -// var value = target.val(); -// -// // update all accounts and return false if we're going overboard. -// var updateResult = updateAccounts(accountId); -// if(!updateResult) { -// return false; -// } -// -// // new value for amount in piggy bank, formatted: -// valueFormatted = '€ ' + (Math.round(value * 100) / 100).toFixed(2); -// var valueId = 'piggy_' + piggyBankId + '_amount'; -// $('#' + valueId).text(valueFormatted); +function inputRange(e) { + var target = $(e.target); + var piggyBankId = target.attr('name').substring(6); + var accountId = target.data('account'); + var value = parseFloat(target.val()); + + var leftInAccount = leftInAccounts(accountId); + + if(leftInAccount <= 0) { + value = parseFloat($('#piggy_'+piggyBankId+'_amount').val()); + target.val(parseFloat(value)); + } + var valueId = 'piggy_' + piggyBankId + '_amount'; + $('#' + valueId).val(value.toFixed(2)); // // // new percentage for amount in piggy bank, formatted. -// var pctId = 'piggy_' + piggyBankId + '_pct'; -// percentage = Math.round((value / parseFloat(target.attr('max'))) * 100) + '%'; //Math.round((value / parseFloat(target.attr('total'))) * 100) + '%'; -// $('#' + pctId).text(percentage); + var pctId = 'piggy_' + piggyBankId + '_pct'; + percentage = Math.round((value / parseFloat(target.attr('max'))) * 100) + '%'; //Math.round((value / parseFloat(target.attr('total'))) * 100) + '%'; + $('#' + pctId).text(percentage); + + // update the bar accordingly. + return true; } -function changeAmount(e) { +function inputNumber(e) { + var target = $(e.target); + var amount = parseFloat(target.val()); + var piggyBankId = target.data('piggy'); + var accountId = target.data('account'); + var leftInAccount = leftInAccounts(accountId); + + if(leftInAccount <= 0) { + amount = parseFloat($('#piggy_'+piggyBankId+'_amount').val()); + } else { + // do something! + } + target.val(amount); + console.log('SERVER'); + $('input[name="piggy_'+piggyBankId+'"]').val(amount); + $.post('piggybanks/updateAmount/' + piggyBankId, {amount: amount}); +} + + +function updateAmount(e) { var target = $(e.target); var piggyBankId = target.attr('name').substring(6); var accountId = target.data('account'); var value = target.val(); - + console.log('SERVER'); $.post('piggybanks/updateAmount/' + piggyBankId, {amount: value}); +} + +function leftInAccounts(accountId) { + // get the total: + var total = parseFloat($('#account_'+accountId+'_total').data('raw')); + + // sub all piggy banks: + var inPiggies = 0; + $('input[type="range"]').each(function(i,v) { + var p = $(v); + if(parseInt(p.data('account')) == accountId) { + inPiggies += parseFloat(p.val()); + } + }); + var left = total - inPiggies; + console.log('LEFT: ' + left); + // return amount left: + return left; + + } function updateAccounts(id) { diff --git a/app/controllers/PiggybankController.php b/app/controllers/PiggybankController.php index 257e1eada6..860f46f41e 100644 --- a/app/controllers/PiggybankController.php +++ b/app/controllers/PiggybankController.php @@ -50,16 +50,13 @@ class PiggybankController extends BaseController // get accounts: foreach ($piggybanks as $piggyBank) { $account = $piggyBank->account; - $piggyBank->pct = round(($piggyBank->amount / $piggyBank->target) * 100, 2) . '%'; + $piggyBank->pct = round(($piggyBank->amount / $piggyBank->target) * 100, 0) . '%'; $id = $account->id; if (!isset($accounts[$id])) { $account->balance = $account->balance(); $account->left = $account->balance - $piggyBank->amount; } else { - echo $account->left.'-'; - echo '('.$piggyBank->amount.')'; $account->left -= $piggyBank->amount; - echo $account->left; } $accounts[$id] = $account; diff --git a/app/views/piggybanks/index.blade.php b/app/views/piggybanks/index.blade.php index f9df18721e..bf59731ce2 100644 --- a/app/views/piggybanks/index.blade.php +++ b/app/views/piggybanks/index.blade.php @@ -32,8 +32,8 @@ @foreach($accounts as $account) {{{$account->name}}} - {{mf($account->balance)}} - {{mf($account->left)}} + {{mf($account->balance)}} + {{mf($account->left)}} @endforeach @@ -48,7 +48,12 @@

{{{$piggybank->name}}} {{mf($piggybank->target)}}

- +
{{mf($piggybank->amount,false)}} +
+ + +
+
{{$piggybank->pct}}