2014-11-28 20:49:41 -05:00
|
|
|
CocoView = require 'views/core/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'
|
2014-09-17 00:43:03 -04:00
|
|
|
'keydown #thang-name-link input': 'toggleNameEditIfReturn'
|
|
|
|
'keydown #thang-type-link input': 'toggleTypeEditIfReturn'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
options ?= {}
|
|
|
|
super options
|
|
|
|
@world = options.world
|
2014-09-02 21:43:02 -04:00
|
|
|
@thangData = $.extend true, {}, options.thangData ? {}
|
2014-01-03 13:32:13 -05:00
|
|
|
@level = options.level
|
2014-09-03 15:30:08 -04:00
|
|
|
@oldPath = options.oldPath
|
2014-09-17 00:43:03 -04:00
|
|
|
@reportChanges = _.debounce @reportChanges, 1000
|
2014-01-03 13:32:13 -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-08-26 12:48:33 -04:00
|
|
|
thangType = @supermodel.getModelByOriginal(ThangType, @thangData.thangType)
|
2014-01-03 13:32:13 -05:00
|
|
|
options =
|
|
|
|
components: @thangData.components
|
|
|
|
supermodel: @supermodel
|
|
|
|
level: @level
|
|
|
|
world: @world
|
2014-09-02 19:02:30 -04:00
|
|
|
|
2015-07-24 20:37:42 -04:00
|
|
|
if @level.get('type', true) in ['hero', 'hero-ladder', 'hero-coop', 'course', 'course-ladder'] then options.thangType = thangType
|
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)
|
2014-04-16 02:28:59 -04:00
|
|
|
@hideLoading()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
navigateToAllThangs: ->
|
2014-09-03 15:30:08 -04:00
|
|
|
Backbone.Mediator.publish 'editor:level-thang-done-editing', {thangData: $.extend(true, {}, @thangData), oldPath: @oldPath}
|
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()
|
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')
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-09-17 00:43:03 -04:00
|
|
|
toggleNameEditIfReturn: (e) ->
|
|
|
|
@$el.find('#thang-name-link input').blur() if e.which is 13
|
|
|
|
|
|
|
|
toggleTypeEditIfReturn: (e) ->
|
|
|
|
@$el.find('#thang-type-link input').blur() if e.which is 13
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
onComponentsChanged: (components) =>
|
|
|
|
@thangData.components = components
|
2014-09-17 00:43:03 -04:00
|
|
|
@reportChanges()
|
|
|
|
|
|
|
|
reportChanges: =>
|
|
|
|
return if @destroyed
|
|
|
|
Backbone.Mediator.publish 'editor:level-thang-edited', {thangData: $.extend(true, {}, @thangData), oldPath: @oldPath}
|