@@ -12,6 +12,8 @@ export const useProportioCalculatorStore = defineStore('proportio-calculator', (
12
12
13
13
const numberOfIngredients = computed ( ( ) => ingredients . value . length )
14
14
15
+ const scaleFactor = ref ( NaN )
16
+
15
17
function emptyIngredient ( ) {
16
18
const ingr = reactive ( {
17
19
id : crypto . randomUUID ( ) ,
@@ -22,7 +24,15 @@ export const useProportioCalculatorStore = defineStore('proportio-calculator', (
22
24
} )
23
25
24
26
ingr . stopWatchingScaledAmount = watch ( ( ) => ingr . scaledAmount , ( ) => { onScaleAmountChanged ( ingr . id ) } )
25
- watch ( ( ) => ingr . originalAmount , ( ) => { ingr . scaledAmount = NaN } )
27
+ watch ( ( ) => ingr . originalAmount , ( ) => {
28
+ // Note. When there are some components and scale factor, update ingr.scaledAmount.
29
+ // It is needed when user backed from scaled mode and add new ingredient.
30
+ if ( scaleFactor . value && scaleFactor . value !== NaN ) {
31
+ updateScaleAmount ( ingr , scaleFactor . value )
32
+ } else {
33
+ ingr . scaledAmount = NaN
34
+ }
35
+ } )
26
36
27
37
ingr . displayedName = computed ( ( ) => ingr . name === '' ? '<Без названия>' : ingr . name )
28
38
@@ -46,16 +56,19 @@ export const useProportioCalculatorStore = defineStore('proportio-calculator', (
46
56
}
47
57
48
58
ingredients . value . push ( ingr )
49
-
50
- // TODO: When there are some components and scale factor, update ingr.scaledAmount
51
59
}
52
60
53
61
function remove ( id ) {
54
- ingredients . value = ingredients . value . filter ( ( x ) => x . id != id )
62
+ if ( ingredients . value . length == 1 ) {
63
+ clear ( )
64
+ } else {
65
+ ingredients . value = ingredients . value . filter ( ( x ) => x . id != id )
66
+ }
55
67
}
56
68
57
69
function clear ( ) {
58
70
ingredients . value = [ ]
71
+ scaleFactor . value = NaN
59
72
}
60
73
61
74
// Moves ingredient with `id` up, if possible
@@ -98,22 +111,26 @@ export const useProportioCalculatorStore = defineStore('proportio-calculator', (
98
111
const changedIngr = ingredients . value . find ( ( x ) => x . id === forId )
99
112
100
113
// TODO: NaNs
101
- const scaleFactor = changedIngr . scaledAmount / changedIngr . originalAmount
102
- updateScaleAmounts ( forId , scaleFactor )
114
+ scaleFactor . value = changedIngr . scaledAmount / changedIngr . originalAmount
115
+ updateScaleAmounts ( forId , scaleFactor . value )
103
116
}
104
117
105
118
function updateScaleAmounts ( excludeId , scaleBy ) {
106
119
ingredients . value
107
120
. filter ( ( ingr ) => ingr . id !== excludeId )
108
121
. forEach ( ( ingr ) => {
109
- console . log ( `${ Date . now ( ) } updateScaleAmounts: updating ${ ingr . id } ` )
110
- ingr . stopWatchingScaledAmount ( )
111
- ingr . scaledAmount = ingr . originalAmount * scaleBy
112
- // DRY
113
- ingr . stopWatchingScaledAmount = watch ( ( ) => ingr . scaledAmount , ( ) => { onScaleAmountChanged ( ingr . id ) } )
122
+ updateScaleAmount ( ingr , scaleBy )
114
123
} )
115
124
}
116
125
126
+ function updateScaleAmount ( ingr , scaleBy ) {
127
+ console . log ( `${ Date . now ( ) } updateScaleAmounts: updating ${ ingr . id } ` )
128
+ ingr . stopWatchingScaledAmount ( )
129
+ ingr . scaledAmount = ingr . originalAmount * scaleBy
130
+ // DRY
131
+ ingr . stopWatchingScaledAmount = watch ( ( ) => ingr . scaledAmount , ( ) => { onScaleAmountChanged ( ingr . id ) } )
132
+ }
133
+
117
134
function getRecipeAsPlainTextTabular ( ) {
118
135
if ( numberOfIngredients > 0 ) {
119
136
console . error ( 'Невозможно создать рецепт в виде строки: рецепт пуст' )
@@ -148,9 +165,6 @@ export const useProportioCalculatorStore = defineStore('proportio-calculator', (
148
165
clear,
149
166
moveTowardsFirstOnce,
150
167
moveTowardsLastOnce,
151
- emptyIngredient,
152
- onScaleAmountChanged,
153
- updateScaleAmounts,
154
168
getRecipeAsPlainTextTabular,
155
169
}
156
170
} )
0 commit comments