2016-01-29 18:39:50 +01:00
|
|
|
/*
|
|
|
|
|
* create.js
|
2017-01-15 19:07:31 +01:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-12-23 07:02:45 +01:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* This file is part of Firefly III.
|
|
|
|
|
*
|
|
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 14:43:13 +01:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-01-29 18:39:50 +01:00
|
|
|
*/
|
|
|
|
|
|
2017-07-23 08:16:11 +02:00
|
|
|
/** global: currencyInfo, overruleCurrency,useAccountCurrency, accountInfo, what,Modernizr, title, breadcrumbs, middleCrumbName, button, piggiesLength, txt, middleCrumbUrl,exchangeRateInstructions, convertForeignToNative, convertSourceToDestination, selectsForeignCurrency, accountInfo */
|
2017-01-02 10:34:01 +01:00
|
|
|
|
2015-02-24 21:10:25 +01:00
|
|
|
$(document).ready(function () {
|
2015-05-24 20:41:14 +02:00
|
|
|
"use strict";
|
2015-03-26 22:52:49 +01:00
|
|
|
|
2017-04-14 22:25:48 +02:00
|
|
|
// hide ALL exchange things and AMOUNT things
|
|
|
|
|
$('#exchange_rate_instruction_holder').hide();
|
|
|
|
|
$('#native_amount_holder').hide();
|
|
|
|
|
$('#amount_holder').hide();
|
|
|
|
|
$('#source_amount_holder').hide();
|
|
|
|
|
$('#destination_amount_holder').hide();
|
|
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
// respond to switch buttons (first time always triggers)
|
2017-03-03 18:19:25 +01:00
|
|
|
updateButtons();
|
|
|
|
|
updateForm();
|
|
|
|
|
updateLayout();
|
|
|
|
|
updateDescription();
|
2015-06-03 17:32:50 +02:00
|
|
|
|
2017-04-14 10:16:52 +02:00
|
|
|
|
2017-04-14 22:25:48 +02:00
|
|
|
// when user changes source account or destination, native currency may be different.
|
2017-04-14 15:42:54 +02:00
|
|
|
$('select[name="source_account_id"]').on('change', updateNativeCurrency);
|
2017-04-14 22:25:48 +02:00
|
|
|
$('select[name="destination_account_id"]').on('change', updateNativeCurrency);
|
2017-01-15 20:05:40 +01:00
|
|
|
|
2017-04-14 22:25:48 +02:00
|
|
|
// convert foreign currency to native currency (when input changes, exchange rate)
|
2017-04-14 15:42:54 +02:00
|
|
|
$('#ffInput_amount').on('change', convertForeignToNative);
|
2017-04-14 10:16:52 +02:00
|
|
|
|
2017-04-14 22:25:48 +02:00
|
|
|
// convert source currency to destination currency (slightly different routine for transfers)
|
|
|
|
|
$('#ffInput_source_amount').on('change', convertSourceToDestination);
|
|
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
// when user selects different currency,
|
|
|
|
|
$('.currency-option').on('click', selectsForeignCurrency);
|
2017-09-03 10:51:02 +02:00
|
|
|
$('#ffInput_description').focus();
|
2015-06-03 17:32:50 +02:00
|
|
|
});
|
|
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
/**
|
|
|
|
|
* This function generates a small helper text to explain the user
|
|
|
|
|
* that they have selected a foreign currency.
|
|
|
|
|
* @returns {XML|string|void}
|
|
|
|
|
*/
|
|
|
|
|
function getExchangeInstructions() {
|
|
|
|
|
var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
|
|
|
|
|
var selectedAccountId = getAccountId();
|
|
|
|
|
var nativeCurrencyId = parseInt(accountInfo[selectedAccountId].preferredCurrency);
|
|
|
|
|
|
|
|
|
|
var text = exchangeRateInstructions.replace('@name', accountInfo[selectedAccountId].name);
|
|
|
|
|
text = text.replace(/@native_currency/g, currencyInfo[nativeCurrencyId].name);
|
|
|
|
|
text = text.replace(/@foreign_currency/g, currencyInfo[foreignCurrencyId].name);
|
|
|
|
|
return text;
|
|
|
|
|
}
|
2017-04-14 10:16:52 +02:00
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
/**
|
|
|
|
|
* There is an input that shows the currency symbol that is native to the selected
|
|
|
|
|
* acccount. So when the user changes the selected account, the native currency is updated:
|
|
|
|
|
*/
|
2017-07-07 17:51:14 +02:00
|
|
|
function updateNativeCurrency(useAccountCurrency) {
|
|
|
|
|
var nativeCurrencyId;
|
|
|
|
|
if (useAccountCurrency) {
|
|
|
|
|
var newAccountId = getAccountId();
|
|
|
|
|
nativeCurrencyId = accountInfo[newAccountId].preferredCurrency;
|
|
|
|
|
}
|
|
|
|
|
if (!useAccountCurrency) {
|
|
|
|
|
nativeCurrencyId = overruleCurrency;
|
|
|
|
|
}
|
2017-03-03 18:19:25 +01:00
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
$('.currency-option[data-id="' + nativeCurrencyId + '"]').click();
|
2017-03-03 18:19:25 +01:00
|
|
|
$('[data-toggle="dropdown"]').parent().removeClass('open');
|
2017-10-02 08:47:57 +02:00
|
|
|
if (what === 'withdrawal') {
|
2017-05-12 06:21:26 +02:00
|
|
|
$('select[name="source_account_id"]').focus();
|
|
|
|
|
}
|
2017-10-02 08:47:57 +02:00
|
|
|
if (what === 'deposit') {
|
|
|
|
|
$('select[name="destination_account_id"]').focus();
|
|
|
|
|
}
|
2017-04-14 22:25:48 +02:00
|
|
|
validateCurrencyForTransfer();
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
2017-01-15 19:07:31 +01:00
|
|
|
function updateDescription() {
|
|
|
|
|
$.getJSON('json/transaction-journals/' + what).done(function (data) {
|
2018-01-22 18:37:59 +01:00
|
|
|
$('input[name="description"]').typeahead('destroy').typeahead({source: data, autoSelect: false});
|
2017-01-15 19:07:31 +01:00
|
|
|
});
|
2017-09-03 10:51:02 +02:00
|
|
|
$('#ffInput_description').focus();
|
2017-01-15 19:07:31 +01:00
|
|
|
}
|
|
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
2015-06-03 17:32:50 +02:00
|
|
|
function updateLayout() {
|
|
|
|
|
"use strict";
|
|
|
|
|
$('#subTitle').text(title[what]);
|
|
|
|
|
$('.breadcrumb .active').text(breadcrumbs[what]);
|
|
|
|
|
$('.breadcrumb li:nth-child(2)').html('<a href="' + middleCrumbUrl[what] + '">' + middleCrumbName[what] + '</a>');
|
2015-06-20 21:55:55 +02:00
|
|
|
$('#transaction-btn').text(button[what]);
|
2015-06-03 17:32:50 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
2015-06-03 17:32:50 +02:00
|
|
|
function updateForm() {
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
$('input[name="what"]').val(what);
|
2017-04-15 07:25:09 +02:00
|
|
|
|
|
|
|
|
var destName = $('#ffInput_destination_account_name');
|
|
|
|
|
var srcName = $('#ffInput_source_account_name');
|
|
|
|
|
|
2015-06-03 17:32:50 +02:00
|
|
|
switch (what) {
|
2017-06-05 11:12:50 +02:00
|
|
|
|
2015-06-03 17:32:50 +02:00
|
|
|
case 'withdrawal':
|
2017-04-14 22:25:48 +02:00
|
|
|
// show source_id and dest_name
|
2017-04-15 07:25:09 +02:00
|
|
|
document.getElementById('source_account_id_holder').style.display = 'block';
|
|
|
|
|
document.getElementById('destination_account_name_holder').style.display = 'block';
|
2016-04-29 17:29:13 +02:00
|
|
|
|
|
|
|
|
// hide others:
|
2017-04-15 07:25:09 +02:00
|
|
|
document.getElementById('source_account_name_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('destination_account_id_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('budget_id_holder').style.display = 'block';
|
2015-06-03 17:32:50 +02:00
|
|
|
|
2016-04-29 17:29:13 +02:00
|
|
|
// hide piggy bank:
|
2017-04-15 07:25:09 +02:00
|
|
|
document.getElementById('piggy_bank_id_holder').style.display = 'none';
|
2015-06-03 17:32:50 +02:00
|
|
|
|
2017-04-15 07:25:09 +02:00
|
|
|
// copy destination account name to source account name:
|
|
|
|
|
if (destName.val().length > 0) {
|
|
|
|
|
srcName.val(destName.val());
|
2015-06-03 17:32:50 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-14 22:25:48 +02:00
|
|
|
// exchange / foreign currencies:
|
2017-04-15 07:25:09 +02:00
|
|
|
// hide explanation, hide source and destination amounts, show normal amount
|
|
|
|
|
document.getElementById('exchange_rate_instruction_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('source_amount_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('destination_amount_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('amount_holder').style.display = 'block';
|
2015-06-03 17:32:50 +02:00
|
|
|
break;
|
|
|
|
|
case 'deposit':
|
2016-04-29 17:29:13 +02:00
|
|
|
// show source_name and dest_id:
|
2017-04-15 07:25:09 +02:00
|
|
|
document.getElementById('source_account_name_holder').style.display = 'block';
|
|
|
|
|
document.getElementById('destination_account_id_holder').style.display = 'block';
|
2016-04-29 17:29:13 +02:00
|
|
|
|
|
|
|
|
// hide others:
|
2017-04-15 07:25:09 +02:00
|
|
|
document.getElementById('source_account_id_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('destination_account_name_holder').style.display = 'none';
|
2016-04-29 17:29:13 +02:00
|
|
|
|
|
|
|
|
// hide budget
|
2017-04-15 07:25:09 +02:00
|
|
|
document.getElementById('budget_id_holder').style.display = 'none';
|
2016-04-29 17:29:13 +02:00
|
|
|
|
|
|
|
|
// hide piggy bank
|
2017-04-15 07:25:09 +02:00
|
|
|
document.getElementById('piggy_bank_id_holder').style.display = 'none';
|
2015-06-03 17:32:50 +02:00
|
|
|
|
2017-04-15 07:25:09 +02:00
|
|
|
// copy name
|
|
|
|
|
if (srcName.val().length > 0) {
|
|
|
|
|
destName.val(srcName.val());
|
2015-06-03 17:32:50 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-14 22:25:48 +02:00
|
|
|
// exchange / foreign currencies:
|
2017-04-15 07:25:09 +02:00
|
|
|
// hide explanation, hide source and destination amounts, show amount
|
|
|
|
|
document.getElementById('exchange_rate_instruction_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('source_amount_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('destination_amount_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('amount_holder').style.display = 'block';
|
2015-06-03 17:32:50 +02:00
|
|
|
break;
|
|
|
|
|
case 'transfer':
|
2016-04-29 17:29:13 +02:00
|
|
|
// show source_id and dest_id:
|
2017-04-15 07:25:09 +02:00
|
|
|
document.getElementById('source_account_id_holder').style.display = 'block';
|
|
|
|
|
document.getElementById('destination_account_id_holder').style.display = 'block';
|
2016-04-29 17:29:13 +02:00
|
|
|
|
|
|
|
|
// hide others:
|
2017-04-15 07:25:09 +02:00
|
|
|
document.getElementById('source_account_name_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('destination_account_name_holder').style.display = 'none';
|
2016-04-29 17:29:13 +02:00
|
|
|
|
|
|
|
|
// hide budget
|
2017-04-15 07:25:09 +02:00
|
|
|
document.getElementById('budget_id_holder').style.display = 'none';
|
|
|
|
|
|
|
|
|
|
// optional piggies
|
|
|
|
|
var showPiggies = 'block';
|
2015-06-03 17:32:50 +02:00
|
|
|
if (piggiesLength === 0) {
|
2017-04-15 07:25:09 +02:00
|
|
|
showPiggies = 'none';
|
2015-06-03 17:32:50 +02:00
|
|
|
}
|
2017-04-15 07:25:09 +02:00
|
|
|
document.getElementById('piggy_bank_id_holder').style.display = showPiggies;
|
2017-01-02 12:18:29 +01:00
|
|
|
break;
|
2017-06-05 11:12:50 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
2015-06-03 17:32:50 +02:00
|
|
|
}
|
2017-07-07 17:51:14 +02:00
|
|
|
// get instructions all the time.
|
|
|
|
|
updateNativeCurrency(useAccountCurrency);
|
|
|
|
|
selectsForeignCurrency();
|
2015-06-03 17:32:50 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
2015-06-03 17:32:50 +02:00
|
|
|
function updateButtons() {
|
|
|
|
|
"use strict";
|
|
|
|
|
$('.switch').each(function (i, v) {
|
|
|
|
|
var button = $(v);
|
|
|
|
|
|
|
|
|
|
// remove click event:
|
|
|
|
|
button.unbind('click');
|
|
|
|
|
// new click event:
|
|
|
|
|
button.bind('click', clickButton);
|
|
|
|
|
|
2017-04-09 07:56:46 +02:00
|
|
|
if (button.data('what') === what) {
|
2015-06-03 17:32:50 +02:00
|
|
|
button.removeClass('btn-default').addClass('btn-info').html('<i class="fa fa-fw fa-check"></i> ' + txt[button.data('what')]);
|
|
|
|
|
} else {
|
|
|
|
|
button.removeClass('btn-info').addClass('btn-default').text(txt[button.data('what')]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
/**
|
2017-04-14 15:56:43 +02:00
|
|
|
*
|
2017-04-14 15:42:54 +02:00
|
|
|
* @param e
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
2015-06-03 17:32:50 +02:00
|
|
|
function clickButton(e) {
|
|
|
|
|
"use strict";
|
|
|
|
|
var button = $(e.target);
|
|
|
|
|
var newWhat = button.data('what');
|
2017-04-09 07:56:46 +02:00
|
|
|
if (newWhat !== what) {
|
2015-06-03 17:32:50 +02:00
|
|
|
what = newWhat;
|
|
|
|
|
updateButtons();
|
|
|
|
|
updateForm();
|
|
|
|
|
updateLayout();
|
2017-01-15 19:07:31 +01:00
|
|
|
updateDescription();
|
2015-06-03 17:32:50 +02:00
|
|
|
}
|
|
|
|
|
return false;
|
2017-04-14 14:37:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get accountID based on some meta info.
|
|
|
|
|
*/
|
|
|
|
|
function getAccountId() {
|
2017-04-14 15:42:54 +02:00
|
|
|
if (what === "withdrawal") {
|
2017-04-14 14:37:04 +02:00
|
|
|
return $('select[name="source_account_id"]').val();
|
|
|
|
|
}
|
2017-04-14 22:25:48 +02:00
|
|
|
if (what === "deposit" || what === "transfer") {
|
2017-04-14 14:48:44 +02:00
|
|
|
return $('select[name="destination_account_id"]').val();
|
|
|
|
|
}
|
2017-06-05 11:12:50 +02:00
|
|
|
return undefined;
|
2017-04-14 15:42:54 +02:00
|
|
|
}
|