1 | /* |
||
2 | * create.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: currencyInfo, overruleCurrency,useAccountCurrency, accountInfo, what,Modernizr, title, breadcrumbs, middleCrumbName, button, piggiesLength, txt, middleCrumbUrl,exchangeRateInstructions, convertForeignToNative, convertSourceToDestination, selectsForeignCurrency, accountInfo */ |
||
22 | |||
23 | $(document).ready(function () { |
||
24 | "use strict"; |
||
25 | |||
26 | // hide ALL exchange things and AMOUNT fields |
||
27 | $('#exchange_rate_instruction_holder').hide(); |
||
28 | $('#native_amount_holder').hide(); |
||
29 | $('#amount_holder').hide(); |
||
30 | $('#source_amount_holder').hide(); |
||
31 | $('#destination_amount_holder').hide(); |
||
32 | |||
33 | // respond to switch buttons (first time always triggers) |
||
34 | updateButtons(); |
||
35 | updateForm(); |
||
36 | updateLayout(); |
||
37 | updateDescription(); |
||
38 | |||
39 | |||
40 | // when user changes source account or destination, native currency may be different. |
||
41 | $('select[name="source_account_id"]').on('change', function() { |
||
42 | selectsDifferentSource(); |
||
43 | // do something for transfers: |
||
44 | validateCurrencyForTransfer(); |
||
45 | }); |
||
46 | $('select[name="destination_account_id"]').on('change', function() { |
||
47 | selectsDifferentDestination(); |
||
48 | // do something for transfers: |
||
49 | validateCurrencyForTransfer(); |
||
50 | }); |
||
51 | |||
52 | //$('select[name="source_account_id"]').on('change', updateNativeCurrency); |
||
53 | //$('select[name="destination_account_id"]').on('change', updateNativeCurrency); |
||
54 | |||
55 | // convert foreign currency to native currency (when input changes, exchange rate) |
||
56 | $('#ffInput_amount').on('change', convertForeignToNative); |
||
57 | |||
58 | // convert source currency to destination currency (slightly different routine for transfers) |
||
59 | $('#ffInput_source_amount').on('change', convertSourceToDestination); |
||
60 | |||
61 | // when user selects different currency, |
||
62 | $('.currency-option').on('click', selectsForeignCurrency); |
||
63 | $('#ffInput_description').focus(); |
||
64 | }); |
||
65 | |||
66 | /** |
||
67 | * The user selects a different source account. Applies to withdrawals |
||
68 | * and transfers. |
||
69 | */ |
||
70 | function selectsDifferentSource() { |
||
71 | if (what === "deposit") { |
||
72 | console.log('User is making a deposit. Don\'t bother with source.'); |
||
73 | $('input[name="source_account_currency"]').val("0"); |
||
74 | return; |
||
75 | } |
||
76 | // store original currency ID of the selected account in a separate var: |
||
77 | var sourceId = $('select[name="source_account_id"]').val(); |
||
78 | var sourceCurrency = accountInfo[sourceId].preferredCurrency; |
||
79 | $('input[name="source_account_currency"]').val(sourceCurrency); |
||
80 | console.log('selectsDifferenctSource(): Set source account currency to ' + sourceCurrency); |
||
81 | |||
82 | // change input thing: |
||
83 | $('.currency-option[data-id="' + sourceCurrency + '"]').click(); |
||
84 | $('[data-toggle="dropdown"]').parent().removeClass('open'); |
||
85 | $('select[name="source_account_id"]').focus(); |
||
86 | |||
87 | return; |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
88 | } |
||
89 | |||
90 | /** |
||
91 | * The user selects a different source account. Applies to withdrawals |
||
92 | * and transfers. |
||
93 | */ |
||
94 | function selectsDifferentDestination() { |
||
95 | if (what === "withdrawal") { |
||
96 | console.log('User is making a withdrawal. Don\'t bother with destination.'); |
||
97 | $('input[name="destination_account_currency"]').val("0"); |
||
98 | return; |
||
99 | } |
||
100 | // store original currency ID of the selected account in a separate var: |
||
101 | var destinationId = $('select[name="destination_account_id"]').val(); |
||
102 | var destinationCurrency = accountInfo[destinationId].preferredCurrency; |
||
103 | $('input[name="destination_account_currency"]').val(destinationCurrency); |
||
104 | console.log('selectsDifferentDestination(): Set destinationId account currency to ' + destinationCurrency); |
||
105 | |||
106 | // change input thing: |
||
107 | $('.currency-option[data-id="' + destinationCurrency + '"]').click(); |
||
108 | $('[data-toggle="dropdown"]').parent().removeClass('open'); |
||
109 | $('select[name="destination_account_id"]').focus(); |
||
110 | |||
111 | return; |
||
0 ignored issues
–
show
|
|||
112 | } |
||
113 | |||
114 | |||
115 | /** |
||
116 | * This function generates a small helper text to explain the user |
||
117 | * that they have selected a foreign currency. |
||
118 | * @returns {XML|string|void} |
||
119 | */ |
||
120 | function getExchangeInstructions() { |
||
121 | var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val()); |
||
122 | var selectedAccountId = getAccountId(); |
||
123 | var nativeCurrencyId = parseInt(accountInfo[selectedAccountId].preferredCurrency); |
||
124 | |||
125 | var text = exchangeRateInstructions.replace('@name', accountInfo[selectedAccountId].name); |
||
126 | text = text.replace(/@native_currency/g, currencyInfo[nativeCurrencyId].name); |
||
127 | text = text.replace(/@foreign_currency/g, currencyInfo[foreignCurrencyId].name); |
||
128 | return text; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * |
||
133 | */ |
||
134 | function updateDescription() { |
||
135 | $.getJSON('json/transaction-journals/' + what).done(function (data) { |
||
136 | $('input[name="description"]').typeahead('destroy').typeahead({source: data, autoSelect: false}); |
||
137 | }); |
||
138 | $('#ffInput_description').focus(); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * |
||
143 | */ |
||
144 | function updateLayout() { |
||
145 | "use strict"; |
||
146 | $('#subTitle').text(title[what]); |
||
147 | $('.breadcrumb .active').text(breadcrumbs[what]); |
||
148 | $('.breadcrumb li:nth-child(2)').html('<a href="' + middleCrumbUrl[what] + '">' + middleCrumbName[what] + '</a>'); |
||
149 | $('#transaction-btn').text(button[what]); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * |
||
154 | */ |
||
155 | function updateForm() { |
||
156 | "use strict"; |
||
157 | |||
158 | $('input[name="what"]').val(what); |
||
159 | |||
160 | var destName = $('#ffInput_destination_account_name'); |
||
161 | var srcName = $('#ffInput_source_account_name'); |
||
162 | |||
163 | switch (what) { |
||
164 | |||
165 | case 'withdrawal': |
||
166 | // show source_id and dest_name |
||
167 | document.getElementById('source_account_id_holder').style.display = 'block'; |
||
168 | document.getElementById('destination_account_name_holder').style.display = 'block'; |
||
169 | |||
170 | // hide others: |
||
171 | document.getElementById('source_account_name_holder').style.display = 'none'; |
||
172 | document.getElementById('destination_account_id_holder').style.display = 'none'; |
||
173 | document.getElementById('budget_id_holder').style.display = 'block'; |
||
174 | |||
175 | // hide piggy bank: |
||
176 | document.getElementById('piggy_bank_id_holder').style.display = 'none'; |
||
177 | |||
178 | // copy destination account name to source account name: |
||
179 | if (destName.val().length > 0) { |
||
180 | srcName.val(destName.val()); |
||
181 | } |
||
182 | |||
183 | // exchange / foreign currencies: |
||
184 | // hide explanation, hide source and destination amounts, show normal amount |
||
185 | document.getElementById('exchange_rate_instruction_holder').style.display = 'none'; |
||
186 | document.getElementById('source_amount_holder').style.display = 'none'; |
||
187 | document.getElementById('destination_amount_holder').style.display = 'none'; |
||
188 | document.getElementById('amount_holder').style.display = 'block'; |
||
189 | break; |
||
190 | case 'deposit': |
||
191 | // show source_name and dest_id: |
||
192 | document.getElementById('source_account_name_holder').style.display = 'block'; |
||
193 | document.getElementById('destination_account_id_holder').style.display = 'block'; |
||
194 | |||
195 | // hide others: |
||
196 | document.getElementById('source_account_id_holder').style.display = 'none'; |
||
197 | document.getElementById('destination_account_name_holder').style.display = 'none'; |
||
198 | |||
199 | // hide budget |
||
200 | document.getElementById('budget_id_holder').style.display = 'none'; |
||
201 | |||
202 | // hide piggy bank |
||
203 | document.getElementById('piggy_bank_id_holder').style.display = 'none'; |
||
204 | |||
205 | // copy name |
||
206 | if (srcName.val().length > 0) { |
||
207 | destName.val(srcName.val()); |
||
208 | } |
||
209 | |||
210 | // exchange / foreign currencies: |
||
211 | // hide explanation, hide source and destination amounts, show amount |
||
212 | document.getElementById('exchange_rate_instruction_holder').style.display = 'none'; |
||
213 | document.getElementById('source_amount_holder').style.display = 'none'; |
||
214 | document.getElementById('destination_amount_holder').style.display = 'none'; |
||
215 | document.getElementById('amount_holder').style.display = 'block'; |
||
216 | break; |
||
217 | case 'transfer': |
||
218 | // show source_id and dest_id: |
||
219 | document.getElementById('source_account_id_holder').style.display = 'block'; |
||
220 | document.getElementById('destination_account_id_holder').style.display = 'block'; |
||
221 | |||
222 | // hide others: |
||
223 | document.getElementById('source_account_name_holder').style.display = 'none'; |
||
224 | document.getElementById('destination_account_name_holder').style.display = 'none'; |
||
225 | |||
226 | // hide budget |
||
227 | document.getElementById('budget_id_holder').style.display = 'none'; |
||
228 | |||
229 | // optional piggies |
||
230 | var showPiggies = 'block'; |
||
231 | if (piggiesLength === 0) { |
||
232 | showPiggies = 'none'; |
||
233 | } |
||
234 | document.getElementById('piggy_bank_id_holder').style.display = showPiggies; |
||
235 | break; |
||
236 | default: |
||
237 | break; |
||
238 | } |
||
239 | // get instructions all the time. |
||
240 | //updateNativeCurrency(useAccountCurrency); |
||
241 | console.log('End of update form'); |
||
242 | selectsDifferentSource(); |
||
243 | selectsDifferentDestination(); |
||
244 | selectsForeignCurrency(); |
||
245 | |||
246 | // do something for transfers: |
||
247 | validateCurrencyForTransfer(); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * |
||
252 | */ |
||
253 | function updateButtons() { |
||
254 | "use strict"; |
||
255 | $('.switch').each(function (i, v) { |
||
256 | var button = $(v); |
||
257 | |||
258 | // remove click event: |
||
259 | button.unbind('click'); |
||
260 | // new click event: |
||
261 | button.bind('click', clickButton); |
||
262 | |||
263 | if (button.data('what') === what) { |
||
264 | button.removeClass('btn-default').addClass('btn-info').html('<i class="fa fa-fw fa-check"></i> ' + txt[button.data('what')]); |
||
265 | } else { |
||
266 | button.removeClass('btn-info').addClass('btn-default').text(txt[button.data('what')]); |
||
267 | } |
||
268 | }); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * |
||
273 | * @param e |
||
274 | * @returns {boolean} |
||
275 | */ |
||
276 | function clickButton(e) { |
||
277 | "use strict"; |
||
278 | var button = $(e.target); |
||
279 | var newWhat = button.data('what'); |
||
280 | if (newWhat !== what) { |
||
281 | what = newWhat; |
||
282 | updateButtons(); |
||
283 | updateForm(); |
||
284 | updateLayout(); |
||
285 | updateDescription(); |
||
286 | } |
||
287 | return false; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Get accountID based on some meta info. |
||
292 | */ |
||
293 | function getAccountId() { |
||
294 | if (what === "withdrawal") { |
||
295 | return $('select[name="source_account_id"]').val(); |
||
296 | } |
||
297 | if (what === "deposit" || what === "transfer") { |
||
298 | return $('select[name="destination_account_id"]').val(); |
||
299 | } |
||
300 | return undefined; |
||
301 | } |
||
302 |