2014-07-17 20:20:11 -04:00
|
|
|
CocoView = require 'views/kinds/CocoView'
|
2014-09-02 20:26:02 -04:00
|
|
|
template = require 'templates/editor/level/component/level-component-edit-view'
|
2014-01-03 13:32:13 -05:00
|
|
|
LevelComponent = require 'models/LevelComponent'
|
2014-07-23 10:02:45 -04:00
|
|
|
ComponentVersionsModal = require 'views/editor/component/ComponentVersionsModal'
|
|
|
|
PatchesView = require 'views/editor/PatchesView'
|
|
|
|
SaveVersionModal = require 'views/modal/SaveVersionModal'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-07-17 20:20:11 -04:00
|
|
|
module.exports = class LevelComponentEditView extends CocoView
|
2014-09-02 20:26:02 -04:00
|
|
|
id: 'level-component-edit-view'
|
2014-01-03 13:32:13 -05:00
|
|
|
template: template
|
2014-06-10 16:20:14 -04:00
|
|
|
editableSettings: ['name', 'description', 'system', 'codeLanguage', 'dependencies', 'propertyDocumentation', 'i18n']
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
events:
|
|
|
|
'click #done-editing-component-button': 'endEditing'
|
2014-01-28 00:06:27 -05:00
|
|
|
'click .nav a': (e) -> $(e.target).tab('show')
|
2014-04-16 13:42:32 -04:00
|
|
|
'click #component-patches-tab': -> @patchesView.load()
|
2014-04-17 17:23:33 -04:00
|
|
|
'click #component-code-tab': 'buildCodeEditor'
|
|
|
|
'click #component-config-schema-tab': 'buildConfigSchemaTreema'
|
|
|
|
'click #component-settings-tab': 'buildSettingsTreema'
|
2014-04-17 16:24:17 -04:00
|
|
|
'click #component-history-button': 'showVersionHistory'
|
2014-04-16 13:42:32 -04:00
|
|
|
'click #patch-component-button': 'startPatchingComponent'
|
2014-04-17 14:35:09 -04:00
|
|
|
'click #component-watch-button': 'toggleWatchComponent'
|
2014-10-28 17:21:22 -04:00
|
|
|
'click #pop-component-i18n-button': 'onPopulateI18N'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super options
|
|
|
|
@levelComponent = @supermodel.getModelByOriginalAndMajorVersion LevelComponent, options.original, options.majorVersion or 0
|
2014-06-30 22:16:26 -04:00
|
|
|
console.log 'Couldn\'t get levelComponent for', options, 'from', @supermodel.models unless @levelComponent
|
2014-06-22 01:31:10 -04:00
|
|
|
@onEditorChange = _.debounce @onEditorChange, 1000
|
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 = "#{@levelComponent.get('system')}.#{@levelComponent.get('name')}"
|
2014-04-16 13:42:32 -04:00
|
|
|
context.component = @levelComponent
|
2014-01-03 13:32:13 -05:00
|
|
|
context
|
|
|
|
|
2014-04-17 19:37:31 -04:00
|
|
|
onLoaded: -> @render()
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
afterRender: ->
|
|
|
|
super()
|
2014-01-28 00:06:27 -05:00
|
|
|
@buildSettingsTreema()
|
|
|
|
@buildConfigSchemaTreema()
|
|
|
|
@buildCodeEditor()
|
2014-04-16 13:42:32 -04:00
|
|
|
@patchesView = @insertSubView(new PatchesView(@levelComponent), @$el.find('.patches-view'))
|
2014-04-17 14:35:09 -04:00
|
|
|
@$el.find('#component-watch-button').find('> span').toggleClass('secret') if @levelComponent.watching()
|
2014-08-31 00:16:01 -04:00
|
|
|
@updatePatchButton()
|
2014-01-28 00:06:27 -05:00
|
|
|
|
|
|
|
buildSettingsTreema: ->
|
|
|
|
data = _.pick @levelComponent.attributes, (value, key) => key in @editableSettings
|
2014-07-04 23:45:42 -04:00
|
|
|
data = $.extend(true, {}, data)
|
2014-04-12 04:35:56 -04:00
|
|
|
schema = _.cloneDeep LevelComponent.schema
|
2014-01-28 00:06:27 -05:00
|
|
|
schema.properties = _.pick schema.properties, (value, key) => key in @editableSettings
|
|
|
|
schema.required = _.intersection schema.required, @editableSettings
|
2014-08-26 18:40:51 -04:00
|
|
|
schema.default = _.pick schema.default, (value, key) => key in @editableSettings
|
2014-05-06 19:58:08 -04:00
|
|
|
|
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-05-06 19:58:08 -04:00
|
|
|
readonly: me.get('anonymous')
|
2014-01-28 00:06:27 -05:00
|
|
|
callbacks: {change: @onComponentSettingsEdited}
|
|
|
|
@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
|
2014-08-31 00:16:01 -04:00
|
|
|
@updatePatchButton()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-01-28 00:06:27 -05:00
|
|
|
buildConfigSchemaTreema: ->
|
2014-09-02 20:26:02 -04:00
|
|
|
configSchema = $.extend true, {}, @levelComponent.get 'configSchema'
|
2014-08-29 21:06:45 -04:00
|
|
|
if configSchema.properties
|
|
|
|
# Alphabetize (#1297)
|
|
|
|
propertyNames = _.keys configSchema.properties
|
|
|
|
propertyNames.sort()
|
|
|
|
orderedProperties = {}
|
|
|
|
for prop in propertyNames
|
|
|
|
orderedProperties[prop] = configSchema.properties[prop]
|
|
|
|
configSchema.properties = orderedProperties
|
2014-01-28 00:06:27 -05:00
|
|
|
treemaOptions =
|
|
|
|
supermodel: @supermodel
|
2014-04-12 04:35:56 -04:00
|
|
|
schema: LevelComponent.schema.properties.configSchema
|
2014-08-29 21:06:45 -04:00
|
|
|
data: configSchema
|
2014-05-08 16:40:42 -04:00
|
|
|
readOnly: me.get('anonymous')
|
2014-01-28 00:06:27 -05:00
|
|
|
callbacks: {change: @onConfigSchemaEdited}
|
|
|
|
@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', LevelComponent.schema.properties.configSchema)
|
2014-01-28 00:06:27 -05:00
|
|
|
|
|
|
|
onConfigSchemaEdited: =>
|
|
|
|
@levelComponent.set 'configSchema', @configSchemaTreema.data
|
2014-08-31 00:16:01 -04:00
|
|
|
@updatePatchButton()
|
2014-01-28 00:06:27 -05:00
|
|
|
|
|
|
|
buildCodeEditor: ->
|
2014-08-29 21:02:29 -04:00
|
|
|
@destroyAceEditor(@editor)
|
2014-04-17 17:23:33 -04:00
|
|
|
editorEl = $('<div></div>').text(@levelComponent.get('code')).addClass('inner-editor')
|
|
|
|
@$el.find('#component-code-editor').empty().append(editorEl)
|
2014-01-28 00:06:27 -05:00
|
|
|
@editor = ace.edit(editorEl[0])
|
2014-04-28 01:58:26 -04:00
|
|
|
@editor.setReadOnly(me.get('anonymous'))
|
2014-01-28 00:06:27 -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-05-06 19:58:08 -04:00
|
|
|
|
2014-03-24 13:26:32 -04:00
|
|
|
onEditorChange: =>
|
2014-06-18 01:17:44 -04:00
|
|
|
return if @destroyed
|
2014-01-28 00:06:27 -05:00
|
|
|
@levelComponent.set 'code', @editor.getValue()
|
2014-08-31 00:16:01 -04:00
|
|
|
@updatePatchButton()
|
|
|
|
|
|
|
|
updatePatchButton: ->
|
|
|
|
@$el.find('#patch-component-button').toggle Boolean @levelComponent.hasLocalChanges()
|
2014-01-28 00:06:27 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
endEditing: (e) ->
|
2014-08-27 15:24:03 -04:00
|
|
|
Backbone.Mediator.publish 'editor:level-component-editing-ended', component: @levelComponent
|
2014-01-03 13:32:13 -05:00
|
|
|
null
|
2014-01-30 15:19:25 -05:00
|
|
|
|
2014-03-12 10:32:55 -04:00
|
|
|
showVersionHistory: (e) ->
|
2014-07-23 10:02:45 -04:00
|
|
|
componentVersionsModal = new ComponentVersionsModal {}, @levelComponent.id
|
|
|
|
@openModalView componentVersionsModal
|
2014-08-27 15:24:03 -04:00
|
|
|
Backbone.Mediator.publish 'editor:view-switched', {}
|
2014-05-06 19:58:08 -04:00
|
|
|
|
2014-04-16 13:42:32 -04:00
|
|
|
startPatchingComponent: (e) ->
|
2014-06-30 22:16:26 -04:00
|
|
|
@openModalView new SaveVersionModal({model: @levelComponent})
|
2014-08-27 15:24:03 -04:00
|
|
|
Backbone.Mediator.publish 'editor:view-switched', {}
|
2014-04-16 14:02:40 -04:00
|
|
|
|
|
|
|
toggleWatchComponent: ->
|
2014-04-17 14:35:09 -04:00
|
|
|
button = @$el.find('#component-watch-button')
|
2014-04-16 14:02:40 -04:00
|
|
|
@levelComponent.watch(button.find('.watch').is(':visible'))
|
|
|
|
button.find('> span').toggleClass('secret')
|
|
|
|
|
2014-10-28 17:21:22 -04:00
|
|
|
onPopulateI18N: ->
|
|
|
|
@levelComponent.populateI18N()
|
|
|
|
@render()
|
|
|
|
|
2014-01-30 15:19:25 -05:00
|
|
|
destroy: ->
|
2014-08-29 21:02:29 -04:00
|
|
|
@destroyAceEditor(@editor)
|
|
|
|
@componentSettingsTreema?.destroy()
|
|
|
|
@configSchemaTreema?.destroy()
|
2014-02-14 13:57:47 -05:00
|
|
|
super()
|