codecombat/app/views/editor/level/component/edit.coffee

100 lines
3.8 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
View = require 'views/kinds/CocoView'
VersionHistoryView = require 'views/editor/component/versions_view'
2014-01-03 13:32:13 -05:00
template = require 'templates/editor/level/component/edit'
LevelComponent = require 'models/LevelComponent'
module.exports = class LevelComponentEditView extends View
id: "editor-level-component-edit-view"
template: template
2014-01-28 00:25:01 -05:00
editableSettings: ['name', 'description', 'system', 'language', 'dependencies', 'propertyDocumentation', 'i18n']
2014-01-03 13:32:13 -05:00
events:
'click #done-editing-component-button': 'endEditing'
'click #history-button': 'showVersionHistory'
2014-01-28 00:06:27 -05:00
'click .nav a': (e) -> $(e.target).tab('show')
2014-01-03 13:32:13 -05:00
constructor: (options) ->
super options
@levelComponent = @supermodel.getModelByOriginalAndMajorVersion LevelComponent, options.original, options.majorVersion or 0
console.log "Couldn't get levelComponent for", options, "from", @supermodel.models unless @levelComponent
2014-02-11 17:58:45 -05:00
getRenderData: (context={}) ->
2014-01-03 13:32:13 -05:00
context = super(context)
context.editTitle = "#{@levelComponent.get('system')}.#{@levelComponent.get('name')}"
2014-01-03 13:32:13 -05:00
context
afterRender: ->
super()
2014-01-28 00:06:27 -05:00
@buildSettingsTreema()
@buildConfigSchemaTreema()
@buildCodeEditor()
buildSettingsTreema: ->
data = _.pick @levelComponent.attributes, (value, key) => key in @editableSettings
schema = _.cloneDeep LevelComponent.schema.attributes
schema.properties = _.pick schema.properties, (value, key) => key in @editableSettings
schema.required = _.intersection schema.required, @editableSettings
2014-01-03 13:32:13 -05:00
treemaOptions =
supermodel: @supermodel
2014-01-28 00:06:27 -05:00
schema: schema
2014-01-03 13:32:13 -05:00
data: data
2014-01-28 00:06:27 -05:00
callbacks: {change: @onComponentSettingsEdited}
treemaOptions.readOnly = true unless me.isAdmin()
@componentSettingsTreema = @$el.find('#edit-component-treema').treema treemaOptions
@componentSettingsTreema.build()
@componentSettingsTreema.open()
2014-01-03 13:32:13 -05:00
2014-01-28 00:06:27 -05:00
onComponentSettingsEdited: =>
2014-01-03 13:32:13 -05:00
# Make sure it validates first?
2014-01-28 00:06:27 -05:00
for key, value of @componentSettingsTreema.data
2014-01-03 13:32:13 -05:00
@levelComponent.set key, value unless key is 'js' # will compile code if needed
Backbone.Mediator.publish 'level-component-edited', levelComponent: @levelComponent
null
2014-01-28 00:06:27 -05:00
buildConfigSchemaTreema: ->
treemaOptions =
supermodel: @supermodel
schema: LevelComponent.schema.get('properties').configSchema
data: @levelComponent.get 'configSchema'
callbacks: {change: @onConfigSchemaEdited}
treemaOptions.readOnly = true unless me.isAdmin()
@configSchemaTreema = @$el.find('#config-schema-treema').treema treemaOptions
@configSchemaTreema.build()
@configSchemaTreema.open()
# TODO: schema is not loaded for the first one here?
@configSchemaTreema.tv4.addSchema('metaschema', LevelComponent.schema.get('properties').configSchema)
onConfigSchemaEdited: =>
@levelComponent.set 'configSchema', @configSchemaTreema.data
Backbone.Mediator.publish 'level-component-edited', levelComponent: @levelComponent
buildCodeEditor: ->
editorEl = @$el.find '#component-code-editor'
editorEl.text @levelComponent.get('code')
@editor = ace.edit(editorEl[0])
@editor.setReadOnly(not me.isAdmin())
session = @editor.getSession()
session.setMode 'ace/mode/coffee'
session.setTabSize 2
session.setNewLineMode = 'unix'
session.setUseSoftTabs true
2014-03-24 13:26:32 -04:00
@editor.on('change', @onEditorChange)
2014-01-28 00:06:27 -05:00
2014-03-24 13:26:32 -04:00
onEditorChange: =>
2014-01-28 00:06:27 -05:00
@levelComponent.set 'code', @editor.getValue()
Backbone.Mediator.publish 'level-component-edited', levelComponent: @levelComponent
null
2014-01-03 13:32:13 -05:00
endEditing: (e) ->
Backbone.Mediator.publish 'level-component-editing-ended', levelComponent: @levelComponent
null
destroy: ->
@editor?.destroy()
super()
showVersionHistory: (e) ->
versionHistoryView = new VersionHistoryView component:@levelComponent, @levelComponent.id
@openModalView versionHistoryView
Backbone.Mediator.publish 'level:view-switched', e