Allow items to be grouped on mod detail pages

This commit is contained in:
Andrew Miner
2015-01-30 13:03:10 -08:00
parent a365217890
commit 6e3843731b
11 changed files with 763 additions and 589 deletions
+3
View File
@@ -14,9 +14,12 @@ Recipe = require './recipe'
module.exports = class Item extends BaseModel
@Group = Other:'Other'
constructor: (attributes={}, options={})->
if not attributes.name? then throw new Error 'attributes.name is required'
attributes.group ?= Item.Group.Other
attributes.isGatherable ?= false
attributes.slug ?= _.slugify attributes.name
options.logEvents ?= false
+19 -4
View File
@@ -21,16 +21,20 @@ module.exports = class ModVersion extends BaseModel
if not attributes.version? then throw new Error 'attributes.version is required'
super attributes, options
@_items = {}
@_names = {}
@_slugs = []
@_groups = {}
@_items = {}
@_names = {}
@_slugs = []
# Public Methods ###############################################################################
addItem: (item)->
if @_items[item.slug]? then throw new Error "duplicate item for #{item.name}"
@_items[item.slug] = item
@_items[item.slug] = item
@_groups[item.group] ?= {}
@_groups[item.group][item.slug] = item
item.modVersion = this
@registerSlug item.slug, item.name
return this
@@ -44,6 +48,10 @@ module.exports = class ModVersion extends BaseModel
return 0
eachGroup: (callback)->
for groupName in _.keys(@_groups).sort()
callback groupName
eachItem: (callback)->
for slug in @_slugs
item = @_items[slug]
@@ -51,6 +59,13 @@ module.exports = class ModVersion extends BaseModel
callback @_items[slug], slug
return this
eachItemInGroup: (group, callback)->
group = @_groups[group]
return unless group?
for slug in _.keys(group).sort()
callback group[slug]
eachName: (callback)->
for slug in @_slugs
callback @_names[slug], slug
@@ -53,10 +53,14 @@ module.exports = class ModVersionParserV1 extends CommandParserVersionBase
@_itemData.gatherable = (gatherable is 'yes')
_command_group: (group)->
if group.length is 0 then throw new Error 'a group name cannot be empty'
@_rawData.group = group
_command_item: (name='')->
if not name.length > 0 then throw new Error 'the item name cannot be empty'
@_itemData = name:name, line:@_lineNumber
@_itemData = name:name, line:@_lineNumber, group:@_rawData.group
@_rawData.items ?= []
@_rawData.items.push @_itemData
@@ -118,7 +122,7 @@ module.exports = class ModVersionParserV1 extends CommandParserVersionBase
itemData.gatherable ?= false
itemData.recipes ?= []
item = new Item name:itemData.name, isGatherable:itemData.gatherable
item = new Item name:itemData.name, isGatherable:itemData.gatherable, group:itemData.group
modVersion.addItem item
for recipeData in itemData.recipes
@@ -186,9 +190,23 @@ module.exports = class ModVersionParserV1 extends CommandParserVersionBase
builder
.line 'schema: ', 1
.line()
.onlyIf itemList.length > 0, =>
builder.loop itemList, delimiter:'\n', onEach:(b, i)=> @_unparseItem(b, i)
.outdent()
modVersion.eachGroup (group)=>
@_unparseGroup builder, modVersion, group
_unparseGroup: (builder, modVersion, group)->
if group isnt Item.Group.Other
builder
.line 'group: ', group
.line()
.indent()
modVersion.eachItemInGroup group, (item)=>
@_unparseItem builder, item
builder.line()
if group isnt Item.Group.Other
builder.outdent()
_unparseItem: (builder, item)->
recipes = []