code refactor

This commit is contained in:
MaysWind
2023-08-13 20:43:20 +08:00
parent f5ce6ed4bc
commit 33cbdfbd13
3 changed files with 146 additions and 170 deletions
+108
View File
@@ -115,3 +115,111 @@ export function allVisibleTransactionCategories(allTransactionCategories) {
return ret; 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;
}
@@ -127,7 +127,17 @@ import { useStatisticsStore } from '@/stores/statistics.js';
import categoryConstants from '@/consts/category.js'; import categoryConstants from '@/consts/category.js';
import { copyObjectTo } from '@/lib/common.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 { import {
mdiSelectAll, mdiSelectAll,
@@ -182,33 +192,10 @@ export default {
return allVisibleTransactionCategories(this.transactionCategoriesStore.allTransactionCategories); return allVisibleTransactionCategories(this.transactionCategoriesStore.allTransactionCategories);
}, },
hasAnyAvailableCategory() { hasAnyAvailableCategory() {
for (let type in this.allVisibleTransactionCategories) { return hasAnyAvailableCategory(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;
}, },
hasAvailableCategory() { hasAvailableCategory() {
const result = {}; return hasAvailableCategory(this.allVisibleTransactionCategories);
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;
} }
}, },
created() { created() {
@@ -284,65 +271,28 @@ export default {
} }
}, },
selectSubCategories(category, value) { selectSubCategories(category, value) {
if (!category || !category.subCategories || !category.subCategories.length) { selectSubCategories(this.filterCategoryIds, category, !value);
return;
}
for (let i = 0; i < category.subCategories.length; i++) {
const subCategory = category.subCategories[i];
this.filterCategoryIds[subCategory.id] = !value;
}
if (this.autoSave) { if (this.autoSave) {
this.save(); this.save();
} }
}, },
selectAll() { selectAll() {
for (let categoryId in this.filterCategoryIds) { selectAll(this.filterCategoryIds, this.transactionCategoriesStore.allTransactionCategoriesMap);
if (!Object.prototype.hasOwnProperty.call(this.filterCategoryIds, categoryId)) {
continue;
}
const category = this.transactionCategoriesStore.allTransactionCategoriesMap[categoryId];
if (category) {
this.filterCategoryIds[category.id] = false;
}
}
if (this.autoSave) { if (this.autoSave) {
this.save(); this.save();
} }
}, },
selectNone() { selectNone() {
for (let categoryId in this.filterCategoryIds) { selectNone(this.filterCategoryIds, this.transactionCategoriesStore.allTransactionCategoriesMap);
if (!Object.prototype.hasOwnProperty.call(this.filterCategoryIds, categoryId)) {
continue;
}
const category = this.transactionCategoriesStore.allTransactionCategoriesMap[categoryId];
if (category) {
this.filterCategoryIds[category.id] = true;
}
}
if (this.autoSave) { if (this.autoSave) {
this.save(); this.save();
} }
}, },
selectInvert() { selectInvert() {
for (let categoryId in this.filterCategoryIds) { selectInvert(this.filterCategoryIds, this.transactionCategoriesStore.allTransactionCategoriesMap);
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];
}
}
if (this.autoSave) { if (this.autoSave) {
this.save(); this.save();
@@ -364,26 +314,10 @@ export default {
return !filterCategoryIds[category.id]; return !filterCategoryIds[category.id];
}, },
isSubCategoriesAllChecked(category, filterCategoryIds) { isSubCategoriesAllChecked(category, filterCategoryIds) {
for (let i = 0; i < category.subCategories.length; i++) { return isSubCategoriesAllChecked(category, filterCategoryIds);
const subCategory = category.subCategories[i];
if (filterCategoryIds[subCategory.id]) {
return false;
}
}
return true;
}, },
isSubCategoriesHasButNotAllChecked(category, filterCategoryIds) { isSubCategoriesHasButNotAllChecked(category, filterCategoryIds) {
let checkedCount = 0; return isSubCategoriesHasButNotAllChecked(category, filterCategoryIds);
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;
} }
} }
} }
@@ -130,7 +130,17 @@ import { useStatisticsStore } from '@/stores/statistics.js';
import categoryConstants from '@/consts/category.js'; import categoryConstants from '@/consts/category.js';
import { copyObjectTo } from '@/lib/common.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 { export default {
props: [ props: [
@@ -169,33 +179,10 @@ export default {
return allVisibleTransactionCategories(this.transactionCategoriesStore.allTransactionCategories); return allVisibleTransactionCategories(this.transactionCategoriesStore.allTransactionCategories);
}, },
hasAnyAvailableCategory() { hasAnyAvailableCategory() {
for (let type in this.allVisibleTransactionCategories) { return hasAnyAvailableCategory(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;
}, },
hasAvailableCategory() { hasAvailableCategory() {
const result = {}; return hasAvailableCategory(this.allVisibleTransactionCategories);
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;
} }
}, },
created() { created() {
@@ -278,53 +265,16 @@ export default {
const categoryId = e.target.value; const categoryId = e.target.value;
const category = this.transactionCategoriesStore.allTransactionCategoriesMap[categoryId]; const category = this.transactionCategoriesStore.allTransactionCategoriesMap[categoryId];
if (!category || !category.subCategories || !category.subCategories.length) { selectSubCategories(this.filterCategoryIds, category, !e.target.checked);
return;
}
for (let i = 0; i < category.subCategories.length; i++) {
const subCategory = category.subCategories[i];
this.filterCategoryIds[subCategory.id] = !e.target.checked;
}
}, },
selectAll() { selectAll() {
for (let categoryId in this.filterCategoryIds) { selectAll(this.filterCategoryIds, this.transactionCategoriesStore.allTransactionCategoriesMap);
if (!Object.prototype.hasOwnProperty.call(this.filterCategoryIds, categoryId)) {
continue;
}
const category = this.transactionCategoriesStore.allTransactionCategoriesMap[categoryId];
if (category) {
this.filterCategoryIds[category.id] = false;
}
}
}, },
selectNone() { selectNone() {
for (let categoryId in this.filterCategoryIds) { selectNone(this.filterCategoryIds, this.transactionCategoriesStore.allTransactionCategoriesMap);
if (!Object.prototype.hasOwnProperty.call(this.filterCategoryIds, categoryId)) {
continue;
}
const category = this.transactionCategoriesStore.allTransactionCategoriesMap[categoryId];
if (category) {
this.filterCategoryIds[category.id] = true;
}
}
}, },
selectInvert() { selectInvert() {
for (let categoryId in this.filterCategoryIds) { selectInvert(this.filterCategoryIds, this.transactionCategoriesStore.allTransactionCategoriesMap);
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];
}
}
}, },
getCategoryTypeName(categoryType) { getCategoryTypeName(categoryType) {
switch (categoryType) { switch (categoryType) {
@@ -342,26 +292,10 @@ export default {
return !filterCategoryIds[category.id]; return !filterCategoryIds[category.id];
}, },
isSubCategoriesAllChecked(category, filterCategoryIds) { isSubCategoriesAllChecked(category, filterCategoryIds) {
for (let i = 0; i < category.subCategories.length; i++) { return isSubCategoriesAllChecked(category, filterCategoryIds);
const subCategory = category.subCategories[i];
if (filterCategoryIds[subCategory.id]) {
return false;
}
}
return true;
}, },
isSubCategoriesHasButNotAllChecked(category, filterCategoryIds) { isSubCategoriesHasButNotAllChecked(category, filterCategoryIds) {
let checkedCount = 0; return isSubCategoriesHasButNotAllChecked(category, filterCategoryIds);
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;
}, },
getCollapseStates() { getCollapseStates() {
const collapseStates = {}; const collapseStates = {};