From 33cbdfbd13e392a143d86b9066b7b7e1f3dce7f8 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 13 Aug 2023 20:43:20 +0800 Subject: [PATCH] code refactor --- src/lib/category.js | 108 ++++++++++++++++++ .../cards/CategoryFilterSettingsCard.vue | 104 +++-------------- .../statistics/CategoryFilterSettingsPage.vue | 104 +++-------------- 3 files changed, 146 insertions(+), 170 deletions(-) diff --git a/src/lib/category.js b/src/lib/category.js index a53125d6..1a0f6b16 100644 --- a/src/lib/category.js +++ b/src/lib/category.js @@ -115,3 +115,111 @@ export function allVisibleTransactionCategories(allTransactionCategories) { return ret; } + +export function hasAnyAvailableCategory(allVisibleTransactionCategories) { + for (let type in allVisibleTransactionCategories) { + if (!Object.prototype.hasOwnProperty.call(allVisibleTransactionCategories, type)) { + continue; + } + + const categoryType = allVisibleTransactionCategories[type]; + + if (categoryType.visibleCategories && categoryType.visibleCategories.length > 0) { + return true; + } + } + + return false; +} + +export function hasAvailableCategory(allVisibleTransactionCategories) { + const result = {}; + + for (let type in allVisibleTransactionCategories) { + if (!Object.prototype.hasOwnProperty.call(allVisibleTransactionCategories, type)) { + continue; + } + + const categoryType = allVisibleTransactionCategories[type]; + result[type] = categoryType.visibleCategories && categoryType.visibleCategories.length > 0; + } + + return result; +} + +export function selectSubCategories(filterCategoryIds, category, value) { + if (!category || !category.subCategories || !category.subCategories.length) { + return; + } + + for (let i = 0; i < category.subCategories.length; i++) { + const subCategory = category.subCategories[i]; + filterCategoryIds[subCategory.id] = value; + } +} + +export function selectAll(filterCategoryIds, allTransactionCategoriesMap) { + for (let categoryId in filterCategoryIds) { + if (!Object.prototype.hasOwnProperty.call(filterCategoryIds, categoryId)) { + continue; + } + + const category = allTransactionCategoriesMap[categoryId]; + + if (category) { + filterCategoryIds[category.id] = false; + } + } +} + +export function selectNone(filterCategoryIds, allTransactionCategoriesMap) { + for (let categoryId in filterCategoryIds) { + if (!Object.prototype.hasOwnProperty.call(filterCategoryIds, categoryId)) { + continue; + } + + const category = allTransactionCategoriesMap[categoryId]; + + if (category) { + filterCategoryIds[category.id] = true; + } + } +} + +export function selectInvert(filterCategoryIds, allTransactionCategoriesMap) { + for (let categoryId in filterCategoryIds) { + if (!Object.prototype.hasOwnProperty.call(filterCategoryIds, categoryId)) { + continue; + } + + const category = allTransactionCategoriesMap[categoryId]; + + if (category) { + filterCategoryIds[category.id] = !filterCategoryIds[category.id]; + } + } +} + +export function isSubCategoriesAllChecked(category, filterCategoryIds) { + for (let i = 0; i < category.subCategories.length; i++) { + const subCategory = category.subCategories[i]; + if (filterCategoryIds[subCategory.id]) { + return false; + } + } + + return true; +} + +export function isSubCategoriesHasButNotAllChecked(category, filterCategoryIds) { + let checkedCount = 0; + + for (let i = 0; i < category.subCategories.length; i++) { + const subCategory = category.subCategories[i]; + if (!filterCategoryIds[subCategory.id]) { + checkedCount++; + } + } + + return checkedCount > 0 && checkedCount < category.subCategories.length; +} diff --git a/src/views/desktop/statistics/settings/cards/CategoryFilterSettingsCard.vue b/src/views/desktop/statistics/settings/cards/CategoryFilterSettingsCard.vue index 98de880a..e4efae1d 100644 --- a/src/views/desktop/statistics/settings/cards/CategoryFilterSettingsCard.vue +++ b/src/views/desktop/statistics/settings/cards/CategoryFilterSettingsCard.vue @@ -127,7 +127,17 @@ import { useStatisticsStore } from '@/stores/statistics.js'; import categoryConstants from '@/consts/category.js'; import { copyObjectTo } from '@/lib/common.js'; -import { allVisibleTransactionCategories } from '@/lib/category.js'; +import { + allVisibleTransactionCategories, + hasAnyAvailableCategory, + hasAvailableCategory, + selectSubCategories, + selectAll, + selectNone, + selectInvert, + isSubCategoriesAllChecked, + isSubCategoriesHasButNotAllChecked +} from '@/lib/category.js'; import { mdiSelectAll, @@ -182,33 +192,10 @@ export default { return allVisibleTransactionCategories(this.transactionCategoriesStore.allTransactionCategories); }, hasAnyAvailableCategory() { - for (let type in this.allVisibleTransactionCategories) { - if (!Object.prototype.hasOwnProperty.call(this.allVisibleTransactionCategories, type)) { - continue; - } - - const categoryType = this.allVisibleTransactionCategories[type]; - - if (categoryType.visibleCategories && categoryType.visibleCategories.length > 0) { - return true; - } - } - - return false; + return hasAnyAvailableCategory(this.allVisibleTransactionCategories); }, hasAvailableCategory() { - const result = {}; - - for (let type in this.allVisibleTransactionCategories) { - if (!Object.prototype.hasOwnProperty.call(this.allVisibleTransactionCategories, type)) { - continue; - } - - const categoryType = this.allVisibleTransactionCategories[type]; - result[type] = categoryType.visibleCategories && categoryType.visibleCategories.length > 0; - } - - return result; + return hasAvailableCategory(this.allVisibleTransactionCategories); } }, created() { @@ -284,65 +271,28 @@ export default { } }, selectSubCategories(category, value) { - if (!category || !category.subCategories || !category.subCategories.length) { - return; - } - - for (let i = 0; i < category.subCategories.length; i++) { - const subCategory = category.subCategories[i]; - this.filterCategoryIds[subCategory.id] = !value; - } + selectSubCategories(this.filterCategoryIds, category, !value); if (this.autoSave) { this.save(); } }, selectAll() { - for (let categoryId in this.filterCategoryIds) { - if (!Object.prototype.hasOwnProperty.call(this.filterCategoryIds, categoryId)) { - continue; - } - - const category = this.transactionCategoriesStore.allTransactionCategoriesMap[categoryId]; - - if (category) { - this.filterCategoryIds[category.id] = false; - } - } + selectAll(this.filterCategoryIds, this.transactionCategoriesStore.allTransactionCategoriesMap); if (this.autoSave) { this.save(); } }, selectNone() { - for (let categoryId in this.filterCategoryIds) { - if (!Object.prototype.hasOwnProperty.call(this.filterCategoryIds, categoryId)) { - continue; - } - - const category = this.transactionCategoriesStore.allTransactionCategoriesMap[categoryId]; - - if (category) { - this.filterCategoryIds[category.id] = true; - } - } + selectNone(this.filterCategoryIds, this.transactionCategoriesStore.allTransactionCategoriesMap); if (this.autoSave) { this.save(); } }, selectInvert() { - for (let categoryId in this.filterCategoryIds) { - if (!Object.prototype.hasOwnProperty.call(this.filterCategoryIds, categoryId)) { - continue; - } - - const category = this.transactionCategoriesStore.allTransactionCategoriesMap[categoryId]; - - if (category) { - this.filterCategoryIds[category.id] = !this.filterCategoryIds[category.id]; - } - } + selectInvert(this.filterCategoryIds, this.transactionCategoriesStore.allTransactionCategoriesMap); if (this.autoSave) { this.save(); @@ -364,26 +314,10 @@ export default { return !filterCategoryIds[category.id]; }, isSubCategoriesAllChecked(category, filterCategoryIds) { - for (let i = 0; i < category.subCategories.length; i++) { - const subCategory = category.subCategories[i]; - if (filterCategoryIds[subCategory.id]) { - return false; - } - } - - return true; + return isSubCategoriesAllChecked(category, filterCategoryIds); }, isSubCategoriesHasButNotAllChecked(category, filterCategoryIds) { - let checkedCount = 0; - - for (let i = 0; i < category.subCategories.length; i++) { - const subCategory = category.subCategories[i]; - if (!filterCategoryIds[subCategory.id]) { - checkedCount++; - } - } - - return checkedCount > 0 && checkedCount < category.subCategories.length; + return isSubCategoriesHasButNotAllChecked(category, filterCategoryIds); } } } diff --git a/src/views/mobile/statistics/CategoryFilterSettingsPage.vue b/src/views/mobile/statistics/CategoryFilterSettingsPage.vue index 92767cb3..e24aa185 100644 --- a/src/views/mobile/statistics/CategoryFilterSettingsPage.vue +++ b/src/views/mobile/statistics/CategoryFilterSettingsPage.vue @@ -130,7 +130,17 @@ import { useStatisticsStore } from '@/stores/statistics.js'; import categoryConstants from '@/consts/category.js'; import { copyObjectTo } from '@/lib/common.js'; -import { allVisibleTransactionCategories } from '@/lib/category.js'; +import { + allVisibleTransactionCategories, + hasAnyAvailableCategory, + hasAvailableCategory, + selectSubCategories, + selectAll, + selectNone, + selectInvert, + isSubCategoriesAllChecked, + isSubCategoriesHasButNotAllChecked +} from '@/lib/category.js'; export default { props: [ @@ -169,33 +179,10 @@ export default { return allVisibleTransactionCategories(this.transactionCategoriesStore.allTransactionCategories); }, hasAnyAvailableCategory() { - for (let type in this.allVisibleTransactionCategories) { - if (!Object.prototype.hasOwnProperty.call(this.allVisibleTransactionCategories, type)) { - continue; - } - - const categoryType = this.allVisibleTransactionCategories[type]; - - if (categoryType.visibleCategories && categoryType.visibleCategories.length > 0) { - return true; - } - } - - return false; + return hasAnyAvailableCategory(this.allVisibleTransactionCategories); }, hasAvailableCategory() { - const result = {}; - - for (let type in this.allVisibleTransactionCategories) { - if (!Object.prototype.hasOwnProperty.call(this.allVisibleTransactionCategories, type)) { - continue; - } - - const categoryType = this.allVisibleTransactionCategories[type]; - result[type] = categoryType.visibleCategories && categoryType.visibleCategories.length > 0; - } - - return result; + return hasAvailableCategory(this.allVisibleTransactionCategories); } }, created() { @@ -278,53 +265,16 @@ export default { const categoryId = e.target.value; const category = this.transactionCategoriesStore.allTransactionCategoriesMap[categoryId]; - if (!category || !category.subCategories || !category.subCategories.length) { - return; - } - - for (let i = 0; i < category.subCategories.length; i++) { - const subCategory = category.subCategories[i]; - this.filterCategoryIds[subCategory.id] = !e.target.checked; - } + selectSubCategories(this.filterCategoryIds, category, !e.target.checked); }, selectAll() { - for (let categoryId in this.filterCategoryIds) { - if (!Object.prototype.hasOwnProperty.call(this.filterCategoryIds, categoryId)) { - continue; - } - - const category = this.transactionCategoriesStore.allTransactionCategoriesMap[categoryId]; - - if (category) { - this.filterCategoryIds[category.id] = false; - } - } + selectAll(this.filterCategoryIds, this.transactionCategoriesStore.allTransactionCategoriesMap); }, selectNone() { - for (let categoryId in this.filterCategoryIds) { - if (!Object.prototype.hasOwnProperty.call(this.filterCategoryIds, categoryId)) { - continue; - } - - const category = this.transactionCategoriesStore.allTransactionCategoriesMap[categoryId]; - - if (category) { - this.filterCategoryIds[category.id] = true; - } - } + selectNone(this.filterCategoryIds, this.transactionCategoriesStore.allTransactionCategoriesMap); }, selectInvert() { - for (let categoryId in this.filterCategoryIds) { - if (!Object.prototype.hasOwnProperty.call(this.filterCategoryIds, categoryId)) { - continue; - } - - const category = this.transactionCategoriesStore.allTransactionCategoriesMap[categoryId]; - - if (category) { - this.filterCategoryIds[category.id] = !this.filterCategoryIds[category.id]; - } - } + selectInvert(this.filterCategoryIds, this.transactionCategoriesStore.allTransactionCategoriesMap); }, getCategoryTypeName(categoryType) { switch (categoryType) { @@ -342,26 +292,10 @@ export default { return !filterCategoryIds[category.id]; }, isSubCategoriesAllChecked(category, filterCategoryIds) { - for (let i = 0; i < category.subCategories.length; i++) { - const subCategory = category.subCategories[i]; - if (filterCategoryIds[subCategory.id]) { - return false; - } - } - - return true; + return isSubCategoriesAllChecked(category, filterCategoryIds); }, isSubCategoriesHasButNotAllChecked(category, filterCategoryIds) { - let checkedCount = 0; - - for (let i = 0; i < category.subCategories.length; i++) { - const subCategory = category.subCategories[i]; - if (!filterCategoryIds[subCategory.id]) { - checkedCount++; - } - } - - return checkedCount > 0 && checkedCount < category.subCategories.length; + return isSubCategoriesHasButNotAllChecked(category, filterCategoryIds); }, getCollapseStates() { const collapseStates = {};