Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Add new shared methods.
James Colef49cc78cc08e
diff --git a/resources/assets/v3/js/pages/transactions/create.js b/resources/assets/v3/js/pages/transactions/create.jsindex 5224c60815..45ca6ac9e1 100644--- a/resources/assets/v3/js/pages/transactions/create.js+++ b/resources/assets/v3/js/pages/transactions/create.js@@ -54,9 +54,12 @@ import {onMapZoom} from "./shared/on-map-zoom.js";import {clearLocation} from './shared/clear-location.js';import {removeSplit} from "./shared/remove-split.js";import {loadTransactionLinks} from './shared/load-transaction-links.js';-import Autocomplete from "bootstrap5-autocomplete";-import formatMoney from "../../util/format-money.js";-import {format} from "date-fns";+import {createLinkAutocomplete} from "./shared/create-link-autocomplete.js";+import {editLink} from "./shared/edit-link.js";+import {switchLink} from "./shared/switch-link.js";+import {removeLink} from "./shared/remove-link.js";+import {saveEditedLink} from "./shared/save-edited-link.js";+import {saveNewLink} from "./shared/save-new-link.js";let create = function () {return {@@ -181,6 +184,12 @@ let create = function () {onMapClick: onMapClick,onMapZoom: onMapZoom,clearLocation: clearLocation,+ createLinkAutocomplete: createLinkAutocomplete,+ editLink: editLink,+ switchLink: switchLink,+ removeLink: removeLink,+ saveEditedLink: saveEditedLink,+ saveNewLink: saveNewLink,filterForeignCurrencies(code) {let list = [];@@ -251,115 +260,6 @@ let create = function () {// TODO don't do this on a timeout!}, 500);},- switchLink(e) {- let rowIndex = parseInt(e.currentTarget.dataset.rowIndex);- let index = parseInt(e.currentTarget.dataset.index);- let linkTypeId = parseInt(this.links[index][rowIndex].link_type.split('_')[0]);- let linkTypeDirection = this.links[index][rowIndex].link_type.split('_')[1];- let linkType = this.formData.linkTypes.find(link => link.id === linkTypeId);- // switch link type.- linkTypeDirection = 'inward' === linkTypeDirection ? 'outward' : 'inward';- if (typeof linkType === 'undefined') {- console.error('Link type not found for id ' + linkTypeId);- console.log(this.formData.linkTypes);- return;- }- this.links[index][rowIndex].link_type = linkType.id + '_' + linkTypeDirection;- this.links[index][rowIndex].link_type_label = linkType[linkTypeDirection];- this.links[index][rowIndex].link_type_direction = linkTypeDirection;- },- removeLink(e) {- let rowIndex = parseInt(e.currentTarget.dataset.rowIndex);- let index = parseInt(e.currentTarget.dataset.index);- this.links[index].splice(rowIndex, 1);- },- saveEditedLink(e) {- let rowIndex = parseInt(e.currentTarget.dataset.rowIndex);- let index = parseInt(e.currentTarget.dataset.index);- let selector = e.currentTarget.parentNode.querySelector('select[name="new-link-type"]');-- let linkType = selector.value;- let linkTypeId = parseInt(linkType.split('_')[0]);- let linkTypeDirection = linkType.split('_')[1];- let linkTypeObj = this.formData.linkTypes.find(link => link.id === linkTypeId);- if (typeof linkTypeObj === 'undefined') {- console.error('Link type not found for id ' + linkTypeId);- return;- }- this.links[index][rowIndex].link_type = linkTypeId + '_' + linkTypeDirection;- this.links[index][rowIndex].link_type_id = linkTypeId;- this.links[index][rowIndex].link_type_direction = linkTypeDirection;- this.links[index][rowIndex].link_type_label = linkTypeObj[linkTypeDirection];- this.links[index][rowIndex].editMode = false;- },- editLink(e) {- let rowIndex = parseInt(e.currentTarget.dataset.rowIndex);- let index = parseInt(e.currentTarget.dataset.index);- this.links[index][rowIndex].editMode = true;- },- saveNewLink(e) {- let index = parseInt(e.currentTarget.dataset.index);-- let linkSelect = document.getElementById('link_type_id_' + index);- let linkType = linkSelect.value;- let linkLabel = linkSelect.options[linkSelect.selectedIndex].innerHTML;- let searchBox = document.getElementById('links_modal_search_' + index);- let hiddenField = searchBox.parentNode.querySelector('input[name="search"]');-- let linkTypeId = parseInt(linkType.split('_')[0]);- let linkTypeDirection = linkType.split('_')[1];- let linkTypeObj = this.formData.linkTypes.find(link => link.id === linkTypeId);-- if ('' === linkType || '' === hiddenField.value || '' === searchBox.value) {- return;- }-- // add entry to temporary table.- console.log('Link ' + linkType + ' ("' + linkLabel + '") to transaction #' + hiddenField.value + '("' + searchBox.value + '")');-- this.links[index].push(- {- id: 0,- link_type: linkTypeId + '_' + linkTypeDirection,- link_type_id: linkTypeId,- link_type_direction: linkTypeDirection,- link_type_label: linkTypeObj[linkTypeDirection],- journal_id: hiddenField.value,- group_id: hiddenField.value,- journal_description: searchBox.value,- editMode: false,- stored: false,- }- );- searchBox.value = '';- hiddenField.value = '';- },-- createLinkAutocomplete(fieldIdentifier, url) {- let token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');-- const renderJournal = function (item, b, c) {- return item.description + '<br><small class="text-muted">' + formatMoney(item.amount, item.currency_code) + ' @ ' + format(new Date(item.date), this.i18next.t('config.date_time_fns')) + '</small>';- };- Autocomplete.init('#' + fieldIdentifier, {-- server: url + '?_token=' + token,- labelField: 'name',- hiddenInput: true,- valueField: 'id',- liveServer: true,- onRenderItem: renderJournal.bind(this),- fetchOptions: {- method: 'GET',- credentials: 'include',- headers: {- 'Content-Type': 'application/json',- 'Accept': 'application/json',- 'X-CSRF-TOKEN': token- }- }- });- },init() {this.i18next = i18next;diff --git a/resources/assets/v3/js/pages/transactions/edit.js b/resources/assets/v3/js/pages/transactions/edit.jsindex 943616514f..f5767096c2 100644--- a/resources/assets/v3/js/pages/transactions/edit.js+++ b/resources/assets/v3/js/pages/transactions/edit.js@@ -56,6 +56,12 @@ import {onMapZoom} from './shared/on-map-zoom.js';import {clearLocation} from './shared/clear-location.js';import {removeSplit} from "./shared/remove-split.js";import {loadTransactionLinks} from "./shared/load-transaction-links.js";+import {createLinkAutocomplete} from "./shared/create-link-autocomplete.js";+import {editLink} from "./shared/edit-link.js";+import {switchLink} from "./shared/switch-link.js";+import {removeLink} from "./shared/remove-link.js";+import {saveEditedLink} from "./shared/save-edited-link.js";+import {saveNewLink} from "./shared/save-new-link.js";const urls = getUrls();@@ -158,6 +164,12 @@ let transactions = function () {onMapZoom: onMapZoom,clearLocation: clearLocation,removeSplit: removeSplit,+ createLinkAutocomplete: createLinkAutocomplete,+ editLink: editLink,+ switchLink: switchLink,+ removeLink: removeLink,+ saveEditedLink: saveEditedLink,+ saveNewLink: saveNewLink,// part of the account selection auto-complete@@ -298,7 +310,7 @@ let transactions = function () {//this.formData.linkTypes = data;// TODO fix for all mdodals.this.formStates.loadingLinks = false;- this.createAutocomplete('links_modal_search_0', 'api/v1/autocomplete/transactions-with-meta');+ this.createLinkAutocomplete('links_modal_search_0', 'api/v1/autocomplete/transactions-with-meta');});}diff --git a/resources/assets/v3/js/pages/transactions/shared/create-link-autocomplete.js b/resources/assets/v3/js/pages/transactions/shared/create-link-autocomplete.jsnew file mode 100644index 0000000000..27d79c3c47--- /dev/null+++ b/resources/assets/v3/js/pages/transactions/shared/create-link-autocomplete.js@@ -0,0 +1,49 @@+/*+ * cr.js+ * Copyright (c) 2026 [email protected]+ *+ * This file is part of Firefly III (https://github.com/firefly-iii).+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program 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 Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */++import Autocomplete from "bootstrap5-autocomplete";+import formatMoney from "../../../util/format-money.js";+import {format} from "date-fns";++export function createLinkAutocomplete(fieldIdentifier, url) {+ let token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');++ const renderJournal = function (item, b, c) {+ return item.description + '<br><small class="text-muted">' + formatMoney(item.amount, item.currency_code) + ' @ ' + format(new Date(item.date), this.i18next.t('config.date_time_fns')) + '</small>';+ };+ Autocomplete.init('#' + fieldIdentifier, {++ server: url + '?_token=' + token,+ labelField: 'name',+ hiddenInput: true,+ valueField: 'id',+ liveServer: true,+ onRenderItem: renderJournal.bind(this),+ fetchOptions: {+ method: 'GET',+ credentials: 'include',+ headers: {+ 'Content-Type': 'application/json',+ 'Accept': 'application/json',+ 'X-CSRF-TOKEN': token+ }+ }+ });+};diff --git a/resources/assets/v3/js/pages/transactions/shared/edit-link.js b/resources/assets/v3/js/pages/transactions/shared/edit-link.jsnew file mode 100644index 0000000000..7d69a01140--- /dev/null+++ b/resources/assets/v3/js/pages/transactions/shared/edit-link.js@@ -0,0 +1,26 @@+/*+ * edit-link.js+ * Copyright (c) 2026 [email protected]+ *+ * This file is part of Firefly III (https://github.com/firefly-iii).+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program 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 Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */+++export function editLink(e) {+ let rowIndex = parseInt(e.currentTarget.dataset.rowIndex);+ let index = parseInt(e.currentTarget.dataset.index);+ this.links[index][rowIndex].editMode = true;+};diff --git a/resources/assets/v3/js/pages/transactions/shared/remove-link.js b/resources/assets/v3/js/pages/transactions/shared/remove-link.jsnew file mode 100644index 0000000000..000938230d--- /dev/null+++ b/resources/assets/v3/js/pages/transactions/shared/remove-link.js@@ -0,0 +1,25 @@+/*+ * remove-link.js+ * Copyright (c) 2026 [email protected]+ *+ * This file is part of Firefly III (https://github.com/firefly-iii).+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program 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 Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */++export function removeLink(e) {+ let rowIndex = parseInt(e.currentTarget.dataset.rowIndex);+ let index = parseInt(e.currentTarget.dataset.index);+ this.links[index].splice(rowIndex, 1);+};diff --git a/resources/assets/v3/js/pages/transactions/shared/save-edited-link.js b/resources/assets/v3/js/pages/transactions/shared/save-edited-link.jsnew file mode 100644index 0000000000..a4976b7de9--- /dev/null+++ b/resources/assets/v3/js/pages/transactions/shared/save-edited-link.js@@ -0,0 +1,39 @@+/*+ * save-edited-link.js+ * Copyright (c) 2026 [email protected]+ *+ * This file is part of Firefly III (https://github.com/firefly-iii).+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program 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 Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */++export function saveEditedLink(e) {+ let rowIndex = parseInt(e.currentTarget.dataset.rowIndex);+ let index = parseInt(e.currentTarget.dataset.index);+ let selector = e.currentTarget.parentNode.querySelector('select[name="new-link-type"]');++ let linkType = selector.value;+ let linkTypeId = parseInt(linkType.split('_')[0]);+ let linkTypeDirection = linkType.split('_')[1];+ let linkTypeObj = this.formData.linkTypes.find(link => link.id === linkTypeId);+ if (typeof linkTypeObj === 'undefined') {+ console.error('Link type not found for id ' + linkTypeId);+ return;+ }+ this.links[index][rowIndex].link_type = linkTypeId + '_' + linkTypeDirection;+ this.links[index][rowIndex].link_type_id = linkTypeId;+ this.links[index][rowIndex].link_type_direction = linkTypeDirection;+ this.links[index][rowIndex].link_type_label = linkTypeObj[linkTypeDirection];+ this.links[index][rowIndex].editMode = false;+};diff --git a/resources/assets/v3/js/pages/transactions/shared/save-new-link.js b/resources/assets/v3/js/pages/transactions/shared/save-new-link.jsnew file mode 100644index 0000000000..f08bcd3bbd--- /dev/null+++ b/resources/assets/v3/js/pages/transactions/shared/save-new-link.js@@ -0,0 +1,57 @@+/*+ * save-new-link.js+ * Copyright (c) 2026 [email protected]+ *+ * This file is part of Firefly III (https://github.com/firefly-iii).+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program 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 Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */++export function saveNewLink(e) {+ let index = parseInt(e.currentTarget.dataset.index);++ let linkSelect = document.getElementById('link_type_id_' + index);+ let linkType = linkSelect.value;+ let linkLabel = linkSelect.options[linkSelect.selectedIndex].innerHTML;+ let searchBox = document.getElementById('links_modal_search_' + index);+ let hiddenField = searchBox.parentNode.querySelector('input[name="search"]');++ let linkTypeId = parseInt(linkType.split('_')[0]);+ let linkTypeDirection = linkType.split('_')[1];+ let linkTypeObj = this.formData.linkTypes.find(link => link.id === linkTypeId);++ if ('' === linkType || '' === hiddenField.value || '' === searchBox.value) {+ return;+ }++ // add entry to temporary table.+ console.log('Link ' + linkType + ' ("' + linkLabel + '") to transaction #' + hiddenField.value + '("' + searchBox.value + '")');++ this.links[index].push(+ {+ id: 0,+ link_type: linkTypeId + '_' + linkTypeDirection,+ link_type_id: linkTypeId,+ link_type_direction: linkTypeDirection,+ link_type_label: linkTypeObj[linkTypeDirection],+ journal_id: hiddenField.value,+ group_id: hiddenField.value,+ journal_description: searchBox.value,+ editMode: false,+ stored: false,+ }+ );+ searchBox.value = '';+ hiddenField.value = '';+};diff --git a/resources/assets/v3/js/pages/transactions/shared/switch-link.js b/resources/assets/v3/js/pages/transactions/shared/switch-link.jsnew file mode 100644index 0000000000..6f34e3736d--- /dev/null+++ b/resources/assets/v3/js/pages/transactions/shared/switch-link.js@@ -0,0 +1,38 @@+/*+ * switch-link.js+ * Copyright (c) 2026 [email protected]+ *+ * This file is part of Firefly III (https://github.com/firefly-iii).+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program 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 Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ */++export function switchLink(e) {+ let rowIndex = parseInt(e.currentTarget.dataset.rowIndex);+ let index = parseInt(e.currentTarget.dataset.index);+ let linkTypeId = parseInt(this.links[index][rowIndex].link_type.split('_')[0]);+ let linkTypeDirection = this.links[index][rowIndex].link_type.split('_')[1];+ let linkType = this.formData.linkTypes.find(link => link.id === linkTypeId);+ // switch link type.+ linkTypeDirection = 'inward' === linkTypeDirection ? 'outward' : 'inward';+ if (typeof linkType === 'undefined') {+ console.error('Link type not found for id ' + linkTypeId);+ console.log(this.formData.linkTypes);+ return;+ }+ this.links[index][rowIndex].link_type = linkType.id + '_' + linkTypeDirection;+ this.links[index][rowIndex].link_type_label = linkType[linkTypeDirection];+ this.links[index][rowIndex].link_type_direction = linkTypeDirection;+};+