2014-01-03 13:32:13 -05:00
|
|
|
View = require 'views/kinds/ModalView'
|
|
|
|
template = require 'templates/editor/level/system/add'
|
|
|
|
availableSystemTemplate = require 'templates/editor/level/system/available_system'
|
|
|
|
LevelSystem = require 'models/LevelSystem'
|
2014-04-22 15:42:26 -04:00
|
|
|
CocoCollection = require 'collections/CocoCollection'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
class LevelSystemSearchCollection extends CocoCollection
|
2014-06-09 10:18:26 -04:00
|
|
|
url: '/db/level_system'
|
2014-01-03 13:32:13 -05:00
|
|
|
model: LevelSystem
|
|
|
|
|
|
|
|
module.exports = class LevelSystemAddView extends View
|
2014-06-30 22:16:26 -04:00
|
|
|
id: 'editor-level-system-add-modal'
|
2014-01-03 13:32:13 -05:00
|
|
|
template: template
|
|
|
|
instant: true
|
|
|
|
|
|
|
|
events:
|
|
|
|
'click .available-systems-list li': 'onAddSystem'
|
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super options
|
|
|
|
@extantSystems = options.extantSystems ? []
|
|
|
|
|
|
|
|
render: ->
|
|
|
|
if not @systems
|
|
|
|
@systems = @supermodel.getCollection new LevelSystemSearchCollection()
|
|
|
|
unless @systems.loaded
|
2014-03-24 12:58:34 -04:00
|
|
|
@listenToOnce(@systems, 'sync', @onSystemsSync)
|
2014-01-03 13:32:13 -05:00
|
|
|
@systems.fetch()
|
|
|
|
super() # do afterRender at the end
|
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
return @showLoading() unless @systems?.loaded
|
|
|
|
@hideLoading()
|
|
|
|
@renderAvailableSystems()
|
|
|
|
|
|
|
|
renderAvailableSystems: ->
|
|
|
|
return unless @systems
|
|
|
|
ul = @$el.find('ul.available-systems-list').empty()
|
|
|
|
systems = (m.attributes for m in @systems.models)
|
|
|
|
_.remove systems, (system) =>
|
|
|
|
_.find @extantSystems, {original: system.original} # already have this one added
|
|
|
|
systems = _.sortBy systems, 'name'
|
|
|
|
for system in systems
|
|
|
|
ul.append $(availableSystemTemplate(system: system))
|
|
|
|
|
2014-03-24 12:58:34 -04:00
|
|
|
onSystemsSync: ->
|
2014-01-03 13:32:13 -05:00
|
|
|
@supermodel.addCollection @systems
|
|
|
|
@render()
|
|
|
|
|
|
|
|
onAddSystem: (e) ->
|
|
|
|
id = $(e.currentTarget).data('system-id')
|
|
|
|
system = _.find @systems.models, id: id
|
|
|
|
unless system
|
2014-06-30 22:16:26 -04:00
|
|
|
return console.error 'Couldn\'t find system for id', id, 'out of', @systems.models
|
2014-01-03 13:32:13 -05:00
|
|
|
# Add all dependencies, recursively, unless we already have them
|
|
|
|
toAdd = system.getDependencies(@systems.models)
|
|
|
|
_.remove toAdd, (s1) =>
|
|
|
|
_.find @extantSystems, original: s1.get('original')
|
|
|
|
for s in toAdd.concat [system]
|
|
|
|
levelSystem =
|
|
|
|
original: s.get('original') ? id
|
|
|
|
majorVersion: s.get('version').major ? 0
|
2014-03-18 16:08:26 -04:00
|
|
|
config: $.extend(true, {}, s.get('configSchema').default ? {})
|
2014-01-03 13:32:13 -05:00
|
|
|
@extantSystems.push levelSystem
|
|
|
|
Backbone.Mediator.publish 'level-system-added', system: levelSystem
|
|
|
|
@renderAvailableSystems()
|