From 19fea4e761472d55b0af69cdb93dcfcdf21bb82a Mon Sep 17 00:00:00 2001 From: MaysWind Date: Mon, 10 Jul 2023 23:26:16 +0800 Subject: [PATCH] code refactor --- src/lib/i18n.js | 64 ++++++++++++++++++++ src/views/mobile/statistics/SettingsPage.vue | 27 ++------- 2 files changed, 70 insertions(+), 21 deletions(-) diff --git a/src/lib/i18n.js b/src/lib/i18n.js index ee4af165..2e9489b1 100644 --- a/src/lib/i18n.js +++ b/src/lib/i18n.js @@ -4,6 +4,7 @@ import { defaultLanguage, allLanguages } from '@/locales/index.js'; import datetime from '@/consts/datetime.js'; import timezone from '@/consts/timezone.js'; import currency from '@/consts/currency.js'; +import statistics from '@/consts/statistics.js'; import { isString, @@ -541,6 +542,66 @@ function getAllWeekDays(translateFn) { return allWeekDays; } +function getAllDateRanges(includeCustom, translateFn) { + const allDateRanges = []; + + for (let dateRangeField in datetime.allDateRanges) { + if (!Object.prototype.hasOwnProperty.call(datetime.allDateRanges, dateRangeField)) { + continue; + } + + const dateRangeType = datetime.allDateRanges[dateRangeField]; + + if (includeCustom || dateRangeType.type !== datetime.allDateRanges.Custom.type) { + allDateRanges.push({ + type: dateRangeType.type, + displayName: translateFn(dateRangeType.name) + }); + } + } + + return allDateRanges; +} + +function getAllStatisticsChartDataTypes(translateFn) { + const allChartDataTypes = []; + + for (const dataTypeField in statistics.allChartDataTypes) { + if (!Object.prototype.hasOwnProperty.call(statistics.allChartDataTypes, dataTypeField)) { + return; + } + + const chartDataType = statistics.allChartDataTypes[dataTypeField]; + + allChartDataTypes.push({ + type: chartDataType.type, + displayName: translateFn(chartDataType.name) + }); + } + + return allChartDataTypes; +} + +function getAllStatisticsSortingTypes(translateFn) { + const allSortingTypes = []; + + for (const sortingTypeField in statistics.allSortingTypes) { + if (!Object.prototype.hasOwnProperty.call(statistics.allSortingTypes, sortingTypeField)) { + return; + } + + const sortingType = statistics.allSortingTypes[sortingTypeField]; + + allSortingTypes.push({ + type: sortingType.type, + displayName: translateFn(sortingType.name), + displayFullName: translateFn(sortingType.fullName) + }); + } + + return allSortingTypes; +} + function getDisplayCurrency(value, currencyCode, options, translateFn) { if (!isNumber(value) && !isString(value)) { return value; @@ -806,6 +867,9 @@ export function i18nFunctions(i18nGlobal) { getAllTimezones: (includeSystemDefault) => getAllTimezones(includeSystemDefault, i18nGlobal.t), getAllCurrencies: () => getAllCurrencies(i18nGlobal.t), getAllWeekDays: () => getAllWeekDays(i18nGlobal.t), + getAllDateRanges: (includeCustom) => getAllDateRanges(includeCustom, i18nGlobal.t), + getAllStatisticsChartDataTypes: () => getAllStatisticsChartDataTypes(i18nGlobal.t), + getAllStatisticsSortingTypes: () => getAllStatisticsSortingTypes(i18nGlobal.t), getDisplayCurrency: (value, currencyCode, options) => getDisplayCurrency(value, currencyCode, options, i18nGlobal.t), setLanguage: (locale, force) => setLanguage(i18nGlobal, locale, force), initLocale: (lastUserLanguage, timezone) => initLocale(i18nGlobal, lastUserLanguage, timezone) diff --git a/src/views/mobile/statistics/SettingsPage.vue b/src/views/mobile/statistics/SettingsPage.vue index ad502843..4dbe7db2 100644 --- a/src/views/mobile/statistics/SettingsPage.vue +++ b/src/views/mobile/statistics/SettingsPage.vue @@ -18,7 +18,7 @@ @@ -28,7 +28,7 @@ @@ -42,7 +42,7 @@ @@ -53,7 +53,6 @@ import { mapStores } from 'pinia'; import { useSettingsStore } from '@/stores/setting.js'; -import datetimeConstants from '@/consts/datetime.js'; import statisticsConstants from '@/consts/statistics.js'; export default { @@ -63,27 +62,13 @@ export default { return statisticsConstants.allChartTypes; }, allChartDataTypes() { - return statisticsConstants.allChartDataTypes; + return this.$locale.getAllStatisticsChartDataTypes(); }, allSortingTypes() { - return statisticsConstants.allSortingTypes; + return this.$locale.getAllStatisticsSortingTypes(); }, allDateRanges() { - const allDateRanges = []; - - for (let dateRangeField in datetimeConstants.allDateRanges) { - if (!Object.prototype.hasOwnProperty.call(datetimeConstants.allDateRanges, dateRangeField)) { - continue; - } - - const dateRangeType = datetimeConstants.allDateRanges[dateRangeField]; - - if (dateRangeType.type !== datetimeConstants.allDateRanges.Custom.type) { - allDateRanges.push(dateRangeType); - } - } - - return allDateRanges; + return this.$locale.getAllDateRanges(false); }, defaultChartType: { get: function () {