diff --git a/public/js/ff/transactions/single/common.js b/public/js/ff/transactions/single/common.js new file mode 100644 index 0000000000..fe86aedf6c --- /dev/null +++ b/public/js/ff/transactions/single/common.js @@ -0,0 +1,125 @@ +/* + * common.js + * Copyright (c) 2017 thegrumpydictator@gmail.com + * This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License. + * + * See the LICENSE file for details. + */ + +$(document).ready(function () { + "use strict"; + setCommonAutocomplete(); + runModernizer(); +}); + +/** + * Give date a datepicker if not natively supported. + */ +function runModernizer() { + if (!Modernizr.inputtypes.date) { + $('input[type="date"]').datepicker( + { + dateFormat: 'yy-mm-dd' + } + ); + } +} + +/** + * Auto complete things in both edit and create routines: + */ +function setCommonAutocomplete() { + $.getJSON('json/tags').done(function (data) { + var opt = { + typeahead: { + source: data, + afterSelect: function () { + this.$element.val(""); + } + } + }; + $('input[name="tags"]').tagsinput( + opt + ); + }); + + if ($('input[name="destination_account_name"]').length > 0) { + $.getJSON('json/expense-accounts').done(function (data) { + $('input[name="destination_account_name"]').typeahead({source: data}); + }); + } + + if ($('input[name="source_account_name"]').length > 0) { + $.getJSON('json/revenue-accounts').done(function (data) { + $('input[name="source_account_name"]').typeahead({source: data}); + }); + } + + $.getJSON('json/categories').done(function (data) { + $('input[name="category"]').typeahead({source: data}); + }); +} + +/** + * When the user changes the currency in the amount drop down, it may jump from being + * the native currency to a foreign currency. This triggers the display of several + * information things that make sure that the user always supplies the amount in the native currency. + * + * @returns {boolean} + */ +function selectsForeignCurrency() { + var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val()); + var selectedAccountId = getAccountId(); + var nativeCurrencyId = parseInt(accountInfo[selectedAccountId].preferredCurrency); + + if (foreignCurrencyId !== nativeCurrencyId) { + console.log('User has selected currency #' + foreignCurrencyId + ' and this is different from native currency #' + nativeCurrencyId); + + // the input where the native amount is entered gets the symbol for the native currency: + $('.non-selectable-currency-symbol').text(currencyInfo[nativeCurrencyId].symbol); + + // the instructions get updated: + $('#ffInput_exchange_rate_instruction').text(getExchangeInstructions()); + + // both holders are shown to the user: + $('#exchange_rate_instruction_holder').show(); + $('#native_amount_holder').show(); + + // if possible the amount is already exchanged for the foreign currency + convertForeignToNative(); + + } + if (foreignCurrencyId === nativeCurrencyId) { + console.log('User has selected currency #' + foreignCurrencyId + ' and this is equal to native currency #' + nativeCurrencyId + ' (phew).'); + $('#exchange_rate_instruction_holder').hide(); + $('#native_amount_holder').hide(); + } + + return false; +} + +/** + * Converts any foreign amount to the native currency. + */ +function convertForeignToNative() { + var accountId = getAccountId(); + var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val()); + var nativeCurrencyId = parseInt(accountInfo[accountId].preferredCurrency); + var foreignCurrencyCode = currencyInfo[foreignCurrencyId].code; + var nativeCurrencyCode = currencyInfo[nativeCurrencyId].code; + var date = $('#ffInput_date').val(); + var amount = $('#ffInput_amount').val(); + var uri = 'json/rate/' + foreignCurrencyCode + '/' + nativeCurrencyCode + '/' + date + '?amount=' + amount; + console.log('Will grab ' + uri); + $.get(uri).done(updateNativeAmount); +} + +/** + * Once the data has been grabbed will update the field in the form. + * @param data + */ +function updateNativeAmount(data) { + console.log('Returned data:'); + console.log(data); + $('#ffInput_native_amount').val(data.amount); +} \ No newline at end of file diff --git a/public/js/ff/transactions/single/create.js b/public/js/ff/transactions/single/create.js index ea3cced69c..a115dae312 100644 --- a/public/js/ff/transactions/single/create.js +++ b/public/js/ff/transactions/single/create.js @@ -16,7 +16,6 @@ $(document).ready(function () { updateForm(); updateLayout(); updateDescription(); - runModernizer(); updateNativeCurrency(); // verify native currency by first account (may be different). // hide ALL exchange things @@ -31,37 +30,8 @@ $(document).ready(function () { // when user selects different currency, $('.currency-option').on('click', selectsForeignCurrency); - - // get JSON things: - getJSONautocomplete(); }); -/** - * Converts any foreign amount to the native currency. - */ -function convertForeignToNative() { - var accountId = getAccountId(); - var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val()); - var nativeCurrencyId = parseInt(accountInfo[accountId].preferredCurrency); - var foreignCurrencyCode = currencyInfo[foreignCurrencyId].code; - var nativeCurrencyCode = currencyInfo[nativeCurrencyId].code; - var date = $('#ffInput_date').val(); - var amount = $('#ffInput_amount').val(); - var uri = 'json/rate/' + foreignCurrencyCode + '/' + nativeCurrencyCode + '/' + date + '?amount=' + amount; - console.log('Will grab ' + uri); - $.get(uri).done(updateNativeAmount); -} - -/** - * Once the data has been grabbed will update the field in the form. - * @param data - */ -function updateNativeAmount(data) { - console.log('Returned data:'); - console.log(data); - $('#ffInput_native_amount').val(data.amount); -} - /** * This function generates a small helper text to explain the user * that they have selected a foreign currency. @@ -78,45 +48,7 @@ function getExchangeInstructions() { return text; } -/** - * When the user changes the currency in the amount drop down, it may jump from being - * the native currency to a foreign currency. This triggers the display of several - * information things that make sure that the user always supplies the amount in the native currency. - * - * @returns {boolean} - */ -function selectsForeignCurrency() { - var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val()); - var selectedAccountId = getAccountId(); - var nativeCurrencyId = parseInt(accountInfo[selectedAccountId].preferredCurrency); - if (foreignCurrencyId !== nativeCurrencyId) { - console.log('User has selected currency #' + foreignCurrencyId + ' and this is different from native currency #' + nativeCurrencyId); - - // the input where the native amount is entered gets the symbol for the native currency: - $('.non-selectable-currency-symbol').text(currencyInfo[nativeCurrencyId].symbol); - - // the instructions get updated: - $('#ffInput_exchange_rate_instruction').text(getExchangeInstructions()); - - // both holders are shown to the user: - $('#exchange_rate_instruction_holder').show(); - $('#native_amount_holder').show(); - - // if possible the amount is already exchanged for the foreign currency - convertForeignToNative(); - - } - if (foreignCurrencyId === nativeCurrencyId) { - console.log('User has selected currency #' + foreignCurrencyId + ' and this is equal to native currency #' + nativeCurrencyId + ' (phew).'); - $('#exchange_rate_instruction_holder').hide(); - $('#native_amount_holder').hide(); - } - - // if the value of the selected currency does not match the account's currency - // show the exchange rate thing! - return false; -} /** * There is an input that shows the currency symbol that is native to the selected @@ -142,45 +74,6 @@ function updateDescription() { }); } -/** - * - */ -function getJSONautocomplete() { - - // for withdrawals - $.getJSON('json/expense-accounts').done(function (data) { - $('input[name="destination_account_name"]').typeahead({source: data}); - }); - - // for tags: - if ($('input[name="tags"]').length > 0) { - $.getJSON('json/tags').done(function (data) { - - var opt = { - typeahead: { - source: data, - afterSelect: function () { - this.$element.val(""); - } - } - }; - $('input[name="tags"]').tagsinput( - opt - ); - }); - } - - // for deposits - $.getJSON('json/revenue-accounts').done(function (data) { - $('input[name="source_account_name"]').typeahead({source: data}); - }); - - $.getJSON('json/categories').done(function (data) { - $('input[name="category"]').typeahead({source: data}); - }); - -} - /** * */ @@ -288,7 +181,7 @@ function updateButtons() { } /** - * + * * @param e * @returns {boolean} */ @@ -318,16 +211,3 @@ function getAccountId() { } alert('Cannot handle ' + what); } - -/** - * - */ -function runModernizer() { - if (!Modernizr.inputtypes.date) { - $('input[type="date"]').datepicker( - { - dateFormat: 'yy-mm-dd' - } - ); - } -} \ No newline at end of file diff --git a/public/js/ff/transactions/single/edit.js b/public/js/ff/transactions/single/edit.js index d722c3adb8..bb05169fa2 100644 --- a/public/js/ff/transactions/single/edit.js +++ b/public/js/ff/transactions/single/edit.js @@ -12,7 +12,6 @@ $(document).ready(function () { "use strict"; - runModernizer(); setAutocompletes(); updateInitialPage(); @@ -38,69 +37,7 @@ function updateInitialPage() { } } -/** - * When the user changes the currency in the amount drop down, it may jump from being - * the native currency to a foreign currency. This triggers the display of several - * information things that make sure that the user always supplies the amount in the native currency. - * - * @returns {boolean} - */ -function selectsForeignCurrency() { - var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val()); - var selectedAccountId = getAccountId(); - var nativeCurrencyId = parseInt(accountInfo[selectedAccountId].preferredCurrency); - if (foreignCurrencyId !== nativeCurrencyId) { - console.log('User has selected currency #' + foreignCurrencyId + ' and this is different from native currency #' + nativeCurrencyId); - - // the input where the native amount is entered gets the symbol for the native currency: - $('.non-selectable-currency-symbol').text(currencyInfo[nativeCurrencyId].symbol); - - // the instructions get updated: - $('#ffInput_exchange_rate_instruction').text(getExchangeInstructions()); - - // both holders are shown to the user: - $('#exchange_rate_instruction_holder').show(); - $('#native_amount_holder').show(); - - // if possible the amount is already exchanged for the foreign currency - convertForeignToNative(); - - } - if (foreignCurrencyId === nativeCurrencyId) { - console.log('User has selected currency #' + foreignCurrencyId + ' and this is equal to native currency #' + nativeCurrencyId + ' (phew).'); - $('#exchange_rate_instruction_holder').hide(); - $('#native_amount_holder').hide(); - } - - return false; -} - -/** - * Converts any foreign amount to the native currency. - */ -function convertForeignToNative() { - var accountId = getAccountId(); - var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val()); - var nativeCurrencyId = parseInt(accountInfo[accountId].preferredCurrency); - var foreignCurrencyCode = currencyInfo[foreignCurrencyId].code; - var nativeCurrencyCode = currencyInfo[nativeCurrencyId].code; - var date = $('#ffInput_date').val(); - var amount = $('#ffInput_amount').val(); - var uri = 'json/rate/' + foreignCurrencyCode + '/' + nativeCurrencyCode + '/' + date + '?amount=' + amount; - console.log('Will grab ' + uri); - $.get(uri).done(updateNativeAmount); -} - -/** - * Once the data has been grabbed will update the field in the form. - * @param data - */ -function updateNativeAmount(data) { - console.log('Returned data:'); - console.log(data); - $('#ffInput_native_amount').val(data.amount); -} /** * Get accountID based on some meta info. @@ -116,58 +53,13 @@ function getAccountId() { alert('Cannot handle ' + journal.transaction_type.type); } - -/** - * Give date a datepicker if not natively supported. - */ -function runModernizer() { - if (!Modernizr.inputtypes.date) { - $('input[type="date"]').datepicker( - { - dateFormat: 'yy-mm-dd' - } - ); - } -} - /** * Set the auto-complete JSON things. */ function setAutocompletes() { - if ($('input[name="destination_account_name"]').length > 0) { - $.getJSON('json/expense-accounts').done(function (data) { - $('input[name="destination_account_name"]').typeahead({source: data}); - }); - } - - $.getJSON('json/tags').done(function (data) { - var opt = { - typeahead: { - source: data, - afterSelect: function () { - this.$element.val(""); - } - } - }; - $('input[name="tags"]').tagsinput( - opt - ); - }); - - if ($('input[name="source_account_name"]').length > 0) { - $.getJSON('json/revenue-accounts').done(function (data) { - $('input[name="source_account_name"]').typeahead({source: data}); - }); - } - $.getJSON('json/transaction-journals/' + what).done(function (data) { $('input[name="description"]').typeahead({source: data}); }); - - - $.getJSON('json/categories').done(function (data) { - $('input[name="category"]').typeahead({source: data}); - }); } /** diff --git a/resources/views/transactions/single/create.twig b/resources/views/transactions/single/create.twig index c64b46d047..a2662e2cbf 100644 --- a/resources/views/transactions/single/create.twig +++ b/resources/views/transactions/single/create.twig @@ -232,6 +232,7 @@ + {% endblock %} diff --git a/resources/views/transactions/single/edit.twig b/resources/views/transactions/single/edit.twig index 53ff1b9ea5..f1110fd358 100644 --- a/resources/views/transactions/single/edit.twig +++ b/resources/views/transactions/single/edit.twig @@ -246,6 +246,7 @@ var journalData = {{ data|json_encode|raw }}; var exchangeRateInstructions = "{{ 'exchange_rate_instructions'|_|escape('js') }}"; + {% endblock %}