Complete the RecipeBook controller

This commit is contained in:
Andrew Miner
2014-12-23 09:12:02 -08:00
parent 9fc0407fe4
commit b05fc4c5c9
19 changed files with 205 additions and 50 deletions
+1 -1
View File
@@ -59,7 +59,7 @@ h2 {
* { display: inline; } * { display: inline; }
img { img {
position: relative; height: 7.5em; width: 7.5em; position: relative; height: 6em; width: 6em;
margin-right: 1em; margin-right: 1em;
vertical-align: bottom; vertical-align: bottom;
} }
+7 -2
View File
@@ -14,14 +14,19 @@ All rights reserved.
position: absolute; right: 1em; position: absolute; right: 1em;
} }
input { border: 0; width: 90%; } input { border: 0; font-size: 1.5em; width: 90%; }
table { table {
background: white; background: white;
margin: 0 auto; margin: 0 auto;
width: 98%; width: 98%;
td { height: 3em; padding: 0 1em; position: relative; vertical-align: middle; } td {
height: 3em; padding: 0 1em; position: relative; vertical-align: middle;
p { font-size: 1.5em; }
}
td:nth-child(1) { width: 30%; } td:nth-child(1) { width: 30%; }
td:nth-child(2) { width: 70%; border-left: 0.1em solid $color-brick-light; } td:nth-child(2) { width: 70%; border-left: 0.1em solid $color-brick-light; }
} }
+5 -3
View File
@@ -1,8 +1,10 @@
{ {
"name": "Applied Energetics", "version": 1,
"description": "Applied Energetics rv12-b, by AlgorithmX2", "mod_name": "Applied Energetics",
"mod_version": "rv-12b",
"description": "Crafting recipes from Applied Energetics, by AlgorithmX2",
"recipes": [ "recipes": [
{ {
"output": [[1, "Grind Stone"]], "output": [[1, "Grind Stone"]],
"input": [[3, "Certus Quartz Dust"], [3, "Stone"], [2, "Cobblestone"],[1, "Wooden Gear"]], "input": [[3, "Certus Quartz Dust"], [3, "Stone"], [2, "Cobblestone"],[1, "Wooden Gear"]],
+4 -2
View File
@@ -1,6 +1,8 @@
{ {
"name": "BuildCraft", "version": 1,
"description": "Crafting recipes from BuildCraft 3, by SpaceToad", "mod_name": "BuildCraft",
"mod_version": "3.0",
"description": "Crafting recipes from BuildCraft, by SpaceToad",
"recipes": [ "recipes": [
{ {
"output": [[1, "Advanced Crafting Table"]], "output": [[1, "Advanced Crafting Table"]],
+3 -1
View File
@@ -1,5 +1,7 @@
{ {
"name": "GraviSuite", "version": 1,
"mod_name": "GraviSuite",
"mod_version": "unknown",
"description": "Crafting recipes from GraviSuite, by flotschi301", "description": "Crafting recipes from GraviSuite, by flotschi301",
"recipes": [ "recipes": [
{ {
+4 -2
View File
@@ -1,6 +1,8 @@
{ {
"name": "Industrial Craft", "version": 1,
"description": "Crafting recipes from IndustrialCraft 2, by Alblaka", "mod_name": "Industrial Craft",
"mod_version": "2.0",
"description": "Crafting recipes from IndustrialCraft, by Alblaka",
"recipes": [ "recipes": [
{ {
"output": [[1, "Static Boots"]], "output": [[1, "Static Boots"]],
+3 -1
View File
@@ -1,5 +1,7 @@
{ {
"name": "MoreBlocks", "version": 1,
"mod_name": "MoreBlocks",
"mod_version": "1.0",
"description": "Crafting recipes from More Blocks Mod, by MCE626", "description": "Crafting recipes from More Blocks Mod, by MCE626",
"recipes": [ "recipes": [
{ {
+3 -1
View File
@@ -1,5 +1,7 @@
{ {
"name": "Thermal Expansion", "version": 1,
"mod_name": "Thermal Expansion",
"mod_version": "2.x",
"description": "Crafting Recipes from Thermal Expansion, by King Lemming and the CoFH team", "description": "Crafting Recipes from Thermal Expansion, by King Lemming and the CoFH team",
"recipes": [ "recipes": [
{ {
+3 -1
View File
@@ -1,5 +1,7 @@
{ {
"name": "Vanilla Recipes", "version": 1,
"mod_name": "Minecraft",
"mod_version": "1.6.4",
"description": "Crafting recipes from vanilla Minecraft (in progress)", "description": "Crafting recipes from vanilla Minecraft (in progress)",
"recipes": [ "recipes": [
{ {
+17 -7
View File
@@ -15,10 +15,20 @@ exports.Opacity = Opacity = {}
Opacity.hidden = 1e-6 Opacity.hidden = 1e-6
Opacity.shown = 1 Opacity.shown = 1
exports.Event = Event = {} exports.Event = Event = {}
Event.book = {} Event.change = 'change'
Event.book.load = {} Event.load = {}
Event.book.load.started = 'book:load:started' # controller, url Event.load.started = 'book:load:started' # controller, url
Event.book.load.succeeded = 'book:load:succeeded' # controller, book Event.load.succeeded = 'book:load:succeeded' # controller, book
Event.book.load.failed = 'book:load:failed' # controller, error message Event.load.failed = 'book:load:failed' # controller, error message
Event.book.load.finished = 'book:load:finished' # controller Event.load.finished = 'book:load:finished' # controller
exports.DefaultBookUrls = DefaultBookUrls = [
'/data/vanilla.json'
'/data/applied_energetics.json'
'/data/buildcraft.json'
'/data/gravisuite.json'
'/data/industrial_craft.json'
'/data/more_blocks.json'
'/data/thermal_expansion.json'
]
+38 -1
View File
@@ -12,6 +12,9 @@ views = require '../views'
module.exports = class BaseController extends Backbone.View module.exports = class BaseController extends Backbone.View
constructor: (options={})-> constructor: (options={})->
Object.defineProperty this, 'model', get:@getModel, set:@setModel
@_model = null
@_rendered = false @_rendered = false
@_parent = options.parent @_parent = options.parent
@_children = [] @_children = []
@@ -35,10 +38,40 @@ module.exports = class BaseController extends Backbone.View
# Event Methods ################################################################################ # Event Methods ################################################################################
onDidModelChange: ->
@_tryRefresh()
onDidModelSync: ->
@_tryRefresh()
onWillRender: -> # do nothing onWillRender: -> # do nothing
onDidRender: -> onDidRender: ->
@refresh() @_tryRefresh()
onWillShow: -> # do nothing
onDidShow: -> # do nothing
onWillChangeModel: (oldModel, newModel)->
if oldModel?.on?
@stopListening oldModel
if newModel?.on?
@listenTo newModel, 'sync', (e)=> @onDidModelSync e
@listenTo newModel, 'change', (e)=> @onDidModelChange e
return true
# Property Methods #############################################################################
getModel: ->
return @_model
setModel: (newModel)->
return if @model is newModel
return unless @onWillChangeModel @_model, newModel
@_model = newModel
@_tryRefresh()
# Backbone.View Overrides ###################################################################### # Backbone.View Overrides ######################################################################
@@ -75,3 +108,7 @@ module.exports = class BaseController extends Backbone.View
_loadTemplate: (templateName)-> _loadTemplate: (templateName)->
if templateName? if templateName?
@_template = views[templateName] @_template = views[templateName]
_tryRefresh: ->
return unless @_rendered
@refresh()
@@ -1,5 +1,5 @@
### ###
# Crafting Guide - crafting_guide_controller.coffee # Crafting Guide - landing_page_controller.coffee
# #
# Copyright (c) 2014 by Redwood Labs # Copyright (c) 2014 by Redwood Labs
# All rights reserved. # All rights reserved.
@@ -21,5 +21,5 @@ module.exports = class LandingPageController extends BaseController
# BaseController Overrides ##################################################################### # BaseController Overrides #####################################################################
onDidRender: -> onDidRender: ->
@recipeBooksController = @addChild RecipeCatalogController, '.view__recipe_catalog' @recipeBooksController = @addChild RecipeCatalogController, '.view__recipe_catalog', model:@model.catalog
super super
@@ -0,0 +1,28 @@
###
# Crafting Guide - recipe_book_controller.coffee
#
# Copyright (c) 2014 by Redwood Labs
# All rights reserved.
###
BaseController = require './base_controller'
########################################################################################################################
module.exports = class RecipeBookController extends BaseController
constructor: (options={})->
if not options.model? then throw new Error "options.model is required"
options.templateName = 'recipe_book'
super options
# BaseController Overrides #####################################################################
onDidRender: ->
@$name = @$('td:nth-child(1) p')
@$description = @$('td:nth-child(2) p')
super
refresh: ->
@$name.html "#{@model.modName} (#{@model.modVersion})"
@$description.html "#{@model.description}"
@@ -5,12 +5,36 @@
# All rights reserved. # All rights reserved.
### ###
BaseController = require './base_controller' BaseController = require './base_controller'
RecipeBookController = require './recipe_book_controller'
######################################################################################################################## ########################################################################################################################
module.exports = class RecipeCatalogController extends BaseController module.exports = class RecipeCatalogController extends BaseController
constructor: (options={})-> constructor: (options={})->
if not options.model? then throw new Error "options.model is required"
@_bookControllers = []
options.templateName = 'recipe_catalog' options.templateName = 'recipe_catalog'
super options super options
# BaseController Overrides #####################################################################
onDidRender: ->
@$table = @$('table')
super
refresh: ->
@$('table tr:not(:last-child)').remove()
return unless @model?
@_bookControllers = []
for i in [@model.books.length-1..0] by -1
book = @model.books[i]
controller = new RecipeBookController model:book
controller.render()
@_bookControllers.push controller
@$table.prepend controller.$el
super
+3 -2
View File
@@ -41,14 +41,15 @@ module.exports = class CraftingGuideRouter extends Backbone.Router
show = => show = =>
@_page = controller @_page = controller
controller.onWillShow()
controller.render() controller.render()
# controller.$el.css 'opacity', Opacity.hidden
$pageContent = $('.page') $pageContent = $('.page')
$pageContent.empty() $pageContent.empty()
$pageContent.append controller.$el $pageContent.append controller.$el
controller.$el.fadeIn showDuration controller.$el.fadeIn showDuration, ->
controller.onDidShow()
if @_mainController? if @_mainController?
showDuration = Duration.fast showDuration = Duration.fast
+38 -21
View File
@@ -5,9 +5,10 @@
# All rights reserved. # All rights reserved.
### ###
BaseModel = require './base_model' BaseModel = require './base_model'
{Event} = require '../constants' {DefaultBookUrls} = require '../constants'
RecipeBookParser = require './recipe_book_parser' {Event} = require '../constants'
RecipeBookParser = require './recipe_book_parser'
######################################################################################################################## ########################################################################################################################
@@ -18,35 +19,51 @@ module.exports = class RecipeCatalog extends BaseModel
super attributes, options super attributes, options
@_parser = new RecipeBookParser @_parser = new RecipeBookParser
if @books.length is 0
@loadAllBooks DefaultBookUrls
# Public Methods ############################################################################### # Public Methods ###############################################################################
loadBook: (url)-> loadBook: (url)->
@trigger Event.book.load.started, this, url w.promise (resolve, reject)=>
$.ajax @trigger Event.load.started, this, url
url: url $.ajax
dataType: 'json' url: url
success: (data, status, xhr)=> @onBookLoaded(data, status, xhr) dataType: 'json'
error: (xhr, status, error)=> @onBookLoadFailed(error, status, xhr) success: (data, status, xhr)=>
resolve @onBookLoaded(url, data, status, xhr)
error: (xhr, status, error)=>
reject @onBookLoadFailed(url, error, status, xhr)
loadAllBooks: (urlList)->
promises = (@loadBook(url) for url in urlList)
return w.settle promises
# Event Methods ################################################################################ # Event Methods ################################################################################
onBookLoaded: (data, status, xhr)-> onBookLoaded: (url, data, status, xhr)->
try try
@books.push @_parser.parse data book = @_parser.parse data
_(@books).sortBy 'name' @books.push book
@books.sort (a, b)->
return 0 if a.modName is b.modName
return if a.modName < b.modName then -1 else +1
logger.info "loaded recipe book: #{book}" logger.info "loaded recipe book from #{url}: #{book}"
@trigger Event.book.load.succeeded, this, book @trigger Event.load.succeeded, this, book
@trigger Event.book.load.finished, this @trigger Event.load.finished, this
@trigger Event.change, this
return book
catch e catch e
@onBookLoadFailed error, status, xhr @onBookLoadFailed url, e, status, xhr
onBookLoadFailed: (url, error, status, xhr)->
onBookLoadFailed: (error, status, xhr)-> message = if error.stack? then error.stack else error
logger.error "failed to load recipe book: #{error}" logger.error "failed to load recipe book from #{url}: #{message}"
@trigger Event.book.load.failed, this, error.message @trigger Event.load.failed, this, error.message
@trigger Event.book.load.finished, this @trigger Event.load.finished, this
return error
# Object Overrides ############################################################################# # Object Overrides #############################################################################
+10 -1
View File
@@ -11,13 +11,22 @@ var jade_indent = [];
buf.push("\n<div class=\"view__landing_page\">\n <div class=\"view__recipe_catalog\"></div>\n <div class=\"view__crafter\"></div>\n</div>");;return buf.join(""); buf.push("\n<div class=\"view__landing_page\">\n <div class=\"view__recipe_catalog\"></div>\n <div class=\"view__crafter\"></div>\n</div>");;return buf.join("");
}; };
this["JST"]["recipe_book"] = function template(locals) {
var buf = [];
var jade_mixins = {};
var jade_interp;
var jade_indent = [];
buf.push("\n<tr>\n <td>\n <p></p>\n </td>\n <td>\n <p></p>\n </td>\n</tr>");;return buf.join("");
};
this["JST"]["recipe_catalog"] = function template(locals) { this["JST"]["recipe_catalog"] = function template(locals) {
var buf = []; var buf = [];
var jade_mixins = {}; var jade_mixins = {};
var jade_interp; var jade_interp;
var jade_indent = []; var jade_indent = [];
buf.push("\n<div class=\"view__recipe_catalog\">\n <h2><img src=\"/images/bookshelf.png\"/>\n <p>Recipe Catalog</p>\n </h2>\n <table class=\"books\">\n <tr>\n <td>&nbsp;</td>\n <td>\n <input placeholder=\"enter a URL to load another recipe book...\" class=\"recipe_book_url\"/>\n <button class=\"recipe_book_load_button\">Load</button>\n </td>\n </tr>\n </table>\n <div class=\"load_error\">\n <p></p>\n </div>\n</div>");;return buf.join(""); buf.push("\n<div class=\"view__recipe_catalog\">\n <h2><img src=\"/images/bookshelf.png\"/>\n <p>Recipe Catalog</p>\n </h2>\n <table>\n <tr>\n <td>&nbsp;</td>\n <td>\n <input placeholder=\"enter a URL to load another recipe book...\" class=\"recipe_book_url\"/>\n <button class=\"recipe_book_load_button\">Load</button>\n </td>\n </tr>\n </table>\n <div class=\"load_error\">\n <p></p>\n </div>\n</div>");;return buf.join("");
}; };
this["JST"]["test"] = function template(locals) { this["JST"]["test"] = function template(locals) {
+10
View File
@@ -0,0 +1,10 @@
//-
//- Crafting Guide - recipe_book.jade
//-
//- Copyright (c) 2014 by Redwood Labs
//- All rights reserved.
//-
tr
td: p
td: p
+1 -1
View File
@@ -10,7 +10,7 @@
img(src="/images/bookshelf.png") img(src="/images/bookshelf.png")
p Recipe Catalog p Recipe Catalog
table.books table
tr tr
td &nbsp; td &nbsp;
td td