Factor out the ReceipeController
This commit is contained in:
@@ -74,7 +74,6 @@ module.exports = class BaseController extends Backbone.View
|
||||
return unless @onWillChangeModel @_model, newModel
|
||||
|
||||
@_model = newModel
|
||||
@_model.silent = false
|
||||
@_tryRefresh()
|
||||
|
||||
# Backbone.View Overrides ######################################################################
|
||||
|
||||
@@ -14,12 +14,14 @@ ImageLoader = require './image_loader'
|
||||
module.exports = class CraftingGridController extends BaseController
|
||||
|
||||
constructor: (options={})->
|
||||
if not options.model? then throw new Error 'options.model is required'
|
||||
if not options.modPack? then throw new Error 'options.modPack is required'
|
||||
options.imageLoader ?= new ImageLoader defaultUrl:'/images/unknown.png'
|
||||
options.templateName = 'crafting_grid'
|
||||
super options
|
||||
|
||||
@_imageLoader = options.imageLoader
|
||||
@_modPack = options.modPack
|
||||
@_slotCount = 9
|
||||
|
||||
# BaseController Methods #######################################################################
|
||||
|
||||
@@ -39,7 +41,7 @@ module.exports = class CraftingGridController extends BaseController
|
||||
slot.img.attr 'src', '/images/empty.png'
|
||||
slot.img.removeAttr 'alt'
|
||||
|
||||
display = @model.getItemDisplayAt index
|
||||
display = @_getItemDisplayAt index
|
||||
if display?
|
||||
slot.a.removeClass 'empty'
|
||||
slot.a.attr 'href', display.itemUrl
|
||||
@@ -49,3 +51,15 @@ module.exports = class CraftingGridController extends BaseController
|
||||
|
||||
@$el.tooltip show:{delay:Duration.slow, duration:Duration.fast}
|
||||
super
|
||||
|
||||
# Private Methods ##############################################################################
|
||||
|
||||
_getItemDisplayAt: (slot)->
|
||||
if slot >= @_slotCount then throw new Error "slot (#{slot}) must be less than #{@_slotCount}"
|
||||
return null unless @model?
|
||||
|
||||
slug = @model.getItemSlugAt slot
|
||||
return null unless slug?
|
||||
|
||||
itemDisplay = @_modPack.findItemDisplay slug
|
||||
return itemDisplay
|
||||
|
||||
@@ -5,11 +5,11 @@ Copyright (c) 2014-2015 by Redwood Labs
|
||||
All rights reserved.
|
||||
###
|
||||
|
||||
BaseController = require './base_controller'
|
||||
CraftingGridController = require './crafting_grid_controller'
|
||||
{Duration} = require '../constants'
|
||||
ImageLoader = require './image_loader'
|
||||
InventoryParser = require '../models/inventory_parser'
|
||||
BaseController = require './base_controller'
|
||||
{Duration} = require '../constants'
|
||||
ImageLoader = require './image_loader'
|
||||
InventoryParser = require '../models/inventory_parser'
|
||||
RecipeController = require './recipe_controller'
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
@@ -22,19 +22,19 @@ module.exports = class CraftingTableController extends BaseController
|
||||
options.templateName = 'crafting_table'
|
||||
super options
|
||||
|
||||
@imageLoader = options.imageLoader
|
||||
@modPack = options.modPack
|
||||
@_imageLoader = options.imageLoader
|
||||
@_modPack = options.modPack
|
||||
|
||||
# Event Methods ################################################################################
|
||||
|
||||
onNextClicked: ->
|
||||
@model.step += 1
|
||||
@model.stepIndex += 1
|
||||
|
||||
onPrevClicked: ->
|
||||
@model.step -= 1
|
||||
@model.stepIndex -= 1
|
||||
|
||||
onReportProblem: ->
|
||||
parser = new InventoryParser @modPack
|
||||
parser = new InventoryParser @_modPack
|
||||
itemList = parser.unparse @model.plan.want
|
||||
message = "When I was on step #{@model.step + 1} of making:\n\n#{itemList}\nI noticed that...\n"
|
||||
global.feedbackController.enterFeedback message
|
||||
@@ -42,20 +42,17 @@ module.exports = class CraftingTableController extends BaseController
|
||||
# BaseController Overrides #####################################################################
|
||||
|
||||
onDidRender: ->
|
||||
@gridController = @addChild CraftingGridController, '.view__crafting_grid',
|
||||
model: @model.grid
|
||||
imageLoader: @imageLoader
|
||||
@recipeController = @addChild RecipeController, '.view__recipe', imageLoader:@_imageLoader, modPack:@_modPack
|
||||
|
||||
@$multiplier = @$('.multiplier')
|
||||
@$next = @$('.next')
|
||||
@$outputImg = @$('.output img')
|
||||
@$outputLink = @$('.output a')
|
||||
@$outputQuantity = @$('.quantity')
|
||||
@$prev = @$('.prev')
|
||||
@$problemControl = @$('.problem')
|
||||
@$title = @$('h2 p')
|
||||
@$tool = @$('.tool p')
|
||||
|
||||
@$multiplier = $('<p class="multiplier"></p>')
|
||||
@$('.output').append @$multiplier
|
||||
|
||||
@defaultTitle = @$title.html()
|
||||
super
|
||||
|
||||
@@ -66,34 +63,18 @@ module.exports = class CraftingTableController extends BaseController
|
||||
if @model.hasSteps
|
||||
if @model.hasPrevStep then @$prev.addClass 'enabled'
|
||||
if @model.hasNextStep then @$next.addClass 'enabled'
|
||||
@$title.html "Step #{@model.step + 1} of #{@model.stepCount}"
|
||||
@$title.html "Step #{@model.stepIndex + 1} of #{@model.stepCount}"
|
||||
else
|
||||
@$title.html @defaultTitle
|
||||
|
||||
@$tool.html @model.toolNames
|
||||
currentStep = @model.currentStep
|
||||
@recipeController.model = currentStep?.recipe
|
||||
|
||||
@$outputImg.attr 'src', '/images/empty.png'
|
||||
@$outputImg.removeAttr 'alt'
|
||||
@$outputLink.removeAttr 'href'
|
||||
@$outputQuantity.html ''
|
||||
|
||||
outputStack = @model.output
|
||||
if outputStack?
|
||||
display = @modPack.findItemDisplay outputStack.slug
|
||||
@$outputLink.attr 'href', display.itemUrl
|
||||
@$outputLink.attr 'title', display.itemName
|
||||
@$outputImg.attr 'alt', display.itemName
|
||||
@$outputQuantity.html outputStack.quantity if outputStack.quantity > 1
|
||||
|
||||
@imageLoader.load display.iconUrl, @$outputImg
|
||||
|
||||
if @model.multiplier > 1
|
||||
@$multiplier.html "×#{@model.multiplier}"
|
||||
if currentStep?.multiplier > 1
|
||||
@$multiplier.html "×#{currentStep.multiplier}"
|
||||
else
|
||||
@$multiplier.html ''
|
||||
|
||||
@$el.tooltip show:{delay:Duration.slow, duration:Duration.fast}
|
||||
|
||||
if not (@model.hasSteps and global.feedbackController?)
|
||||
@$problemControl.hide duration:Duration.fast
|
||||
else
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
###
|
||||
Crafting Guide - recipe_controller.coffee
|
||||
|
||||
Copyright (c) 2015 by Redwood Labs
|
||||
All rights reserved.
|
||||
###
|
||||
|
||||
BaseController = require './base_controller'
|
||||
CraftingGridController = require './crafting_grid_controller'
|
||||
{Duration} = require '../constants'
|
||||
ImageLoader = require './image_loader'
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
module.exports = class RecipeController extends BaseController
|
||||
|
||||
constructor: (options={})->
|
||||
if not options.modPack? then throw new Error 'options.modPack is required'
|
||||
options.imageLoader ?= new ImageLoader defaultUrl:'/images/unknown.png'
|
||||
options.templateName = 'recipe'
|
||||
super options
|
||||
|
||||
@_imageLoader = options.imageLoader
|
||||
@_modPack = options.modPack
|
||||
|
||||
# BaseController Overrides #####################################################################
|
||||
|
||||
onDidRender: ->
|
||||
@gridController = @addChild CraftingGridController, '.view__crafting_grid',
|
||||
modPack: @_modPack
|
||||
imageLoader: @_imageLoader
|
||||
|
||||
@$outputImg = @$('.output img')
|
||||
@$outputLink = @$('.output a')
|
||||
@$outputQuantity = @$('.quantity')
|
||||
@$tool = @$('.tool p')
|
||||
super
|
||||
|
||||
refresh: ->
|
||||
@gridController.model = @model
|
||||
@$tool.html @_findToolNames().join ', '
|
||||
|
||||
@$outputImg.attr 'src', '/images/empty.png'
|
||||
@$outputImg.removeAttr 'alt'
|
||||
@$outputLink.removeAttr 'href'
|
||||
@$outputQuantity.html ''
|
||||
|
||||
if @model?
|
||||
outputStack = @model.output[0]
|
||||
if outputStack?
|
||||
display = @_modPack.findItemDisplay outputStack.slug
|
||||
@$outputLink.attr 'href', display.itemUrl
|
||||
@$outputLink.attr 'title', display.itemName
|
||||
@$outputImg.attr 'alt', display.itemName
|
||||
@$outputQuantity.html outputStack.quantity if outputStack.quantity > 1
|
||||
|
||||
@_imageLoader.load display.iconUrl, @$outputImg
|
||||
|
||||
@$el.tooltip show:{delay:Duration.fast, duration:Duration.fast}
|
||||
|
||||
super
|
||||
|
||||
# Private Methods ##############################################################################
|
||||
|
||||
_findToolNames: ->
|
||||
result = []
|
||||
if @model?
|
||||
for stack in @model.tools
|
||||
name = @_modPack.findName stack.slug
|
||||
result.push name if name?
|
||||
return result
|
||||
Reference in New Issue
Block a user