Files
firefly-iii/public/js/ff/budgets/index.js

118 lines
3.6 KiB
JavaScript
Raw Normal View History

2015-06-27 12:04:53 +02:00
/* globals $, budgeted:true, currencySymbol, budgetIncomeTotal, columnChart, budgetedMuch, budgetedPercentage, token, budgetID, repetitionID, spent, lineChart */
2015-05-24 18:22:41 +02:00
2015-05-24 22:54:59 +02:00
function drawSpentBar() {
2015-05-24 18:22:41 +02:00
"use strict";
2015-06-02 18:13:23 +02:00
if ($('.spentBar').length > 0) {
var overspent = spent > budgeted;
var pct;
if (overspent) {
// draw overspent bar
pct = (budgeted / spent) * 100;
$('.spentBar .progress-bar-warning').css('width', pct + '%');
$('.spentBar .progress-bar-danger').css('width', (100 - pct) + '%');
} else {
// draw normal bar:
pct = (spent / budgeted) * 100;
$('.spentBar .progress-bar-info').css('width', pct + '%');
}
2015-02-22 09:46:21 +01:00
}
2015-05-24 22:54:59 +02:00
}
2015-02-22 09:46:21 +01:00
2015-05-24 22:54:59 +02:00
function drawBudgetedBar() {
2015-05-24 18:22:41 +02:00
"use strict";
2015-02-22 09:46:21 +01:00
2015-06-02 18:13:23 +02:00
if ($('.budgetedBar').length > 0) {
var budgetedMuch = budgeted > budgetIncomeTotal;
// recalculate percentage:
var pct;
if (budgetedMuch) {
// budgeted too much.
pct = (budgetIncomeTotal / budgeted) * 100;
$('.budgetedBar .progress-bar-warning').css('width', pct + '%');
$('.budgetedBar .progress-bar-danger').css('width', (100 - pct) + '%');
$('.budgetedBar .progress-bar-info').css('width', 0);
} else {
pct = (budgeted / budgetIncomeTotal) * 100;
$('.budgetedBar .progress-bar-warning').css('width', 0);
$('.budgetedBar .progress-bar-danger').css('width', 0);
$('.budgetedBar .progress-bar-info').css('width', pct + '%');
}
$('#budgetedAmount').html(currencySymbol + ' ' + budgeted.toFixed(2));
}
2015-02-22 09:46:21 +01:00
}
2015-05-24 22:54:59 +02:00
function updateBudgetedAmounts(e) {
2015-05-24 18:22:41 +02:00
"use strict";
2015-05-24 22:54:59 +02:00
var target = $(e.target);
var id = target.data('id');
var value = target.val();
var original = target.data('original');
var difference = value - original;
if (difference !== 0) {
// add difference to 'budgeted' var
budgeted = budgeted + difference;
// update original:
target.data('original', value);
// run drawBudgetedBar() again:
drawBudgetedBar();
2015-02-22 09:46:21 +01:00
2015-05-24 22:54:59 +02:00
// send a post to Firefly to update the amount:
2016-02-04 07:30:12 +01:00
$.post('budgets/amount/' + id, {amount: value, _token: token}).done(function (data) {
2015-05-24 22:54:59 +02:00
// update the link if relevant:
if (data.repetition > 0) {
$('.budget-link[data-id="' + id + '"]').attr('href', 'budgets/show/' + id + '/' + data.repetition);
} else {
$('.budget-link[data-id="' + id + '"]').attr('href', 'budgets/show/' + id);
}
});
2015-02-22 09:46:21 +01:00
}
2015-05-24 22:54:59 +02:00
console.log('Budget id is ' + id);
console.log('Difference = ' + (value - original ));
2015-02-22 09:46:21 +01:00
}
2015-05-24 22:54:59 +02:00
$(function () {
2015-05-24 18:22:41 +02:00
"use strict";
2015-05-24 20:48:31 +02:00
2015-05-24 22:54:59 +02:00
$('.updateIncome').on('click', updateIncome);
2015-05-24 20:48:31 +02:00
2015-05-24 22:54:59 +02:00
/*
On start, fill the "spent"-bar using the content from the page.
*/
drawSpentBar();
drawBudgetedBar();
2015-02-22 09:46:21 +01:00
2015-05-24 22:54:59 +02:00
/*
When the input changes, update the percentages for the budgeted bar:
*/
$('input[type="number"]').on('input', updateBudgetedAmounts);
2015-02-22 09:46:21 +01:00
2015-05-24 22:54:59 +02:00
/*
Draw the charts, if necessary:
2015-02-22 09:46:21 +01:00
*/
2015-05-24 22:54:59 +02:00
if (typeof budgetID !== 'undefined' && typeof repetitionID === 'undefined') {
2015-06-27 12:04:53 +02:00
columnChart('chart/budget/' + budgetID, 'budgetOverview');
2015-05-24 22:54:59 +02:00
}
if (typeof budgetID !== 'undefined' && typeof repetitionID !== 'undefined') {
2015-06-27 12:04:53 +02:00
lineChart('chart/budget/' + budgetID + '/' + repetitionID, 'budgetOverview');
2015-02-22 09:46:21 +01:00
}
2015-05-24 22:54:59 +02:00
});
2015-02-22 09:46:21 +01:00
2015-06-28 08:24:12 +02:00
function updateIncome() {
2015-05-24 22:54:59 +02:00
"use strict";
2015-05-31 20:23:49 +02:00
$('#defaultModal').empty().load('budgets/income', function () {
$('#defaultModal').modal('show');
2015-05-24 22:54:59 +02:00
});
return false;
}