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;
}
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 { copyObjectTo } from '@/lib/common.js';
import { getVisibleCategorizedAccounts } from '@/lib/account.js';
import {
getVisibleCategorizedAccounts,
selectAccountOrSubAccounts,
selectAll,
selectNone,
selectInvert,
isAccountOrSubAccountsAllChecked,
isAccountOrSubAccountsHasButNotAllChecked
} from '@/lib/account.js';
import {
mdiSelectAll,
@@ -248,18 +256,7 @@ export default {
this.$emit('settings:change', false);
},
selectAccountOrSubAccounts(account, value) {
if (account.type === this.allAccountTypes.SingleAccount) {
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;
}
}
selectAccountOrSubAccounts(this.filterAccountIds, account, !value);
if (this.autoSave) {
this.save();
@@ -273,51 +270,21 @@ export default {
}
},
selectAll() {
for (let accountId in this.filterAccountIds) {
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;
}
}
selectAll(this.filterAccountIds, this.accountsStore.allAccountsMap);
if (this.autoSave) {
this.save();
}
},
selectNone() {
for (let accountId in this.filterAccountIds) {
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;
}
}
selectNone(this.filterAccountIds, this.accountsStore.allAccountsMap);
if (this.autoSave) {
this.save();
}
},
selectInvert() {
for (let accountId in this.filterAccountIds) {
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];
}
}
selectInvert(this.filterAccountIds, this.accountsStore.allAccountsMap);
if (this.autoSave) {
this.save();
@@ -327,34 +294,10 @@ export default {
return !filterAccountIds[account.id];
},
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;
return isAccountOrSubAccountsAllChecked(account, filterAccountIds);
},
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;
return isAccountOrSubAccountsHasButNotAllChecked(account, filterAccountIds);
}
}
}
@@ -121,7 +121,15 @@ import { useStatisticsStore } from '@/stores/statistics.js';
import accountConstants from '@/consts/account.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 {
props: [
@@ -240,18 +248,7 @@ export default {
return;
}
if (account.type === this.allAccountTypes.SingleAccount) {
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;
}
}
selectAccountOrSubAccounts(this.filterAccountIds, account, !e.target.checked);
},
selectAccount(e) {
const accountId = e.target.value;
@@ -264,76 +261,22 @@ export default {
this.filterAccountIds[account.id] = !e.target.checked;
},
selectAll() {
for (let accountId in this.filterAccountIds) {
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;
}
}
selectAll(this.filterAccountIds, this.accountsStore.allAccountsMap);
},
selectNone() {
for (let accountId in this.filterAccountIds) {
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;
}
}
selectNone(this.filterAccountIds, this.accountsStore.allAccountsMap);
},
selectInvert() {
for (let accountId in this.filterAccountIds) {
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];
}
}
selectInvert(this.filterAccountIds, this.accountsStore.allAccountsMap);
},
isAccountChecked(account, filterAccountIds) {
return !filterAccountIds[account.id];
},
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;
return isAccountOrSubAccountsAllChecked(account, filterAccountIds);
},
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;
return isAccountOrSubAccountsHasButNotAllChecked(account, filterAccountIds);
},
getCollapseStates() {
const collapseStates = {};