2014-11-28 20:49:41 -05:00
|
|
|
ModalView = require 'views/core/ModalView'
|
2016-02-25 18:24:16 -05:00
|
|
|
CreateAccountModal = require 'views/core/CreateAccountModal'
|
2014-01-03 13:32:13 -05:00
|
|
|
template = require 'templates/play/level/modal/victory'
|
2014-11-28 20:49:41 -05:00
|
|
|
{me} = require 'core/auth'
|
2014-07-23 10:02:45 -04:00
|
|
|
LadderSubmissionView = require 'views/play/common/LadderSubmissionView'
|
2014-01-03 13:32:13 -05:00
|
|
|
LevelFeedback = require 'models/LevelFeedback'
|
2014-11-28 20:49:41 -05:00
|
|
|
utils = require 'core/utils'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-07-17 20:22:26 -04:00
|
|
|
module.exports = class VictoryModal extends ModalView
|
2014-01-03 13:32:13 -05:00
|
|
|
id: 'level-victory-modal'
|
|
|
|
template: template
|
|
|
|
|
2014-05-17 14:55:41 -04:00
|
|
|
subscriptions:
|
|
|
|
'ladder:game-submitted': 'onGameSubmitted'
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
events:
|
2014-12-08 16:45:01 -05:00
|
|
|
'click .sign-up-button': 'onClickSignupButton'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
# review events
|
|
|
|
'mouseover .rating i': (e) -> @showStars(@starNum($(e.target)))
|
|
|
|
'mouseout .rating i': -> @showStars()
|
|
|
|
'click .rating i': (e) ->
|
|
|
|
@setStars(@starNum($(e.target)))
|
|
|
|
@$el.find('.review').show()
|
|
|
|
'keypress .review textarea': -> @saveReviewEventually()
|
|
|
|
|
|
|
|
constructor: (options) ->
|
2014-10-15 16:43:26 -04:00
|
|
|
application.router.initializeSocialMediaServices()
|
2014-10-18 21:57:33 -04:00
|
|
|
victory = options.level.get('victory', true)
|
2014-03-13 12:02:19 -04:00
|
|
|
body = utils.i18n(victory, 'body') or 'Sorry, this level has no victory message yet.'
|
2014-01-03 13:32:13 -05:00
|
|
|
@body = marked(body)
|
|
|
|
@level = options.level
|
|
|
|
@session = options.session
|
|
|
|
@saveReviewEventually = _.debounce(@saveReviewEventually, 2000)
|
|
|
|
@loadExistingFeedback()
|
|
|
|
super options
|
|
|
|
|
|
|
|
loadExistingFeedback: ->
|
|
|
|
url = "/db/level/#{@level.id}/feedback"
|
|
|
|
@feedback = new LevelFeedback()
|
2014-08-26 20:34:00 -04:00
|
|
|
@feedback.setURL url
|
2015-02-11 16:12:42 -05:00
|
|
|
@feedback.fetch cache: false
|
2014-03-25 17:55:11 -04:00
|
|
|
@listenToOnce(@feedback, 'sync', -> @onFeedbackLoaded())
|
|
|
|
@listenToOnce(@feedback, 'error', -> @onFeedbackNotFound())
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
onFeedbackLoaded: ->
|
|
|
|
@feedback.url = -> '/db/level.feedback/' + @id
|
|
|
|
@$el.find('.review textarea').val(@feedback.get('review'))
|
|
|
|
@$el.find('.review').show()
|
|
|
|
@showStars()
|
|
|
|
|
|
|
|
onFeedbackNotFound: ->
|
|
|
|
@feedback = new LevelFeedback()
|
|
|
|
@feedback.set('levelID', @level.get('slug') or @level.id)
|
|
|
|
@feedback.set('levelName', @level.get('name') or '')
|
2014-06-30 22:16:26 -04:00
|
|
|
@feedback.set('level', {majorVersion: @level.get('version').major, original: @level.get('original')})
|
2014-01-03 13:32:13 -05:00
|
|
|
@showStars()
|
|
|
|
|
2014-12-08 16:45:01 -05:00
|
|
|
onClickSignupButton: (e) ->
|
|
|
|
e.preventDefault()
|
|
|
|
window.tracker?.trackEvent 'Started Signup', category: 'Play Level', label: 'Victory Modal', level: @level.get('slug')
|
2016-02-25 18:24:16 -05:00
|
|
|
@openModalView new CreateAccountModal()
|
2014-12-08 16:45:01 -05:00
|
|
|
|
2014-05-17 14:55:41 -04:00
|
|
|
onGameSubmitted: (e) ->
|
2014-03-10 13:36:28 -04:00
|
|
|
ladderURL = "/play/ladder/#{@level.get('slug')}#my-matches"
|
2014-05-17 14:55:41 -04:00
|
|
|
Backbone.Mediator.publish 'router:navigate', route: ladderURL
|
2014-03-10 13:36:28 -04:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
getRenderData: ->
|
|
|
|
c = super()
|
|
|
|
c.body = @body
|
|
|
|
c.me = me
|
2014-03-13 12:02:19 -04:00
|
|
|
c.levelName = utils.i18n @level.attributes, 'name'
|
2014-03-08 00:20:09 -05:00
|
|
|
c.level = @level
|
2016-07-14 11:58:43 -04:00
|
|
|
if c.level.isType('ladder')
|
2014-05-05 19:59:12 -04:00
|
|
|
c.readyToRank = @session.readyToRank()
|
2014-01-03 13:32:13 -05:00
|
|
|
c
|
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
super()
|
2014-05-17 14:55:41 -04:00
|
|
|
@ladderSubmissionView = new LadderSubmissionView session: @session, level: @level
|
|
|
|
@insertSubView @ladderSubmissionView, @$el.find('.ladder-submission-view')
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
afterInsert: ->
|
|
|
|
super()
|
2015-09-09 17:36:05 -04:00
|
|
|
@playSound 'victory'
|
2014-01-09 13:48:51 -05:00
|
|
|
gapi?.plusone?.go? @$el[0]
|
|
|
|
FB?.XFBML?.parse? @$el[0]
|
|
|
|
twttr?.widgets?.load?()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
destroy: ->
|
|
|
|
@saveReview() if @$el.find('.review textarea').val()
|
2014-02-18 13:03:57 -05:00
|
|
|
@feedback.off()
|
2014-02-14 13:57:47 -05:00
|
|
|
super()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
# rating, review
|
|
|
|
|
|
|
|
starNum: (starEl) -> starEl.prevAll('i').length + 1
|
|
|
|
|
|
|
|
showStars: (num) ->
|
|
|
|
@$el.find('.rating').show()
|
|
|
|
num ?= @feedback?.get('rating') or 0
|
|
|
|
stars = @$el.find('.rating i')
|
|
|
|
stars.removeClass('icon-star').addClass('icon-star-empty')
|
|
|
|
stars.slice(0, num).removeClass('icon-star-empty').addClass('icon-star')
|
|
|
|
|
|
|
|
setStars: (num) ->
|
|
|
|
@feedback.set('rating', num)
|
|
|
|
@feedback.save()
|
|
|
|
|
|
|
|
saveReviewEventually: ->
|
|
|
|
@saveReview()
|
|
|
|
|
|
|
|
saveReview: ->
|
|
|
|
@feedback.set('review', @$el.find('.review textarea').val())
|
|
|
|
@feedback.save()
|