2014-11-28 20:49:41 -05:00
|
|
|
UserView = require 'views/common/UserView'
|
2014-07-29 10:50:07 -04:00
|
|
|
CocoCollection = require 'collections/CocoCollection'
|
|
|
|
LevelSession = require 'models/LevelSession'
|
2014-11-25 13:19:38 -05:00
|
|
|
template = require 'templates/user/main-user-view'
|
2014-11-28 20:49:41 -05:00
|
|
|
{me} = require 'core/auth'
|
2015-04-06 14:17:13 -04:00
|
|
|
Clan = require 'models/Clan'
|
2014-08-07 16:03:00 -04:00
|
|
|
EarnedAchievementCollection = require 'collections/EarnedAchievementCollection'
|
2014-07-15 10:15:21 -04:00
|
|
|
|
2014-07-29 10:50:07 -04:00
|
|
|
class LevelSessionsCollection extends CocoCollection
|
|
|
|
model: LevelSession
|
|
|
|
|
|
|
|
constructor: (userID) ->
|
2014-09-24 02:57:54 -04:00
|
|
|
@url = "/db/user/#{userID}/level.sessions?project=state.complete,levelID,levelName,changed,team,codeLanguage,submittedCodeLanguage,totalScore&order=-1"
|
2014-07-29 10:50:07 -04:00
|
|
|
super()
|
|
|
|
|
2014-07-21 13:49:16 -04:00
|
|
|
module.exports = class MainUserView extends UserView
|
2014-08-12 07:41:14 -04:00
|
|
|
id: 'user-home'
|
2014-07-15 10:15:21 -04:00
|
|
|
template: template
|
2015-02-11 16:12:42 -05:00
|
|
|
|
2014-11-25 13:19:38 -05:00
|
|
|
events:
|
|
|
|
'click .more-button': 'onClickMoreButton'
|
2014-07-15 10:15:21 -04:00
|
|
|
|
2014-07-21 13:49:16 -04:00
|
|
|
constructor: (userID, options) ->
|
2014-07-15 10:15:21 -04:00
|
|
|
super options
|
|
|
|
|
2015-04-06 14:17:13 -04:00
|
|
|
destroy: ->
|
|
|
|
@stopListening?()
|
|
|
|
|
2014-07-29 10:50:07 -04:00
|
|
|
onLoaded: ->
|
2016-05-26 06:19:02 -04:00
|
|
|
if @user.loaded
|
|
|
|
if !@levelSessions
|
|
|
|
@levelSessions = new LevelSessionsCollection @user.getSlugOrID()
|
|
|
|
@listenTo @levelSessions, 'sync', =>
|
|
|
|
@onSyncLevelSessions @levelSessions?.models
|
|
|
|
@render()
|
|
|
|
@supermodel.loadCollection @levelSessions, 'levelSessions', {cache: false}
|
|
|
|
|
|
|
|
if !@earnedAchievements
|
|
|
|
@earnedAchievements = new EarnedAchievementCollection @user.getSlugOrID()
|
|
|
|
@listenTo @earnedAchievements, 'sync', =>
|
|
|
|
@render()
|
|
|
|
@supermodel.loadCollection @earnedAchievements, 'earnedAchievements', {cache: false}
|
|
|
|
|
2015-04-06 14:17:13 -04:00
|
|
|
sortClanList = (a, b) ->
|
|
|
|
if a.get('members').length isnt b.get('members').length
|
|
|
|
if a.get('members').length < b.get('members').length then 1 else -1
|
|
|
|
else
|
|
|
|
b.id.localeCompare(a.id)
|
2016-05-26 06:19:02 -04:00
|
|
|
|
2015-04-06 14:17:13 -04:00
|
|
|
@clans = new CocoCollection([], { url: "/db/user/#{@userID}/clans", model: Clan, comparator: sortClanList })
|
|
|
|
@listenTo @clans, 'sync', =>
|
2016-05-26 06:19:02 -04:00
|
|
|
@onSyncClans @clans?.models
|
2015-04-06 14:17:13 -04:00
|
|
|
@render?()
|
|
|
|
@supermodel.loadCollection(@clans, 'clans', {cache: false})
|
2016-05-26 06:19:02 -04:00
|
|
|
|
2014-07-29 10:50:07 -04:00
|
|
|
super()
|
2014-11-25 13:19:38 -05:00
|
|
|
|
2016-05-26 06:19:02 -04:00
|
|
|
onSyncClans: (clans) ->
|
2015-04-06 14:17:13 -04:00
|
|
|
return unless clans?
|
2016-05-26 06:19:02 -04:00
|
|
|
@idNameMap = []
|
|
|
|
@clanModels = clans
|
2015-04-06 14:17:13 -04:00
|
|
|
options =
|
|
|
|
url: '/db/user/-/names'
|
|
|
|
method: 'POST'
|
|
|
|
data: {ids: _.map(clans, (clan) -> clan.get('ownerID'))}
|
|
|
|
success: (models, response, options) =>
|
|
|
|
@idNameMap[userID] = models[userID].name for userID of models
|
|
|
|
@render?()
|
|
|
|
@supermodel.addRequestResource('user_names', options, 0).load()
|
|
|
|
|
2016-05-26 06:19:02 -04:00
|
|
|
onSyncLevelSessions: (levelSessions) ->
|
|
|
|
return unless levelSessions?
|
|
|
|
@multiPlayerSessions = []
|
|
|
|
@singlePlayerSessions = []
|
|
|
|
languageCounts = []
|
|
|
|
mostUsedCount = 0
|
|
|
|
for levelSession in levelSessions
|
|
|
|
if levelSession.isMultiplayer()
|
|
|
|
@multiPlayerSessions.push levelSession
|
|
|
|
else
|
|
|
|
@singlePlayerSessions.push levelSession
|
|
|
|
language = levelSession.get('codeLanguage') or levelSession.get('submittedCodeLanguage')
|
|
|
|
if language
|
|
|
|
languageCounts[language] = (languageCounts[language] or 0) + 1
|
|
|
|
for language, count of languageCounts
|
|
|
|
if count > mostUsedCount
|
|
|
|
mostUsedCount = count
|
|
|
|
@favoriteLanguage = language
|
|
|
|
|
2014-11-25 13:19:38 -05:00
|
|
|
onClickMoreButton: (e) ->
|
|
|
|
panel = $(e.target).closest('.panel')
|
|
|
|
panel.find('tr.hide').removeClass('hide')
|
2015-02-11 16:12:42 -05:00
|
|
|
panel.find('.panel-footer').remove()
|