Piggy [skip ci]

This commit is contained in:
James Cole 2014-08-01 12:58:54 +02:00
parent bae750e0ab
commit af856a135f
3 changed files with 73 additions and 30 deletions

View File

@ -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) {

View File

@ -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;

View File

@ -32,8 +32,8 @@
@foreach($accounts as $account)
<tr>
<td>{{{$account->name}}}</td>
<td>{{mf($account->balance)}}</td>
<td id="account_{{$account->id}}_left">{{mf($account->left)}}</td>
<td id="account_{{$account->id}}_total" data-raw="{{$account->balance}}">{{mf($account->balance)}}</td>
<td id="account_{{$account->id}}_left" data-raw="{{$account->left}}">{{mf($account->left)}}</td>
</tr>
@endforeach
</table>
@ -48,7 +48,12 @@
<h4>{{{$piggybank->name}}} <small>{{mf($piggybank->target)}}</small></h4>
<table class="table table-bordered">
<tr>
<td style="width:10%;"><span id="piggy_{{$piggybank->id}}_amount">{{mf($piggybank->amount,false)}}</span></td>
<td style="width:15%;">
<div class="input-group">
<span class="input-group-addon">&euro;</span>
<input class="form-control" type="number" data-piggy="{{$piggybank->id}}" data-account="{{$piggybank->account_id}}" step="any" min="0" max="{{$piggybank->target}}" id="piggy_{{$piggybank->id}}_amount" value="{{$piggybank->amount}}" />
</div>
</td>
<td><input type="range" data-account="{{$piggybank->account_id}}" name="piggy_{{$piggybank->id}}" min="0" max="{{$piggybank->target}}" step="any" value="{{$piggybank->amount}}" /></td>
<td style="width: 10%;"><span id="piggy_{{$piggybank->id}}_pct">{{$piggybank->pct}}</span></td>
</tr>