1 | /* |
||
2 | * index.js |
||
3 | * Copyright (c) 2017 [email protected] |
||
4 | * |
||
5 | * This file is part of Firefly III. |
||
6 | * |
||
7 | * Firefly III is free software: you can redistribute it and/or modify |
||
8 | * it under the terms of the GNU General Public License as published by |
||
9 | * the Free Software Foundation, either version 3 of the License, or |
||
10 | * (at your option) any later version. |
||
11 | * |
||
12 | * Firefly III is distributed in the hope that it will be useful, |
||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | * GNU General Public License for more details. |
||
16 | * |
||
17 | * You should have received a copy of the GNU General Public License |
||
18 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
19 | */ |
||
20 | |||
21 | /** global: infoIncomeUri, page, token, spent, budgeted, available, currencySymbol, budgetIndexUri, updateIncomeUri, periodStart, periodEnd, budgetAmountUri, accounting */ |
||
22 | /** |
||
23 | * |
||
24 | */ |
||
25 | $(function () { |
||
26 | "use strict"; |
||
27 | |||
28 | $('.updateIncome').on('click', updateIncome); |
||
29 | $('.infoIncome').on('click', infoIncome); |
||
30 | |||
31 | /* |
||
32 | On start, fill the "spent"-bar using the content from the page. |
||
33 | */ |
||
34 | drawSpentBar(); |
||
35 | drawBudgetedBar(); |
||
36 | |||
37 | /* |
||
38 | When the input changes, update the percentages for the budgeted bar: |
||
39 | */ |
||
40 | $('input[type="number"]').on('change', updateBudgetedAmounts); |
||
41 | |||
42 | // |
||
43 | $('.selectPeriod').change(function (e) { |
||
44 | var sel = $(e.target).val(); |
||
45 | if (sel !== "x") { |
||
46 | var newUri = budgetIndexUri.replace("REPLACE", sel); |
||
47 | window.location.assign(newUri + "?page=" + page); |
||
48 | } |
||
49 | }); |
||
50 | |||
51 | }); |
||
52 | |||
53 | function drawSpentBar() { |
||
54 | "use strict"; |
||
55 | if ($('.spentBar').length > 0) { |
||
56 | var overspent = spent > budgeted; |
||
57 | var pct; |
||
58 | |||
59 | if (overspent) { |
||
60 | // draw overspent bar |
||
61 | pct = (budgeted / spent) * 100; |
||
62 | $('.spentBar .progress-bar-warning').css('width', pct + '%'); |
||
63 | $('.spentBar .progress-bar-danger').css('width', (100 - pct) + '%'); |
||
64 | } else { |
||
65 | // draw normal bar: |
||
66 | pct = (spent / budgeted) * 100; |
||
67 | $('.spentBar .progress-bar-info').css('width', pct + '%'); |
||
68 | } |
||
69 | } |
||
70 | } |
||
71 | |||
72 | function drawBudgetedBar() { |
||
73 | "use strict"; |
||
74 | |||
75 | if ($('.budgetedBar').length > 0) { |
||
76 | var budgetedMuch = budgeted > available; |
||
77 | |||
78 | // recalculate percentage: |
||
79 | |||
80 | var pct; |
||
81 | if (budgetedMuch) { |
||
82 | // budgeted too much. |
||
83 | pct = (available / budgeted) * 100; |
||
84 | $('.budgetedBar .progress-bar-warning').css('width', pct + '%'); |
||
85 | $('.budgetedBar .progress-bar-danger').css('width', (100 - pct) + '%'); |
||
86 | $('.budgetedBar .progress-bar-info').css('width', 0); |
||
87 | } else { |
||
88 | pct = (budgeted / available) * 100; |
||
89 | $('.budgetedBar .progress-bar-warning').css('width', 0); |
||
90 | $('.budgetedBar .progress-bar-danger').css('width', 0); |
||
91 | $('.budgetedBar .progress-bar-info').css('width', pct + '%'); |
||
92 | } |
||
93 | |||
94 | $('#budgetedAmount').html(currencySymbol + ' ' + budgeted.toFixed(2)); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * |
||
100 | * @param e |
||
101 | */ |
||
102 | function updateBudgetedAmounts(e) { |
||
103 | "use strict"; |
||
104 | var target = $(e.target); |
||
105 | var id = target.data('id'); |
||
106 | var leftCell = $('td[class$="left"][data-id="' + id + '"]'); |
||
107 | var link = $('a[data-id="' + id + '"][class="budget-link"]'); |
||
108 | var value = target.val(); |
||
109 | var original = target.data('original'); |
||
110 | |||
111 | // disable input |
||
112 | target.prop('disabled', true); |
||
113 | |||
114 | // replace link (for now) |
||
115 | link.attr('href', '#'); |
||
116 | |||
117 | // replace "left" with spinner. |
||
118 | leftCell.empty().html('<i class="fa fa-fw fa-spin fa-spinner"></i>'); |
||
119 | |||
120 | // send a post to Firefly to update the amount: |
||
121 | var newUri = budgetAmountUri.replace("REPLACE", id); |
||
122 | |||
123 | $.post(newUri, {amount: value, start: periodStart, end: periodEnd, _token: token}).done(function (data) { |
||
124 | |||
125 | // difference between new value and original value |
||
126 | var difference = value - original; |
||
127 | |||
128 | // update budgeted value |
||
129 | budgeted = budgeted + difference; |
||
130 | |||
131 | // fill in "left" value: |
||
132 | leftCell.html(data.left); |
||
133 | |||
134 | // update "budgeted" input: |
||
135 | target.val(data.amount); |
||
136 | |||
137 | // enable thing again |
||
138 | target.prop('disabled', false); |
||
139 | |||
140 | // set new original value: |
||
141 | target.data('original', data.amount); |
||
142 | |||
143 | // run drawBudgetedBar() again: |
||
144 | drawBudgetedBar(); |
||
145 | |||
146 | // update the link if relevant: |
||
147 | link.attr('href', 'budgets/show/' + id); |
||
148 | if (data.limit > 0) { |
||
149 | link.attr('href', 'budgets/show/' + id + '/' + data.limit); |
||
150 | } |
||
151 | |||
152 | // update the warning if relevant: |
||
153 | if (data.large_diff === true) { |
||
154 | $('span[class$="budget_warning"][data-id="' + id + '"]').html(data.warn_text).show(); |
||
155 | console.log('Show warning for budget'); |
||
0 ignored issues
–
show
Debugging Code
introduced
by
Loading history...
|
|||
156 | } else { |
||
157 | $('span[class$="budget_warning"][data-id="' + id + '"]').empty().hide(); |
||
158 | } |
||
159 | }); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * |
||
164 | * @returns {boolean} |
||
165 | */ |
||
166 | function updateIncome() { |
||
167 | "use strict"; |
||
168 | $('#defaultModal').empty().load(updateIncomeUri, function () { |
||
169 | $('#defaultModal').modal('show'); |
||
170 | }); |
||
171 | |||
172 | return false; |
||
173 | } |
||
174 | |||
175 | function infoIncome() { |
||
176 | $('#defaultModal').empty().load(infoIncomeUri, function () { |
||
177 | $('#defaultModal').modal('show'); |
||
178 | }); |
||
179 | |||
180 | return false; |
||
181 | } |
||
182 |