codecombat/app/views/editor/level/components/ComponentsTabView.coffee

97 lines
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/components_tab'
ThangType = require 'models/ThangType'
2014-01-03 13:32:13 -05:00
LevelComponent = require 'models/LevelComponent'
LevelComponentEditView = require './LevelComponentEditView'
LevelComponentNewView = require './NewLevelComponentModal'
2014-01-03 13:32:13 -05:00
class LevelComponentCollection extends Backbone.Collection
url: '/db/level.component'
model: LevelComponent
2014-07-17 20:20:11 -04:00
module.exports = class ComponentsTabView extends CocoView
2014-06-30 22:16:26 -04:00
id: 'editor-level-components-tab-view'
2014-01-03 13:32:13 -05:00
template: template
className: 'tab-pane'
subscriptions:
'editor:level-component-editing-ended': 'onLevelComponentEditingEnded'
2014-01-03 13:32:13 -05:00
events:
'click #create-new-component-button': 'createNewLevelComponent'
'click #create-new-component-button-no-select': 'createNewLevelComponent'
2014-01-03 13:32:13 -05:00
2014-04-16 02:28:59 -04:00
onLoaded: ->
refreshLevelThangsTreema: (thangsData) ->
2014-01-03 13:32:13 -05:00
presentComponents = {}
for thang in thangsData
componentMap = {}
thangType = @supermodel.getModelByOriginal ThangType, thang.thangType
for component in thangType.get('components') ? []
componentMap[component.original] = component
2014-01-03 13:32:13 -05:00
for component in thang.components
componentMap[component.original] = component
for component in _.values(componentMap)
2014-01-03 13:32:13 -05:00
haveThisComponent = (presentComponents[component.original + '.' + (component.majorVersion ? 0)] ?= [])
haveThisComponent.push thang.id if haveThisComponent.length < 100 # for performance when adding many Thangs
return if _.isEqual presentComponents, @presentComponents
@presentComponents = presentComponents
2014-01-26 18:59:33 -05:00
componentModels = @supermodel.getModels LevelComponent
componentModelMap = {}
componentModelMap[comp.get('original')] = comp for comp in componentModels
2014-01-26 18:59:33 -05:00
components = ({original: key.split('.')[0], majorVersion: parseInt(key.split('.')[1], 10), thangs: value, count: value.length} for key, value of @presentComponents)
treemaData = _.sortBy components, (comp) ->
comp = componentModelMap[comp.original]
res = [comp.get('system'), comp.get('name')]
return res
2014-01-03 13:32:13 -05:00
treemaOptions =
supermodel: @supermodel
schema: {type: 'array', items: {type: 'object', format: 'level-component'}}
data: treemaData
callbacks:
select: @onTreemaComponentSelected
readOnly: true
nodeClasses: {'level-component': LevelComponentNode}
@componentsTreema = @$el.find('#components-treema').treema treemaOptions
@componentsTreema.build()
@componentsTreema.open()
onTreemaComponentSelected: (e, selected) =>
selected = if selected.length > 1 then selected[0].getLastSelectedTreema() else selected[0]
unless selected
@removeSubView @levelComponentEditView
@levelComponentEditView = null
return
@editLevelComponent original: selected.data.original, majorVersion: selected.data.majorVersion
createNewLevelComponent: (e) ->
levelComponentNewView = new LevelComponentNewView supermodel: @supermodel
@openModalView levelComponentNewView
Backbone.Mediator.publish 'editor:view-switched', {}
2014-01-03 13:32:13 -05:00
editLevelComponent: (e) ->
@levelComponentEditView = @insertSubView new LevelComponentEditView(original: e.original, majorVersion: e.majorVersion, supermodel: @supermodel)
onLevelComponentEditingEnded: (e) ->
@removeSubView @levelComponentEditView
@levelComponentEditView = null
class LevelComponentNode extends TreemaObjectNode
valueClass: 'treema-level-component'
collection: false
buildValueForDisplay: (valEl, data) ->
count = if data.count is 1 then data.thangs[0] else ((if data.count >= 100 then '100+' else data.count) + ' Thangs')
if data.original.match ':'
name = 'Old: ' + data.original.replace('systems/', '')
2014-01-03 13:32:13 -05:00
else
comp = _.find @settings.supermodel.getModels(LevelComponent), (m) =>
m.get('original') is data.original and m.get('version').major is data.majorVersion
2014-01-03 13:32:13 -05:00
name = "#{comp.get('system')}.#{comp.get('name')} v#{comp.get('version').major}"
@buildValueForDisplaySimply valEl, "#{name} (#{count})"