2014-07-17 20:20:11 -04:00
|
|
|
CocoView = require 'views/kinds/CocoView'
|
2014-08-11 16:24:08 -04:00
|
|
|
template = require 'templates/editor/level/thang/level-thang-edit-view'
|
2014-08-07 21:49:39 -04:00
|
|
|
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.
|
2014-08-07 21:49:39 -04:00
|
|
|
Everything below is part of the ThangComponentsEditView, which is shared with the
|
2014-01-03 13:32:13 -05:00
|
|
|
ThangType editor view.
|
|
|
|
###
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-08-11 16:24:08 -04:00
|
|
|
id: 'level-thang-edit-view'
|
2014-01-03 13:32:13 -05:00
|
|
|
template: template
|
2014-02-13 12:26:21 -05:00
|
|
|
|
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
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-04-17 19:37:31 -04:00
|
|
|
onLoaded: -> @render()
|
2014-01-03 13:32:13 -05:00
|
|
|
afterRender: ->
|
2014-04-17 19:37:31 -04:00
|
|
|
super()
|
2014-01-03 13:32:13 -05:00
|
|
|
options =
|
|
|
|
components: @thangData.components
|
|
|
|
supermodel: @supermodel
|
|
|
|
level: @level
|
|
|
|
world: @world
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-08-07 21:49:39 -04:00
|
|
|
@thangComponentEditView = new ThangComponentsEditView options
|
2014-08-11 16:24:08 -04:00
|
|
|
@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?
|
|
|
|
event =
|
|
|
|
thangData: @thangData
|
|
|
|
id: @oldID
|
|
|
|
Backbone.Mediator.publish 'level-thang-edited', event
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
navigateToAllThangs: ->
|
|
|
|
Backbone.Mediator.publish 'level-thang-done-editing'
|
2014-02-13 12:26:21 -05:00
|
|
|
|
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())
|
2014-02-13 12:26:21 -05:00
|
|
|
link.find('span, input').toggle()
|
2014-01-03 13:32:13 -05:00
|
|
|
input.select() unless wasEditing
|
|
|
|
@thangData.id = span.text()
|
|
|
|
@saveThang()
|
2014-02-13 12:26:21 -05:00
|
|
|
|
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')
|
2014-02-13 12:26:21 -05:00
|
|
|
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-02-13 12:26:21 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
onComponentsChanged: (components) =>
|
|
|
|
@thangData.components = components
|
|
|
|
@saveThang()
|