code refactor

This commit is contained in:
MaysWind
2021-05-04 22:50:06 +08:00
parent d2d2babf0b
commit ec19a18fc2
10 changed files with 91 additions and 143 deletions
+32
View File
@@ -0,0 +1,32 @@
export default function (oldRate, currentCurrency, allExchangeRates) {
const exchangeRateMap = {};
for (let i = 0; i < allExchangeRates.length; i++) {
const exchangeRate = allExchangeRates[i];
exchangeRateMap[exchangeRate.currency] = exchangeRate;
}
const toCurrencyExchangeRate = exchangeRateMap[currentCurrency];
if (!toCurrencyExchangeRate) {
return '';
}
const newRate = parseFloat(oldRate) / parseFloat(toCurrencyExchangeRate.rate);
const newRateStr = newRate.toString();
if (newRateStr.indexOf('.') < 0) {
return newRateStr;
} else {
let firstNonZeroPos = 0;
for (let i = 0; i < newRateStr.length; i++) {
if (newRateStr.charAt(i) !== '.' && newRateStr.charAt(i) !== '0') {
firstNonZeroPos = Math.min(i + 4, newRateStr.length);
break;
}
}
return newRateStr.substr(0, Math.max(6, Math.max(firstNonZeroPos, newRateStr.indexOf('.') + 2)));
}
}
+29 -13
View File
@@ -1,27 +1,43 @@
import utils from '../lib/utils.js';
export default function (value, options, keyName, valueName) {
export default function (value, options, keyField, nameField, defaultName) {
if (utils.isArray(options)) {
for (let i = 0; i < options.length; i++) {
const option = options[i];
if (keyField) {
for (let i = 0; i < options.length; i++) {
const option = options[i];
if (option[keyName] === value) {
return option[valueName];
if (option[keyField] === value) {
return option[nameField];
}
}
} else {
if (options[value]) {
const option = options[value];
return option[nameField];
}
}
} else if (utils.isObject(options)) {
for (let key in options) {
if (!Object.prototype.hasOwnProperty.call(options, key)) {
continue;
if (keyField) {
for (let key in options) {
if (!Object.prototype.hasOwnProperty.call(options, key)) {
continue;
}
const option = options[key];
if (option[keyField] === value) {
return option[nameField];
}
}
} else {
if (options[value]) {
const option = options[value];
const option = options[key];
if (option[keyName] === value) {
return option[valueName];
return option[nameField];
}
}
}
return '';
return defaultName;
}
+3 -1
View File
@@ -77,6 +77,7 @@ import optionNameFilter from './filters/optionName.js';
import itemFieldContentFilter from './filters/itemFieldContent.js';
import languageNameFilter from './filters/languageName.js';
import currencyFilter from './filters/currency.js';
import exchangeRateFilter from './filters/exchangeRate.js';
import utcOffsetFilter from './filters/utcOffset.js';
import textLimitFilter from './filters/textLimit.js';
import iconFilter from './filters/icon.js';
@@ -161,10 +162,11 @@ Vue.filter('localized', (value, options) => localizedFilter({ i18n }, value, opt
Vue.filter('moment', (value, format, options) => momentFilter(value, format, options));
Vue.filter('percent', (value, precision, lowPrecisionValue) => percentFilter(value, precision, lowPrecisionValue));
Vue.filter('format', (value, format) => formatFilter(value, format));
Vue.filter('optionName', (value, options, keyName, valueName) => optionNameFilter(value, options, keyName, valueName));
Vue.filter('optionName', (value, options, keyField, nameField, defaultName) => optionNameFilter(value, options, keyField, nameField, defaultName));
Vue.filter('itemFieldContent', (value, fieldName, defaultValue, translate) => itemFieldContentFilter({ i18n }, value, fieldName, defaultValue, translate));
Vue.filter('languageName', (languageCode) => languageNameFilter(languageCode));
Vue.filter('currency', (value, currencyCode) => currencyFilter({ i18n }, value, currencyCode));
Vue.filter('exchangeRate', (value, currentCurrency, allExchangeRates) => exchangeRateFilter(value, currentCurrency, allExchangeRates));
Vue.filter('utcOffset', (value) => utcOffsetFilter(value));
Vue.filter('textLimit', (value, maxLength) => textLimitFilter(value, maxLength));
Vue.filter('icon', (value, iconType) => iconFilter(value, iconType));
+1 -35
View File
@@ -34,7 +34,7 @@
<f7-list>
<f7-list-item v-for="exchangeRate in availableExchangeRates" :key="exchangeRate.currencyCode"
:title="exchangeRate.currencyDisplayName"
:after="exchangeRate.rate | exchangeRate(exchangeRatesData.exchangeRates, baseCurrency)"></f7-list-item>
:after="exchangeRate.rate | exchangeRate(baseCurrency, exchangeRatesData.exchangeRates)"></f7-list-item>
</f7-list>
</f7-card-content>
<f7-card-footer v-if="exchangeRatesData.exchangeRates && exchangeRatesData.exchangeRates.length">
@@ -145,40 +145,6 @@ export default {
}
});
}
},
filters: {
exchangeRate(oldRate, exchangeRates, currentCurrency) {
const exchangeRateMap = {};
for (let i = 0; i < exchangeRates.length; i++) {
const exchangeRate = exchangeRates[i];
exchangeRateMap[exchangeRate.currency] = exchangeRate;
}
const toCurrencyExchangeRate = exchangeRateMap[currentCurrency];
if (!toCurrencyExchangeRate) {
return '';
}
const newRate = parseFloat(oldRate) / parseFloat(toCurrencyExchangeRate.rate);
const newRateStr = newRate.toString();
if (newRateStr.indexOf('.') < 0) {
return newRateStr;
} else {
let firstNonZeroPos = 0;
for (let i = 0; i < newRateStr.length; i++) {
if (newRateStr.charAt(i) !== '.' && newRateStr.charAt(i) !== '0') {
firstNonZeroPos = Math.min(i + 4, newRateStr.length);
break;
}
}
return newRateStr.substr(0, Math.max(6, Math.max(firstNonZeroPos, newRateStr.indexOf('.') + 2)));
}
}
}
}
</script>
+1 -4
View File
@@ -13,7 +13,7 @@
<small>Expense</small>
</span>
<span class="card-header-content" v-else-if="!loading">
<span class="home-summary-month">{{ dateRange.thisMonth.startTime | moment('MMMM') | monthNameLocalizedKey | localized }}</span>
<span class="home-summary-month">{{ dateRange.thisMonth.startTime | moment('MMMM') | format('datetime.#{value}.long') | localized }}</span>
<span>·</span>
<small>{{ $t('Expense') }}</small>
</span>
@@ -301,9 +301,6 @@ export default {
}
return amount + (incomplete ? '+' : '');
},
monthNameLocalizedKey(monthName) {
return `datetime.${monthName}.long`;
}
}
}
+12 -23
View File
@@ -25,7 +25,7 @@
class="list-item-with-header-and-title"
link="#"
:header="$t('Account Category')"
:title="account.category | accountCategoryName(allAccountCategories) | localized"
:title="account.category | optionName(allAccountCategories, 'id', 'name') | localized"
@click="showAccountCategorySheet = true"
>
<list-item-selection-sheet value-type="item"
@@ -43,13 +43,13 @@
link="#"
:class="{ 'disabled': editAccountId }"
:header="$t('Account Type')"
:title="account.type | accountTypeName | localized"
:title="account.type | optionName(allAccountTypes, 'id', 'name') | localized"
:no-chevron="!!editAccountId"
@click="showAccountTypeSheet = true"
>
<list-item-selection-sheet value-type="item"
key-field="id" value-field="id" title-field="name"
:items="[{ id: 1, name: 'Single Account' }, { id: 2, name: 'Multi Sub Accounts' }]"
:items="allAccountTypes"
:title-i18n="true"
:show.sync="showAccountTypeSheet"
v-model="account.type">
@@ -390,6 +390,15 @@ export default {
allAccountCategories() {
return this.$constants.account.allCategories;
},
allAccountTypes() {
return [{
id: 1,
name: 'Single Account'
}, {
id: 2,
name: 'Multi Sub Accounts'
}];
},
allAccountIcons() {
return this.$constants.icons.allAccountIcons;
},
@@ -668,26 +677,6 @@ export default {
return null;
}
}
},
filters: {
accountCategoryName(categoryId, allCategories) {
for (let i = 0; i < allCategories.length; i++) {
if (allCategories[i].id === categoryId) {
return allCategories[i].name;
}
}
return '';
},
accountTypeName(type) {
if (type === 1) {
return 'Single Account';
} else if (type === 2) {
return 'Multi Sub Accounts';
}
return '';
}
}
}
</script>
+1 -14
View File
@@ -4,7 +4,7 @@
<f7-nav-left :back-link="$t('Back')"></f7-nav-left>
<f7-nav-title>
<f7-link popover-open=".chart-data-type-popover-menu">
<span>{{ query.chartDataType | chartDataTypeName(allChartDataTypes) | localized }}</span>
<span>{{ query.chartDataType | optionName(allChartDataTypes, 'type', 'name', 'Statistics') | localized }}</span>
<f7-icon size="14px" :f7="showChartDataTypePopover ? 'arrowtriangle_up_fill' : 'arrowtriangle_down_fill'"></f7-icon>
</f7-link>
</f7-nav-title>
@@ -622,19 +622,6 @@ export default {
return amount;
},
chartDataTypeName(dataType, allChartDataTypes) {
for (let chartDataTypeField in allChartDataTypes) {
if (!Object.prototype.hasOwnProperty.call(allChartDataTypes, chartDataTypeField)) {
return;
}
if (allChartDataTypes[chartDataTypeField].type === dataType) {
return allChartDataTypes[chartDataTypeField].name;
}
}
return 'Statistics';
},
totalAmountName(dataType, allChartDataTypes) {
if (dataType === allChartDataTypes.IncomeByAccount.type || dataType === allChartDataTypes.IncomeByPrimaryCategory.type || dataType === allChartDataTypes.IncomeBySecondaryCategory.type) {
return 'Total Income';
+4 -31
View File
@@ -154,7 +154,7 @@
link="#"
:class="{ 'disabled': !allVisibleAccounts.length }"
:header="$t(sourceAccountName)"
:title="transaction.sourceAccountId | accountName(allAccounts)"
:title="transaction.sourceAccountId | optionName(allAccounts, 'id', 'name')"
@click="showSourceAccountSheet = true"
>
<two-column-list-item-selection-sheet primary-key-field="id" primary-value-field="category"
@@ -176,7 +176,7 @@
link="#"
:class="{ 'disabled': !allVisibleAccounts.length }"
:header="$t('Destination Account')"
:title="transaction.destinationAccountId | accountName(allAccounts)"
:title="transaction.destinationAccountId | optionName(allAccounts, 'id', 'name')"
v-if="transaction.type === $constants.transaction.allTransactionTypes.Transfer"
@click="showDestinationAccountSheet = true"
>
@@ -225,7 +225,7 @@
</select>
<f7-block slot="title" class="list-item-custom-title no-padding">
<span>{{ transaction.utcOffset | utcOffset }}</span>
<span class="transaction-edit-timezone-name" v-if="transaction.timeZone || transaction.timeZone === ''">{{ transaction.timeZone | timeZoneName(allTimezones) }}</span>
<span class="transaction-edit-timezone-name" v-if="transaction.timeZone || transaction.timeZone === ''">{{ transaction.timeZone | optionName(allTimezones, 'name', 'displayName') }}</span>
</f7-block>
</f7-list-item>
@@ -243,7 +243,7 @@
<f7-chip class="transaction-edit-tag" media-bg-color="black"
v-for="tagId in transaction.tagIds"
:key="tagId"
:text="tagId | tagName(allTags)">
:text="tagId | optionName(allTags, 'id', 'name')">
<f7-icon slot="media" f7="number"></f7-icon>
</f7-chip>
</f7-block>
@@ -861,33 +861,6 @@ export default {
}
}
return '';
},
accountName(accountId, allAccounts) {
for (let i = 0; i < allAccounts.length; i++) {
if (allAccounts[i].id === accountId) {
return allAccounts[i].name;
}
}
return '';
},
timeZoneName(timeZoneName, allTimezones) {
for (let i = 0; i < allTimezones.length; i++) {
if (allTimezones[i].name === timeZoneName) {
return allTimezones[i].displayName;
}
}
return '';
},
tagName(tagId, allTags) {
for (let i = 0; i < allTags.length; i++) {
if (allTags[i].id === tagId) {
return allTags[i].name;
}
}
return '';
}
}
+2 -16
View File
@@ -32,10 +32,10 @@
<span :class="{ 'tabbar-item-changed': query.type > 0 }">{{ query.type | typeName('Type') | localized }}</span>
</f7-link>
<f7-link class="tabbar-text-with-ellipsis" popover-open=".category-popover-menu" :class="{ 'disabled': query.type === 1 }">
<span :class="{ 'tabbar-item-changed': query.categoryId > 0 }">{{ query.categoryId | categoryName(allCategories, $t('Category')) }}</span>
<span :class="{ 'tabbar-item-changed': query.categoryId > 0 }">{{ query.categoryId | optionName(allCategories, null, 'name', $t('Category')) }}</span>
</f7-link>
<f7-link class="tabbar-text-with-ellipsis" popover-open=".account-popover-menu">
<span :class="{ 'tabbar-item-changed': query.accountId > 0 }">{{ query.accountId | accountName(allAccounts, $t('Account')) }}</span>
<span :class="{ 'tabbar-item-changed': query.accountId > 0 }">{{ query.accountId | optionName(allAccounts, null, 'name', $t('Account')) }}</span>
</f7-link>
</f7-toolbar>
@@ -903,20 +903,6 @@ export default {
return defaultName;
}
},
categoryName(categoryId, allCategories, defaultName) {
if (allCategories[categoryId]) {
return allCategories[categoryId].name;
}
return defaultName;
},
accountName(accountId, allAccounts, defaultName) {
if (allAccounts[accountId]) {
return allAccounts[accountId].name;
}
return defaultName;
},
categoryListItemCheckedClass(category, queryCategoryId) {
if (category.id === queryCategoryId) {
return {
+6 -6
View File
@@ -173,22 +173,22 @@ export default {
return [{
value: 0,
name: 'None'
},{
}, {
value: 1,
name: 'All'
},{
}, {
value: 2,
name: 'Today or later'
},{
}, {
value: 3,
name: 'Recent 24 hours or later'
},{
}, {
value: 4,
name: 'This week or later'
},{
}, {
value: 5,
name: 'This month or later'
},{
}, {
value: 6,
name: 'This year or later'
}];