Fix bug with recipe books table
This commit is contained in:
@@ -2,3 +2,4 @@
|
||||
.sass-cache
|
||||
dist
|
||||
node_modules
|
||||
src/scripts/views.js
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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]
|
||||
|
||||
|
||||
@@ -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}"
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -6,5 +6,6 @@
|
||||
//-
|
||||
|
||||
tr
|
||||
td: input(type="checkbox")
|
||||
td: p
|
||||
td: p
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
table
|
||||
tr
|
||||
td
|
||||
td
|
||||
td
|
||||
input.recipe_book_url(placeholder="enter a URL to load another recipe book...")
|
||||
|
||||
Reference in New Issue
Block a user