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-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-03-31 16:28:57 -04:00
|
|
|
module.exports = class MainAdminView extends RootView
|
|
|
|
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-03-31 16:28:57 -04:00
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super options
|
2015-04-01 14:56:48 -04:00
|
|
|
@publicClans = new CocoCollection([], { url: '/db/clan', model: Clan, comparator:'_id' })
|
|
|
|
@supermodel.loadCollection(@publicClans, 'public_clans', {cache: false})
|
2015-03-31 16:28:57 -04:00
|
|
|
|
|
|
|
getRenderData: ->
|
|
|
|
context = super()
|
2015-04-01 14:56:48 -04:00
|
|
|
context.publicClans = @publicClans.models
|
2015-04-01 18:24:45 -04:00
|
|
|
context.myClans = _.filter @publicClans.models, (c) ->
|
|
|
|
return true if c.ownerID is me.get('_id')
|
|
|
|
return true for member in c.get('members') when me.get('_id') is member.id
|
2015-04-01 14:56:48 -04:00
|
|
|
context.myClanIDs = _.map context.myClans, (c) -> c.id
|
2015-03-31 16:28:57 -04:00
|
|
|
context
|
|
|
|
|
2015-04-01 14:56:48 -04:00
|
|
|
onClickCreateClan: (e) ->
|
2015-04-01 18:24:45 -04:00
|
|
|
return @openModalView(new AuthModal()) if me.isAnonymous()
|
2015-04-01 14:56:48 -04:00
|
|
|
if name = $('.create-clan-name').val()
|
|
|
|
clan = new Clan()
|
|
|
|
clan.set 'type', 'public'
|
|
|
|
clan.set 'name', name
|
|
|
|
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) =>
|
|
|
|
window.location.reload()
|
|
|
|
@supermodel.addRequestResource( 'leave_clan', options).load()
|
|
|
|
else
|
|
|
|
console.error "No clan ID attached to leave button."
|