2014-01-03 13:32:13 -05:00
|
|
|
View = require 'views/kinds/RootView'
|
|
|
|
template = require 'templates/editor/level/edit'
|
|
|
|
Level = require 'models/Level'
|
|
|
|
LevelSystem = require 'models/LevelSystem'
|
|
|
|
World = require 'lib/world/world'
|
|
|
|
DocumentFiles = require 'collections/DocumentFiles'
|
|
|
|
|
|
|
|
ThangsTabView = require './thangs_tab_view'
|
|
|
|
SettingsTabView = require './settings_tab_view'
|
|
|
|
ScriptsTabView = require './scripts_tab_view'
|
|
|
|
ComponentsTabView = require './components_tab_view'
|
|
|
|
SystemsTabView = require './systems_tab_view'
|
|
|
|
LevelSaveView = require './save_view'
|
|
|
|
LevelForkView = require './fork_view'
|
2014-04-12 11:48:49 -04:00
|
|
|
SaveVersionModal = require 'views/modal/save_version_modal'
|
|
|
|
PatchesView = require 'views/editor/patches_view'
|
2014-03-10 07:16:08 -04:00
|
|
|
VersionHistoryView = require './versions_view'
|
2014-03-27 14:35:20 -04:00
|
|
|
ErrorView = require '../../error_view'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
module.exports = class EditorLevelView extends View
|
|
|
|
id: "editor-level-view"
|
|
|
|
template: template
|
|
|
|
cache: false
|
|
|
|
|
|
|
|
events:
|
|
|
|
'click #play-button': 'onPlayLevel'
|
|
|
|
'click #commit-level-start-button': 'startCommittingLevel'
|
|
|
|
'click #fork-level-start-button': 'startForkingLevel'
|
2014-04-17 14:29:12 -04:00
|
|
|
'click #level-history-button': 'showVersionHistory'
|
2014-04-12 11:48:49 -04:00
|
|
|
'click #patches-tab': -> @patchesView.load()
|
2014-04-17 16:37:08 -04:00
|
|
|
'click #level-patch-button': 'startPatchingLevel'
|
2014-04-17 14:35:09 -04:00
|
|
|
'click #level-watch-button': 'toggleWatchLevel'
|
2014-04-17 17:23:33 -04:00
|
|
|
|
|
|
|
subscriptions:
|
|
|
|
'refresh-level-editor': 'rerenderAllViews'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-04-17 17:23:33 -04:00
|
|
|
rerenderAllViews: ->
|
|
|
|
for view in [@thangsTab, @settingsTab, @scriptsTab, @componentsTab, @systemsTab, @patchesView]
|
|
|
|
view.render()
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
constructor: (options, @levelID) ->
|
|
|
|
super options
|
2014-03-24 12:58:34 -04:00
|
|
|
@listenToOnce(@supermodel, 'loaded-all', @onAllLoaded)
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
# load only the level itself and the one it points to, but no others
|
|
|
|
# TODO: this is duplicated in views/play/level_view.coffee; need cleaner method
|
|
|
|
@supermodel.shouldPopulate = (model) ->
|
|
|
|
@levelsLoaded ?= 0
|
|
|
|
@levelsLoaded += 1 if model.constructor.className is "Level"
|
|
|
|
return false if @levelsLoaded > 1
|
|
|
|
return true
|
|
|
|
|
2014-01-26 17:46:25 -05:00
|
|
|
@supermodel.shouldSaveBackups = (model) ->
|
|
|
|
model.constructor.className in ['Level', 'LevelComponent', 'LevelSystem']
|
|
|
|
|
2014-04-16 02:28:59 -04:00
|
|
|
@worldRes = @supermodel.addSomethingResource('world')
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
@level = new Level _id: @levelID
|
2014-04-16 02:28:59 -04:00
|
|
|
#@listenToOnce(@level, 'sync', @onLevelLoaded)
|
|
|
|
@listenToOnce(@supermodel, 'error', =>
|
2014-04-16 11:03:41 -04:00
|
|
|
@hideLoading()
|
|
|
|
@insertSubView(new ErrorView())
|
2014-03-27 14:35:20 -04:00
|
|
|
)
|
2014-04-13 13:29:43 -04:00
|
|
|
|
2014-04-16 02:28:59 -04:00
|
|
|
@levelRes = @supermodel.addModelResource(@level, 'level')
|
|
|
|
@listenToOnce(@levelRes, 'resource:loaded', ->
|
|
|
|
@world = new World @level.name
|
|
|
|
@worldRes.markLoaded()
|
|
|
|
)
|
|
|
|
@levelRes.load()
|
|
|
|
|
|
|
|
@files = new DocumentFiles(@level)
|
|
|
|
@supermodel.addModelResource(@files, 'level_document').load()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
showLoading: ($el) ->
|
|
|
|
$el ?= @$el.find('.tab-content')
|
|
|
|
super($el)
|
|
|
|
|
2014-02-11 17:58:45 -05:00
|
|
|
getRenderData: (context={}) ->
|
2014-01-03 13:32:13 -05:00
|
|
|
context = super(context)
|
|
|
|
context.level = @level
|
2014-03-03 14:41:35 -05:00
|
|
|
context.authorized = me.isAdmin() or @level.hasWriteAccess(me)
|
2014-03-04 05:24:38 -05:00
|
|
|
context.anonymous = me.get('anonymous')
|
2014-01-03 13:32:13 -05:00
|
|
|
context
|
|
|
|
|
2014-04-17 19:37:31 -04:00
|
|
|
onLoaded: -> @render()
|
2014-01-03 13:32:13 -05:00
|
|
|
afterRender: ->
|
|
|
|
super()
|
2014-04-16 02:28:59 -04:00
|
|
|
return unless @world and @level
|
|
|
|
console.debug 'gintau', 'edit-afterRender'
|
2014-01-30 19:36:36 -05:00
|
|
|
@$el.find('a[data-toggle="tab"]').on 'shown.bs.tab', (e) =>
|
2014-01-03 13:32:13 -05:00
|
|
|
Backbone.Mediator.publish 'level:view-switched', e
|
|
|
|
@thangsTab = @insertSubView new ThangsTabView world: @world, supermodel: @supermodel
|
|
|
|
@settingsTab = @insertSubView new SettingsTabView world: @world, supermodel: @supermodel
|
|
|
|
@scriptsTab = @insertSubView new ScriptsTabView world: @world, supermodel: @supermodel, files: @files
|
|
|
|
@componentsTab = @insertSubView new ComponentsTabView supermodel: @supermodel
|
|
|
|
@systemsTab = @insertSubView new SystemsTabView supermodel: @supermodel
|
|
|
|
Backbone.Mediator.publish 'level-loaded', level: @level
|
2014-04-12 11:48:49 -04:00
|
|
|
@showReadOnly() if me.get('anonymous')
|
|
|
|
@patchesView = @insertSubView(new PatchesView(@level), @$el.find('.patches-view'))
|
2014-04-17 18:44:19 -04:00
|
|
|
@listenTo @patchesView, 'accepted-patch', -> setTimeout "location.reload()", 400
|
2014-04-17 14:35:09 -04:00
|
|
|
@$el.find('#level-watch-button').find('> span').toggleClass('secret') if @level.watching()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
onPlayLevel: (e) ->
|
|
|
|
sendLevel = =>
|
|
|
|
@childWindow.Backbone.Mediator.publish 'level-reload-from-data', level: @level, supermodel: @supermodel
|
|
|
|
if @childWindow and not @childWindow.closed
|
|
|
|
# Reset the LevelView's world, but leave the rest of the state alone
|
|
|
|
sendLevel()
|
|
|
|
else
|
|
|
|
# Create a new Window with a blank LevelView
|
|
|
|
scratchLevelID = @level.get('slug') + "?dev=true"
|
|
|
|
@childWindow = window.open("/play/level/#{scratchLevelID}", 'child_window', 'width=1024,height=560,left=10,top=10,location=0,menubar=0,scrollbars=0,status=0,titlebar=0,toolbar=0', true)
|
|
|
|
@childWindow.onPlayLevelViewLoaded = (e) => sendLevel() # still a hack
|
|
|
|
@childWindow.focus()
|
|
|
|
|
2014-04-12 11:48:49 -04:00
|
|
|
startPatchingLevel: (e) ->
|
|
|
|
@openModalView new SaveVersionModal({model:@level})
|
|
|
|
Backbone.Mediator.publish 'level:view-switched', e
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
startCommittingLevel: (e) ->
|
2014-04-12 11:48:49 -04:00
|
|
|
@openModalView new LevelSaveView level: @level, supermodel: @supermodel
|
2014-01-03 13:32:13 -05:00
|
|
|
Backbone.Mediator.publish 'level:view-switched', e
|
|
|
|
|
|
|
|
startForkingLevel: (e) ->
|
|
|
|
levelForkView = new LevelForkView level: @level
|
|
|
|
@openModalView levelForkView
|
|
|
|
Backbone.Mediator.publish 'level:view-switched', e
|
2014-03-10 07:16:08 -04:00
|
|
|
|
|
|
|
showVersionHistory: (e) ->
|
|
|
|
versionHistoryView = new VersionHistoryView level:@level, @levelID
|
|
|
|
@openModalView versionHistoryView
|
2014-04-01 16:15:30 -04:00
|
|
|
Backbone.Mediator.publish 'level:view-switched', e
|
2014-04-15 18:09:36 -04:00
|
|
|
|
|
|
|
toggleWatchLevel: ->
|
2014-04-17 14:35:09 -04:00
|
|
|
button = @$el.find('#level-watch-button')
|
2014-04-15 18:09:36 -04:00
|
|
|
@level.watch(button.find('.watch').is(':visible'))
|
2014-04-15 18:18:57 -04:00
|
|
|
button.find('> span').toggleClass('secret')
|