firefly-iii/public/js/budgets.js

158 lines
4.9 KiB
JavaScript
Raw Normal View History

2015-05-24 11:22:41 -05:00
/* globals $, token, budgetID, repetitionID */
2015-02-22 02:46:21 -06:00
$(function () {
2015-05-24 11:22:41 -05:00
"use strict";
2015-02-22 02:46:21 -06:00
updateRanges();
//$('input[type="range"]').change(updateSingleRange);
$('input[type="range"]').on('input', updateSingleRange);
//$('input[type="number"]').on('change', updateSingleRange);
$('input[type="number"]').on('input', updateSingleRange);
$('.updateIncome').on('click', updateIncome);
if (typeof budgetID !== 'undefined' && typeof repetitionID === 'undefined') {
2015-05-19 23:50:24 -05:00
googleColumnChart('chart/budget/' + budgetID, 'budgetOverview');
2015-02-22 02:46:21 -06:00
}
if (typeof budgetID !== 'undefined' && typeof repetitionID !== 'undefined') {
googleLineChart('chart/budget/' + budgetID + '/' + repetitionID, 'budgetOverview');
}
});
function updateSingleRange(e) {
2015-05-24 11:22:41 -05:00
"use strict";
2015-02-22 02:46:21 -06:00
// get some values:
var input = $(e.target);
var id = input.data('id');
2015-02-22 02:46:21 -06:00
var value = parseInt(input.val());
var spent = parseFloat($('#spent-' + id).data('value'));
// update small display:
if (value > 0) {
// show the input:
$('#budget-info-' + id + ' span').show();
$('#budget-info-' + id + ' input').show();
// update the text:
$('#budget-description-' + id).text('Budgeted: ');
} else {
// hide the input:
$('#budget-info-' + id + ' span').hide();
$('#budget-info-' + id + ' input').hide();
// update the text:
$('#budget-description-' + id).html('<em>No budget</em>');
}
// update the range display text:
$('#budget-range-display-' + id).text('\u20AC ' + value.toFixed(2));
// send a post to Firefly to update the amount:
$.post('budgets/amount/' + id, {amount: value, _token: token}).success(function (data) {
// update the link if relevant:
$('#budget-link-' + id).attr('href', 'budgets/show/' + id + '/' + data.repetition);
});
2015-05-24 13:41:14 -05:00
if (input.attr('type') === 'number') {
2015-02-22 02:46:21 -06:00
// update the range-input:
$('#budget-range-' + id).val(value);
} else {
// update the number-input:
$('#budget-info-' + id + ' input').val(value);
}
// update or hide the bar, whichever is necessary.
updateTotal();
return value;
}
function updateTotal() {
2015-05-24 11:22:41 -05:00
"use strict";
2015-02-22 02:46:21 -06:00
var sum = 0;
$('input[type="range"]').each(function (i, v) {
// get some values:
sum += parseInt($(v).val());
});
/**
* Update total sum:
*/
var totalAmount = parseInt($('#totalAmount').data('value'));
var pct;
if (sum <= totalAmount) {
pct = sum / totalAmount * 100;
$('#progress-bar-default').css('width', pct + '%');
$('#progress-bar-warning').css('width', '0');
$('#progress-bar-danger').css('width', '0');
$('#budgetedAmount').text('\u20AC ' + sum.toFixed(2)).addClass('text-success').removeClass('text-danger');
} else {
// we gaan er X overheen,
pct = totalAmount / sum * 100;
var danger = 100 - pct;
var err = 100 - danger;
$('#progress-bar-default').css('width', 0);
$('#progress-bar-warning').css('width', err + '%');
$('#progress-bar-danger').css('width', danger + '%');
$('#budgetedAmount').text('\u20AC ' + sum.toFixed(2)).addClass('text-danger').removeClass('text-success');
}
}
2015-05-24 11:22:41 -05:00
function updateIncome() {
"use strict";
$('#monthlyBudgetModal').empty().load('budgets/income', function () {
$('#monthlyBudgetModal').modal('show');
});
2015-02-22 02:46:21 -06:00
return false;
}
function updateRanges() {
2015-05-24 11:22:41 -05:00
"use strict";
2015-02-22 02:46:21 -06:00
/**
* Update all ranges.
*/
var sum = 0;
$('input[type="range"]').each(function (i, v) {
// get some values:
var input = $(v);
var id = input.data('id');
var value = parseInt(input.val());
// calculate sum:
sum += value;
2015-02-22 02:46:21 -06:00
// update small display:
$('#budget-range-display-' + id).text('\u20AC ' + value.toFixed(2));
// send a post to Firefly to update the amount:
2015-05-24 13:41:14 -05:00
$.post('budgets/amount/' + id, {amount: value, _token: token});
2015-02-22 02:46:21 -06:00
});
/**
* Update total sum:
*/
var totalAmount = parseInt($('#totalAmount').data('value'));
2015-05-24 11:22:41 -05:00
var pct;
2015-02-22 02:46:21 -06:00
if (sum <= totalAmount) {
2015-05-24 11:22:41 -05:00
pct = sum / totalAmount * 100;
2015-02-22 02:46:21 -06:00
$('#progress-bar-default').css('width', pct + '%');
$('#progress-bar-warning').css('width', '0');
$('#progress-bar-danger').css('width', '0');
$('#budgetedAmount').text('\u20AC ' + sum.toFixed(2)).addClass('text-success').removeClass('text-danger');
} else {
// we gaan er X overheen,
2015-05-24 11:22:41 -05:00
pct = totalAmount / sum * 100;
2015-02-22 02:46:21 -06:00
var danger = 100 - pct;
var err = 100 - danger;
$('#progress-bar-default').css('width', 0);
$('#progress-bar-warning').css('width', err + '%');
$('#progress-bar-danger').css('width', danger + '%');
$('#budgetedAmount').text('\u20AC ' + sum.toFixed(2)).addClass('text-danger').removeClass('text-success');
}
}