codecombat/app/views/editor/level/systems/SystemsTabView.coffee

162 lines
6.4 KiB
CoffeeScript
Raw Normal View History

2014-07-17 20:20:11 -04:00
CocoView = require 'views/kinds/CocoView'
2014-01-03 13:32:13 -05:00
template = require 'templates/editor/level/systems_tab'
Level = require 'models/Level'
LevelSystem = require 'models/LevelSystem'
LevelSystemEditView = require './LevelSystemEditView'
NewLevelSystemModal = require './NewLevelSystemModal'
AddLevelSystemModal = require './AddLevelSystemModal'
{ThangTypeNode} = require './../treema_nodes'
2014-01-03 13:32:13 -05:00
2014-07-17 20:20:11 -04:00
module.exports = class SystemsTabView extends CocoView
2014-06-30 22:16:26 -04:00
id: 'editor-level-systems-tab-view'
2014-01-03 13:32:13 -05:00
template: template
className: 'tab-pane'
subscriptions:
'editor:level-system-added': 'onLevelSystemAdded'
'editor:edit-level-system': 'editLevelSystem'
'editor:level-system-editing-ended': 'onLevelSystemEditingEnded'
'editor:level-loaded': 'onLevelLoaded'
2014-01-03 13:32:13 -05:00
events:
'click #add-system-button': 'addLevelSystem'
'click #create-new-system-button': 'createNewLevelSystem'
'click #create-new-system': 'createNewLevelSystem'
2014-01-03 13:32:13 -05:00
constructor: (options) ->
super options
for system in @buildDefaultSystems()
url = "/db/level.system/#{system.original}/version/#{system.majorVersion}"
ls = new LevelSystem().setURL(url)
@supermodel.loadModel(ls, 'system')
2014-01-03 13:32:13 -05:00
afterRender: ->
@buildSystemsTreema()
2014-06-30 22:16:26 -04:00
2014-04-16 02:28:59 -04:00
onLoaded: ->
super()
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
onLevelLoaded: (e) ->
@level = e.level
@buildSystemsTreema()
buildSystemsTreema: ->
return unless @level and @supermodel.finished()
systems = $.extend(true, [], @level.get('systems') ? [])
2014-01-03 13:32:13 -05:00
unless systems.length
systems = @buildDefaultSystems()
insertedDefaults = true
systems = @getSortedByName systems
2014-01-03 13:32:13 -05:00
treemaOptions =
# TODO: somehow get rid of the + button, or repurpose it to open the AddLevelSystemModal instead
2014-01-03 13:32:13 -05:00
supermodel: @supermodel
2014-04-12 04:35:56 -04:00
schema: Level.schema.properties.systems
2014-01-03 13:32:13 -05:00
data: systems
readOnly: me.get('anonymous')
2014-01-03 13:32:13 -05:00
callbacks:
change: @onSystemsChanged
select: @onSystemSelected
nodeClasses:
'level-system': LevelSystemNode
'level-system-configuration': LevelSystemConfigurationNode
'thang-type': ThangTypeNode # Not until we actually want CocoSprite IndieSprites
@systemsTreema = @$el.find('#systems-treema').treema treemaOptions
@systemsTreema.build()
@systemsTreema.open()
@onSystemsChanged() if insertedDefaults
onSystemsChanged: (e) =>
systems = @getSortedByName @systemsTreema.data
@level.set 'systems', systems
getSortedByName: (systems) =>
systemModels = @supermodel.getModels LevelSystem
systemModelMap = {}
systemModelMap[sys.get('original')] = sys.get('name') for sys in systemModels
_.sortBy systems, (sys) -> systemModelMap[sys.original]
2014-01-03 13:32:13 -05:00
onSystemSelected: (e, selected) =>
selected = if selected.length > 1 then selected[0].getLastSelectedTreema() else selected[0]
unless selected
@removeSubView @levelSystemEditView if @levelSystemEditView
@levelSystemEditView = null
return
until selected.data.original
selected = selected.parent
@editLevelSystem original: selected.data.original, majorVersion: selected.data.majorVersion
onLevelSystemAdded: (e) ->
@systemsTreema.insert '/', e.system
addLevelSystem: (e) ->
@openModalView new AddLevelSystemModal supermodel: @supermodel, extantSystems: _.cloneDeep @systemsTreema.data
Backbone.Mediator.publish 'editor:view-switched', {}
2014-01-03 13:32:13 -05:00
createNewLevelSystem: (e) ->
@openModalView new NewLevelSystemModal supermodel: @supermodel
Backbone.Mediator.publish 'editor:view-switched', {}
2014-01-03 13:32:13 -05:00
editLevelSystem: (e) ->
@levelSystemEditView = @insertSubView new LevelSystemEditView(original: e.original, majorVersion: e.majorVersion, supermodel: @supermodel)
onLevelSystemEditingEnded: (e) ->
@removeSubView @levelSystemEditView
@levelSystemEditView = null
buildDefaultSystems: ->
[
2014-06-30 22:16:26 -04:00
{original: '528112c00268d018e3000008', majorVersion: 0} # Event
{original: '5280f83b8ae1581b66000001', majorVersion: 0, config: {lifespan: 60}} # Existence
{original: '5281146f0268d018e3000014', majorVersion: 0} # Programming
{original: '528110f30268d018e3000001', majorVersion: 0} # AI
{original: '52810ffa33e01a6e86000012', majorVersion: 0} # Action
{original: '528114b20268d018e3000017', majorVersion: 0} # Targeting
{original: '528105f833e01a6e86000007', majorVersion: 0} # Collision
{original: '528113240268d018e300000c', majorVersion: 0, config: {gravity: 9.81}} # Movement
{original: '528112530268d018e3000007', majorVersion: 0} # Combat
{original: '52810f4933e01a6e8600000c', majorVersion: 0} # Hearing
{original: '528115040268d018e300001b', majorVersion: 0} # Vision
{original: '5280dc4d251616c907000001', majorVersion: 0} # Inventory
{original: '528111b30268d018e3000004', majorVersion: 0} # Alliance
{original: '528114e60268d018e300001a', majorVersion: 0} # UI
{original: '528114040268d018e3000011', majorVersion: 0} # Physics
2014-01-03 13:32:13 -05:00
]
class LevelSystemNode extends TreemaObjectNode
valueClass: 'treema-level-system'
constructor: ->
super(arguments...)
@grabDBComponent()
@collection = @system?.attributes?.configSchema?.properties?
grabDBComponent: ->
unless _.isString @data.original
return alert('Press the "Add System" button at the bottom instead of the "+". Sorry.')
2014-04-16 02:28:59 -04:00
@system = @settings.supermodel.getModelByOriginalAndMajorVersion(LevelSystem, @data.original, @data.majorVersion)
2014-06-30 22:16:26 -04:00
console.error 'Couldn\'t find system for', @data.original, @data.majorVersion, 'from models', @settings.supermodel.models unless @system
2014-01-03 13:32:13 -05:00
getChildSchema: (key) ->
return @system.attributes.configSchema if key is 'config'
return super(key)
buildValueForDisplay: (valEl) ->
return super valEl unless @data.original and @system
name = "#{@system.get('name')} v#{@system.get('version').major}"
@buildValueForDisplaySimply valEl, "#{name}"
2014-04-11 20:36:15 -04:00
onEnterPressed: (e) ->
super e
Backbone.Mediator.publish 'editor:edit-level-system', original: @data.original, majorVersion: @data.majorVersion
2014-01-03 13:32:13 -05:00
2014-04-11 20:36:15 -04:00
open: (depth) ->
super depth
2014-01-03 13:32:13 -05:00
cTreema = @childrenTreemas.config
if cTreema? and (cTreema.getChildren().length or cTreema.canAddChild())
cTreema.open()
# No easy way to flatten the config object, so for now just keep it longer than it needs to be
class LevelSystemConfigurationNode extends TreemaObjectNode
valueClass: 'treema-level-system-configuration'
buildValueForDisplay: (valEl) ->
return