2014-01-03 10:32:13 -08:00
|
|
|
# A root view is one that replaces everything else on the screen when it
|
|
|
|
# comes into being, as opposed to sub-views which get inserted into other views.
|
|
|
|
|
|
|
|
CocoView = require './CocoView'
|
|
|
|
|
|
|
|
{logoutUser, me} = require('lib/auth')
|
|
|
|
locale = require 'locale/locale'
|
|
|
|
|
2014-08-15 10:27:36 -07:00
|
|
|
Achievement = require 'models/Achievement'
|
2014-08-11 14:11:26 +02:00
|
|
|
AchievementPopup = require 'views/achievements/AchievementPopup'
|
2014-07-30 22:23:43 +02:00
|
|
|
utils = require 'lib/utils'
|
|
|
|
|
2014-05-29 11:10:57 +02:00
|
|
|
# TODO remove
|
2014-05-24 20:45:53 +02:00
|
|
|
|
2014-01-03 10:32:13 -08:00
|
|
|
filterKeyboardEvents = (allowedEvents, func) ->
|
|
|
|
return (splat...) ->
|
|
|
|
e = splat[0]
|
|
|
|
return unless e.keyCode in allowedEvents or not e.keyCode
|
|
|
|
return func(splat...)
|
|
|
|
|
|
|
|
module.exports = class RootView extends CocoView
|
2014-07-17 09:22:52 -07:00
|
|
|
showBackground: true
|
2014-08-15 10:27:36 -07:00
|
|
|
|
2014-01-03 10:32:13 -08:00
|
|
|
events:
|
2014-07-01 10:16:26 +08:00
|
|
|
'click #logout-button': 'logoutAccount'
|
2014-02-22 21:06:37 -05:00
|
|
|
'change .language-dropdown': 'onLanguageChanged'
|
2014-02-20 13:19:49 -08:00
|
|
|
'click .toggle-fullscreen': 'toggleFullscreen'
|
2014-08-31 15:08:52 -07:00
|
|
|
'click .auth-button': 'onClickAuthButton'
|
2014-10-21 16:49:25 -07:00
|
|
|
'click a': 'onClickAnchor'
|
2014-06-13 13:35:57 -07:00
|
|
|
'click button': 'toggleModal'
|
|
|
|
'click li': 'toggleModal'
|
2014-01-06 16:58:50 -08:00
|
|
|
|
2014-05-24 20:45:53 +02:00
|
|
|
subscriptions:
|
|
|
|
'achievements:new': 'handleNewAchievements'
|
|
|
|
|
2014-06-17 21:42:27 +02:00
|
|
|
showNewAchievement: (achievement, earnedAchievement) ->
|
2014-08-11 14:11:26 +02:00
|
|
|
popup = new AchievementPopup achievement: achievement, earnedAchievement: earnedAchievement
|
2014-05-24 20:45:53 +02:00
|
|
|
|
2014-08-27 12:24:03 -07:00
|
|
|
handleNewAchievements: (e) ->
|
|
|
|
_.each e.earnedAchievements.models, (earnedAchievement) =>
|
2014-05-26 18:21:56 +02:00
|
|
|
achievement = new Achievement(_id: earnedAchievement.get('achievement'))
|
2014-07-30 22:23:43 +02:00
|
|
|
achievement.fetch
|
2014-06-17 21:42:27 +02:00
|
|
|
success: (achievement) => @showNewAchievement(achievement, earnedAchievement)
|
2014-05-24 20:45:53 +02:00
|
|
|
|
2014-01-03 10:32:13 -08:00
|
|
|
logoutAccount: ->
|
2014-11-02 19:02:48 -05:00
|
|
|
Backbone.Mediator.publish("auth:logging-out")
|
2014-10-21 16:49:25 -07:00
|
|
|
window.tracker?.trackEvent 'Homepage', Action: 'Log Out' if @id is 'home-view'
|
2014-01-03 10:32:13 -08:00
|
|
|
logoutUser($('#login-email').val())
|
|
|
|
|
|
|
|
showWizardSettingsModal: ->
|
2014-07-23 07:02:45 -07:00
|
|
|
WizardSettingsModal = require('views/modal/WizardSettingsModal')
|
2014-02-24 09:46:40 -08:00
|
|
|
subview = new WizardSettingsModal {}
|
2014-01-03 10:32:13 -08:00
|
|
|
@openModalView subview
|
|
|
|
|
2014-08-31 15:08:52 -07:00
|
|
|
onClickAuthButton: ->
|
2014-07-23 07:02:45 -07:00
|
|
|
AuthModal = require 'views/modal/AuthModal'
|
2014-10-21 16:49:25 -07:00
|
|
|
window.tracker?.trackEvent 'Homepage', Action: 'Auth Modal' if @id is 'home-view'
|
2014-05-22 11:24:35 -07:00
|
|
|
@openModalView new AuthModal {}
|
|
|
|
|
2014-10-21 16:49:25 -07:00
|
|
|
onClickAnchor: (e) ->
|
|
|
|
anchorText = e?.currentTarget?.text
|
|
|
|
window.tracker?.trackEvent 'Homepage', Action: anchorText if @id is 'home-view' and anchorText
|
|
|
|
@toggleModal e
|
|
|
|
|
2014-01-03 10:32:13 -08:00
|
|
|
showLoading: ($el) ->
|
|
|
|
$el ?= @$el.find('.main-content-area')
|
|
|
|
super($el)
|
|
|
|
|
|
|
|
afterInsert: ->
|
|
|
|
# force the browser to scroll to the hash
|
|
|
|
# also messes with the browser history, so perhaps come up with a better solution
|
|
|
|
super()
|
2014-05-08 11:58:44 -07:00
|
|
|
#hash = location.hash
|
|
|
|
#location.hash = ''
|
|
|
|
#location.hash = hash
|
2014-04-08 03:04:36 +05:30
|
|
|
@renderScrollbar()
|
2014-03-20 22:00:34 -07:00
|
|
|
|
2014-07-17 09:22:52 -07:00
|
|
|
getRenderData: ->
|
|
|
|
c = super()
|
|
|
|
c.showBackground = @showBackground
|
2014-10-15 16:43:26 -04:00
|
|
|
c.usesSocialMedia = @usesSocialMedia
|
2014-07-17 09:22:52 -07:00
|
|
|
c
|
2014-08-15 10:27:36 -07:00
|
|
|
|
2014-03-03 13:21:05 -08:00
|
|
|
afterRender: ->
|
|
|
|
super(arguments...)
|
2014-07-01 10:16:26 +08:00
|
|
|
@chooseTab(location.hash.replace('#', '')) if location.hash
|
2014-04-12 12:35:45 -07:00
|
|
|
@buildLanguages()
|
2014-04-11 16:15:26 -07:00
|
|
|
$('body').removeClass('is-playing')
|
2014-01-03 10:32:13 -08:00
|
|
|
|
2014-10-03 10:45:01 -07:00
|
|
|
if application.isProduction()
|
|
|
|
title = 'CodeCombat - ' + (@getTitle() or 'Learn how to code by playing a game')
|
|
|
|
else
|
|
|
|
title = @getTitle() or @constructor.name
|
|
|
|
|
|
|
|
$('title').text(title)
|
|
|
|
|
|
|
|
getTitle: -> ''
|
|
|
|
|
2014-03-03 13:21:05 -08:00
|
|
|
chooseTab: (category) ->
|
|
|
|
$("a[href='##{category}']", @$el).tab('show')
|
|
|
|
|
|
|
|
# TODO: automate tabs to put in hashes when they are clicked
|
2014-01-03 10:32:13 -08:00
|
|
|
|
|
|
|
buildLanguages: ->
|
2014-07-01 10:16:26 +08:00
|
|
|
$select = @$el.find('.language-dropdown').empty()
|
|
|
|
if $select.hasClass('fancified')
|
2014-04-12 12:35:45 -07:00
|
|
|
$select.parent().find('.options, .trigger').remove()
|
2014-07-01 10:16:26 +08:00
|
|
|
$select.unwrap().removeClass('fancified')
|
2014-08-23 15:51:59 -07:00
|
|
|
preferred = me.get('preferredLanguage', true)
|
2014-10-27 17:11:48 -07:00
|
|
|
@addLanguagesToSelect($select, preferred)
|
|
|
|
$select.fancySelect().parent().find('.trigger').addClass('header-font')
|
|
|
|
$('body').attr('lang', preferred)
|
|
|
|
|
|
|
|
addLanguagesToSelect: ($select, initialVal) ->
|
|
|
|
initialVal ?= me.get('preferredLanguage', true)
|
2014-01-03 10:32:13 -08:00
|
|
|
codes = _.keys(locale)
|
|
|
|
genericCodes = _.filter codes, (code) ->
|
|
|
|
_.find(codes, (code2) ->
|
|
|
|
code2 isnt code and code2.split('-')[0] is code)
|
2014-10-27 17:11:48 -07:00
|
|
|
for code, localeInfo of locale when not (code in genericCodes) or code is initialVal
|
2014-01-03 10:32:13 -08:00
|
|
|
$select.append(
|
2014-07-01 10:16:26 +08:00
|
|
|
$('<option></option>').val(code).text(localeInfo.nativeDescription))
|
2014-10-27 17:11:48 -07:00
|
|
|
$select.val(initialVal)
|
2014-01-03 10:32:13 -08:00
|
|
|
|
2014-02-22 21:06:37 -05:00
|
|
|
onLanguageChanged: ->
|
2014-07-01 10:16:26 +08:00
|
|
|
newLang = $('.language-dropdown').val()
|
2014-01-03 10:32:13 -08:00
|
|
|
$.i18n.setLng(newLang, {})
|
|
|
|
@saveLanguage(newLang)
|
|
|
|
@render()
|
2014-07-01 10:16:26 +08:00
|
|
|
unless newLang.split('-')[0] is 'en'
|
2014-07-23 07:02:45 -07:00
|
|
|
DiplomatModal = require 'views/modal/DiplomatSuggestionModal'
|
|
|
|
@openModalView(new DiplomatModal())
|
2014-01-03 10:32:13 -08:00
|
|
|
|
|
|
|
saveLanguage: (newLang) ->
|
|
|
|
me.set('preferredLanguage', newLang)
|
2014-06-11 13:16:17 -07:00
|
|
|
res = me.patch()
|
2014-01-03 10:32:13 -08:00
|
|
|
return unless res
|
|
|
|
res.error ->
|
|
|
|
errors = JSON.parse(res.responseText)
|
2014-07-01 10:16:26 +08:00
|
|
|
console.warn 'Error saving language:', errors
|
2014-01-03 10:32:13 -08:00
|
|
|
res.success (model, response, options) ->
|
2014-07-01 10:16:26 +08:00
|
|
|
#console.log 'Saved language:', newLang
|