2014-01-03 13:32:13 -05:00
|
|
|
View = require 'views/kinds/CocoView'
|
|
|
|
template = require 'templates/editor/level/system/edit'
|
|
|
|
LevelSystem = require 'models/LevelSystem'
|
2014-04-17 16:24:17 -04:00
|
|
|
VersionHistoryView = require 'views/editor/system/versions_view'
|
|
|
|
PatchesView = require 'views/editor/patches_view'
|
|
|
|
SaveVersionModal = require 'views/modal/save_version_modal'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
module.exports = class LevelSystemEditView extends View
|
2014-06-30 22:16:26 -04:00
|
|
|
id: 'editor-level-system-edit-view'
|
2014-01-03 13:32:13 -05:00
|
|
|
template: template
|
2014-06-10 16:20:14 -04:00
|
|
|
editableSettings: ['name', 'description', 'codeLanguage', 'dependencies', 'propertyDocumentation', 'i18n']
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
events:
|
|
|
|
'click #done-editing-system-button': 'endEditing'
|
2014-01-28 00:25:01 -05:00
|
|
|
'click .nav a': (e) -> $(e.target).tab('show')
|
2014-04-17 16:24:17 -04:00
|
|
|
'click #system-patches-tab': -> @patchesView.load()
|
2014-04-17 17:23:33 -04:00
|
|
|
'click #system-code-tab': 'buildCodeEditor'
|
|
|
|
'click #system-config-schema-tab': 'buildConfigSchemaTreema'
|
|
|
|
'click #system-settings-tab': 'buildSettingsTreema'
|
2014-04-17 16:24:17 -04:00
|
|
|
'click #system-history-button': 'showVersionHistory'
|
|
|
|
'click #patch-system-button': 'startPatchingSystem'
|
|
|
|
'click #system-watch-button': 'toggleWatchSystem'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super options
|
|
|
|
@levelSystem = @supermodel.getModelByOriginalAndMajorVersion LevelSystem, options.original, options.majorVersion or 0
|
2014-06-30 22:16:26 -04:00
|
|
|
console.log 'Couldn\'t get levelSystem for', options, 'from', @supermodel.models unless @levelSystem
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-02-11 17:58:45 -05:00
|
|
|
getRenderData: (context={}) ->
|
2014-01-03 13:32:13 -05:00
|
|
|
context = super(context)
|
2014-01-11 11:25:04 -05:00
|
|
|
context.editTitle = "#{@levelSystem.get('name')}"
|
2014-01-03 13:32:13 -05:00
|
|
|
context
|
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
super()
|
2014-01-28 00:25:01 -05:00
|
|
|
@buildSettingsTreema()
|
|
|
|
@buildConfigSchemaTreema()
|
|
|
|
@buildCodeEditor()
|
2014-04-17 16:24:17 -04:00
|
|
|
@patchesView = @insertSubView(new PatchesView(@levelSystem), @$el.find('.patches-view'))
|
2014-01-28 00:25:01 -05:00
|
|
|
|
|
|
|
buildSettingsTreema: ->
|
|
|
|
data = _.pick @levelSystem.attributes, (value, key) => key in @editableSettings
|
2014-04-12 04:35:56 -04:00
|
|
|
schema = _.cloneDeep LevelSystem.schema
|
2014-01-28 00:25:01 -05:00
|
|
|
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:25:01 -05:00
|
|
|
schema: schema
|
2014-01-03 13:32:13 -05:00
|
|
|
data: data
|
2014-01-28 00:25:01 -05:00
|
|
|
callbacks: {change: @onSystemSettingsEdited}
|
2014-04-24 20:25:16 -04:00
|
|
|
treemaOptions.readOnly = me.get('anonymous')
|
2014-01-28 00:25:01 -05:00
|
|
|
@systemSettingsTreema = @$el.find('#edit-system-treema').treema treemaOptions
|
|
|
|
@systemSettingsTreema.build()
|
|
|
|
@systemSettingsTreema.open()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-01-28 00:25:01 -05:00
|
|
|
onSystemSettingsEdited: =>
|
2014-01-03 13:32:13 -05:00
|
|
|
# Make sure it validates first?
|
2014-01-28 00:25:01 -05:00
|
|
|
for key, value of @systemSettingsTreema.data
|
2014-01-03 13:32:13 -05:00
|
|
|
@levelSystem.set key, value unless key is 'js' # will compile code if needed
|
|
|
|
Backbone.Mediator.publish 'level-system-edited', levelSystem: @levelSystem
|
|
|
|
null
|
|
|
|
|
2014-01-28 00:25:01 -05:00
|
|
|
buildConfigSchemaTreema: ->
|
|
|
|
treemaOptions =
|
|
|
|
supermodel: @supermodel
|
2014-04-12 04:35:56 -04:00
|
|
|
schema: LevelSystem.schema.properties.configSchema
|
2014-01-28 00:25:01 -05:00
|
|
|
data: @levelSystem.get 'configSchema'
|
|
|
|
callbacks: {change: @onConfigSchemaEdited}
|
2014-04-24 20:25:16 -04:00
|
|
|
treemaOptions.readOnly = me.get('anonymous')
|
2014-01-28 00:25:01 -05:00
|
|
|
@configSchemaTreema = @$el.find('#config-schema-treema').treema treemaOptions
|
|
|
|
@configSchemaTreema.build()
|
|
|
|
@configSchemaTreema.open()
|
|
|
|
# TODO: schema is not loaded for the first one here?
|
2014-04-12 04:35:56 -04:00
|
|
|
@configSchemaTreema.tv4.addSchema('metaschema', LevelSystem.schema.properties.configSchema)
|
2014-01-28 00:25:01 -05:00
|
|
|
|
|
|
|
onConfigSchemaEdited: =>
|
|
|
|
@levelSystem.set 'configSchema', @configSchemaTreema.data
|
|
|
|
Backbone.Mediator.publish 'level-system-edited', levelSystem: @levelSystem
|
|
|
|
|
|
|
|
buildCodeEditor: ->
|
2014-04-17 17:23:33 -04:00
|
|
|
@editor?.destroy()
|
|
|
|
editorEl = $('<div></div>').text(@levelSystem.get('code')).addClass('inner-editor')
|
|
|
|
@$el.find('#system-code-editor').empty().append(editorEl)
|
2014-01-28 00:25:01 -05:00
|
|
|
@editor = ace.edit(editorEl[0])
|
2014-04-24 20:25:16 -04:00
|
|
|
@editor.setReadOnly(me.get('anonymous'))
|
2014-01-28 00:25:01 -05:00
|
|
|
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:25:01 -05:00
|
|
|
|
2014-03-24 13:26:32 -04:00
|
|
|
onEditorChange: =>
|
2014-01-28 00:25:01 -05:00
|
|
|
@levelSystem.set 'code', @editor.getValue()
|
|
|
|
Backbone.Mediator.publish 'level-system-edited', levelSystem: @levelSystem
|
|
|
|
null
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
endEditing: (e) ->
|
|
|
|
Backbone.Mediator.publish 'level-system-editing-ended', levelSystem: @levelSystem
|
|
|
|
null
|
2014-01-30 15:19:25 -05:00
|
|
|
|
2014-04-17 16:24:17 -04:00
|
|
|
showVersionHistory: (e) ->
|
|
|
|
versionHistoryView = new VersionHistoryView {}, @levelSystem.id
|
|
|
|
@openModalView versionHistoryView
|
|
|
|
Backbone.Mediator.publish 'level:view-switched', e
|
|
|
|
|
|
|
|
startPatchingSystem: (e) ->
|
2014-06-30 22:16:26 -04:00
|
|
|
@openModalView new SaveVersionModal({model: @levelSystem})
|
2014-04-17 16:24:17 -04:00
|
|
|
Backbone.Mediator.publish 'level:view-switched', e
|
|
|
|
|
|
|
|
toggleWatchSystem: ->
|
|
|
|
console.log 'toggle watch system?'
|
|
|
|
button = @$el.find('#system-watch-button')
|
|
|
|
@levelSystem.watch(button.find('.watch').is(':visible'))
|
|
|
|
button.find('> span').toggleClass('secret')
|
|
|
|
|
2014-01-30 15:19:25 -05:00
|
|
|
destroy: ->
|
|
|
|
@editor?.destroy()
|
2014-02-14 13:57:47 -05:00
|
|
|
super()
|