code refactor

This commit is contained in:
MaysWind
2023-08-13 20:32:51 +08:00
parent eb9a2ac2e0
commit f5ce6ed4bc
3 changed files with 119 additions and 144 deletions
+89
View File
@@ -214,3 +214,92 @@ export function getAllFilteredAccountsBalance(categorizedAccounts, accountFilter
return ret; return ret;
} }
export function selectAccountOrSubAccounts(filterAccountIds, account, value) {
if (account.type === accountConstants.allAccountTypes.SingleAccount) {
filterAccountIds[account.id] = value;
} else if (account.type === accountConstants.allAccountTypes.MultiSubAccounts) {
if (!account.subAccounts || !account.subAccounts.length) {
return;
}
for (let i = 0; i < account.subAccounts.length; i++) {
const subAccount = account.subAccounts[i];
filterAccountIds[subAccount.id] = value;
}
}
}
export function selectAll(filterAccountIds, allAccountsMap) {
for (let accountId in filterAccountIds) {
if (!Object.prototype.hasOwnProperty.call(filterAccountIds, accountId)) {
continue;
}
const account = allAccountsMap[accountId];
if (account && account.type === accountConstants.allAccountTypes.SingleAccount) {
filterAccountIds[account.id] = false;
}
}
}
export function selectNone(filterAccountIds, allAccountsMap) {
for (let accountId in filterAccountIds) {
if (!Object.prototype.hasOwnProperty.call(filterAccountIds, accountId)) {
continue;
}
const account = allAccountsMap[accountId];
if (account && account.type === accountConstants.allAccountTypes.SingleAccount) {
filterAccountIds[account.id] = true;
}
}
}
export function selectInvert(filterAccountIds, allAccountsMap) {
for (let accountId in filterAccountIds) {
if (!Object.prototype.hasOwnProperty.call(filterAccountIds, accountId)) {
continue;
}
const account = allAccountsMap[accountId];
if (account && account.type === accountConstants.allAccountTypes.SingleAccount) {
filterAccountIds[account.id] = !filterAccountIds[account.id];
}
}
}
export function isAccountOrSubAccountsAllChecked(account, filterAccountIds) {
if (!account.subAccounts) {
return !filterAccountIds[account.id];
}
for (let i = 0; i < account.subAccounts.length; i++) {
const subAccount = account.subAccounts[i];
if (filterAccountIds[subAccount.id]) {
return false;
}
}
return true;
}
export function isAccountOrSubAccountsHasButNotAllChecked(account, filterAccountIds) {
if (!account.subAccounts) {
return false;
}
let checkedCount = 0;
for (let i = 0; i < account.subAccounts.length; i++) {
const subAccount = account.subAccounts[i];
if (!filterAccountIds[subAccount.id]) {
checkedCount++;
}
}
return checkedCount > 0 && checkedCount < account.subAccounts.length;
}
@@ -129,7 +129,15 @@ import { useStatisticsStore } from '@/stores/statistics.js';
import accountConstants from '@/consts/account.js'; import accountConstants from '@/consts/account.js';
import { copyObjectTo } from '@/lib/common.js'; import { copyObjectTo } from '@/lib/common.js';
import { getVisibleCategorizedAccounts } from '@/lib/account.js'; import {
getVisibleCategorizedAccounts,
selectAccountOrSubAccounts,
selectAll,
selectNone,
selectInvert,
isAccountOrSubAccountsAllChecked,
isAccountOrSubAccountsHasButNotAllChecked
} from '@/lib/account.js';
import { import {
mdiSelectAll, mdiSelectAll,
@@ -248,18 +256,7 @@ export default {
this.$emit('settings:change', false); this.$emit('settings:change', false);
}, },
selectAccountOrSubAccounts(account, value) { selectAccountOrSubAccounts(account, value) {
if (account.type === this.allAccountTypes.SingleAccount) { selectAccountOrSubAccounts(this.filterAccountIds, account, !value);
this.filterAccountIds[account.id] = !value;
} else if (account.type === this.allAccountTypes.MultiSubAccounts) {
if (!account.subAccounts || !account.subAccounts.length) {
return;
}
for (let i = 0; i < account.subAccounts.length; i++) {
const subAccount = account.subAccounts[i];
this.filterAccountIds[subAccount.id] = !value;
}
}
if (this.autoSave) { if (this.autoSave) {
this.save(); this.save();
@@ -273,51 +270,21 @@ export default {
} }
}, },
selectAll() { selectAll() {
for (let accountId in this.filterAccountIds) { selectAll(this.filterAccountIds, this.accountsStore.allAccountsMap);
if (!Object.prototype.hasOwnProperty.call(this.filterAccountIds, accountId)) {
continue;
}
const account = this.accountsStore.allAccountsMap[accountId];
if (account && account.type === this.allAccountTypes.SingleAccount) {
this.filterAccountIds[account.id] = false;
}
}
if (this.autoSave) { if (this.autoSave) {
this.save(); this.save();
} }
}, },
selectNone() { selectNone() {
for (let accountId in this.filterAccountIds) { selectNone(this.filterAccountIds, this.accountsStore.allAccountsMap);
if (!Object.prototype.hasOwnProperty.call(this.filterAccountIds, accountId)) {
continue;
}
const account = this.accountsStore.allAccountsMap[accountId];
if (account && account.type === this.allAccountTypes.SingleAccount) {
this.filterAccountIds[account.id] = true;
}
}
if (this.autoSave) { if (this.autoSave) {
this.save(); this.save();
} }
}, },
selectInvert() { selectInvert() {
for (let accountId in this.filterAccountIds) { selectInvert(this.filterAccountIds, this.accountsStore.allAccountsMap);
if (!Object.prototype.hasOwnProperty.call(this.filterAccountIds, accountId)) {
continue;
}
const account = this.accountsStore.allAccountsMap[accountId];
if (account && account.type === this.allAccountTypes.SingleAccount) {
this.filterAccountIds[account.id] = !this.filterAccountIds[account.id];
}
}
if (this.autoSave) { if (this.autoSave) {
this.save(); this.save();
@@ -327,34 +294,10 @@ export default {
return !filterAccountIds[account.id]; return !filterAccountIds[account.id];
}, },
isAccountOrSubAccountsAllChecked(account, filterAccountIds) { isAccountOrSubAccountsAllChecked(account, filterAccountIds) {
if (!account.subAccounts) { return isAccountOrSubAccountsAllChecked(account, filterAccountIds);
return !filterAccountIds[account.id];
}
for (let i = 0; i < account.subAccounts.length; i++) {
const subAccount = account.subAccounts[i];
if (filterAccountIds[subAccount.id]) {
return false;
}
}
return true;
}, },
isAccountOrSubAccountsHasButNotAllChecked(account, filterAccountIds) { isAccountOrSubAccountsHasButNotAllChecked(account, filterAccountIds) {
if (!account.subAccounts) { return isAccountOrSubAccountsHasButNotAllChecked(account, filterAccountIds);
return false;
}
let checkedCount = 0;
for (let i = 0; i < account.subAccounts.length; i++) {
const subAccount = account.subAccounts[i];
if (!filterAccountIds[subAccount.id]) {
checkedCount++;
}
}
return checkedCount > 0 && checkedCount < account.subAccounts.length;
} }
} }
} }
@@ -121,7 +121,15 @@ import { useStatisticsStore } from '@/stores/statistics.js';
import accountConstants from '@/consts/account.js'; import accountConstants from '@/consts/account.js';
import { copyObjectTo } from '@/lib/common.js'; import { copyObjectTo } from '@/lib/common.js';
import { getVisibleCategorizedAccounts } from '@/lib/account.js'; import {
getVisibleCategorizedAccounts,
selectAccountOrSubAccounts,
selectAll,
selectNone,
selectInvert,
isAccountOrSubAccountsAllChecked,
isAccountOrSubAccountsHasButNotAllChecked
} from '@/lib/account.js';
export default { export default {
props: [ props: [
@@ -240,18 +248,7 @@ export default {
return; return;
} }
if (account.type === this.allAccountTypes.SingleAccount) { selectAccountOrSubAccounts(this.filterAccountIds, account, !e.target.checked);
this.filterAccountIds[account.id] = !e.target.checked;
} else if (account.type === this.allAccountTypes.MultiSubAccounts) {
if (!account.subAccounts || !account.subAccounts.length) {
return;
}
for (let i = 0; i < account.subAccounts.length; i++) {
const subAccount = account.subAccounts[i];
this.filterAccountIds[subAccount.id] = !e.target.checked;
}
}
}, },
selectAccount(e) { selectAccount(e) {
const accountId = e.target.value; const accountId = e.target.value;
@@ -264,76 +261,22 @@ export default {
this.filterAccountIds[account.id] = !e.target.checked; this.filterAccountIds[account.id] = !e.target.checked;
}, },
selectAll() { selectAll() {
for (let accountId in this.filterAccountIds) { selectAll(this.filterAccountIds, this.accountsStore.allAccountsMap);
if (!Object.prototype.hasOwnProperty.call(this.filterAccountIds, accountId)) {
continue;
}
const account = this.accountsStore.allAccountsMap[accountId];
if (account && account.type === this.allAccountTypes.SingleAccount) {
this.filterAccountIds[account.id] = false;
}
}
}, },
selectNone() { selectNone() {
for (let accountId in this.filterAccountIds) { selectNone(this.filterAccountIds, this.accountsStore.allAccountsMap);
if (!Object.prototype.hasOwnProperty.call(this.filterAccountIds, accountId)) {
continue;
}
const account = this.accountsStore.allAccountsMap[accountId];
if (account && account.type === this.allAccountTypes.SingleAccount) {
this.filterAccountIds[account.id] = true;
}
}
}, },
selectInvert() { selectInvert() {
for (let accountId in this.filterAccountIds) { selectInvert(this.filterAccountIds, this.accountsStore.allAccountsMap);
if (!Object.prototype.hasOwnProperty.call(this.filterAccountIds, accountId)) {
continue;
}
const account = this.accountsStore.allAccountsMap[accountId];
if (account && account.type === this.allAccountTypes.SingleAccount) {
this.filterAccountIds[account.id] = !this.filterAccountIds[account.id];
}
}
}, },
isAccountChecked(account, filterAccountIds) { isAccountChecked(account, filterAccountIds) {
return !filterAccountIds[account.id]; return !filterAccountIds[account.id];
}, },
isAccountOrSubAccountsAllChecked(account, filterAccountIds) { isAccountOrSubAccountsAllChecked(account, filterAccountIds) {
if (!account.subAccounts) { return isAccountOrSubAccountsAllChecked(account, filterAccountIds);
return !filterAccountIds[account.id];
}
for (let i = 0; i < account.subAccounts.length; i++) {
const subAccount = account.subAccounts[i];
if (filterAccountIds[subAccount.id]) {
return false;
}
}
return true;
}, },
isAccountOrSubAccountsHasButNotAllChecked(account, filterAccountIds) { isAccountOrSubAccountsHasButNotAllChecked(account, filterAccountIds) {
if (!account.subAccounts) { return isAccountOrSubAccountsHasButNotAllChecked(account, filterAccountIds);
return false;
}
let checkedCount = 0;
for (let i = 0; i < account.subAccounts.length; i++) {
const subAccount = account.subAccounts[i];
if (!filterAccountIds[subAccount.id]) {
checkedCount++;
}
}
return checkedCount > 0 && checkedCount < account.subAccounts.length;
}, },
getCollapseStates() { getCollapseStates() {
const collapseStates = {}; const collapseStates = {};