Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Further optimize account list.
James Cole3cbdf8faf0c8
diff --git a/resources/assets/v3/js/pages/accounts/index.js b/resources/assets/v3/js/pages/accounts/index.jsindex 87548f76a0..da2c14bf6d 100644--- a/resources/assets/v3/js/pages/accounts/index.js+++ b/resources/assets/v3/js/pages/accounts/index.js@@ -22,19 +22,20 @@ import "../../boot/bootstrap.js";import sidebar from "../shared/sidebar.js";import dates from "../shared/dates.js";import Alpine from "@alpinejs/csp";-import { getVariable } from "../../store/get-variable.js";+import {getVariable} from "../../store/get-variable.js";import Put from "../../api/model/account/put.js";import Get from "../../api/model/account/get.js";import format from "../../util/format.js";import formatMoney from "../../util/format-money.js";import i18next from "i18next";-import { addDrag } from "../shared/drag-and-droppable-rows.js";-import {sortableTable} from "../shared/sortable-tables.js";+import {addDrag} from "../shared/drag-and-droppable-rows.js";+import {sortableTable, storeFilterAndSort, updateHistory} from "../shared/sortable-tables.js";window.enableDates = true;let index = function () {return {+ // necessary for indexlistPageSize: 50,objectType: "invalid",accounts: [],@@ -60,125 +61,113 @@ let index = function () {// variables necessary for sorting tables.handleFunc: null,pageNavUrl: "./accounts/",- sortColumn: "order",- sortDirection: "asc",+ sortColumn: "order", // used in GET request.+ sortDirection: "asc", // used in GET request.loadingNewSort: true,isFiltering: false,filter: {},- // sort functions+ // sort functions, imported+ updateHistory: updateHistory,+ storeFilterAndSort: storeFilterAndSort,sortableTable: null,- objectToQueryString(obj, prefix) {- if (null === obj) {- return "";- }- return Object.keys(obj)- .map((objKey) => {- if (Object.hasOwn(obj, objKey)) {- const key = prefix ? `${prefix}[${objKey}]` : objKey;- const value = obj[objKey];-- return typeof value === "object"- ? this.objectToQueryString(value, key)- : `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;- }-- return null;- })- .join("&");- },-- updateHistory() {- if (history.pushState) {- // TODO fix this.- let obj = {- page: this.page,- column: this.sortColumn,- direction: this.sortDirection,- filter: this.filter,- };- let string = this.objectToQueryString(obj);- let newUrl =- window.location.protocol + "//" + window.location.host + window.location.pathname + "?" + string;- window.history.pushState({ path: newUrl }, "", newUrl);- window.store.set(this.storageKey, { column: this.sortColumn, direction: this.sortDirection });- }- },- updateFilterValue(field, newValue) {- console.log("Update", field, newValue);- this.filter[field] = newValue;- if ("" !== newValue) {- this.isFiltering = true;- }- if ("" === newValue) {- this.isFiltering = false;- }- this.updateHistory();- this.downloadAccounts();- },+ // objectToQueryString(obj, prefix) {+ // if (null === obj) {+ // return "";+ // }+ // return Object.keys(obj)+ // .map((objKey) => {+ // if (Object.hasOwn(obj, objKey)) {+ // const key = prefix ? `${prefix}[${objKey}]` : objKey;+ // const value = obj[objKey];+ //+ // return typeof value === "object"+ // ? this.objectToQueryString(value, key)+ // : `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;+ // }+ //+ // return null;+ // })+ // .join("&");+ // },++ // updateFilterValue(field, newValue) {+ // console.log("Update", field, newValue);+ // this.filter[field] = newValue;+ // if ("" !== newValue) {+ // this.isFiltering = true;+ // }+ // if ("" === newValue) {+ // this.isFiltering = false;+ // }+ // this.updateHistory();+ // this.downloadAccounts();+ // },init() {- const page = window.location.href.split("?")[0].split("/");- this.objectType = page[page.length - 1].substring(0, 15);++ // prepare some variables:+ const address = window.location.href.split("?")[0].split("/");+ this.objectType = address[address.length - 1].substring(0, 15);this.i18next = i18next;this.anonymous = window.store.get("anonymous");this.storageKey = "accounts-" + this.objectType + (this.active ? "-active" : "-inactive");- this.handleFunc = this.handlePageClick.bind(this);this.pageNavUrl = "./accounts/" + this.objectType;-- if ("expense" === this.objectType || "revenue" === this.objectType) {- this.defaultSortColumn = "name";- }-- // sort and paging settings.- if ("inactive-accounts" === page[page.length - 2]) {- this.active = false;- }+ this.defaultSortColumn = "expense" === this.objectType || "revenue" === this.objectType ? "name" : this.defaultSortColumn;+ this.active = "inactive-accounts" !== address[address.length - 2];let fromStore = window.store.get(this.storageKey);- console.log("from store", fromStore);if (fromStore) {this.defaultSortColumn = fromStore.column;this.defaultSortDirection = fromStore.direction;+ this.filter = fromStore.filter;+ console.log('Restore from store:', fromStore);}- // document.querySelectorAll('table[data-sort-identifier="main"] thead th[data-filter-column]').forEach((th) => {- // let column = th.getAttribute('data-filter-column');- // console.log('[index] Will watch filter for column "' + column + '"');- // this.$watch('filter.' + column, (newValue, oldValue) => {- // console.log('filter for column "' + column + '" changed from "' + oldValue + '" to "' + newValue + '"');- // });- // });--const params = new Proxy(new URLSearchParams(window.location.search), {get: (searchParams, prop) => searchParams.get(prop),});- this.sortColumn = params.column ?? this.defaultSortColumn;- this.sortDirection = params.direction ?? this.defaultSortDirection;- this.page = parseInt(params.page) || 1;+ this.page = parseInt(params.page || '1');+ this.defaultSortColumn = params.column ?? this.defaultSortColumn;+ this.defaultSortDirection = params.direction ?? this.defaultSortDirection;++ console.log('Restore from params:', {+ page: this.page,+ column: this.defaultSortColumn,+ direction: this.defaultSortDirection+ });+this.sortableTable = new sortableTable('main');- this.sortableTable.sortColumn = params.column ?? this.defaultSortColumn;- this.sortableTable.sortDirection = params.direction ?? this.defaultSortDirection;+ this.sortableTable.sortColumn = this.defaultSortColumn;+ this.sortableTable.storageKey = this.storageKey;+ this.sortableTable.sortDirection = this.defaultSortDirection;this.sortableTable.page = this.page;+this.sortableTable.init(this);+ //updateHistory(this.page, this.defaultSortColumn, this.defaultSortDirection, this.filter);+// watch for changes in the sortable table instructions.document.addEventListener('sortable-table-sort-change', (event) => {console.log('sortable-table-sort-change', event.detail);this.sortColumn = event.detail.sortColumn;this.sortDirection = event.detail.sortDirection;- // this.page = event.detail.page;- this.updateHistory();+ this.updateHistory(this.page, this.sortColumn, this.sortDirection, this.filter);+ this.storeFilterAndSort(this.storageKey, this.sortColumn, this.sortDirection, this.filter);this.downloadAccounts();});-- // grab the account list.this.downloadAccounts();+ console.log(this.isFiltering);+ // grab the account list.+ // document.addEventListener('alpine:initialized', (function () {+ // console.log('Init!');+ //+ // }).bind(this));+ // respond to drag and drop.getVariable("listPageSize").then((listPageSize) => {this.listPageSize = listPageSize;@@ -189,11 +178,10 @@ let index = function () {let item = e.detail[i];if (item.order !== item.currentOrder) {// PUT new order to system.- new Put().put({ order: item.order }, { id: item.id });+ new Put().put({order: item.order}, {id: item.id});+// save new order as current order in the row.- document- .querySelector(`tr[data-id="${item.id}"]`)- .setAttribute("data-current-order", item.order);+ document.querySelector(`tr[data-id="${item.id}"]`).setAttribute("data-current-order", item.order);}}}@@ -235,6 +223,7 @@ let index = function () {let start = window.store.get("start");let end = window.store.get("end");this.convertToPrimary = window.store.get("convert_to_primary");+ console.log('Download accounts using', JSON.parse(JSON.stringify(this.filter)));new Get().list({active: this.active,@@ -242,7 +231,7 @@ let index = function () {page: this.page,type: this.objectType,start: start,- filter: this.filter,+ filter: JSON.parse(JSON.stringify(this.filter)),end: end,}).then((response) => {@@ -323,8 +312,10 @@ let index = function () {this.accounts.push(account);}}+ console.log('Done downloading accounts.', JSON.parse(JSON.stringify(this.filter)), this.accounts.length);this.loadingPage = false;this.loadingNewSort = false;+ this.sortableTable.disableRefresh = false;if (0 === this.accounts.length && false === this.isFiltering) {document.querySelectorAll(".data-holder").forEach((el) => {el.classList.add("d-none");@@ -344,7 +335,7 @@ let index = function () {}return format(new Date(date),- i18next.t("config.date_time_fns_short", { lng: window.store.get("locale") }),+ i18next.t("config.date_time_fns_short", {lng: window.store.get("locale")}),window.store.get("locale"),);},diff --git a/resources/assets/v3/js/pages/shared/sortable-tables.js b/resources/assets/v3/js/pages/shared/sortable-tables.jsindex 478bcb4dac..9a5c1442c9 100644--- a/resources/assets/v3/js/pages/shared/sortable-tables.js+++ b/resources/assets/v3/js/pages/shared/sortable-tables.js@@ -22,7 +22,8 @@ export class sortableTable {tableId = '';sortColumn = 'order';sortDirection = 'asc';- isFiltering = false;+ storageKey = '';+ disableRefresh = false;constructor(tableId) {this.tableId = tableId;@@ -30,22 +31,18 @@ export class sortableTable {}init(parent) {- const params = new Proxy(new URLSearchParams(window.location.search), {- get: (searchParams, prop) => searchParams.get(prop),- });- // shitty solution but for now it works.- // if ("" !== params["filter[name]"]) {- // this.isFiltering = true;- // }-queueMicrotask(() => {this.updateHeaderClasses();this.addClickEvents();- this.addWatch(parent);});+ this.addWatch(parent);}addWatch(parent) {+ const params = new Proxy(new URLSearchParams(window.location.search), {+ get: (searchParams, prop) => searchParams.get(prop),+ });+// add a watch for sortColumn, sortDirection and pageparent.$watch('sortColumn', (newValue, oldValue) => {console.log('sortColumn changed from "' + oldValue + '" to "' + newValue + '"');@@ -62,11 +59,28 @@ export class sortableTable {document.querySelectorAll('table[data-sort-identifier="' + this.tableId + '"] thead th[data-filter-column]').forEach((th) => {let column = th.getAttribute('data-filter-column');console.log('[class] Will watch filter for column "' + column + '"');++ // also grab current filter value from store AND from url and push back to the parent whichever is set.+ const key = 'filter[' + column + ']';+ let urlValue = params[key] ?? null;+ let fromStore = window.store.get(this.storageKey);+ let storeValue = fromStore ? fromStore.filter[column] ?? null : null;+ let valueToUse = null !== urlValue ? urlValue : storeValue;+parent.$watch('filter.' + column, (newValue, oldValue) => {// parent.filter[column] = newValue;console.log('filter for column "' + column + '" changed from "' + oldValue + '" to "' + newValue + '"');this.fireSortChangeEvent();});++ if (null !== valueToUse) {+ console.log('DISABLE refresh');+ this.disableRefresh = true;+ parent.isFiltering = true;+ console.log('Setting initial filter value for column "' + column + '" to "' + valueToUse + '"');+ parent.filter[column] = valueToUse;++ }});}@@ -84,6 +98,7 @@ export class sortableTable {//this.updateHistory();//this.downloadAccounts();}+fireSortChangeEvent() {this.updateHeaderClasses();let obj = {@@ -95,15 +110,25 @@ export class sortableTable {let event = new CustomEvent('sortable-table-sort-change', {detail: obj});- document.dispatchEvent(event);+ if(!this.disableRefresh) {+ console.log('Fire event!');+ document.dispatchEvent(event);+ }+ if(this.disableRefresh) {+ console.log('Do NOT fire event!');+ this.disableRefresh = false;+ }}+clickCloseButton(event) {let th = event.currentTarget.parentNode.parentNode.parentNode;let column = th.dataset.filterColumn;console.log('close again', column);th.querySelector('.search-filter').classList.add('d-none');th.querySelector('.title').classList.remove('d-none');+ th.querySelector('span.search-spacer').classList.remove("d-none");th.querySelector('.search-button').classList.remove('d-none');+ // console.log(th.querySelector('.form-control').value);th.querySelector('.form-control').value = '';this.fireSortChangeEvent();@@ -116,81 +141,69 @@ export class sortableTable {});// create search buttons and inputs.- document.querySelectorAll('table[data-sort-identifier="' + this.tableId + '"] th[data-filter-column]').forEach((el) => {-- let column = el.dataset.filterColumn;- // create filter inset.- let inset = document.createElement('span');- inset.classList.add('search-inset');-- // add spacer- let searchSpacer = document.createElement('span');- searchSpacer.classList.add('search-spacer');- searchSpacer.innerHTML = ' ';- inset.appendChild(searchSpacer);-- // make button.- let searchButton = document.createElement('em');- searchButton.classList.add('search-button', 'bi', 'bi-search');- searchButton.setAttribute('data-filter-column', column);- inset.appendChild(searchButton);-- // add hidden div- let div = document.createElement('div');- div.classList.add('d-none', 'input-group', 'search-filter');- // append input to div.- let input = document.createElement('input');- input.classList.add('form-control', 'form-control-sm');- input.setAttribute('type', 'search');- input.setAttribute('placeholder', 'TODO placeholder');- input.setAttribute('x-model', 'filter.' + column);- div.appendChild(input);-- // create button- let button = document.createElement('button');- button.classList.add('btn', 'btn-outline-secondary', 'btn-sm', 'hide-button');- button.setAttribute('type', 'button');- button.addEventListener('click', this.clickCloseButton.bind(this));- let icon = document.createElement('em');- icon.classList.add('bi', 'bi-x', 'text-danger');- // append icon to button- button.appendChild(icon);- // add button to div- div.appendChild(button);-- // append div to element- inset.appendChild(div);- el.appendChild(inset);- });+ document.querySelectorAll('table[data-sort-identifier="' + this.tableId + '"] th[data-filter-column]').forEach(function(el) {+ this.createSearchBox(el);+ }.bind(this));document.querySelectorAll('table[data-sort-identifier="' + this.tableId + '"] th[data-filter-column] .search-button').forEach((el) => {el.addEventListener("click", () => {- let column = el.dataset.filterColumn;-- // hide search buttons etc.- el.classList.add("d-none");- let th = el.parentNode.parentNode;- th.querySelector('span.title').classList.add("d-none");- th.querySelector('span.search-spacer').classList.add("d-none");- // add class to parent if it's used to sort-- // if th has class sortable_sorted, add class is-searching- if (th.classList.contains('sortable_sorted')) {- th.classList.add("is-searching");- }-- // show search input in div.search-filter- let input = th.querySelector('div.search-filter');- input.classList.remove("d-none");- input.querySelector("input").focus();+ const th = el.parentNode.parentNode;+ showSearchBox(th);});});}+ createSearchBox(th) {+ console.log('createSearchBox', th);+ let column = th.dataset.filterColumn;+ // create filter inset.+ let inset = document.createElement('span');+ inset.classList.add('search-inset');++ // add spacer+ let searchSpacer = document.createElement('span');+ searchSpacer.classList.add('search-spacer');+ searchSpacer.innerHTML = ' ';+ inset.appendChild(searchSpacer);++ // make button.+ let searchButton = document.createElement('em');+ searchButton.classList.add('search-button', 'bi', 'bi-search');+ searchButton.setAttribute('data-filter-column', column);+ inset.appendChild(searchButton);++ // add hidden div+ let div = document.createElement('div');+ div.classList.add('d-none', 'input-group', 'search-filter');+ // append input to div.+ let input = document.createElement('input');+ input.classList.add('form-control', 'form-control-sm');+ input.setAttribute('type', 'search');+ input.setAttribute('placeholder', 'TODO placeholder');+ input.setAttribute('x-model', 'filter.' + column);+ div.appendChild(input);+ // create button+ let button = document.createElement('button');+ button.classList.add('btn', 'btn-outline-secondary', 'btn-sm', 'hide-button');+ button.setAttribute('type', 'button');+ button.addEventListener('click', this.clickCloseButton.bind(this));+ let icon = document.createElement('em');+ icon.classList.add('bi', 'bi-x', 'text-danger');+ // append icon to button+ button.appendChild(icon);+ // add button to div+ div.appendChild(button);- updateHeaderClasses(tableId, column, direction) {+ // append div to element+ inset.appendChild(div);+ th.appendChild(inset);+ }+++ updateHeaderClasses() {+ // console.log('updateHeaderClasses()', this.sortColumn, this.sortDirection);document.querySelectorAll('table[data-sort-identifier="' + this.tableId + '"] thead th[data-sort-column]').forEach((th) => {- // console.log('th', th.dataset.sortColumn);+ // console.log(th);if (this.sortColumn === th.dataset.sortColumn) {// th.classList.add('sortable');th.classList.add('sortable_sorted');@@ -210,6 +223,75 @@ export class sortableTable {}});}+}+function showSearchBox(th) {+ let column = th.dataset.filterColumn;++ // hide search buttons etc.+ th.querySelector('.search-button').classList.add("d-none");+ th.querySelector('span.title').classList.add("d-none");+ th.querySelector('span.search-spacer').classList.add("d-none");+ // add class to parent if it's used to sort++ // if th has class sortable_sorted, add class is-searching+ if (th.classList.contains('sortable_sorted')) {+ th.classList.add("is-searching");+ }++ // show search input in div.search-filter+ let input = th.querySelector('div.search-filter');+ input.classList.remove("d-none");+ input.querySelector("input").focus();+}++++function objectToQueryString(obj, prefix) {+ if (null === obj) {+ return "";+ }+ return Object.keys(obj)+ .map((objKey) => {+ if (Object.hasOwn(obj, objKey)) {+ const key = prefix ? `${prefix}[${objKey}]` : objKey;+ const value = obj[objKey];++ return typeof value === "object"+ ? objectToQueryString(value, key)+ : `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;+ }++ return null;+ })+ .join("&");+}++export function updateHistory(page, column, direction, filter) {+ if (history.pushState) {+ let obj = {+ page: page,+ column: column,+ direction: direction,+ filter: JSON.parse(JSON.stringify(filter)),+ };+ if(filter.length > 0) {+ this.isFiltering = true;+ this.loadingPage = true;+ }+ let string = objectToQueryString(obj);+ console.log('Update history', string);+ let newUrl = window.location.protocol + "//" + window.location.host + window.location.pathname + "?" + string;+ window.history.pushState({path: newUrl}, "", newUrl);+ }+}+export function storeFilterAndSort(storageKey, column, direction, filter) {+ let obj = {+ column: column,+ direction: direction,+ filter: JSON.parse(JSON.stringify(filter)),+ };+ console.log('storeFilterAndSort', storageKey, obj);+ window.store.set(storageKey, obj);}diff --git a/resources/assets/v3/js/pages/transactions/shared/disable-split-inputs.js b/resources/assets/v3/js/pages/transactions/shared/disable-split-inputs.jsindex 578e40d102..702539fd63 100644--- a/resources/assets/v3/js/pages/transactions/shared/disable-split-inputs.js+++ b/resources/assets/v3/js/pages/transactions/shared/disable-split-inputs.js@@ -55,8 +55,7 @@ export function disableSplitInputs() {"withdrawal" === this.groupProperties.transactionType) {console.log("Prefill destination account #", i, this.entries[i].destination_account);- this.entries[i].destination_account = JSON.parse(- JSON.stringify(this.entries[i - 1].destination_account),+ this.entries[i].destination_account = JSON.parse(JSON.stringify(this.entries[i - 1].destination_account),); //structuredClone();}diff --git a/resources/assets/v3/sass/app.scss b/resources/assets/v3/sass/app.scssindex 69178fa001..1d491f027f 100644--- a/resources/assets/v3/sass/app.scss+++ b/resources/assets/v3/sass/app.scss@@ -63,10 +63,10 @@ table.sortable thead th[data-sort-column] span {.search-button {cursor: pointer;}-table.sortable th[data-sort-column].sortable_sorted.sortable_sorted_asc:not(th.is-searching):before {+table.sortable th[data-sort-column].sortable_sorted_asc:not(th.is-searching):before {content: "\25B4 ";}-table.sortable th[data-sort-column].sortable_sorted.sortable_sorted_desc:not(th.is-searching):before {+table.sortable th[data-sort-column].sortable_sorted_desc:not(th.is-searching):before {content: "\25BE ";}diff --git a/resources/views/accounts/index.blade.php b/resources/views/accounts/index.blade.phpindex f97dbee3fc..5f724ee3ba 100644--- a/resources/views/accounts/index.blade.php+++ b/resources/views/accounts/index.blade.php@@ -26,7 +26,6 @@<span class="title"> </span></th><th data-sort-column="name" data-filter-column="name" class="w-20"><span class="title">{{ trans('list.name') }}</span></th>-<template x-if="'asset' === objectType">{{-- hide on LG and smaller. --}}<th data-sort-column="role" class="d-lg-table-cell d-none"><span class="title">{{ trans('list.role') }}</span></th>@@ -76,7 +75,7 @@<tr :data-id="account.id"><td><template- x-if="('asset' === objectType || 'liabilities' === objectType) && 'asc' === sortDirection && 'order' === sortColumn && accounts.length > 1">+ x-if="('asset' === objectType || 'liabilities' === objectType) && 'asc' === sortableTable.sortDirection && 'order' === sortableTable.sortColumn && accounts.length > 1"><span class="btn btn-outline-secondary btn-sm bi bi-list object-handle"></span></template></td>