codecombat/app/views/editor/level/settings/SettingsTabView.coffee
2014-08-27 18:43:17 -07:00

65 lines
2.1 KiB
CoffeeScript

CocoView = require 'views/kinds/CocoView'
template = require 'templates/editor/level/settings_tab'
Level = require 'models/Level'
Surface = require 'lib/surface/Surface'
nodes = require './../treema_nodes'
{me} = require 'lib/auth'
module.exports = class SettingsTabView extends CocoView
id: 'editor-level-settings-tab-view'
className: 'tab-pane'
template: template
# not thangs or scripts or the backend stuff
editableSettings: [
'name', 'description', 'documentation', 'nextLevel', 'background', 'victory', 'i18n', 'icon', 'goals',
'type', 'showsGuide', 'banner', 'employerDescription'
]
subscriptions:
'editor:level-loaded': 'onLevelLoaded'
'editor:thangs-edited': 'onThangsEdited'
constructor: (options) ->
super options
onLoaded: ->
onLevelLoaded: (e) ->
@level = e.level
data = _.pick @level.attributes, (value, key) => key in @editableSettings
schema = _.cloneDeep Level.schema
schema.properties = _.pick schema.properties, (value, key) => key in @editableSettings
schema.required = _.intersection schema.required, @editableSettings
@thangIDs = @getThangIDs()
treemaOptions =
filePath: "db/level/#{@level.get('original')}"
supermodel: @supermodel
schema: schema
data: data
readOnly: me.get('anonymous')
callbacks: {change: @onSettingsChanged}
thangIDs: @thangIDs
nodeClasses:
object: SettingsNode
thang: nodes.ThangNode
@settingsTreema = @$el.find('#settings-treema').treema treemaOptions
@settingsTreema.build()
@settingsTreema.open()
getThangIDs: ->
(t.id for t in @level.get('thangs'))
onSettingsChanged: (e) =>
$('.level-title').text @settingsTreema.data.name
for key in @editableSettings
continue if @settingsTreema.data[key] is undefined
@level.set key, @settingsTreema.data[key]
onThangsEdited: (e) ->
# Update in-place so existing Treema nodes refer to the same array.
@thangIDs.splice(0, @thangIDs.length, @getThangIDs()...)
class SettingsNode extends TreemaObjectNode
nodeDescription: 'Settings'