codecombat/app/views/play/modal/LeaderboardModal.coffee

59 lines
2.1 KiB
CoffeeScript
Raw Normal View History

2015-01-30 15:27:19 -05:00
ModalView = require 'views/core/ModalView'
template = require 'templates/play/modal/leaderboard-modal'
LeaderboardTabView = require 'views/play/modal/LeaderboardTabView'
2015-01-31 00:36:36 -05:00
Level = require 'models/Level'
utils = require 'core/utils'
2015-01-30 15:27:19 -05:00
module.exports = class LeaderboardModal extends ModalView
id: 'leaderboard-modal'
template: template
instant: true
timespans: ['day', 'week', 'all']
subscriptions: {}
events:
'shown.bs.tab #leaderboard-nav a': 'onTabShown'
'click #close-modal': 'hide'
constructor: (options) ->
super options
2015-01-31 00:36:36 -05:00
@levelSlug = @options.levelSlug
@level = @supermodel.loadModel(new Level({_id: @levelSlug}, {project: ['name', 'i18n', 'scoreType', 'original']}), 'level').model
2015-01-30 15:27:19 -05:00
getRenderData: (c) ->
c = super c
2015-01-31 00:36:36 -05:00
c.submenus = []
for scoreType in @level.get('scoreTypes') ? []
for timespan in @timespans
c.submenus.push scoreType: scoreType, timespan: timespan
c.levelName = utils.i18n @level.attributes, 'name'
2015-01-30 15:27:19 -05:00
c
afterRender: ->
super()
2015-01-31 00:36:36 -05:00
return unless @supermodel.finished()
for scoreType, scoreTypeIndex in @level.get('scoreTypes') ? []
for timespan, timespanIndex in @timespans
submenuView = new LeaderboardTabView scoreType: scoreType, timespan: timespan, level: @level
@insertSubView submenuView, @$el.find "##{scoreType}-#{timespan}-view .leaderboard-tab-view"
if scoreTypeIndex + timespanIndex is 0
submenuView.$el.parent().addClass 'active'
submenuView.onShown?()
2015-01-30 15:27:19 -05:00
@playSound 'game-menu-open'
@$el.find('.nano:visible').nanoScroller()
onTabShown: (e) ->
@playSound 'game-menu-tab-switch'
2015-01-31 00:36:36 -05:00
tabChunks = e.target.hash.substring(1).split '-'
scoreType = tabChunks[0 ... tabChunks.length - 2].join '-'
timespan = tabChunks[tabChunks.length - 2]
subview = _.find @subviews, scoreType: scoreType, timespan: timespan
2015-01-30 15:27:19 -05:00
subview.onShown?()
otherSubview.onHidden?() for subviewKey, otherSubview of @subviews when otherSubview isnt subview
onHidden: ->
super()
subview.onHidden?() for subviewKey, subview of @subviews
@playSound 'game-menu-close'