Initial effort to convert craft page to new models
This commit is contained in:
@@ -5,14 +5,13 @@
|
||||
# All rights reserved.
|
||||
#
|
||||
|
||||
BaseController = require '../base_controller'
|
||||
CraftPage = require '../../models/site/craft_page'
|
||||
{Craftsman} = require('crafting-guide-common').deprecated.crafting
|
||||
CraftsmanWorkingController = require './craftsman_working/craftsman_working_controller'
|
||||
InventoryController = require '../common/inventory/inventory_controller'
|
||||
PageController = require '../page_controller'
|
||||
{SimpleInventory} = require('crafting-guide-common').deprecated.crafting
|
||||
StepController = require './step/step_controller'
|
||||
CraftPage = require "../../models/site/craft_page"
|
||||
WorkingPanelController = require "./working_panel/working_panel_controller"
|
||||
{Inventory} = require("crafting-guide-common").models
|
||||
InventoryController = require "../common/inventory/inventory_controller"
|
||||
{Observable} = require("crafting-guide-common").util
|
||||
PageController = require "../page_controller"
|
||||
StepController = require "./step/step_controller"
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
@@ -21,12 +20,12 @@ module.exports = class CraftPageController extends PageController
|
||||
@::SCROLL_BUFFER = 8 # px
|
||||
|
||||
constructor: (options={})->
|
||||
if not options.imageLoader? then throw new Error 'options.imageLoader is required'
|
||||
if not options.modPack? then throw new Error 'options.modPack is required'
|
||||
if not options.router? then throw new Error 'options.router is required'
|
||||
if not options.storage? then throw new Error 'options.storage is required'
|
||||
if not options.imageLoader? then throw new Error "options.imageLoader is required"
|
||||
if not options.modPack? then throw new Error "options.modPack is required"
|
||||
if not options.router? then throw new Error "options.router is required"
|
||||
if not options.storage? then throw new Error "options.storage is required"
|
||||
|
||||
options.model ?= new CraftPage modPack:options.modPack
|
||||
options.model ?= new CraftPage modPack:options.modPack, params:options.params
|
||||
options.templateName = 'craft_page'
|
||||
super options
|
||||
|
||||
@@ -35,41 +34,46 @@ module.exports = class CraftPageController extends PageController
|
||||
@_router = options.router
|
||||
@_storage = options.storage
|
||||
|
||||
@model.have.on Observable::ANY, this, "onHaveInventoryChanged"
|
||||
@model.want.on Observable::ANY, this, "onWantInventoryChanged"
|
||||
|
||||
# Event Methods ################################################################################
|
||||
|
||||
onHaveInventoryChanged: ->
|
||||
@_storage.store 'crafting-plan:have', @model.craftsman.have.unparse()
|
||||
@_storage.store 'crafting-plan:have', @model.have.toUrlString()
|
||||
|
||||
onMoveNeedToHave: (itemSlug)->
|
||||
quantity = @_needInventoryController.model.quantityOf itemSlug
|
||||
@model.craftsman.have.add itemSlug, quantity
|
||||
onMoveNeedToHave: (itemId)->
|
||||
quantity = @_needInventoryController.model.getQuantity itemId
|
||||
item = @_modPack.findItem itemId
|
||||
@model.have.add item, quantity
|
||||
|
||||
onRemoveFromHaveInventory: (itemSlug)->
|
||||
@model.craftsman.have.remove itemSlug
|
||||
onRemoveFromHave: (itemId)->
|
||||
@model.have.remove itemId
|
||||
|
||||
onRemoveFromWant: (itemSlug)->
|
||||
@model.craftsman.want.remove itemSlug
|
||||
onRemoveFromWant: (itemId)->
|
||||
@model.want.remove itemId
|
||||
@onWantInventoryChanged()
|
||||
|
||||
onSampleClicked: (event)->
|
||||
$el = $(event.target)
|
||||
while $el.length > 0
|
||||
inventoryText = $el.attr 'data-slug'
|
||||
inventoryText = $el.attr "data-slug"
|
||||
break if inventoryText
|
||||
$el = $el.parent()
|
||||
|
||||
if inventoryText?
|
||||
tracker.trackEvent c.tracking.category.craft, 'add-sample', inventoryText
|
||||
sampleInventory = new SimpleInventory
|
||||
sampleInventory.parse inventoryText
|
||||
@model.craftsman.want.addInventory sampleInventory
|
||||
@_scrollTo @$workingSection
|
||||
@onWantInventoryChanged()
|
||||
try
|
||||
tracker.trackEvent c.tracking.category.craft, "add-sample", inventoryText
|
||||
sampleInventory = Inventory.fromUrlString inventoryText, @_modPack
|
||||
@model.want.merge sampleInventory
|
||||
@_scrollTo @$workingSection
|
||||
catch e
|
||||
logger.error e
|
||||
|
||||
return false
|
||||
|
||||
onWantInventoryChanged: ->
|
||||
text = @model.craftsman.want.unparse()
|
||||
text = @model.want.toUrlString()
|
||||
url = c.url.crafting inventoryText:text
|
||||
@router.navigate url
|
||||
|
||||
@@ -79,7 +83,7 @@ module.exports = class CraftPageController extends PageController
|
||||
return c.text.craftDescription()
|
||||
|
||||
getTitle: ->
|
||||
description = @model.craftsman.want.toDescription()
|
||||
description = @model.want.toDescription()
|
||||
return null unless description?
|
||||
return "Crafting Plan for #{description}"
|
||||
|
||||
@@ -91,21 +95,19 @@ module.exports = class CraftPageController extends PageController
|
||||
imageLoader: @_imageLoader
|
||||
isAcceptable: (item)=> item.isCraftable
|
||||
modPack: @_modPack
|
||||
model: @model.craftsman.want
|
||||
model: @model.want
|
||||
router: @_router
|
||||
trackingContext: c.tracking.category.craftWant
|
||||
@_wantInventoryController.on c.event.button.first, (c, s)=> @onRemoveFromWant(s)
|
||||
@_wantInventoryController.on c.event.change, (c)=> @onWantInventoryChanged()
|
||||
@_wantInventoryController.on c.event.button.first, (controller, itemId)=> @onRemoveFromWant itemId
|
||||
|
||||
@_haveInventoryController = @addChild InventoryController, '.have .view__inventory',
|
||||
firstButtonType: 'down'
|
||||
imageLoader: @_imageLoader
|
||||
modPack: @_modPack
|
||||
model: @model.craftsman.have
|
||||
model: @model.have
|
||||
router: @_router
|
||||
trackingContext: c.tracking.category.craftHave
|
||||
@_haveInventoryController.on c.event.button.first, (controller, itemSlug)=>
|
||||
@onRemoveFromHaveInventory itemSlug
|
||||
@_haveInventoryController.on c.event.button.first, (controller, itemId)=> @onRemoveFromHave itemId
|
||||
|
||||
@_needInventoryController = @addChild InventoryController, '.need .view__inventory',
|
||||
editable: false
|
||||
@@ -115,11 +117,9 @@ module.exports = class CraftPageController extends PageController
|
||||
model: null
|
||||
router: @_router
|
||||
trackingContext: c.tracking.category.craftNeed
|
||||
@_needInventoryController.on c.event.button.first, (c, s)=> @onMoveNeedToHave(s)
|
||||
@_needInventoryController.on c.event.button.first, (controller, itemId)=> @onMoveNeedToHave itemId
|
||||
|
||||
@_workingSectionController = @addChild CraftsmanWorkingController, '.view__craftsman_working',
|
||||
model: @model.craftsman
|
||||
@_workingSectionController.on c.event.click, => @_scrollTo @$workingSection
|
||||
@_workingPanelController = @addChild WorkingPanelController, '.view__working_panel', model:@model
|
||||
|
||||
@$haveSection = @$('section.have')
|
||||
@$instructionsSection = @$('section.instructions')
|
||||
@@ -128,7 +128,7 @@ module.exports = class CraftPageController extends PageController
|
||||
@$stepsContainer = @$('section.steps .panel')
|
||||
@$stepsSection = @$('section.steps')
|
||||
@$toolsSection = @$('section.tools')
|
||||
@$workingSection = @$('.view__craftsman_working')
|
||||
@$workingSection = @$('.view__working_panel')
|
||||
|
||||
if c.screen.type.compute() is c.screen.type.mobile
|
||||
@$('.view__inventory.large').removeClass 'large'
|
||||
@@ -136,20 +136,15 @@ module.exports = class CraftPageController extends PageController
|
||||
super
|
||||
|
||||
onWillRender: ->
|
||||
@model.craftsman.have.clear()
|
||||
@model.craftsman.have.parse @_storage.load('crafting-plan:have')
|
||||
@model.craftsman.have.on c.event.change, => @onHaveInventoryChanged()
|
||||
|
||||
@model.craftsman.want.on c.event.change, => @refresh()
|
||||
|
||||
@model.craftsman.on c.event.change + ":stage", =>
|
||||
return unless @model.craftsman.stage is Craftsman::STAGE.COMPLETE
|
||||
@_scrollTo @$needSection
|
||||
@model.have.clear()
|
||||
@model.have.merge Inventory.fromUrlString @_storage.load('crafting-plan:have')
|
||||
@model.have.on Observable::ANY, this, "onHaveInventoryChanged"
|
||||
@model.want.on Observable::ANY, this, "tryRefresh"
|
||||
|
||||
super
|
||||
|
||||
refresh: ->
|
||||
@_needInventoryController.model = @model.craftsman.plan?.need
|
||||
@_needInventoryController.model = @model.currentPlan?.need
|
||||
@_refreshOutdated()
|
||||
@_refreshSectionVisibility()
|
||||
@_refreshSteps()
|
||||
@@ -165,33 +160,30 @@ module.exports = class CraftPageController extends PageController
|
||||
# Private Methods ################################################################################
|
||||
|
||||
_addTools: (controller)->
|
||||
controller.model.addToolsTo @model.craftsman.want
|
||||
controller.model.addToolsTo @model.want
|
||||
|
||||
_completeStep: (controller)->
|
||||
controller.markComplete @model.craftsman.have
|
||||
controller.markComplete @model.have
|
||||
|
||||
_isAddingToolsPossible: (controller)->
|
||||
tools = controller.model.recipe.tools
|
||||
return false unless tools.length > 0
|
||||
|
||||
have = @model.craftsman.have
|
||||
want = @model.craftsman.want
|
||||
have = @model.have
|
||||
want = @model.want
|
||||
|
||||
for stack in tools
|
||||
return false if have.hasAtLeast stack.itemSlug
|
||||
return false if want.hasAtLeast stack.itemSlug
|
||||
return false if @model.have.contains stack.item.id
|
||||
return false if @model.want.contains stack.item.id
|
||||
|
||||
return true
|
||||
|
||||
_isStepCompletable: (controller)->
|
||||
recipe = controller.model.recipe
|
||||
for stack in recipe.output
|
||||
return false if @model.craftsman.want.hasAtLeast stack.itemSlug
|
||||
|
||||
return true
|
||||
wantsOutput = @model.want.contains controller.model.recipe.output.item.id
|
||||
return wantsOutput
|
||||
|
||||
_refreshOutdated: ->
|
||||
if @model.craftsman.stage is Craftsman::STAGE.OUTDATED
|
||||
if @model.isOutdated
|
||||
@$el.addClass 'outdated'
|
||||
else
|
||||
@$el.removeClass 'outdated'
|
||||
@@ -205,22 +197,25 @@ module.exports = class CraftPageController extends PageController
|
||||
|
||||
visibleSections = []
|
||||
|
||||
if @model.craftsman.want.isEmpty
|
||||
# TODO: Figure out what to do if the planner can't make a valid plan
|
||||
# else if @model.craftsman.stage is Craftsman::STAGE.INVALID
|
||||
# visibleSections.push el for el in [@$wantSection, @$haveSection, @$workingSection]
|
||||
# TODO: Play around and see if the algo ever takes too long
|
||||
# else if not @model.craftsman.complete
|
||||
# visibleSections.push el for el in [@$wantSection, @$haveSection, @$workingSection]
|
||||
|
||||
if @model.want.isEmpty
|
||||
visibleSections.push el for el in [@$instructionsSection, @$wantSection]
|
||||
else if @model.craftsman.stage is Craftsman::STAGE.INVALID
|
||||
visibleSections.push el for el in [@$wantSection, @$haveSection, @$workingSection]
|
||||
else if not @model.craftsman.complete
|
||||
visibleSections.push el for el in [@$wantSection, @$haveSection, @$workingSection]
|
||||
else
|
||||
visibleSections.push el for el in [@$haveSection, @$wantSection, @$workingSection]
|
||||
if @model.craftsman.plan?
|
||||
if @model.currentPlan?
|
||||
visibleSections.push el for el in [@$needSection, @$stepsSection]
|
||||
|
||||
@hide $el for $el in allSections
|
||||
@show $el for $el in visibleSections
|
||||
|
||||
_refreshSteps: ->
|
||||
steps = @model.craftsman.plan?.steps or []
|
||||
steps = @model.currentPlan?.steps or []
|
||||
@_stepControllers ?= []
|
||||
index = 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user