2014-07-06 14:45:27 -04:00
|
|
|
UserView = require 'views/kinds/UserView'
|
|
|
|
template = require 'templates/user/achievements'
|
|
|
|
{me} = require 'lib/auth'
|
2014-07-06 16:48:33 -04:00
|
|
|
Achievement = require 'models/Achievement'
|
2014-07-06 17:38:57 -04:00
|
|
|
EarnedAchievement = require 'models/EarnedAchievement'
|
2014-07-06 16:48:33 -04:00
|
|
|
AchievementCollection = require 'collections/AchievementCollection'
|
2014-07-06 17:38:57 -04:00
|
|
|
EarnedAchievementCollection = require 'collections/EarnedAchievementCollection'
|
2014-07-06 14:45:27 -04:00
|
|
|
|
2014-07-21 13:49:16 -04:00
|
|
|
module.exports = class AchievementsView extends UserView
|
2014-07-06 14:45:27 -04:00
|
|
|
id: 'user-achievements-view'
|
|
|
|
template: template
|
2014-07-30 16:23:43 -04:00
|
|
|
viewName: 'Stats'
|
|
|
|
activeLayout: 'grid'
|
|
|
|
|
|
|
|
events:
|
|
|
|
'click #grid-layout-button': 'layoutChanged'
|
|
|
|
'click #table-layout-button': 'layoutChanged'
|
2014-07-06 14:45:27 -04:00
|
|
|
|
2014-07-21 13:49:16 -04:00
|
|
|
constructor: (userID, options) ->
|
2014-07-14 06:14:43 -04:00
|
|
|
super options, userID
|
2014-07-06 16:48:33 -04:00
|
|
|
|
2014-07-07 13:56:30 -04:00
|
|
|
onLoaded: ->
|
2014-08-07 16:03:00 -04:00
|
|
|
unless @achievements or @earnedAchievements
|
|
|
|
@supermodel.resetProgress()
|
|
|
|
@achievements = new AchievementCollection
|
|
|
|
@earnedAchievements = new EarnedAchievementCollection @user.getSlugOrID()
|
|
|
|
@supermodel.loadCollection @achievements, 'achievements'
|
|
|
|
@supermodel.loadCollection @earnedAchievements, 'earnedAchievements'
|
|
|
|
else
|
|
|
|
for earned in @earnedAchievements.models
|
|
|
|
return unless relatedAchievement = _.find @achievements.models, (achievement) ->
|
|
|
|
achievement.get('_id') is earned.get 'achievement'
|
|
|
|
relatedAchievement.set 'unlocked', true
|
|
|
|
earned.set 'achievement', relatedAchievement
|
|
|
|
deferredImages = (achievement.cacheLockedImage() for achievement in @achievements.models when not achievement.get 'unlocked')
|
|
|
|
whenever = $.when deferredImages...
|
|
|
|
whenever.done => @render()
|
2014-07-07 13:56:30 -04:00
|
|
|
super()
|
|
|
|
|
2014-07-30 16:23:43 -04:00
|
|
|
layoutChanged: (e) ->
|
|
|
|
@activeLayout = $(e.currentTarget).data 'layout'
|
|
|
|
@render()
|
|
|
|
|
2014-07-07 09:03:28 -04:00
|
|
|
getRenderData: ->
|
|
|
|
context = super()
|
2014-07-30 16:23:43 -04:00
|
|
|
context.activeLayout = @activeLayout
|
|
|
|
|
|
|
|
# After user is loaded
|
2014-07-09 12:12:31 -04:00
|
|
|
if @user and not @user.isAnonymous()
|
2014-08-07 16:03:00 -04:00
|
|
|
context.earnedAchievements = @earnedAchievements
|
2014-07-30 16:23:43 -04:00
|
|
|
context.achievementsByCategory = {}
|
2014-10-17 18:25:31 -04:00
|
|
|
for achievement in @achievements.models when achievement.get('category')
|
2014-07-30 16:23:43 -04:00
|
|
|
context.achievementsByCategory[achievement.get('category')] ?= []
|
|
|
|
context.achievementsByCategory[achievement.get('category')].push achievement
|
2014-07-07 09:03:28 -04:00
|
|
|
context
|