diff --git a/src/pages/includes/_header.jade b/src/pages/includes/_header.jade index 81fe1900a..42aed2be6 100644 --- a/src/pages/includes/_header.jade +++ b/src/pages/includes/_header.jade @@ -8,6 +8,6 @@ .view__header h1 a(href="/") - img(src="images/workbench_front.png") + img(src="/images/workbench_front.png") p Crafting Guide .divider.bottom diff --git a/src/pages/layouts/basic.jade b/src/pages/layouts/basic.jade index b46a4fc62..426a0d21f 100644 --- a/src/pages/layouts/basic.jade +++ b/src/pages/layouts/basic.jade @@ -15,7 +15,7 @@ html meta(charset="UTF-8") block styles - link(rel="stylesheet", type="text/css", href="css/main.css") + link(rel="stylesheet", type="text/css", href="/css/main.css") body .content diff --git a/src/scripts/controllers/crafting_table_controller.coffee b/src/scripts/controllers/crafting_table_controller.coffee index 76fea1aa4..08c9acebf 100644 --- a/src/scripts/controllers/crafting_table_controller.coffee +++ b/src/scripts/controllers/crafting_table_controller.coffee @@ -29,7 +29,7 @@ module.exports = class CraftingTableController extends BaseController onCatalogChanged: -> return unless @rendered @_updateNameAutocomplete() - @_parseUrlParameters() + @_craft() onHaveFieldChanged: -> return unless @rendered @@ -69,12 +69,19 @@ module.exports = class CraftingTableController extends BaseController @$makeList = @$('td.make ul') @$resultList = @$('td.result ul') + @_updateNameAutocomplete() + @_craft() + super - @_updateNameAutocomplete() - @_parseUrlParameters() - refresh: -> + @$nameField.val @model.name + @$quantityField.val @model.quantity + if @model.includingTools + @$includingToolsField.attr 'checked', 'checked' + else + @$includingToolsField.removeAttr 'checked' + @$needList.empty() @$makeList.empty() @$resultList.empty() @@ -92,45 +99,18 @@ module.exports = class CraftingTableController extends BaseController # Backbone.View Overrides ###################################################################### events: - 'input textarea[name="have"]': 'onHaveFieldChanged' - 'focus input[name="name"]': 'onNameFieldFocused' - 'input input[name="name"]': 'onNameFieldChanged' - 'input select[name="quantity"]': 'onQuantityFieldChanged' + 'input textarea[name="have"]': 'onHaveFieldChanged' + 'focus input[name="name"]': 'onNameFieldFocused' + 'input input[name="name"]': 'onNameFieldChanged' + 'input select[name="quantity"]': 'onQuantityFieldChanged' 'change input[name="including_tools"]': 'onIncludingToolsFieldChanged' # Private Methods ############################################################################## _craft: -> + router.navigate "/item/#{encodeURIComponent(@model.name)}" @model.craft() - _parseUrlParameters: -> - params = url.parse(window.location.href, true).query - - if params[UrlParam.includingTools]? - param = params[UrlParam.includingTools] - if param in ['true', 'yes'] - @$includingToolsField.attr 'checked', 'checked' - else if param in ['false', 'no'] - @$includingToolsField.removeAttr 'checked' - else - console.log "invalid including tools value in URL param: #{params[UrlParam.includingTools]}" - @onIncludingToolsFieldChanged() - - if params[UrlParam.quantity]? - @$quantityField.val parseInt params[UrlParam.quantity] - if @$quantityField.find(':selected').length is 0 - console.log "invalid quantity in URL param: #{params[UrlParam.quantity]}" - @onQuantityFieldChanged() - - # Do this check last to avoid recalculting the recipe multiple times - if params[UrlParam.recipe]? - recipeName = params[UrlParam.recipe] - if @model.catalog.findRecipes(recipeName).length > 0 - @$nameField.val recipeName - else - console.log "invalid recipe name in URL param: #{params[UrlParam.recipe]}" - @onNameFieldChanged() - _updateNameAutocomplete: -> onChanged = => @onNameFieldChanged() diff --git a/src/scripts/controllers/landing_page_controller.coffee b/src/scripts/controllers/item_page_controller.coffee similarity index 56% rename from src/scripts/controllers/landing_page_controller.coffee rename to src/scripts/controllers/item_page_controller.coffee index b55ee4516..d0da9ea8f 100644 --- a/src/scripts/controllers/landing_page_controller.coffee +++ b/src/scripts/controllers/item_page_controller.coffee @@ -1,5 +1,5 @@ ### -# Crafting Guide - landing_page_controller.coffee +# Crafting Guide - item_page_controller.coffee # # Copyright (c) 2014 by Redwood Labs # All rights reserved. @@ -7,18 +7,25 @@ BaseController = require './base_controller' CraftingTableController = require './crafting_table_controller' -LandingPage = require '../models/landing_page' +ItemPage = require '../models/item_page' RecipeCatalogController = require './recipe_catalog_controller' ######################################################################################################################## -module.exports = class LandingPageController extends BaseController +module.exports = class ItemPageController extends BaseController constructor: (options={})-> - options.model ?= new LandingPage - options.templateName = 'landing_page' + options.model ?= new ItemPage + options.templateName = 'item_page' super options + # Public Methods ############################################################################### + + setParams: (params)-> + @model.table.name = params.name if params.name? + @model.table.quantity = params.quantity if params.quantity? + logger.debug "quantity updated to: #{@model.table.quantity} with params: #{params.count}" + # BaseController Overrides ##################################################################### onDidRender: -> diff --git a/src/scripts/crafting_guide_router.coffee b/src/scripts/crafting_guide_router.coffee index d6f5e323c..72a070b08 100644 --- a/src/scripts/crafting_guide_router.coffee +++ b/src/scripts/crafting_guide_router.coffee @@ -5,9 +5,10 @@ # All rights reserved. ### -{Duration} = require './constants' -LandingPageController = require './controllers/landing_page_controller' -{Opacity} = require './constants' +{Duration} = require './constants' +ItemPageController = require './controllers/item_page_controller' +{Opacity} = require './constants' +UrlParams = require './url_params' ######################################################################################################################## @@ -21,13 +22,25 @@ module.exports = class CraftingGuideRouter extends Backbone.Router # Backbone.Router Overrides #################################################################### routes: - '': 'landing' + '': 'root' + 'item(/:name)': 'item' # Route Methods ################################################################################ - landing: -> - @_pageControllers.landing ?= new LandingPageController - @_setPage 'landing' + root: -> + params = new UrlParams + recipeName: {type:'string'} + count: {type:'integer'} + + if params.recipeName? + @navigate "/item/#{encodeURIComponent(params.recipeName)}" + + @item params.recipeName, params.count + + item: (name, quantity=1)-> + @_pageControllers.item ?= new ItemPageController + @_pageControllers.item.setParams name:name, quantity:quantity + @_setPage 'item' # Private Methods ############################################################################## diff --git a/src/scripts/models/landing_page.coffee b/src/scripts/models/item_page.coffee similarity index 85% rename from src/scripts/models/landing_page.coffee rename to src/scripts/models/item_page.coffee index 447b88c10..431210dfe 100644 --- a/src/scripts/models/landing_page.coffee +++ b/src/scripts/models/item_page.coffee @@ -1,5 +1,5 @@ ### -# Crafting Guide - landing_page.coffee +# Crafting Guide - item_page.coffee # # Copyright (c) 2014 by Redwood Labs # All rights reserved. @@ -11,7 +11,7 @@ RecipeCatalog = require './recipe_catalog' ######################################################################################################################## -module.exports = class LandingPage extends BaseModel +module.exports = class ItemPage extends BaseModel constructor: (attributes={}, options={})-> attributes.catalog ?= new RecipeCatalog diff --git a/src/scripts/url_params.coffee b/src/scripts/url_params.coffee new file mode 100644 index 000000000..3c24f5d7a --- /dev/null +++ b/src/scripts/url_params.coffee @@ -0,0 +1,52 @@ +### +# Crafting Guide - url_params.coffee +# +# Copyright (c) 2014 by Redwood Labs +# All rights reserved. +### + +url = require 'url' + +######################################################################################################################## + +module.exports = class UrlParams + + constructor: (parseMap, url=null)-> + if not parseMap? then throw new Error 'parseMap is required' + url ?= window.location.href + + @_parseMap = {} + for name, options of parseMap + options.type ?= 'string' + options.default ?= null + + options.parse = this["_#{options.type}"] + if not options.parse? then throw new Error "#{options.type} is not a valid type" + + @_parseMap[name] = options + + @parse url + + parse: (urlText)-> + params = url.parse(urlText, true).query + for name, options of @_parseMap + value = params[name] + if value? + this[name] = options.parse value + else + this[name] = options.default + + # Private Methods ############################################################################## + + _boolean: (text)-> + return true if text in ['true', 'yes'] + return false + + _integer: (text)-> + return null unless text? + result = parseInt text + return null unless _.isNumber result + return result + + _string: (text)-> + return text diff --git a/src/scripts/views.js b/src/scripts/views.js index c58fa6bf9..f6ae866fb 100644 --- a/src/scripts/views.js +++ b/src/scripts/views.js @@ -11,7 +11,7 @@ var jade_indent = []; buf.push("\n
\n

\n

Crafting Table

\n

\n
\n

I want to make

\n \n \n \n
\n \n \n \n \n \n \n \n
\n

\n

You have...

\n

\n \n
\n

\n

You'll need...

\n

\n
    \n
    \n

    \n

    You'll make...

    \n

    \n
      \n
      \n

      \n

      You'll get...

      \n

      \n
        \n
        \n
        ");;return buf.join(""); }; -this["JST"]["landing_page"] = function template(locals) { +this["JST"]["item_page"] = function template(locals) { var buf = []; var jade_mixins = {}; var jade_interp; diff --git a/src/templates/landing_page.jade b/src/templates/item_page.jade similarity index 100% rename from src/templates/landing_page.jade rename to src/templates/item_page.jade