code refactor

This commit is contained in:
MaysWind
2023-06-23 21:19:24 +08:00
parent 59b883ff7f
commit 8e6aece9ae
3 changed files with 40 additions and 20 deletions
+28 -2
View File
@@ -241,6 +241,14 @@ function getCurrentLanguageInfo(i18nGlobal) {
return getDefaultLanguage(); return getDefaultLanguage();
} }
function getDefaultCurrency(translateFn) {
return translateFn('default.currency');
}
function getDefaultFirstDayOfWeek(translateFn) {
return translateFn('default.firstDayOfWeek');
}
function getAllLongMonthNames(translateFn) { function getAllLongMonthNames(translateFn) {
return [ return [
translateFn('datetime.January.long'), translateFn('datetime.January.long'),
@@ -484,6 +492,21 @@ function getAllCurrencies(translateFn) {
return allCurrencies; return allCurrencies;
} }
function getAllWeekDays(translateFn) {
const allWeekDays = [];
for (let i = 0; i < datetime.allWeekDaysArray.length; i++) {
const weekDay = datetime.allWeekDaysArray[i];
allWeekDays.push({
type: weekDay.type,
displayName: translateFn(`datetime.${weekDay.name}.long`)
});
}
return allWeekDays;
}
function getDisplayCurrency(value, currencyCode, notConvertValue, translateFn) { function getDisplayCurrency(value, currencyCode, notConvertValue, translateFn) {
if (!isNumber(value) && !isString(value)) { if (!isNumber(value) && !isString(value)) {
return value; return value;
@@ -623,8 +646,8 @@ function setLanguage(i18nGlobal, locale, force) {
services.setLocale(locale); services.setLocale(locale);
document.querySelector('html').setAttribute('lang', locale); document.querySelector('html').setAttribute('lang', locale);
const defaultCurrency = i18nGlobal.t('default.currency'); const defaultCurrency = getDefaultCurrency(i18nGlobal.t);
const defaultFirstDayOfWeekName = i18nGlobal.t('default.firstDayOfWeek'); const defaultFirstDayOfWeekName = getDefaultFirstDayOfWeek(i18nGlobal.t);
let defaultFirstDayOfWeek = datetime.defaultFirstDayOfWeek; let defaultFirstDayOfWeek = datetime.defaultFirstDayOfWeek;
if (datetime.allWeekDays[defaultFirstDayOfWeekName]) { if (datetime.allWeekDays[defaultFirstDayOfWeekName]) {
@@ -716,6 +739,8 @@ export function i18nFunctions(i18nGlobal) {
getLanguageInfo: getLanguageInfo, getLanguageInfo: getLanguageInfo,
getDefaultLanguage: getDefaultLanguage, getDefaultLanguage: getDefaultLanguage,
getCurrentLanguageInfo: () => getCurrentLanguageInfo(i18nGlobal), getCurrentLanguageInfo: () => getCurrentLanguageInfo(i18nGlobal),
getDefaultCurrency: () => getDefaultCurrency(i18nGlobal.t),
getDefaultFirstDayOfWeek: () => getDefaultFirstDayOfWeek(i18nGlobal.t),
getAllLongMonthNames: () => getAllLongMonthNames(i18nGlobal.t), getAllLongMonthNames: () => getAllLongMonthNames(i18nGlobal.t),
getAllShortMonthNames: () => getAllShortMonthNames(i18nGlobal.t), getAllShortMonthNames: () => getAllShortMonthNames(i18nGlobal.t),
getAllLongWeekdayNames: () => getAllLongWeekdayNames(i18nGlobal.t), getAllLongWeekdayNames: () => getAllLongWeekdayNames(i18nGlobal.t),
@@ -743,6 +768,7 @@ export function i18nFunctions(i18nGlobal) {
isShortTime24HourFormat: (userStore) => isShortTime24HourFormat(i18nGlobal.t, userStore.currentUserShortTimeFormat), isShortTime24HourFormat: (userStore) => isShortTime24HourFormat(i18nGlobal.t, userStore.currentUserShortTimeFormat),
getAllTimezones: (includeSystemDefault) => getAllTimezones(includeSystemDefault, i18nGlobal.t), getAllTimezones: (includeSystemDefault) => getAllTimezones(includeSystemDefault, i18nGlobal.t),
getAllCurrencies: () => getAllCurrencies(i18nGlobal.t), getAllCurrencies: () => getAllCurrencies(i18nGlobal.t),
getAllWeekDays: () => getAllWeekDays(i18nGlobal.t),
getDisplayCurrency: (value, currencyCode, notConvertValue) => getDisplayCurrency(value, currencyCode, notConvertValue, i18nGlobal.t), getDisplayCurrency: (value, currencyCode, notConvertValue) => getDisplayCurrency(value, currencyCode, notConvertValue, i18nGlobal.t),
setLanguage: (locale, force) => setLanguage(i18nGlobal, locale, force), setLanguage: (locale, force) => setLanguage(i18nGlobal, locale, force),
getTimezone: settings.getTimezone, getTimezone: settings.getTimezone,
+9 -12
View File
@@ -95,13 +95,13 @@
class="list-item-with-header-and-title list-item-no-item-after" class="list-item-with-header-and-title list-item-no-item-after"
:key="currentLocale + '_firstDayOfWeek'" :key="currentLocale + '_firstDayOfWeek'"
:header="$t('First Day of Week')" :header="$t('First Day of Week')"
:title="getDayOfWeekName(user.firstDayOfWeek)" :title="currentDayOfWeekName"
smart-select :smart-select-params="{ openIn: 'popup', popupPush: true, closeOnSelect: true, scrollToSelectedItem: true, searchbar: true, searchbarPlaceholder: $t('Date'), searchbarDisableText: $t('Cancel'), appendSearchbarNotFound: $t('No results'), pageTitle: $t('First Day of Week'), popupCloseLinkText: $t('Done') }" smart-select :smart-select-params="{ openIn: 'popup', popupPush: true, closeOnSelect: true, scrollToSelectedItem: true, searchbar: true, searchbarPlaceholder: $t('Date'), searchbarDisableText: $t('Cancel'), appendSearchbarNotFound: $t('No results'), pageTitle: $t('First Day of Week'), popupCloseLinkText: $t('Done') }"
> >
<select v-model="user.firstDayOfWeek"> <select v-model="user.firstDayOfWeek">
<option :value="weekDay.type" <option :value="weekDay.type"
:key="weekDay.type" :key="weekDay.type"
v-for="weekDay in allWeekDays">{{ $t(`datetime.${weekDay.name}.long`) }}</option> v-for="weekDay in allWeekDays">{{ weekDay.displayName }}</option>
</select> </select>
</f7-list-item> </f7-list-item>
</f7-list> </f7-list>
@@ -180,7 +180,6 @@ import { useSettingsStore } from '@/stores/setting.js';
import { useTransactionCategoriesStore } from '@/stores/transactionCategory.js'; import { useTransactionCategoriesStore } from '@/stores/transactionCategory.js';
import { useExchangeRatesStore } from '@/stores/exchangeRates.js'; import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import datetimeConstants from '@/consts/datetime.js';
import categoryConstants from '@/consts/category.js'; import categoryConstants from '@/consts/category.js';
import { getNameByKeyValue, copyArrayTo } from '@/lib/common.js'; import { getNameByKeyValue, copyArrayTo } from '@/lib/common.js';
@@ -201,7 +200,7 @@ export default {
nickname: '', nickname: '',
language: self.$i18n.locale, language: self.$i18n.locale,
defaultCurrency: settingsStore.defaultSetting.currency, defaultCurrency: settingsStore.defaultSetting.currency,
firstDayOfWeek: datetimeConstants.allWeekDays[self.$t('default.firstDayOfWeek')] ? datetimeConstants.allWeekDays[self.$t('default.firstDayOfWeek')].type : 0 firstDayOfWeek: settingsStore.defaultSetting.firstDayOfWeek,
}, },
submitting: false, submitting: false,
presetCategories: { presetCategories: {
@@ -224,7 +223,7 @@ export default {
return this.$locale.getAllCurrencies(); return this.$locale.getAllCurrencies();
}, },
allWeekDays() { allWeekDays() {
return datetimeConstants.allWeekDays; return this.$locale.getAllWeekDays();
}, },
currentLocale: { currentLocale: {
get: function () { get: function () {
@@ -232,7 +231,7 @@ export default {
}, },
set: function (value) { set: function (value) {
const isCurrencyDefault = this.user.defaultCurrency === this.settingsStore.defaultSetting.currency; const isCurrencyDefault = this.user.defaultCurrency === this.settingsStore.defaultSetting.currency;
const isFirstWeekDayDefault = this.user.firstDayOfWeek === (datetimeConstants.allWeekDays[this.$t('default.firstDayOfWeek')] ? datetimeConstants.allWeekDays[this.$t('default.firstDayOfWeek')].type : 0); const isFirstWeekDayDefault = this.user.firstDayOfWeek === this.settingsStore.defaultSetting.firstDayOfWeek;
this.user.language = value; this.user.language = value;
@@ -244,7 +243,7 @@ export default {
} }
if (isFirstWeekDayDefault) { if (isFirstWeekDayDefault) {
this.user.firstDayOfWeek = datetimeConstants.allWeekDays[this.$t('default.firstDayOfWeek')] ? datetimeConstants.allWeekDays[this.$t('default.firstDayOfWeek')].type : 0; this.user.firstDayOfWeek = this.settingsStore.defaultSetting.firstDayOfWeek;
} }
} }
}, },
@@ -257,6 +256,9 @@ export default {
return languageInfo.displayName; return languageInfo.displayName;
}, },
currentDayOfWeekName() {
return getNameByKeyValue(this.allWeekDays, this.user.firstDayOfWeek, 'type', 'displayName');
},
inputIsEmpty() { inputIsEmpty() {
return !!this.inputEmptyProblemMessage; return !!this.inputEmptyProblemMessage;
}, },
@@ -397,11 +399,6 @@ export default {
} }
}); });
}, },
getDayOfWeekName(dayOfWeek) {
const weekName = getNameByKeyValue(datetimeConstants.allWeekDays, dayOfWeek, 'type', 'name');
const i18nWeekNameKey = `datetime.${weekName}.long`;
return this.$t(i18nWeekNameKey);
},
getCategoryTypeName(categoryType) { getCategoryTypeName(categoryType) {
switch (categoryType) { switch (categoryType) {
case categoryConstants.allCategoryTypes.Income.toString(): case categoryConstants.allCategoryTypes.Income.toString():
+3 -6
View File
@@ -149,7 +149,7 @@
<select v-model="newProfile.firstDayOfWeek"> <select v-model="newProfile.firstDayOfWeek">
<option :value="weekDay.type" <option :value="weekDay.type"
:key="weekDay.type" :key="weekDay.type"
v-for="weekDay in allWeekDays">{{ $t(`datetime.${weekDay.name}.long`) }}</option> v-for="weekDay in allWeekDays">{{ weekDay.displayName }}</option>
</select> </select>
</f7-list-item> </f7-list-item>
@@ -226,7 +226,6 @@ import { useSettingsStore } from '@/stores/setting.js';
import { useUserStore } from '@/stores/user.js'; import { useUserStore } from '@/stores/user.js';
import { useAccountsStore } from '@/stores/account.js'; import { useAccountsStore } from '@/stores/account.js';
import datetimeConstants from '@/consts/datetime.js';
import { getNameByKeyValue } from '@/lib/common.js'; import { getNameByKeyValue } from '@/lib/common.js';
import { getCategorizedAccounts } from '@/lib/account.js'; import { getCategorizedAccounts } from '@/lib/account.js';
@@ -311,7 +310,7 @@ export default {
return getCategorizedAccounts(this.allVisibleAccounts); return getCategorizedAccounts(this.allVisibleAccounts);
}, },
allWeekDays() { allWeekDays() {
return datetimeConstants.allWeekDays; return this.$locale.getAllWeekDays();
}, },
allLongDateFormats() { allLongDateFormats() {
return this.$locale.getAllLongDateFormats(); return this.$locale.getAllLongDateFormats();
@@ -359,9 +358,7 @@ export default {
return this.$t('Unknown'); return this.$t('Unknown');
}, },
currentDayOfWeekName() { currentDayOfWeekName() {
const weekName = getNameByKeyValue(datetimeConstants.allWeekDays, this.newProfile.firstDayOfWeek, 'type', 'name'); return getNameByKeyValue(this.allWeekDays, this.newProfile.firstDayOfWeek, 'type', 'displayName');
const i18nWeekNameKey = `datetime.${weekName}.long`;
return this.$t(i18nWeekNameKey);
}, },
inputIsNotChanged() { inputIsNotChanged() {
return !!this.inputIsNotChangedProblemMessage; return !!this.inputIsNotChangedProblemMessage;