From f5ce6ed4bcf58dbaef2bd114bab2870f86cadc34 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 13 Aug 2023 20:32:51 +0800 Subject: [PATCH] code refactor --- src/lib/account.js | 89 +++++++++++++++++++ .../cards/AccountFilterSettingsCard.vue | 87 ++++-------------- .../statistics/AccountFilterSettingsPage.vue | 87 ++++-------------- 3 files changed, 119 insertions(+), 144 deletions(-) diff --git a/src/lib/account.js b/src/lib/account.js index 92016d9f..da05b0e5 100644 --- a/src/lib/account.js +++ b/src/lib/account.js @@ -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; +} diff --git a/src/views/desktop/statistics/settings/cards/AccountFilterSettingsCard.vue b/src/views/desktop/statistics/settings/cards/AccountFilterSettingsCard.vue index fecdffc3..317f51e8 100644 --- a/src/views/desktop/statistics/settings/cards/AccountFilterSettingsCard.vue +++ b/src/views/desktop/statistics/settings/cards/AccountFilterSettingsCard.vue @@ -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); } } } diff --git a/src/views/mobile/statistics/AccountFilterSettingsPage.vue b/src/views/mobile/statistics/AccountFilterSettingsPage.vue index ca0e1c7e..2d707e80 100644 --- a/src/views/mobile/statistics/AccountFilterSettingsPage.vue +++ b/src/views/mobile/statistics/AccountFilterSettingsPage.vue @@ -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 = {};