support fraction display

This commit is contained in:
vabene1111
2025-02-01 08:51:13 +01:00
parent 7b2ade5fcd
commit d0703f9639
3 changed files with 9 additions and 9 deletions

View File

@@ -36,7 +36,7 @@
<td style="width: 1%; text-wrap: nowrap" class="pa-0"> <td style="width: 1%; text-wrap: nowrap" class="pa-0">
<v-checkbox-btn v-model="i.checked" color="success" v-if="!i.isHeader"></v-checkbox-btn> <v-checkbox-btn v-model="i.checked" color="success" v-if="!i.isHeader"></v-checkbox-btn>
</td> </td>
<td style="width: 1%; text-wrap: nowrap" class="pr-1">{{ i.amount * props.ingredientFactor }}</td> <td style="width: 1%; text-wrap: nowrap" class="pr-1" v-html="calculateFoodAmount(i.amount, props.ingredientFactor, useUserPreferenceStore().userSettings.useFractions)"></td>
<td style="width: 1%; text-wrap: nowrap" class="pr-1"> <td style="width: 1%; text-wrap: nowrap" class="pr-1">
<template v-if="i.unit"> {{ i.unit.name }}</template> <template v-if="i.unit"> {{ i.unit.name }}</template>
</td> </td>
@@ -59,6 +59,8 @@
<script lang="ts" setup> <script lang="ts" setup>
import {Ingredient} from "@/openapi"; import {Ingredient} from "@/openapi";
import {computed} from "vue"; import {computed} from "vue";
import {calculateFoodAmount} from "../../utils/number_utils";
import {useUserPreferenceStore} from "../../stores/UserPreferenceStore";
const ingredients = defineModel<Ingredient[]>({required: true}) const ingredients = defineModel<Ingredient[]>({required: true})

View File

@@ -6,6 +6,8 @@
<script lang="ts" setup> <script lang="ts" setup>
import {useUserPreferenceStore} from "@/stores/UserPreferenceStore";
const props = defineProps({ const props = defineProps({
number: Number, number: Number,
factor: { factor: {
@@ -22,6 +24,6 @@ import {calculateFoodAmount} from "@/utils/number_utils.ts";
* @return {number | string} * @return {number | string}
*/ */
function calculateAmount(x) { function calculateAmount(x) {
return calculateFoodAmount(x, props.factor) // TODO reactive bind props return calculateFoodAmount(x, props.factor, useUserPreferenceStore().userSettings.useFractions)
} }
</script> </script>

View File

@@ -1,8 +1,3 @@
function getUserPreference(pref: string) {
return false // TODO only placeholder, add real function
}
/** /**
* round to the number of decimals specified in user preferences * round to the number of decimals specified in user preferences
* @param num number to round * @param num number to round
@@ -16,9 +11,10 @@ export function roundDecimals(num: number) {
* calculates the amount of food, based on the factor and converts it to a fraction if that is the users preference * calculates the amount of food, based on the factor and converts it to a fraction if that is the users preference
* @param amount food amount to calculate based upon * @param amount food amount to calculate based upon
* @param factor factor to scale food amount by * @param factor factor to scale food amount by
* @param useFractions if the returned value should be a fraction or not
*/ */
export function calculateFoodAmount(amount: number, factor: number) { export function calculateFoodAmount(amount: number, factor: number, useFractions: boolean = false) {
if (getUserPreference("use_fractions")) { if (useFractions) {
let return_string = "" let return_string = ""
let fraction = frac(amount * factor, 16, true) let fraction = frac(amount * factor, 16, true)