2015-03-31 16:28:57 -04:00
|
|
|
app = require 'core/application'
|
2015-04-01 18:24:45 -04:00
|
|
|
AuthModal = require 'views/core/AuthModal'
|
2015-03-31 16:28:57 -04:00
|
|
|
RootView = require 'views/core/RootView'
|
|
|
|
template = require 'templates/clans/clans'
|
2015-04-01 14:56:48 -04:00
|
|
|
CocoCollection = require 'collections/CocoCollection'
|
|
|
|
Clan = require 'models/Clan'
|
2015-04-10 17:33:16 -04:00
|
|
|
SubscribeModal = require 'views/core/SubscribeModal'
|
2015-03-31 16:28:57 -04:00
|
|
|
|
2015-04-01 19:00:39 -04:00
|
|
|
# TODO: Waiting for async messages
|
|
|
|
# TODO: Invalid clan name message
|
|
|
|
# TODO: Refresh data instead of page
|
|
|
|
|
2015-06-22 13:15:18 -04:00
|
|
|
module.exports = class ClansView extends RootView
|
2015-03-31 16:28:57 -04:00
|
|
|
id: 'clans-view'
|
|
|
|
template: template
|
|
|
|
|
|
|
|
events:
|
2015-04-01 14:56:48 -04:00
|
|
|
'click .create-clan-btn': 'onClickCreateClan'
|
2015-04-01 18:24:45 -04:00
|
|
|
'click .join-clan-btn': 'onJoinClan'
|
2015-04-01 19:00:39 -04:00
|
|
|
'click .leave-clan-btn': 'onLeaveClan'
|
2015-04-28 12:20:37 -04:00
|
|
|
'click .private-clan-checkbox': 'onClickPrivateCheckbox'
|
2015-03-31 16:28:57 -04:00
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super options
|
2015-04-03 12:52:25 -04:00
|
|
|
@initData()
|
2015-04-02 20:00:28 -04:00
|
|
|
|
|
|
|
destroy: ->
|
|
|
|
@stopListening?()
|
2015-03-31 16:28:57 -04:00
|
|
|
|
|
|
|
getRenderData: ->
|
|
|
|
context = super()
|
2015-04-03 12:52:25 -04:00
|
|
|
context.idNameMap = @idNameMap
|
2015-04-10 17:33:16 -04:00
|
|
|
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') ? []
|
2015-03-31 16:28:57 -04:00
|
|
|
context
|
|
|
|
|
2015-04-10 17:33:16 -04:00
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
@setupPrivateInfoPopover()
|
|
|
|
|
2015-04-03 12:52:25 -04:00
|
|
|
initData: ->
|
|
|
|
@idNameMap = {}
|
2015-04-03 15:22:44 -04:00
|
|
|
|
|
|
|
sortClanList = (a, b) ->
|
2015-11-04 19:00:54 -05:00
|
|
|
if a.get('memberCount') isnt b.get('memberCount')
|
|
|
|
if a.get('memberCount') < b.get('memberCount') then 1 else -1
|
2015-04-03 15:22:44 -04:00
|
|
|
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})
|
2015-04-06 14:17:13 -04:00
|
|
|
@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) ->
|
2015-04-10 17:33:16 -04:00
|
|
|
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'
|
2015-04-10 17:33:16 -04:00
|
|
|
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()
|
|
|
|
|
2015-04-10 17:33:16 -04:00
|
|
|
setupPrivateInfoPopover: ->
|
2015-10-23 11:54:42 -04:00
|
|
|
popoverTitle = "<h3>" + $.i18n.t('teachers.sub_includes_7') + "</h3>"
|
2015-07-02 21:44:13 -04:00
|
|
|
popoverContent = "<ul>"
|
2015-10-23 11:54:42 -04:00
|
|
|
popoverContent += "<li><span style='font-weight:bold;'>" + $.i18n.t('clans.track_concepts1') + "</span> " + $.i18n.t('clans.track_concepts2b')
|
|
|
|
popoverContent += "<li>" + $.i18n.t('clans.track_concepts3b')
|
|
|
|
popoverContent += "<li>" + $.i18n.t('clans.track_concepts4b') + " <span style='font-weight:bold;'>" + $.i18n.t('clans.track_concepts5') + "</span>"
|
|
|
|
popoverContent += "<li>" + $.i18n.t('clans.track_concepts6b')
|
|
|
|
popoverContent += "<li><span style='font-weight:bold;'>" + $.i18n.t('clans.track_concepts7') + "</span> " + $.i18n.t('clans.track_concepts8')
|
2015-07-02 21:44:13 -04:00
|
|
|
popoverContent += "</ul>"
|
|
|
|
popoverContent += "<p><img src='/images/pages/clans/dashboard_preview.png' height='400'></p>"
|
2015-10-23 11:54:42 -04:00
|
|
|
popoverContent += "<p>" + $.i18n.t('clans.private_require_sub') + "</p>"
|
2015-04-10 17:33:16 -04:00
|
|
|
@$el.find('.private-more-info').popover(
|
|
|
|
animation: true
|
|
|
|
html: true
|
|
|
|
placement: 'right'
|
|
|
|
trigger: 'hover'
|
|
|
|
title: popoverTitle
|
|
|
|
content: popoverContent
|
|
|
|
container: @$el
|
|
|
|
)
|
|
|
|
|
2015-04-01 14:56:48 -04:00
|
|
|
onClickCreateClan: (e) ->
|
2015-04-20 17:16:44 -04:00
|
|
|
return @openModalView new AuthModal() if me.isAnonymous()
|
2015-04-10 17:33:16 -04:00
|
|
|
clanType = if $('.private-clan-checkbox').prop('checked') then 'private' else 'public'
|
2015-04-20 17:16:44 -04:00
|
|
|
if clanType is 'private' and not me.isPremium()
|
|
|
|
@openModalView new SubscribeModal()
|
|
|
|
window.tracker?.trackEvent 'Show subscription modal', category: 'Subscription', label: 'create clan'
|
|
|
|
return
|
2015-04-01 14:56:48 -04:00
|
|
|
if name = $('.create-clan-name').val()
|
|
|
|
clan = new Clan()
|
2015-04-10 17:33:16 -04:00
|
|
|
clan.set 'type', clanType
|
2015-04-01 14:56:48 -04:00
|
|
|
clan.set 'name', name
|
2015-04-03 17:09:43 -04:00
|
|
|
clan.set 'description', description if description = $('.create-clan-description').val()
|
2015-04-01 14:56:48 -04:00
|
|
|
clan.save {},
|
|
|
|
error: (model, response, options) =>
|
2015-04-01 18:24:45 -04:00
|
|
|
console.error 'Error saving clan', response.status
|
2015-04-01 14:56:48 -04:00
|
|
|
success: (model, response, options) =>
|
|
|
|
app.router.navigate "/clans/#{model.id}"
|
|
|
|
window.location.reload()
|
2015-03-31 16:28:57 -04:00
|
|
|
else
|
2015-04-01 14:56:48 -04:00
|
|
|
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) =>
|
2015-04-01 19:00:39 -04:00
|
|
|
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."
|
2015-04-01 19:00:39 -04:00
|
|
|
|
|
|
|
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
|
2015-04-01 19:00:39 -04:00
|
|
|
@supermodel.addRequestResource( 'leave_clan', options).load()
|
|
|
|
else
|
|
|
|
console.error "No clan ID attached to leave button."
|
2015-04-28 12:20:37 -04:00
|
|
|
|
|
|
|
onClickPrivateCheckbox: (e) ->
|
|
|
|
return @openModalView new AuthModal() if me.isAnonymous()
|
|
|
|
if $('.private-clan-checkbox').prop('checked') and not me.isPremium()
|
|
|
|
$('.private-clan-checkbox').attr('checked', false)
|
|
|
|
@openModalView new SubscribeModal()
|
|
|
|
window.tracker?.trackEvent 'Show subscription modal', category: 'Subscription', label: 'check private clan'
|