codecombat/app/views/editor/level/thangs/LevelThangEditView.coffee

95 lines
3.3 KiB
CoffeeScript
Raw Normal View History

2014-07-17 20:20:11 -04:00
CocoView = require 'views/kinds/CocoView'
template = require 'templates/editor/level/thang/level-thang-edit-view'
ThangComponentsEditView = require 'views/editor/component/ThangComponentsEditView'
2014-01-03 13:32:13 -05:00
ThangType = require 'models/ThangType'
2014-07-17 20:20:11 -04:00
module.exports = class LevelThangEditView extends CocoView
2014-01-03 13:32:13 -05:00
###
In the level editor, is the bar at the top when editing a single thang.
Everything below is part of the ThangComponentsEditView, which is shared with the
2014-01-03 13:32:13 -05:00
ThangType editor view.
###
id: 'level-thang-edit-view'
2014-01-03 13:32:13 -05:00
template: template
2014-01-03 13:32:13 -05:00
events:
'click #all-thangs-link': 'navigateToAllThangs'
'click #thang-name-link span': 'toggleNameEdit'
'click #thang-type-link span': 'toggleTypeEdit'
'blur #thang-name-link input': 'toggleNameEdit'
'blur #thang-type-link input': 'toggleTypeEdit'
constructor: (options) ->
options ?= {}
super options
@world = options.world
@thangData = options.thangData ? {}
@level = options.level
@oldID = @thangData.id
2014-02-11 17:58:45 -05:00
getRenderData: (context={}) ->
2014-01-03 13:32:13 -05:00
context = super(context)
context.thang = @thangData
context
onLoaded: -> @render()
2014-01-03 13:32:13 -05:00
afterRender: ->
super()
thangType = @supermodel.getModelByOriginal(ThangType, @thangData.thangType)
2014-01-03 13:32:13 -05:00
options =
components: @thangData.components
supermodel: @supermodel
level: @level
world: @world
if @level.get('type') is 'hero' then options.thangType = thangType
@thangComponentEditView = new ThangComponentsEditView options
@listenTo @thangComponentEditView, 'components-changed', @onComponentsChanged
2014-01-03 13:32:13 -05:00
@insertSubView @thangComponentEditView
thangTypeNames = (m.get('name') for m in @supermodel.getModels ThangType)
input = @$el.find('#thang-type-link input').autocomplete(source: thangTypeNames, minLength: 0, delay: 0, autoFocus: true)
thangType = _.find @supermodel.getModels(ThangType), (m) => m.get('original') is @thangData.thangType
thangTypeName = thangType?.get('name') or 'None'
input.val(thangTypeName)
@$el.find('#thang-type-link span').text(thangTypeName)
window.input = input
2014-04-16 02:28:59 -04:00
@hideLoading()
2014-01-03 13:32:13 -05:00
saveThang: (e) ->
# Make sure it validates first?
Backbone.Mediator.publish 'editor:level-thang-edited', thangData: @thangData, thangID: @oldID
2014-01-03 13:32:13 -05:00
navigateToAllThangs: ->
Backbone.Mediator.publish 'editor:level-thang-done-editing', {}
2014-01-03 13:32:13 -05:00
toggleNameEdit: ->
link = @$el.find '#thang-name-link'
wasEditing = link.find('input:visible').length
span = link.find('span')
input = link.find('input')
if wasEditing then span.text(input.val()) else input.val(span.text())
link.find('span, input').toggle()
2014-01-03 13:32:13 -05:00
input.select() unless wasEditing
@thangData.id = span.text()
@saveThang()
2014-01-03 13:32:13 -05:00
toggleTypeEdit: ->
link = @$el.find '#thang-type-link'
wasEditing = link.find('input:visible').length
span = link.find('span')
input = link.find('input')
span.text(input.val()) if wasEditing
link.find('span, input').toggle()
2014-01-03 13:32:13 -05:00
input.select() unless wasEditing
thangTypeName = input.val()
thangType = _.find @supermodel.getModels(ThangType), (m) -> m.get('name') is thangTypeName
if thangType and wasEditing
@thangData.thangType = thangType.get('original')
@saveThang()
2014-01-03 13:32:13 -05:00
onComponentsChanged: (components) =>
@thangData.components = components
@saveThang()