enrecipes/app/components/EditRecipe.vue

1017 lines
31 KiB
Vue
Raw Permalink Normal View History

2020-10-14 19:32:32 +00:00
<template>
2021-04-01 10:55:35 +00:00
<Page @loaded="onPageLoad" @unloaded="onPageUnload" actionBarHidden="true">
<GridLayout rows="*, auto" columns="auto, *">
<ScrollView
rowSpan="2"
colSpan="2"
@scroll="!showUndo && onScroll($event)"
>
<StackLayout padding="0 16 72">
<Label class="pageTitle" padding="16 0" :text="`${title}` | L" />
<Image
margin="8 0 32"
2021-04-12 18:09:48 +00:00
v-if="recipe.imageSrc"
:src="recipe.imageSrc"
2021-04-01 10:55:35 +00:00
stretch="aspectFit"
class="imgHolder"
:width="screenWidth - 32"
:height="screenWidth - 32"
@tap="imageHandler"
/>
<Button
v-else
margin="8 0 32"
class="ico imgHolder"
fontSize="128"
:width="screenWidth - 32"
:height="screenWidth - 32"
:text="icon.img"
@tap="imageHandler"
/>
<!-- OVERVIEW -->
2021-03-21 17:02:04 +00:00
<StackLayout class="inputField">
<Label class="fieldLabel" :text="'title' | L" />
<TextField
:hint="'recTitle' | L"
2021-04-12 18:09:48 +00:00
v-model="recipe.title"
2021-03-21 17:02:04 +00:00
@loaded="setInputTypeText($event, 'words')"
/>
</StackLayout>
2021-04-01 10:55:35 +00:00
<GridLayout columns="*, 8, *">
<StackLayout class="inputField">
2021-03-21 17:02:04 +00:00
<Label class="fieldLabel" :text="'cui' | L" />
<TextField
2021-04-12 18:09:48 +00:00
:text="recipe.cuisine | L"
2021-03-21 17:02:04 +00:00
editable="false"
2021-04-12 18:09:48 +00:00
@focus="!modalOpen && showCuisine(true)"
2021-03-21 17:02:04 +00:00
@tap="showCuisine(false)"
/>
</StackLayout>
<StackLayout class="inputField" col="2">
<Label class="fieldLabel" :text="'cat' | L" />
<TextField
ref="category"
2021-04-12 18:09:48 +00:00
:text="recipe.category | L"
2021-03-21 17:02:04 +00:00
editable="false"
2021-04-12 18:09:48 +00:00
@focus="!modalOpen && showCategories(true)"
2021-03-21 17:02:04 +00:00
@tap="showCategories(false)"
/>
</StackLayout>
</GridLayout>
<StackLayout class="inputField">
2021-04-01 10:55:35 +00:00
<Label class="fieldLabel" :text="'ts' | L" />
2021-03-21 17:02:04 +00:00
<TextField
2021-04-01 10:55:35 +00:00
:hint="'tsInfo' | L"
2021-03-21 17:02:04 +00:00
autocapitalizationType="words"
ref="tags"
v-model="tags"
@textChange="splitTags"
returnKeyType="next"
/>
</StackLayout>
2021-04-01 10:55:35 +00:00
<GridLayout columns="*, 8, *">
<StackLayout class="inputField">
2021-03-21 17:02:04 +00:00
<Label class="fieldLabel" :text="'prepT' | L" />
<TextField
:text="timeRequired('prepTime')"
editable="false"
2021-04-12 18:09:48 +00:00
@focus="!modalOpen && setTimeRequired(true, 'prepTime')"
2021-03-21 17:02:04 +00:00
@tap="setTimeRequired(false, 'prepTime')"
/>
</StackLayout>
<StackLayout class="inputField" col="2">
<Label class="fieldLabel" :text="'cookT' | L" />
<TextField
ref="cookTime"
:text="timeRequired('cookTime')"
editable="false"
2021-04-12 18:09:48 +00:00
@focus="!modalOpen && setTimeRequired(true, 'cookTime')"
2021-03-21 17:02:04 +00:00
@tap="setTimeRequired(false, 'cookTime')"
/>
</StackLayout>
</GridLayout>
2021-04-01 10:55:35 +00:00
<GridLayout columns="*, 8, *">
<StackLayout class="inputField">
2021-03-21 17:02:04 +00:00
<Label class="fieldLabel" :text="'yieldQ' | L" />
<TextField
ref="yieldQuantity"
2021-04-12 18:09:48 +00:00
v-model="recipe.yield.quantity"
2021-03-21 17:02:04 +00:00
hint="1"
keyboardType="number"
returnKeyType="next"
/>
</StackLayout>
<StackLayout class="inputField" col="2">
<Label class="fieldLabel" :text="'yieldU' | L" />
<TextField
2021-04-12 18:09:48 +00:00
:text="`${recipe.yield.unit}` | L"
2021-03-21 17:02:04 +00:00
editable="false"
2021-04-12 18:09:48 +00:00
@focus="!modalOpen && showYieldUnits(true)"
2021-03-21 17:02:04 +00:00
@tap="showYieldUnits(false)"
/>
</StackLayout>
</GridLayout>
2021-04-01 10:55:35 +00:00
<GridLayout columns="*, 8, *">
<StackLayout class="inputField">
2021-03-21 17:02:04 +00:00
<Label class="fieldLabel" :text="'Difficulty level' | L" />
<TextField
ref="difficultyLevel"
2021-04-12 18:09:48 +00:00
:text="`${recipe.difficulty}` | L"
2021-03-21 17:02:04 +00:00
editable="false"
2021-04-12 18:09:48 +00:00
@focus="!modalOpen && showDifficultyLevel(true)"
2021-03-21 17:02:04 +00:00
@tap="showDifficultyLevel(false)"
/>
</StackLayout>
</GridLayout>
2021-04-01 10:55:35 +00:00
<!-- INGREDIENTS -->
2021-03-21 17:02:04 +00:00
<Label
2021-04-01 10:55:35 +00:00
:text="getTitleCount('ings', 'ingredients')"
2021-03-21 17:02:04 +00:00
class="sectionTitle"
/>
<GridLayout
2021-04-21 10:31:14 +00:00
columns="auto,8,auto,8,*,auto"
2021-04-12 18:09:48 +00:00
v-for="(ingredient, index) in recipe.ingredients"
2021-04-01 10:55:35 +00:00
:key="'ing' + index"
2021-03-21 17:02:04 +00:00
>
<TextField
2021-04-21 10:31:14 +00:00
width="52"
2021-04-12 18:09:48 +00:00
@loaded="!recipe.ingredients[index].item && focusField($event)"
v-model="recipe.ingredients[index].quantity"
2021-03-21 17:02:04 +00:00
hint="1.00"
keyboardType="number"
returnKeyType="next"
/>
2020-12-29 10:35:19 +00:00
2021-03-21 17:02:04 +00:00
<TextField
2021-04-21 10:31:14 +00:00
width="68"
2021-03-21 17:02:04 +00:00
col="2"
2021-04-12 18:09:48 +00:00
:text="`${recipe.ingredients[index].unit}` | L"
2021-03-21 17:02:04 +00:00
editable="false"
2021-04-12 18:09:48 +00:00
@focus="!modalOpen && showUnits($event, true, index)"
2021-03-21 17:02:04 +00:00
@tap="showUnits($event, false, index)"
/>
2020-12-29 10:35:19 +00:00
2021-03-21 17:02:04 +00:00
<TextField
ref="ingredient"
@loaded="setInputTypeText($event, 'sentence')"
col="4"
2021-04-12 18:09:48 +00:00
v-model="recipe.ingredients[index].item"
2021-03-21 17:02:04 +00:00
:hint="`${$options.filters.L('it')} ${index + 1}`"
@returnPress="
2021-04-12 18:09:48 +00:00
index + 1 == recipe.ingredients.length && addIngredient()
2021-03-21 17:02:04 +00:00
"
/>
2021-04-01 10:55:35 +00:00
<Button
2021-04-21 10:31:14 +00:00
col="5"
2021-04-01 10:55:35 +00:00
class="ico x"
2021-03-21 17:02:04 +00:00
:text="icon.x"
@tap="removeIngredient(index)"
/>
</GridLayout>
2021-04-01 10:55:35 +00:00
<Button
class="text big"
:text="'aIngBtn' | L"
2021-03-21 17:02:04 +00:00
@tap="addIngredient()"
/>
2021-04-01 10:55:35 +00:00
<!-- INSTRUCTIONS -->
<Label
:text="getTitleCount('inss', 'instructions')"
class="sectionTitle"
/>
2021-03-21 17:02:04 +00:00
<GridLayout
2021-04-21 10:31:14 +00:00
columns="*,auto"
2021-04-12 18:09:48 +00:00
v-for="(instruction, index) in recipe.instructions"
2021-04-01 10:55:35 +00:00
:key="'ins' + index"
2021-03-21 17:02:04 +00:00
>
<TextView
@loaded="focusField($event, 'multiLine')"
:hint="`${$options.filters.L('stp')} ${index + 1}`"
2021-04-12 18:09:48 +00:00
v-model="recipe.instructions[index]"
2021-03-21 17:02:04 +00:00
/>
2021-04-01 10:55:35 +00:00
<Button
2021-04-21 10:31:14 +00:00
col="1"
2021-04-01 10:55:35 +00:00
class="ico x"
2021-03-21 17:02:04 +00:00
:text="icon.x"
@tap="removeInstruction(index)"
/>
</GridLayout>
2021-04-01 10:55:35 +00:00
<Button
class="text big"
:text="'aStpBtn' | L"
2021-03-21 17:02:04 +00:00
@tap="addInstruction"
/>
2021-04-01 10:55:35 +00:00
<!-- COMBINATIONS -->
<Label
:text="getTitleCount('cmbs', 'combinations')"
class="sectionTitle"
/>
2021-03-21 17:02:04 +00:00
<GridLayout
2021-04-21 10:31:14 +00:00
columns="*,auto"
2021-04-12 18:09:48 +00:00
v-for="(combination, index) in recipe.combinations"
2021-04-01 10:55:35 +00:00
:key="'cmbs' + index"
2021-03-21 17:02:04 +00:00
>
2021-04-01 10:55:35 +00:00
<TextField
class="combField"
:text="getCombinationTitle(combination)"
editable="false"
2021-03-21 17:02:04 +00:00
/>
2021-04-01 10:55:35 +00:00
<Button
2021-04-21 10:31:14 +00:00
col="1"
2021-04-01 10:55:35 +00:00
class="ico x"
2021-03-21 17:02:04 +00:00
:text="icon.x"
2021-04-01 10:55:35 +00:00
@tap="removeCombination(combination)"
2021-03-21 17:02:04 +00:00
/>
</GridLayout>
2021-04-01 10:55:35 +00:00
<Button
class="text big"
:text="'addCmbBtn' | L"
@tap="showCombinations"
2021-03-21 17:02:04 +00:00
/>
2021-04-01 10:55:35 +00:00
<!-- NOTES -->
<Label :text="getTitleCount('nos', 'notes')" class="sectionTitle" />
2021-03-21 17:02:04 +00:00
<GridLayout
2021-04-21 10:31:14 +00:00
columns="*,auto"
2021-04-12 18:09:48 +00:00
v-for="(note, index) in recipe.notes"
2021-04-01 10:55:35 +00:00
:key="'nos' + index"
2021-03-21 17:02:04 +00:00
>
2021-04-01 10:55:35 +00:00
<TextView
@loaded="focusField($event, 'multiLine')"
:hint="`${$options.filters.L('no')} ${index + 1}`"
2021-04-12 18:09:48 +00:00
v-model="recipe.notes[index]"
2021-03-21 17:02:04 +00:00
/>
2021-04-01 10:55:35 +00:00
<Button
2021-04-21 10:31:14 +00:00
col="1"
2021-04-01 10:55:35 +00:00
class="ico x"
2021-03-21 17:02:04 +00:00
:text="icon.x"
2021-04-01 10:55:35 +00:00
@tap="removeNote(index)"
2021-03-21 17:02:04 +00:00
/>
</GridLayout>
2021-04-01 10:55:35 +00:00
<Button class="text big" :text="'aNoBtn' | L" @tap="addNote" />
2021-03-21 17:02:04 +00:00
</StackLayout>
2021-04-01 10:55:35 +00:00
</ScrollView>
<GridLayout
row="1"
@loaded="onAppBarLoad"
:colSpan="hasChanges || showUndo ? '2' : '1'"
class="appbar"
:class="{ snackBar: showUndo }"
columns="auto, *, auto"
>
2021-04-12 18:09:48 +00:00
<Button v-if="showUndo" :text="countdown" class="countdown ico tb" />
2021-04-01 10:55:35 +00:00
<Button v-else class="ico" :text="icon.back" @tap="navigateBack" />
2021-04-12 18:09:48 +00:00
<Label class="title" col="1" :hidden="!showUndo" :text="snackMsg | L" />
2021-04-01 10:55:35 +00:00
<Button
v-if="(hasChanges && !saving) || showUndo"
class="ico fab"
2021-04-12 18:09:48 +00:00
:text="showUndo ? icon.undo : icon.save"
2021-04-01 10:55:35 +00:00
col="2"
@tap="showUndo ? undoDel() : saveOperation()"
/>
2021-04-12 18:09:48 +00:00
<ActivityIndicator col="2" v-if="saving" :busy="saving" />
2021-04-01 10:55:35 +00:00
</GridLayout>
</GridLayout>
2021-03-21 17:02:04 +00:00
</Page>
2020-10-14 19:32:32 +00:00
</template>
<script>
import {
AndroidApplication,
2020-11-10 18:28:48 +00:00
Application,
ApplicationSettings,
File,
getFileAccess,
2020-11-10 18:28:48 +00:00
ImageSource,
knownFolders,
2020-11-10 18:28:48 +00:00
path,
Screen,
2020-11-02 11:36:53 +00:00
Utils,
2021-03-21 17:02:04 +00:00
Observable,
2021-04-12 18:09:48 +00:00
CoreTypes,
2021-03-21 17:02:04 +00:00
} from "@nativescript/core";
import { localize } from "@nativescript/localize";
2021-04-12 18:09:48 +00:00
import { ImageCropper } from "nativescript-imagecropper";
2021-03-21 17:02:04 +00:00
import { mapState, mapActions } from "vuex";
import ActionDialog from "./modal/ActionDialog.vue";
import ActionDialogWithSearch from "./modal/ActionDialogWithSearch.vue";
import ConfirmDialog from "./modal/ConfirmDialog.vue";
import PromptDialog from "./modal/PromptDialog.vue";
import ListPicker from "./modal/ListPicker.vue";
import * as utils from "~/shared/utils";
2021-04-01 10:55:35 +00:00
let undoTimer;
2020-10-14 19:32:32 +00:00
export default {
2021-03-21 17:02:04 +00:00
props: [
"recipeID",
"filterFavourites",
"filterTrylater",
"navigationFromView",
],
2020-10-14 19:32:32 +00:00
data() {
return {
2021-01-13 05:02:48 +00:00
title: "newRec",
2021-04-12 18:09:48 +00:00
recipe: {
2020-10-14 19:32:32 +00:00
imageSrc: null,
title: undefined,
2020-12-29 10:35:19 +00:00
cuisine: "Undefined",
category: "Undefined",
2020-12-29 10:35:19 +00:00
tags: [],
prepTime: "00:00",
cookTime: "00:00",
2020-11-02 11:36:53 +00:00
yield: {
quantity: undefined,
unit: "Serving",
2020-11-02 11:36:53 +00:00
},
2020-12-29 10:35:19 +00:00
difficulty: "Easy",
rating: 0,
2020-11-02 11:36:53 +00:00
ingredients: [],
instructions: [],
2020-11-15 10:51:10 +00:00
combinations: [],
2020-11-28 19:21:57 +00:00
notes: [],
2020-10-26 20:49:54 +00:00
isFavorite: false,
2020-11-23 09:49:58 +00:00
tried: true,
2020-11-02 11:36:53 +00:00
lastTried: null,
2020-10-26 20:49:54 +00:00
lastModified: null,
2020-12-29 10:35:19 +00:00
created: null,
2021-01-23 17:20:15 +00:00
inBag: false,
2020-10-26 20:49:54 +00:00
},
2021-04-12 18:09:48 +00:00
tempRecipe: {},
2020-12-29 10:35:19 +00:00
tags: undefined,
2020-11-11 13:50:33 +00:00
modalOpen: false,
newRecipeID: null,
2020-11-23 09:49:58 +00:00
saving: false,
2020-11-10 18:28:48 +00:00
cacheImagePath: null,
2020-11-15 10:51:10 +00:00
unSyncCombinations: [],
2021-03-21 17:02:04 +00:00
difficultyLevels: ["Easy", "Moderate", "Challenging"],
2021-04-01 10:55:35 +00:00
appbar: null,
scrollPos: 1,
countdown: 5,
snackMsg: null,
showUndo: false,
undo: false,
2021-03-21 17:02:04 +00:00
};
2020-10-14 19:32:32 +00:00
},
computed: {
2021-03-21 17:02:04 +00:00
...mapState([
"icon",
"units",
"yieldUnits",
"recipes",
"cuisines",
"categories",
"currentComponent",
2021-04-01 10:55:35 +00:00
"selectedCuisine",
"selectedCategory",
"selectedTag",
2021-04-12 18:09:48 +00:00
"appTheme",
2021-03-21 17:02:04 +00:00
]),
2020-10-14 19:32:32 +00:00
screenWidth() {
2021-03-21 17:02:04 +00:00
return Screen.mainScreen.widthDIPs;
2020-10-14 19:32:32 +00:00
},
2020-11-10 18:28:48 +00:00
hasChanges() {
2021-04-12 18:09:48 +00:00
return JSON.stringify(this.recipe) !== JSON.stringify(this.tempRecipe);
2020-10-14 19:32:32 +00:00
},
},
methods: {
2021-03-21 17:02:04 +00:00
...mapActions([
2021-04-01 10:55:35 +00:00
"setComponent",
2021-03-21 17:02:04 +00:00
"addRecipeAction",
"overwriteRecipeAction",
"addListItemAction",
"unSyncCombinationsAction",
]),
onPageLoad(args) {
const page = args.object;
page.bindingContext = new Observable();
2021-04-12 18:09:48 +00:00
this.hijackBackEvent();
},
2021-01-13 05:02:48 +00:00
onPageUnload() {
2021-03-21 17:02:04 +00:00
this.releaseBackEvent();
2021-04-01 10:55:35 +00:00
},
onAppBarLoad({ object }) {
this.appbar = object;
},
onScroll(args) {
let scrollUp;
let y = args.scrollY;
if (y) {
scrollUp = y < this.scrollPos;
this.scrollPos = Math.abs(y);
let ab = this.appbar.translateY;
if (!scrollUp && ab == 0) {
2021-04-12 18:09:48 +00:00
this.appbar.animate({
translate: { x: 0, y: 64 },
duration: 250,
curve: CoreTypes.AnimationCurve.ease,
});
2021-04-01 10:55:35 +00:00
} else if (scrollUp && ab == 64) {
Utils.ad.dismissSoftInput();
2021-04-12 18:09:48 +00:00
this.appbar.animate({
translate: { x: 0, y: 0 },
duration: 250,
curve: CoreTypes.AnimationCurve.ease,
});
2021-04-01 10:55:35 +00:00
}
}
2021-01-13 05:02:48 +00:00
},
2021-04-12 18:09:48 +00:00
// PHOTO HANDLERS
imageHandler() {
this.clearEmptyFields(true);
if (this.recipe.imageSrc) {
this.modalOpen = true;
2021-04-14 09:27:40 +00:00
this.$showModal(ActionDialog, {
2021-04-12 18:09:48 +00:00
props: {
title: "recPic",
2021-04-14 09:27:40 +00:00
list: ["aap", "rp"],
2021-04-12 18:09:48 +00:00
},
}).then((action) => {
this.modalOpen = false;
2021-04-14 09:27:40 +00:00
switch (action) {
case "aap":
this.imagePicker();
break;
case "rp":
this.recipe.imageSrc = null;
break;
}
2021-04-12 18:09:48 +00:00
});
} else this.imagePicker();
2020-11-02 11:36:53 +00:00
},
2021-04-12 18:09:48 +00:00
imagePicker() {
let aT = this.appTheme;
utils.getRecipePhoto().then((uri) => {
if (uri != null) {
this.cacheImagePath = path.join(
knownFolders.temp().path,
`${this.getRandomID()}.jpg`
2021-03-21 17:02:04 +00:00
);
2021-04-12 18:09:48 +00:00
utils.copyPhotoToCache(uri, this.cacheImagePath).then((imgPath) => {
if (imgPath) {
ImageSource.fromFile(imgPath).then((image) => {
ImageCropper.prototype
.show(
image,
{
width: 1080,
height: 1080,
},
{
hideBottomControls: true,
toolbarTitle: localize("cPic"),
statusBarColor: "#ff5200",
toolbarTextColor: aT == "Light" ? "#212529" : "#f1f3f5",
toolbarColor:
aT == "Light"
? "#f1f3f5"
: aT == "Dark"
? "#212529"
: "#000000",
cropFrameColor: "#ff5200",
}
)
.then((cropped) => {
cropped.image.saveToFile(this.cacheImagePath, "jpg", 75);
this.recipe.imageSrc = this.cacheImagePath;
});
});
}
2021-04-12 18:09:48 +00:00
});
2020-10-21 17:54:45 +00:00
}
2021-03-21 17:02:04 +00:00
});
2020-10-14 19:32:32 +00:00
},
2021-04-12 18:09:48 +00:00
2020-11-02 11:36:53 +00:00
// DATA LIST
2021-03-21 17:02:04 +00:00
showCuisine(focus) {
this.modalOpen = true;
this.$showModal(ActionDialog, {
2020-12-29 10:35:19 +00:00
props: {
2021-01-13 05:02:48 +00:00
title: "cui",
2020-12-29 10:35:19 +00:00
list: this.cuisines,
2021-01-13 05:02:48 +00:00
action: "aNBtn",
2020-12-29 10:35:19 +00:00
},
2021-03-21 17:02:04 +00:00
}).then((action) => {
if (action == "aNBtn") {
this.$showModal(PromptDialog, {
2020-12-29 10:35:19 +00:00
props: {
2021-01-13 05:02:48 +00:00
title: "newCui",
action: "aBtn",
2020-12-29 10:35:19 +00:00
},
2021-03-21 17:02:04 +00:00
}).then((item) => {
2021-04-12 18:09:48 +00:00
this.modalOpen = false;
2021-03-21 17:02:04 +00:00
if (item.length) {
2021-04-12 18:09:48 +00:00
this.recipe.cuisine = item;
2021-03-21 17:02:04 +00:00
this.addListItemAction({
2020-12-29 10:35:19 +00:00
item,
2021-03-21 17:02:04 +00:00
listName: "cuisines",
});
if (focus) this.autoFocusField("category", false);
2020-12-29 10:35:19 +00:00
}
2021-03-21 17:02:04 +00:00
});
2020-12-29 10:35:19 +00:00
} else {
2021-04-12 18:09:48 +00:00
this.modalOpen = false;
if (action) {
this.recipe.cuisine = action;
if (focus) this.autoFocusField("category", false);
} else
this.cuisines.includes(this.recipe.cuisine)
? mull
: (this.recipe.cuisine = "Undefined");
2020-12-29 10:35:19 +00:00
}
2021-03-21 17:02:04 +00:00
});
2020-12-29 10:35:19 +00:00
},
2021-03-21 17:02:04 +00:00
showCategories(focus) {
this.modalOpen = true;
this.$showModal(ActionDialog, {
2020-10-21 17:54:45 +00:00
props: {
2021-01-13 05:02:48 +00:00
title: "cat",
2020-12-29 10:35:19 +00:00
list: this.categories,
2021-01-13 05:02:48 +00:00
action: "aNBtn",
2020-10-21 17:54:45 +00:00
},
2021-03-21 17:02:04 +00:00
}).then((action) => {
if (action == "aNBtn") {
this.$showModal(PromptDialog, {
2020-10-21 17:54:45 +00:00
props: {
2021-01-13 05:02:48 +00:00
title: "nwCat",
action: "aBtn",
2020-10-21 17:54:45 +00:00
},
2021-03-21 17:02:04 +00:00
}).then((item) => {
2021-04-12 18:09:48 +00:00
this.modalOpen = false;
2021-03-21 17:02:04 +00:00
if (item.length) {
2021-04-12 18:09:48 +00:00
this.recipe.category = item;
2021-03-21 17:02:04 +00:00
this.addListItemAction({
2020-12-29 10:35:19 +00:00
item,
2021-03-21 17:02:04 +00:00
listName: "categories",
});
if (focus) this.autoFocusField("tags", true);
2020-10-21 17:54:45 +00:00
}
2021-03-21 17:02:04 +00:00
});
} else {
2021-04-12 18:09:48 +00:00
this.modalOpen = false;
if (action) {
this.recipe.category = action;
if (focus) this.autoFocusField("tags", true);
} else
this.categories.includes(this.recipe.category)
? mull
: (this.recipe.category = "Undefined");
2020-10-14 19:32:32 +00:00
}
2021-03-21 17:02:04 +00:00
});
},
2021-03-21 17:02:04 +00:00
showYieldUnits(focus) {
this.modalOpen = true;
this.$showModal(ActionDialog, {
2020-11-02 11:36:53 +00:00
props: {
2021-01-13 05:02:48 +00:00
title: "yieldU",
2020-12-29 10:35:19 +00:00
list: this.yieldUnits,
2021-01-13 05:02:48 +00:00
action: "aNBtn",
2020-11-02 11:36:53 +00:00
},
2021-03-21 17:02:04 +00:00
}).then((action) => {
if (action == "aNBtn") {
this.$showModal(PromptDialog, {
2020-11-02 11:36:53 +00:00
props: {
2021-01-13 05:02:48 +00:00
title: "nwYiU",
action: "aBtn",
2020-11-02 11:36:53 +00:00
},
2021-03-21 17:02:04 +00:00
}).then((item) => {
2021-04-12 18:09:48 +00:00
this.modalOpen = false;
2021-03-21 17:02:04 +00:00
if (item.length) {
2021-04-12 18:09:48 +00:00
this.recipe.yield.unit = item;
2021-03-21 17:02:04 +00:00
this.addListItemAction({
2020-12-29 10:35:19 +00:00
item,
2021-03-21 17:02:04 +00:00
listName: "yieldUnits",
});
if (focus) this.autoFocusField("difficultyLevel", false);
2020-11-02 11:36:53 +00:00
}
2021-03-21 17:02:04 +00:00
});
2020-12-29 10:35:19 +00:00
} else {
2021-04-12 18:09:48 +00:00
this.modalOpen = false;
if (action) {
this.recipe.yield.unit = action;
if (focus) this.autoFocusField("difficultyLevel", false);
} else
this.yieldUnits.includes(this.recipe.yield.unit)
? mull
: (this.recipe.yield.unit = "Serving");
2020-12-29 10:35:19 +00:00
}
2021-03-21 17:02:04 +00:00
});
2020-12-29 10:35:19 +00:00
},
2021-03-21 17:02:04 +00:00
showDifficultyLevel(focus) {
this.modalOpen = true;
this.$showModal(ActionDialog, {
2020-12-29 10:35:19 +00:00
props: {
title: "Difficulty level",
list: this.difficultyLevels,
},
2021-03-21 17:02:04 +00:00
}).then((action) => {
2021-04-12 18:09:48 +00:00
this.modalOpen = false;
2021-03-21 17:02:04 +00:00
if (action) {
2021-04-12 18:09:48 +00:00
this.recipe.difficulty = action;
2021-03-21 17:02:04 +00:00
if (focus) this.addIngredient();
2021-04-12 18:09:48 +00:00
} else
this.difficultyLevels.includes(this.recipe.difficulty)
2021-03-21 17:02:04 +00:00
? mull
2021-04-12 18:09:48 +00:00
: (this.recipe.difficulty = "Easy");
2021-03-21 17:02:04 +00:00
});
2020-11-02 11:36:53 +00:00
},
2021-03-21 17:02:04 +00:00
showUnits(e, focus, index) {
this.modalOpen = true;
this.$showModal(ActionDialog, {
2020-11-02 11:36:53 +00:00
props: {
2021-01-13 05:02:48 +00:00
title: "Unit",
2020-12-29 10:35:19 +00:00
list: this.units,
2021-01-13 05:02:48 +00:00
action: "aNBtn",
2020-11-02 11:36:53 +00:00
},
2021-03-21 17:02:04 +00:00
}).then((action) => {
if (action == "aNBtn") {
this.$showModal(PromptDialog, {
2020-12-29 10:35:19 +00:00
props: {
2021-01-13 05:02:48 +00:00
title: "newUnit",
action: "aBtn",
2020-12-29 10:35:19 +00:00
},
2021-03-21 17:02:04 +00:00
}).then((item) => {
2021-04-12 18:09:48 +00:00
this.modalOpen = false;
2021-03-21 17:02:04 +00:00
if (item.length) {
2021-04-12 18:09:48 +00:00
this.recipe.ingredients[index].unit = item;
2021-03-21 17:02:04 +00:00
this.addListItemAction({
2020-12-29 10:35:19 +00:00
item,
2021-03-21 17:02:04 +00:00
listName: "units",
});
2021-04-12 18:09:48 +00:00
if (focus && this.recipe.ingredients.length - 1 === index)
2021-03-21 17:02:04 +00:00
this.autoFocusRefField("ingredient", index);
2020-11-15 21:13:06 +00:00
}
2021-03-21 17:02:04 +00:00
});
2021-04-12 18:09:48 +00:00
} else {
2021-03-21 17:02:04 +00:00
this.modalOpen = false;
2021-04-12 18:09:48 +00:00
if (action) {
this.recipe.ingredients[index].unit = action;
if (focus && this.recipe.ingredients.length - 1 === index)
this.autoFocusRefField("ingredient", index);
}
2020-11-11 13:50:33 +00:00
}
2021-03-21 17:02:04 +00:00
});
},
2021-04-12 18:09:48 +00:00
showCombinations() {
Utils.ad.dismissSoftInput();
this.modalOpen = true;
let existingCombinations = [...this.recipe.combinations, this.recipe.id];
let filteredRecipes = this.recipes.filter(
(e) => !existingCombinations.includes(e.id)
2021-03-21 17:02:04 +00:00
);
2021-04-12 18:09:48 +00:00
this.$showModal(ActionDialogWithSearch, {
2020-11-06 09:07:41 +00:00
props: {
2021-04-12 18:09:48 +00:00
title: "selRec",
recipes: filteredRecipes,
2020-11-06 09:07:41 +00:00
},
2021-04-12 18:09:48 +00:00
}).then((res) => {
this.modalOpen = false;
if (res) this.recipe.combinations.push(res);
2021-03-21 17:02:04 +00:00
});
},
2021-04-12 18:09:48 +00:00
2020-11-10 18:28:48 +00:00
// INPUT FIELD HANDLERS
2020-10-14 19:32:32 +00:00
addIngredient() {
2021-04-12 18:09:48 +00:00
let ingredients = this.recipe.ingredients;
2021-03-21 17:02:04 +00:00
let unit = ingredients.length
? ingredients[ingredients.length - 1].unit
: "unit";
2021-04-12 18:09:48 +00:00
this.recipe.ingredients.push({
2020-10-21 17:54:45 +00:00
item: "",
2020-10-14 19:32:32 +00:00
quantity: null,
2020-12-29 10:35:19 +00:00
unit,
2021-03-21 17:02:04 +00:00
});
},
removeIngredient(index) {
this.modalOpen = true;
2021-04-12 18:09:48 +00:00
if (this.recipe.ingredients[index].item.length) {
let item = this.recipe.ingredients[index];
this.recipe.ingredients.splice(index, 1);
2021-04-01 10:55:35 +00:00
this.showUndoBar("rmIng").then(
2021-04-12 18:09:48 +00:00
(res) => res && this.recipe.ingredients.splice(index, 0, item)
2021-04-01 10:55:35 +00:00
);
2020-12-29 10:35:19 +00:00
} else {
2021-04-12 18:09:48 +00:00
this.recipe.ingredients.splice(index, 1);
2020-12-29 10:35:19 +00:00
}
2021-04-21 10:31:14 +00:00
setTimeout(() => (this.modalOpen = false), 200);
2020-10-14 19:32:32 +00:00
},
addInstruction() {
2021-04-12 18:09:48 +00:00
this.recipe.instructions.push("");
2021-03-21 17:02:04 +00:00
},
removeInstruction(index) {
2021-04-12 18:09:48 +00:00
if (this.recipe.instructions[index].length) {
let item = this.recipe.instructions[index];
this.recipe.instructions.splice(index, 1);
2021-04-01 10:55:35 +00:00
this.showUndoBar("rmIns").then(
2021-04-12 18:09:48 +00:00
(res) => res && this.recipe.instructions.splice(index, 0, item)
2021-04-01 10:55:35 +00:00
);
2021-04-12 18:09:48 +00:00
} else this.recipe.instructions.splice(index, 1);
2020-10-14 19:32:32 +00:00
},
2021-01-13 05:02:48 +00:00
addNote() {
2021-04-12 18:09:48 +00:00
this.recipe.notes.push("");
2021-03-21 17:02:04 +00:00
},
removeNote(index) {
2021-04-12 18:09:48 +00:00
if (this.recipe.notes[index].length) {
let item = this.recipe.notes[index];
this.recipe.notes.splice(index, 1);
2021-04-01 10:55:35 +00:00
this.showUndoBar("rmN").then((res) =>
2021-04-12 18:09:48 +00:00
this.recipe.notes.splice(index, 0, item)
2021-04-01 10:55:35 +00:00
);
2021-04-12 18:09:48 +00:00
} else this.recipe.notes.splice(index, 1);
2021-01-13 05:02:48 +00:00
},
2021-03-21 17:02:04 +00:00
getCombinationTitle(id) {
return this.recipes.filter((e) => e.id === id)[0].title;
2020-11-15 10:51:10 +00:00
},
2021-03-21 17:02:04 +00:00
removeCombination(id) {
2021-04-12 18:09:48 +00:00
let index = this.recipe.combinations.indexOf(id);
this.recipe.combinations.splice(index, 1);
2021-03-21 17:02:04 +00:00
this.unSyncCombinations.push(id);
2021-04-01 10:55:35 +00:00
this.showUndoBar("rmCmb").then((res) =>
2021-04-12 18:09:48 +00:00
this.recipe.combinations.splice(index, 0, id)
2021-04-01 10:55:35 +00:00
);
2020-11-15 10:51:10 +00:00
},
2021-04-12 18:09:48 +00:00
2020-11-10 18:28:48 +00:00
// SAVE OPERATION
2021-04-01 10:55:35 +00:00
clearEmptyFields(bool) {
2021-04-12 18:09:48 +00:00
if (!this.recipe.title && !bool) this.recipe.title = localize("untRec");
if (!this.recipe.yield.quantity) this.recipe.yield.quantity = 1;
this.recipe.ingredients = this.recipe.ingredients.filter((e) => e.item);
2021-03-21 17:02:04 +00:00
let vm = this;
2021-03-21 17:02:04 +00:00
function clearEmpty(arr) {
2021-04-12 18:09:48 +00:00
vm.recipe[arr] = vm.recipe[arr].filter((e) => e);
2020-11-02 11:36:53 +00:00
}
2021-03-21 17:02:04 +00:00
clearEmpty("instructions");
clearEmpty("notes");
2020-11-02 11:36:53 +00:00
},
saveOperation() {
2021-03-21 17:02:04 +00:00
this.saving = this.modalOpen = true;
this.clearEmptyFields();
2021-04-12 18:09:48 +00:00
this.recipe.lastModified = new Date();
ApplicationSettings.setString("previousCuisine", this.recipe.cuisine);
ApplicationSettings.setString("previousCategory", this.recipe.category);
2021-03-21 17:02:04 +00:00
ApplicationSettings.setString(
"previousYieldUnit",
2021-04-12 18:09:48 +00:00
this.recipe.yield.unit
2021-03-21 17:02:04 +00:00
);
if (this.cacheImagePath) {
let recipeImage = path.join(
knownFolders.documents().getFolder("EnRecipes").getFolder("Images")
.path,
`${this.getRandomID()}.jpg`
);
let binarySource = File.fromPath(this.cacheImagePath).readSync();
File.fromPath(recipeImage).writeSync(binarySource);
2021-04-12 18:09:48 +00:00
this.recipe.imageSrc = recipeImage;
2021-04-21 10:31:14 +00:00
knownFolders.temp().clear();
2020-11-10 18:28:48 +00:00
}
2021-04-12 18:09:48 +00:00
if (this.recipe.imageSrc) {
2021-03-21 17:02:04 +00:00
if (
2021-04-12 18:09:48 +00:00
this.tempRecipe.imageSrc &&
this.tempRecipe.imageSrc !== this.recipe.imageSrc
2021-03-21 17:02:04 +00:00
) {
2021-04-12 18:09:48 +00:00
getFileAccess().deleteFile(this.tempRecipe.imageSrc);
2020-11-10 18:28:48 +00:00
}
2021-04-12 18:09:48 +00:00
} else if (this.tempRecipe.imageSrc) {
getFileAccess().deleteFile(this.tempRecipe.imageSrc);
}
2021-03-21 17:02:04 +00:00
this.unSyncCombinationsAction({
2020-11-15 10:51:10 +00:00
id: this.recipeID,
combinations: this.unSyncCombinations,
2021-03-21 17:02:04 +00:00
});
this.saveRecipe();
2020-11-02 11:36:53 +00:00
},
saveRecipe() {
2021-03-21 17:02:04 +00:00
if (this.recipeID) {
this.overwriteRecipeAction({
2020-11-02 11:36:53 +00:00
id: this.recipeID,
2021-04-12 18:09:48 +00:00
recipe: this.recipe,
2021-03-21 17:02:04 +00:00
});
} else {
2021-04-12 18:09:48 +00:00
this.recipe.id = this.newRecipeID;
2021-03-21 17:02:04 +00:00
this.addRecipeAction({
2020-11-02 11:36:53 +00:00
id: this.newRecipeID,
2021-04-12 18:09:48 +00:00
recipe: this.recipe,
2021-03-21 17:02:04 +00:00
});
2020-11-02 11:36:53 +00:00
}
2021-04-12 18:09:48 +00:00
setTimeout(() => (this.saving = false), 100);
this.$navigateBack();
},
// UNDO OPERATION
showUndoBar(message) {
return new Promise((resolve, reject) => {
this.showUndo = true;
this.appbar.translateY = 0;
this.snackMsg = message;
this.countdown = 5;
let a = 5;
clearTimeout(undoTimer);
undoTimer = setInterval(() => {
if (this.undo) {
this.showUndo = this.undo = false;
clearTimeout(undoTimer);
resolve(true);
}
this.countdown = Math.round((a -= 0.1));
if (this.countdown < 1) {
this.showUndo = false;
clearTimeout(undoTimer);
reject(true);
}
}, 100);
});
},
undoDel() {
this.undo = true;
},
// HELPERS
autoFocusField(ref, showSoftInput) {
this.$refs[ref].nativeView.focus();
if (showSoftInput) {
setTimeout(() => {
Utils.ad.showSoftInput(this.$refs[ref].nativeView.android);
}, 100);
}
},
autoFocusRefField(ref, index) {
this.$refs[ref][index].nativeView.focus();
2021-03-21 17:02:04 +00:00
setTimeout(() => {
2021-04-12 18:09:48 +00:00
Utils.ad.showSoftInput(this.$refs[ref][index].nativeView.android);
2021-03-21 17:02:04 +00:00
}, 100);
2021-04-12 18:09:48 +00:00
},
splitTags() {
let tags = [];
let string;
if (this.tags) {
tags = this.tags
.split(" ")
.map((e) => {
string = e.replace(/^[^\w\s]+/, "");
if (/^[A-Za-z]+/.test(string)) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
})
.filter((e) => e);
}
this.recipe.tags = tags;
},
joinTags() {
this.tags = this.recipe.tags.join(" ");
},
timeRequired(time) {
let t = this.recipe[time].split(":");
let h = parseInt(t[0]);
let m = parseInt(t[1]);
let hr = localize("hr");
let min = localize("min");
return h ? (m ? `${h} ${hr} ${m} ${min}` : `${h} ${hr}`) : `${m} ${min}`;
},
focusField(args, type) {
if (type) this.setInputTypeText(args, type);
if (!args.object.text) {
args.object.focus();
2021-04-21 10:31:14 +00:00
setTimeout(() => Utils.ad.showSoftInput(args.object.android), 100);
2021-04-12 18:09:48 +00:00
}
},
setInputTypeText(args, type) {
let field = args.object;
let common =
android.text.InputType.TYPE_CLASS_TEXT |
android.text.InputType.TYPE_TEXT_FLAG_AUTO_CORRECT;
switch (type) {
case "words":
field.android.setInputType(
android.text.InputType.TYPE_TEXT_FLAG_CAP_WORDS | common
);
break;
case "sentence":
field.android.setInputType(
android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | common
);
break;
case "multiLine":
field.android.setInputType(
android.text.InputType.TYPE_TEXT_FLAG_MULTI_LINE |
android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES |
common
);
break;
}
},
getRandomID() {
let res = "";
let chars = "abcdefghijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < 16; i++) {
res += chars.charAt(Math.floor(Math.random() * chars.length));
}
return res;
},
setTimeRequired(focus, time) {
this.modalOpen = true;
let t = this.recipe[time].split(":");
let hr = t[0];
let min = t[1];
this.$showModal(ListPicker, {
props: {
title: `${time == "prepTime" ? "prepT" : "cookT"}`,
action: "SET",
selectedHr: hr,
selectedMin: min,
},
}).then((result) => {
this.modalOpen = false;
if (result) {
this.recipe[time] = result;
if (focus) {
switch (time) {
case "prepTime":
this.autoFocusField("cookTime", false);
break;
case "cookTime":
this.autoFocusField("yieldQuantity", true);
break;
}
}
}
});
},
getTitleCount(title, type) {
let count = this.recipe[type].length;
let text = count ? ` (${count})` : "";
return localize(title) + text;
},
// NAVIGATION HANDLERS
hijackBackEvent() {
AndroidApplication.on(
AndroidApplication.activityBackPressedEvent,
this.backEvent
);
},
releaseBackEvent() {
AndroidApplication.off(
AndroidApplication.activityBackPressedEvent,
this.backEvent
);
},
backEvent(args) {
if (!this.modalOpen) {
args.cancel = true;
this.navigateBack();
}
},
navigateBack() {
if (this.hasChanges) {
this.modalOpen = true;
this.$showModal(ConfirmDialog, {
props: {
title: "unsaved",
description: localize("disc"),
cancelButtonText: "disBtn",
okButtonText: "kEdit",
},
}).then((action) => {
this.modalOpen = false;
if (action != null && !action) this.$navigateBack();
});
} else this.$navigateBack();
2020-10-14 19:32:32 +00:00
},
},
2020-10-26 20:49:54 +00:00
created() {
2021-04-21 10:31:14 +00:00
setTimeout(() => {
2021-04-01 10:55:35 +00:00
this.setComponent("EditRecipe");
2021-03-21 17:02:04 +00:00
}, 500);
this.title = this.recipeID ? "editRec" : "newRec";
if (this.recipeID) {
let recipe = this.recipes.filter((e) => e.id === this.recipeID)[0];
2021-04-12 18:09:48 +00:00
Object.assign(this.recipe, JSON.parse(JSON.stringify(recipe)));
Object.assign(this.tempRecipe, JSON.parse(JSON.stringify(this.recipe)));
if (this.recipe.tags.length) this.joinTags();
} else {
2021-04-12 18:09:48 +00:00
this.recipe.cuisine = this.selectedCuisine
2021-03-21 17:02:04 +00:00
? /all/.test(this.selectedCuisine)
? "Undefined"
: this.selectedCuisine
: ApplicationSettings.getString("previousCuisine", "Undefined");
2021-04-12 18:09:48 +00:00
this.recipe.category = this.selectedCategory
2021-03-21 17:02:04 +00:00
? /all/.test(this.selectedCategory)
? "Undefined"
: this.selectedCategory
: ApplicationSettings.getString("previousCategory", "Undefined");
if (this.selectedTag && !/all/.test(this.selectedTag)) {
this.tags = this.selectedTag;
this.splitTags();
2020-12-29 10:35:19 +00:00
}
2021-04-12 18:09:48 +00:00
this.recipe.yield.unit = ApplicationSettings.getString(
2021-03-21 17:02:04 +00:00
"previousYieldUnit",
"Serving"
);
2021-04-12 18:09:48 +00:00
if (this.filterFavourites) this.recipe.isFavorite = true;
if (this.filterTrylater) this.recipe.tried = false;
this.recipe.created = new Date();
Object.assign(this.tempRecipe, JSON.parse(JSON.stringify(this.recipe)));
2021-03-21 17:02:04 +00:00
this.newRecipeID = this.getRandomID();
2020-10-26 20:49:54 +00:00
}
},
2021-03-21 17:02:04 +00:00
};
2020-10-14 19:32:32 +00:00
</script>