codecombat/app/views/editor/component/ThangComponentConfigView.coffee

103 lines
3.2 KiB
CoffeeScript
Raw Normal View History

CocoView = require 'views/core/CocoView'
template = require 'templates/editor/component/thang-component-config-view'
2014-01-03 13:32:13 -05:00
Level = require 'models/Level'
LevelComponent = require 'models/LevelComponent'
nodes = require '../level/treema_nodes'
module.exports = class ThangComponentConfigView extends CocoView
className: 'thang-component-config-view'
2014-01-03 13:32:13 -05:00
template: template
changed: false
2014-01-03 13:32:13 -05:00
constructor: (options) ->
super options
@component = options.component
@config = options.config or {}
@additionalDefaults = options.additionalDefaults
2014-08-28 16:53:00 -04:00
@isDefaultComponent = false
2014-01-03 13:32:13 -05:00
@world = options.world
@level = options.level
@callback = options.callback
2014-02-11 17:58:45 -05:00
getRenderData: (context={}) ->
2014-01-03 13:32:13 -05:00
context = super(context)
context.component = @component.attributes
2014-01-03 13:32:13 -05:00
context.configProperties = []
context.isDefaultComponent = @isDefaultComponent
2014-01-03 13:32:13 -05:00
context
afterRender: ->
super()
@buildTreema()
2014-09-01 16:51:30 -04:00
setConfig: (config) ->
@handlingChange = true
@editThangTreema.set('/', config)
@handlingChange = false
2014-06-30 22:16:26 -04:00
setIsDefaultComponent: (isDefaultComponent) ->
changed = @isDefaultComponent isnt isDefaultComponent
if isDefaultComponent then @config = undefined
@isDefaultComponent = isDefaultComponent
@render() if changed
2014-01-03 13:32:13 -05:00
buildTreema: ->
thangs = if @level? then @level.get('thangs') else []
thangIDs = _.filter(_.pluck(thangs, 'id'))
teams = _.filter(_.pluck(thangs, 'team'))
superteams = _.filter(_.pluck(thangs, 'superteam'))
superteams = _.union(teams, superteams)
schema = $.extend true, {}, @component.get('configSchema')
schema.default ?= {}
_.merge schema.default, @additionalDefaults if @additionalDefaults
2014-09-01 16:51:30 -04:00
if @level?.get('type', true) in ['hero', 'hero-ladder', 'hero-coop']
schema.required = []
2014-01-03 13:32:13 -05:00
treemaOptions =
supermodel: @supermodel
schema: schema
data: @config
2014-01-03 13:32:13 -05:00
callbacks: {change: @onConfigEdited}
world: @world
view: @
thangIDs: thangIDs
teams: teams
superteams: superteams
nodeClasses:
object: ComponentConfigNode
2014-01-03 13:32:13 -05:00
'point2d': nodes.WorldPointNode
'viewport': nodes.WorldViewportNode
'bounds': nodes.WorldBoundsNode
'radians': nodes.RadiansNode
'team': nodes.TeamNode
'superteam': nodes.SuperteamNode
'meters': nodes.MetersNode
'kilograms': nodes.KilogramsNode
'seconds': nodes.SecondsNode
'speed': nodes.SpeedNode
'acceleration': nodes.AccelerationNode
2014-09-01 16:51:30 -04:00
'thang-type': nodes.ThangTypeNode
'item-thang-type': nodes.ItemThangTypeNode
2014-01-03 13:32:13 -05:00
@editThangTreema = @$el.find('.treema').treema treemaOptions
@editThangTreema.build()
@editThangTreema.open(2)
if _.isEqual(@editThangTreema.data, {}) and not @editThangTreema.canAddChild()
@$el.find('.panel-body').hide()
2014-01-03 13:32:13 -05:00
onConfigEdited: =>
2014-08-28 18:42:46 -04:00
return if @destroyed or @handlingChange
2014-08-28 16:53:00 -04:00
@config = @data()
2014-01-03 13:32:13 -05:00
@changed = true
2014-08-28 16:53:00 -04:00
@trigger 'changed', { component: @component, config: @config }
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
data: -> @editThangTreema.data
2014-09-01 16:51:30 -04:00
destroy: ->
@editThangTreema?.destroy()
super()
class ComponentConfigNode extends TreemaObjectNode
nodeDescription: 'Component Property'