From 004320d052a65f1a29e9a002d5039d1c85929bd0 Mon Sep 17 00:00:00 2001 From: Andrew Miner Date: Mon, 29 Dec 2014 09:07:39 -0800 Subject: [PATCH] Fix bug with recipe books table --- .gitignore | 1 + src/css/templates/recipe_catalog.scss | 5 +-- .../crafting_table_controller.coffee | 4 +-- .../controllers/recipe_book_controller.coffee | 18 +++++++++-- src/scripts/models/crafting_plan.coffee | 2 +- src/scripts/models/recipe_book.coffee | 21 +++++++++++-- src/scripts/models/recipe_catalog.coffee | 31 ++++++++++--------- src/templates/recipe_book.jade | 1 + src/templates/recipe_catalog.jade | 1 + 9 files changed, 61 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 1e8e451e2..19fe9bb4b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .sass-cache dist node_modules +src/scripts/views.js diff --git a/src/css/templates/recipe_catalog.scss b/src/css/templates/recipe_catalog.scss index 896d7bc10..294afeb0a 100644 --- a/src/css/templates/recipe_catalog.scss +++ b/src/css/templates/recipe_catalog.scss @@ -24,7 +24,8 @@ All rights reserved. vertical-align: middle; } - td:nth-child(1) { width: 30%; } - td:nth-child(2) { width: 70%; border-left: 0.1em solid $color-brick-light; } + td:nth-child(1) { width: 5%; } + td:nth-child(2) { width: 30%; } + td:nth-child(3) { width: 65%; border-left: 0.1em solid $color-brick-light; } } } diff --git a/src/scripts/controllers/crafting_table_controller.coffee b/src/scripts/controllers/crafting_table_controller.coffee index 2bcb5554e..cf38ad003 100644 --- a/src/scripts/controllers/crafting_table_controller.coffee +++ b/src/scripts/controllers/crafting_table_controller.coffee @@ -108,7 +108,7 @@ module.exports = class CraftingTableController extends BaseController # Private Methods ############################################################################## _craft: -> - if @model.catalog.findRecipes(@model.name).length > 0 + if @model.catalog.hasRecipe @model.name router.navigate "/item/#{encodeURIComponent(@model.name)}" @model.craft() else @@ -118,7 +118,7 @@ module.exports = class CraftingTableController extends BaseController onChanged = => @onNameFieldChanged() @$nameField.autocomplete - source: @model.catalog.getRecipeNames() + source: @model.catalog.gatherNames() delay: 0 minLength: 0 change: onChanged diff --git a/src/scripts/controllers/recipe_book_controller.coffee b/src/scripts/controllers/recipe_book_controller.coffee index c1c85e871..027a75806 100644 --- a/src/scripts/controllers/recipe_book_controller.coffee +++ b/src/scripts/controllers/recipe_book_controller.coffee @@ -16,13 +16,27 @@ module.exports = class RecipeBookController extends BaseController options.templateName = 'recipe_book' super options + # Event Methods ################################################################################ + + onEnabledChanged: -> + return unless @rendered + + @model.enabled = @$(':checked').length > 0 + # BaseController Overrides ##################################################################### onDidRender: -> - @$name = @$('td:nth-child(1) p') - @$description = @$('td:nth-child(2) p') + @$enabled = @$('td:nth-child(1) input') + @$name = @$('td:nth-child(2) p') + @$description = @$('td:nth-child(3) p') super refresh: -> + if @model.enabled then @$enabled.attr('checked', 'checked') else @$enabled.removeAttr('checked') @$name.html "#{@model.modName} (#{@model.modVersion})" @$description.html "#{@model.description}" + + # Backbone.View Overrides ###################################################################### + + events: + 'change input[type="checkbox"]': 'onEnabledChanged' diff --git a/src/scripts/models/crafting_plan.coffee b/src/scripts/models/crafting_plan.coffee index 261e44f87..9f6280f65 100644 --- a/src/scripts/models/crafting_plan.coffee +++ b/src/scripts/models/crafting_plan.coffee @@ -50,7 +50,7 @@ module.exports = class CraftingPlan targetItem = @_pending.pop() return unless targetItem? - recipes = @catalog.findRecipes targetItem.name + recipes = @catalog.gatherRecipes targetItem.name return if not recipes.length > 0 recipe = recipes[0] diff --git a/src/scripts/models/recipe_book.coffee b/src/scripts/models/recipe_book.coffee index ec8594ae7..a080aa0e1 100644 --- a/src/scripts/models/recipe_book.coffee +++ b/src/scripts/models/recipe_book.coffee @@ -17,22 +17,39 @@ module.exports = class RecipeBook extends BaseModel attributes.description ?= '' attributes.recipes ?= [] + attributes.enabled ?= true super attributes, options # Public Methods ############################################################################### - findRecipes: (name)-> - result = [] + gatherNames: (result)-> + return unless @enabled + + for recipe in @recipes + continue if result[recipe.name] + result[recipe.name] = value:recipe.name, label:"#{recipe.name} (from #{@modName} #{@modVersion})" + + return result + + gatherRecipes: (name, result)-> + return unless @enabled + for recipe in @recipes if recipe.name is name result.push recipe return result + hasRecipe: (name)-> + for recipe in @recipes + return true if recipe.name is name + return false + # Object Overrides ############################################################################# toString: -> return "RecipeBook (#{@cid}) { + enabled:#{@enabled}, modName:#{@modName}, modVersion:#{@modVersion}, recipes:#{@recipes.length} items}" diff --git a/src/scripts/models/recipe_catalog.coffee b/src/scripts/models/recipe_catalog.coffee index 369490cd4..82771e10d 100644 --- a/src/scripts/models/recipe_catalog.coffee +++ b/src/scripts/models/recipe_catalog.coffee @@ -21,27 +21,29 @@ module.exports = class RecipeCatalog extends BaseModel # Public Methods ############################################################################### - findRecipes: (name)-> - result = [] + gatherNames: -> + nameData = {} for book in @books - for recipe in book.findRecipes name - result.push recipe + book.gatherNames nameData - return result - - getRecipeNames: -> - nameHash = {} - for book in @books - for recipe in book.recipes - nameHash[recipe.name] = "#{recipe.name} (from #{book.modName} #{book.modVersion})" - - names = (k for k, v of nameHash).sort() result = [] + names = _.keys(nameData).sort() for name in names - result.push value:name, label:nameHash[name] + result.push nameData[name] + return result + + gatherRecipes: (name)-> + result = [] + for book in @books + book.gatherRecipes name, result return result + hasRecipe: (name)-> + for book in @books + return true if book.hasRecipe(name) + return false + loadBook: (url)-> w.promise (resolve, reject)=> @trigger Event.load.started, this, url @@ -59,6 +61,7 @@ module.exports = class RecipeCatalog extends BaseModel @books.sort (a, b)-> return 0 if a.modName is b.modName return if a.modName < b.modName then -1 else +1 + book.on Event.change, => @trigger Event.change, this return book diff --git a/src/templates/recipe_book.jade b/src/templates/recipe_book.jade index 443e7f6c1..925197b93 100644 --- a/src/templates/recipe_book.jade +++ b/src/templates/recipe_book.jade @@ -6,5 +6,6 @@ //- tr + td: input(type="checkbox") td: p td: p diff --git a/src/templates/recipe_catalog.jade b/src/templates/recipe_catalog.jade index 5d51a5e2a..0bc86e524 100644 --- a/src/templates/recipe_catalog.jade +++ b/src/templates/recipe_catalog.jade @@ -12,6 +12,7 @@ table tr + td   td   td input.recipe_book_url(placeholder="enter a URL to load another recipe book...")