codecombat/app/views/clans/ClansView.coffee

140 lines
4.9 KiB
CoffeeScript
Raw Normal View History

app = require 'core/application'
2015-04-01 18:24:45 -04:00
AuthModal = require 'views/core/AuthModal'
RootView = require 'views/core/RootView'
template = require 'templates/clans/clans'
CocoCollection = require 'collections/CocoCollection'
Clan = require 'models/Clan'
SubscribeModal = require 'views/core/SubscribeModal'
# TODO: Waiting for async messages
# TODO: Invalid clan name message
# TODO: Refresh data instead of page
module.exports = class MainAdminView extends RootView
id: 'clans-view'
template: template
events:
'click .create-clan-btn': 'onClickCreateClan'
2015-04-01 18:24:45 -04:00
'click .join-clan-btn': 'onJoinClan'
'click .leave-clan-btn': 'onLeaveClan'
constructor: (options) ->
super options
2015-04-03 12:52:25 -04:00
@initData()
2015-04-02 20:00:28 -04:00
destroy: ->
@stopListening?()
getRenderData: ->
context = super()
2015-04-03 12:52:25 -04:00
context.idNameMap = @idNameMap
context.publicClans = _.filter(@publicClans.models, (clan) -> clan.get('type') is 'public')
2015-04-02 20:00:28 -04:00
context.myClans = @myClans.models
context.myClanIDs = me.get('clans') ? []
context
afterRender: ->
super()
@setupPrivateInfoPopover()
2015-04-03 12:52:25 -04:00
initData: ->
@idNameMap = {}
sortClanList = (a, b) ->
if a.get('members').length isnt b.get('members').length
2015-04-03 17:09:43 -04:00
if a.get('members').length < b.get('members').length then 1 else -1
else
b.id.localeCompare(a.id)
@publicClans = new CocoCollection([], { url: '/db/clan/-/public', model: Clan, comparator: sortClanList })
2015-04-03 12:52:25 -04:00
@listenTo @publicClans, 'sync', =>
@refreshNames @publicClans.models
@render?()
@supermodel.loadCollection(@publicClans, 'public_clans', {cache: false})
@myClans = new CocoCollection([], { url: "/db/user/#{me.id}/clans", model: Clan, comparator: sortClanList })
2015-04-03 12:52:25 -04:00
@listenTo @myClans, 'sync', =>
@refreshNames @myClans.models
@render?()
@supermodel.loadCollection(@myClans, 'my_clans', {cache: false})
@listenTo me, 'sync', => @render?()
refreshNames: (clans) ->
clanIDs = _.filter(clans, (clan) -> clan.get('type') is 'public')
clanIDs = _.map(clans, (clan) -> clan.get('ownerID'))
2015-04-03 12:52:25 -04:00
options =
url: '/db/user/-/names'
method: 'POST'
data: {ids: clanIDs}
2015-04-03 12:52:25 -04:00
success: (models, response, options) =>
@idNameMap[userID] = models[userID].name for userID of models
@render?()
@supermodel.addRequestResource('user_names', options, 0).load()
setupPrivateInfoPopover: ->
popoverTitle = 'Private Clans'
popoverContent = "<p>Additional features:"
popoverContent += "<ul>"
popoverContent += "<li>Detailed progress reporting"
popoverContent += "<li>Not visible in Public Clans list below"
popoverContent += "<li>Only students with the invite link will be able to join"
popoverContent += "</ul>"
popoverContent += "<p><img src='/images/loading_image.png'></p>"
popoverContent += "<p>*A CodeCombat subscription is required to create or join private Clans.</p>"
@$el.find('.private-more-info').popover(
animation: true
html: true
placement: 'right'
trigger: 'hover'
title: popoverTitle
content: popoverContent
container: @$el
)
onClickCreateClan: (e) ->
2015-04-01 18:24:45 -04:00
return @openModalView(new AuthModal()) if me.isAnonymous()
clanType = if $('.private-clan-checkbox').prop('checked') then 'private' else 'public'
return @openModalView new SubscribeModal() if clanType is 'private' and not me.isPremium()
if name = $('.create-clan-name').val()
clan = new Clan()
clan.set 'type', clanType
clan.set 'name', name
2015-04-03 17:09:43 -04:00
clan.set 'description', description if description = $('.create-clan-description').val()
clan.save {},
error: (model, response, options) =>
2015-04-01 18:24:45 -04:00
console.error 'Error saving clan', response.status
success: (model, response, options) =>
app.router.navigate "/clans/#{model.id}"
window.location.reload()
else
console.log 'Invalid name'
2015-04-01 18:24:45 -04:00
onJoinClan: (e) ->
return @openModalView(new AuthModal()) if me.isAnonymous()
if clanID = $(e.target).data('id')
options =
url: "/db/clan/#{clanID}/join"
method: 'PUT'
error: (model, response, options) =>
console.error 'Error joining clan', response
success: (model, response, options) =>
app.router.navigate "/clans/#{clanID}"
window.location.reload()
2015-04-01 18:24:45 -04:00
@supermodel.addRequestResource( 'join_clan', options).load()
else
console.error "No clan ID attached to join button."
onLeaveClan: (e) ->
if clanID = $(e.target).data('id')
options =
url: "/db/clan/#{clanID}/leave"
method: 'PUT'
error: (model, response, options) =>
console.error 'Error leaving clan', response
success: (model, response, options) =>
2015-04-02 20:00:28 -04:00
me.fetch cache: false
@publicClans.fetch cache: false
@myClans.fetch cache: false
@supermodel.addRequestResource( 'leave_clan', options).load()
else
console.error "No clan ID attached to leave button."