From fef9d155d84f0eab05a08d0da1a22320dc86529a Mon Sep 17 00:00:00 2001 From: Andrew Miner Date: Mon, 12 Jan 2015 14:18:07 -0800 Subject: [PATCH] Store tool and modVersion enabled states --- src/scripts/models/crafting_plan.coffee | 20 +++++++++++++++----- src/scripts/models/item_page.coffee | 5 +++-- src/scripts/models/mod_pack.coffee | 1 - src/scripts/models/mod_version.coffee | 12 +++++++++++- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/scripts/models/crafting_plan.coffee b/src/scripts/models/crafting_plan.coffee index 6b54ecbf1..190f1d3ef 100644 --- a/src/scripts/models/crafting_plan.coffee +++ b/src/scripts/models/crafting_plan.coffee @@ -13,19 +13,23 @@ Inventory = require './inventory' module.exports = class CraftingPlan extends BaseModel constructor: (attributes={}, options={})-> + options.storage ?= window.localStorage + if not attributes.modPack then throw new Error 'modPack is required' + attributes.includingTools ?= options.storage.getItem('includingTools') attributes.includingTools ?= false super attributes, options - @have = new Inventory - @want = new Inventory - @need = new Inventory - @result = new Inventory + @have = new Inventory + @want = new Inventory + @need = new Inventory + @result = new Inventory + @storage = options.storage @have.on 'change', => @craft() @want.on 'change', => @craft() @modPack.on 'change', => @craft() - @on 'change:includingTools', => @craft() + @on 'change:includingTools', => @onIncludingToolsChanged() @clear() @@ -55,6 +59,12 @@ module.exports = class CraftingPlan extends BaseModel @trigger 'change', this + # Event Methods ################################################################################ + + onIncludingToolsChanged: -> + @storage.setItem 'includingTools', "#{@includingTools}" + @craft() + # Object Overrides ############################################################################# toString: -> diff --git a/src/scripts/models/item_page.coffee b/src/scripts/models/item_page.coffee index 2fe2e298d..316d1ab77 100644 --- a/src/scripts/models/item_page.coffee +++ b/src/scripts/models/item_page.coffee @@ -8,6 +8,7 @@ All rights reserved. BaseModel = require './base_model' CraftingPlan = require './crafting_plan' CraftingTable = require './crafting_table' +{Event} = require '../constants' ModPack = require './mod_pack' ######################################################################################################################## @@ -21,8 +22,8 @@ module.exports = class ItemPage extends BaseModel attributes.table ?= new CraftingTable plan:attributes.plan super attributes, options - @modPack.on 'change', => @_consumeParams() - @plan.on 'change', => @_updateLocation() + @modPack.on Event.change, => @_consumeParams() + @plan.on Event.change, => @_updateLocation() # Private Methods ############################################################################## diff --git a/src/scripts/models/mod_pack.coffee b/src/scripts/models/mod_pack.coffee index 4fc72a442..cde3b3805 100644 --- a/src/scripts/models/mod_pack.coffee +++ b/src/scripts/models/mod_pack.coffee @@ -146,7 +146,6 @@ module.exports = class ModPack extends BaseModel onModVersionLoaded: (url, data, status, xhr)-> try modVersion = @loadModVersionData data - modVersion.enabled = true logger.info "loaded ModVersion from #{url}: #{modVersion}" @trigger Event.load.succeeded, this, modVersion diff --git a/src/scripts/models/mod_version.coffee b/src/scripts/models/mod_version.coffee index 3a359490c..642443123 100644 --- a/src/scripts/models/mod_version.coffee +++ b/src/scripts/models/mod_version.coffee @@ -13,16 +13,26 @@ BaseModel = require './base_model' module.exports = class ModVersion extends BaseModel constructor: (attributes={}, options={})-> + options.storage ?= window.localStorage + if _.isEmpty(attributes.name) then throw new Error 'name cannot be empty' if _.isEmpty(attributes.version) then throw new Error 'version cannot be empty' attributes.description ?= '' - attributes.enabled ?= attributes.name in RequiredMods + attributes.enabled ?= true attributes.items ?= {} attributes.names ?= {} attributes.slug ?= _.slugify attributes.name super attributes, options + enabled = options.storage.getItem("#{@slug}.enabled") + if enabled? + logger.debug "loading #{@slug}.enabled as #{@enabled}" + @enabled = enabled is 'true' + @on 'change:enabled', => + logger.debug "saving #{@slug}.enabled as #{@enabled}" + options.storage.setItem "#{@slug}.enabled", "#{@enabled}" + # Public Methods ############################################################################### addItem: (item)->