From 26b4401450fdb632e66cf2b5e13226f412d8d409 Mon Sep 17 00:00:00 2001 From: Dan Elbert Date: Tue, 17 Apr 2018 09:59:38 -0500 Subject: [PATCH] recipe --- app/javascript/components/RecipeShow.vue | 26 ++++++++++++++----- app/javascript/components/TheRecipe.vue | 32 +++++++++++++++++++---- app/javascript/lib/GlobalMixins.js | 33 ++++++++++++++++++++++++ app/javascript/styles/index.scss | 4 +++ app/models/recipe.rb | 7 +++++ app/views/home/index.html.erb | 2 ++ app/views/recipes/_recipe.json.jbuilder | 2 +- public/manifest.json | 8 ++++++ 8 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 public/manifest.json diff --git a/app/javascript/components/RecipeShow.vue b/app/javascript/components/RecipeShow.vue index c211713..47547e0 100644 --- a/app/javascript/components/RecipeShow.vue +++ b/app/javascript/components/RecipeShow.vue @@ -29,14 +29,14 @@
-
+
Ingredients - +
-
    +
    • {{i.display_name}}
    • @@ -47,7 +47,7 @@
      Directions
      -
      +
      @@ -130,8 +130,8 @@
      - - + +
      @@ -185,8 +185,22 @@ } }, + watch: { + recipe: { + handler: function(r) { + if (r) { + this.scaleValue = r.converted_scale || '1'; + this.systemConvertValue = r.converted_system; + this.unitConvertValue = r.converted_unit; + } + }, + immediate: true + } + }, + methods: { convert() { + this.showConvertDialog = false; this.$router.push({name: 'recipe', query: { scale: this.scaleValue, system: this.systemConvertValue, unit: this.unitConvertValue }}); }, diff --git a/app/javascript/components/TheRecipe.vue b/app/javascript/components/TheRecipe.vue index 2c198bc..3a4ba72 100644 --- a/app/javascript/components/TheRecipe.vue +++ b/app/javascript/components/TheRecipe.vue @@ -8,6 +8,11 @@
      {{tag}}
      +
      + {{recipe.converted_scale}} X + {{recipe.converted_system}} + {{recipe.converted_unit}} +

@@ -34,17 +39,34 @@ computed: { ...mapState({ recipeId: state => state.route.params.id, + routeQuery: state => state.route.query, scale: state => state.route.query.scale || null, system: state => state.route.query.system || null, unit: state => state.route.query.unit || null - }) + }), + + isScaled() { + return this.recipe.converted_scale !== null && this.recipe.converted_scale.length > 0 && this.recipe.converted_scale !== "1"; + } + }, + + watch: { + routeQuery() { + this.refreshData(); + } + }, + + methods: { + refreshData() { + this.loadResource( + api.getRecipe(this.recipeId, this.scale, this.system, this.unit) + .then(data => { this.recipe = data; return data; }) + ); + } }, created() { - this.loadResource( - api.getRecipe(this.recipeId, this.scale, this.system, this.unit) - .then(data => { this.recipe = data; return data; }) - ); + this.refreshData(); }, components: { diff --git a/app/javascript/lib/GlobalMixins.js b/app/javascript/lib/GlobalMixins.js index 1c1ef95..699be15 100644 --- a/app/javascript/lib/GlobalMixins.js +++ b/app/javascript/lib/GlobalMixins.js @@ -27,4 +27,37 @@ Vue.mixin({ .then(() => this.setLoading(false)); } } +}); + +function clickStrikeClick(evt) { + const isStrikable = el => el && el.tagName === "LI"; + const strikeClass = "is-strikethrough"; + + let t = evt.target; + + while (t !== null && t !== this && !isStrikable(t)) { + t = t.parentElement; + } + + if (isStrikable(t)) { + const classList = t.className.split(" "); + const strIdx = classList.findIndex(c => c === strikeClass); + if (strIdx >= 0) { + classList.splice(strIdx, 1); + } else { + classList.push(strikeClass); + } + + t.className = classList.join(" "); + } +} + +Vue.directive('click-strike', { + bind(el) { + el.addEventListener("click", clickStrikeClick); + }, + + unbind(el) { + el.removeEventListener("click", clickStrikeClick); + } }); \ No newline at end of file diff --git a/app/javascript/styles/index.scss b/app/javascript/styles/index.scss index b584b49..133ec82 100644 --- a/app/javascript/styles/index.scss +++ b/app/javascript/styles/index.scss @@ -42,3 +42,7 @@ body { .pagination:not(:last-child) { margin-bottom: 1em; } + +.is-strikethrough { + text-decoration: line-through; +} \ No newline at end of file diff --git a/app/models/recipe.rb b/app/models/recipe.rb index 5e6aeac..2c83260 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -16,31 +16,38 @@ class Recipe < ApplicationRecord validates :total_time, numericality: true, allow_blank: true validates :active_time, numericality: true, allow_blank: true + attr_accessor :converted_scale, :converted_system, :converted_unit + def scale(factor, auto_unit = false) + self.converted_scale = factor recipe_ingredients.each do |ri| ri.scale(factor, auto_unit) end end def convert_to_metric + self.converted_system = 'metric' recipe_ingredients.each do |ri| ri.to_metric end end def convert_to_standard + self.converted_system = 'standard' recipe_ingredients.each do |ri| ri.to_standard end end def convert_to_mass + self.converted_unit = 'mass' recipe_ingredients.each do |ri| ri.to_mass end end def convert_to_volume + self.converted_unit = 'volume' recipe_ingredients.each do |ri| ri.to_volume end diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index bcd7507..353c127 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -3,6 +3,8 @@ + + Parsley <%= stylesheet_pack_tag 'application' %> diff --git a/app/views/recipes/_recipe.json.jbuilder b/app/views/recipes/_recipe.json.jbuilder index 5264a39..317fa21 100644 --- a/app/views/recipes/_recipe.json.jbuilder +++ b/app/views/recipes/_recipe.json.jbuilder @@ -1,6 +1,6 @@ -json.extract! recipe, :id, :name, :rating, :yields, :total_time, :active_time, :created_at, :updated_at, :step_text +json.extract! recipe, :id, :name, :rating, :yields, :total_time, :active_time, :created_at, :updated_at, :step_text, :converted_scale, :converted_system, :converted_unit json.rendered_steps MarkdownProcessor.render(recipe.step_text) diff --git a/public/manifest.json b/public/manifest.json new file mode 100644 index 0000000..0709599 --- /dev/null +++ b/public/manifest.json @@ -0,0 +1,8 @@ +{ + "name": "Parsley", + "short_name": "Parsley", + "start_url": ".", + "display": "standalone", + "background_color": "#4a4a4a", + "description": "A simply readable Hacker News app." +} \ No newline at end of file