codecombat/app/views/kinds/RootView.coffee

137 lines
4.4 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05: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 13:27:36 -04:00
Achievement = require 'models/Achievement'
AchievementPopup = require 'views/achievements/AchievementPopup'
utils = require 'lib/utils'
# TODO remove
2014-01-03 13:32:13 -05: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
showBackground: true
2014-08-15 13:27:36 -04:00
2014-01-03 13:32:13 -05:00
events:
2014-06-30 22:16:26 -04:00
'click #logout-button': 'logoutAccount'
'change .language-dropdown': 'onLanguageChanged'
2014-02-20 16:19:49 -05:00
'click .toggle-fullscreen': 'toggleFullscreen'
2014-08-31 18:08:52 -04:00
'click .auth-button': 'onClickAuthButton'
'click a': 'onClickAnchor'
2014-06-13 16:35:57 -04:00
'click button': 'toggleModal'
'click li': 'toggleModal'
subscriptions:
'achievements:new': 'handleNewAchievements'
2014-06-17 15:42:27 -04:00
showNewAchievement: (achievement, earnedAchievement) ->
popup = new AchievementPopup achievement: achievement, earnedAchievement: earnedAchievement
handleNewAchievements: (e) ->
_.each e.earnedAchievements.models, (earnedAchievement) =>
achievement = new Achievement(_id: earnedAchievement.get('achievement'))
achievement.fetch
2014-06-17 15:42:27 -04:00
success: (achievement) => @showNewAchievement(achievement, earnedAchievement)
2014-01-03 13:32:13 -05:00
logoutAccount: ->
window.tracker?.trackEvent 'Homepage', Action: 'Log Out' if @id is 'home-view'
2014-01-03 13:32:13 -05:00
logoutUser($('#login-email').val())
showWizardSettingsModal: ->
WizardSettingsModal = require('views/modal/WizardSettingsModal')
subview = new WizardSettingsModal {}
2014-01-03 13:32:13 -05:00
@openModalView subview
2014-08-31 18:08:52 -04:00
onClickAuthButton: ->
AuthModal = require 'views/modal/AuthModal'
window.tracker?.trackEvent 'Homepage', Action: 'Auth Modal' if @id is 'home-view'
@openModalView new AuthModal {}
onClickAnchor: (e) ->
anchorText = e?.currentTarget?.text
window.tracker?.trackEvent 'Homepage', Action: anchorText if @id is 'home-view' and anchorText
@toggleModal e
2014-01-03 13:32:13 -05: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()
#hash = location.hash
#location.hash = ''
#location.hash = hash
@renderScrollbar()
getRenderData: ->
c = super()
c.showBackground = @showBackground
c.usesSocialMedia = @usesSocialMedia
c
2014-08-15 13:27:36 -04:00
afterRender: ->
super(arguments...)
2014-06-30 22:16:26 -04:00
@chooseTab(location.hash.replace('#', '')) if location.hash
2014-04-12 15:35:45 -04:00
@buildLanguages()
$('body').removeClass('is-playing')
2014-01-03 13:32:13 -05: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: -> ''
chooseTab: (category) ->
$("a[href='##{category}']", @$el).tab('show')
# TODO: automate tabs to put in hashes when they are clicked
2014-01-03 13:32:13 -05:00
buildLanguages: ->
2014-06-30 22:16:26 -04:00
$select = @$el.find('.language-dropdown').empty()
if $select.hasClass('fancified')
2014-04-12 15:35:45 -04:00
$select.parent().find('.options, .trigger').remove()
2014-06-30 22:16:26 -04:00
$select.unwrap().removeClass('fancified')
2014-08-23 18:51:59 -04:00
preferred = me.get('preferredLanguage', true)
2014-01-03 13:32:13 -05:00
codes = _.keys(locale)
genericCodes = _.filter codes, (code) ->
_.find(codes, (code2) ->
code2 isnt code and code2.split('-')[0] is code)
for code, localeInfo of locale when not (code in genericCodes) or code is preferred
$select.append(
2014-06-30 22:16:26 -04:00
$('<option></option>').val(code).text(localeInfo.nativeDescription))
2014-03-30 20:24:07 -04:00
$select.val(preferred).fancySelect().parent().find('.trigger').addClass('header-font')
$('body').attr('lang', preferred)
2014-01-03 13:32:13 -05:00
onLanguageChanged: ->
2014-06-30 22:16:26 -04:00
newLang = $('.language-dropdown').val()
2014-01-03 13:32:13 -05:00
$.i18n.setLng(newLang, {})
@saveLanguage(newLang)
@render()
2014-06-30 22:16:26 -04:00
unless newLang.split('-')[0] is 'en'
DiplomatModal = require 'views/modal/DiplomatSuggestionModal'
@openModalView(new DiplomatModal())
2014-01-03 13:32:13 -05:00
saveLanguage: (newLang) ->
me.set('preferredLanguage', newLang)
2014-06-11 16:16:17 -04:00
res = me.patch()
2014-01-03 13:32:13 -05:00
return unless res
res.error ->
errors = JSON.parse(res.responseText)
2014-06-30 22:16:26 -04:00
console.warn 'Error saving language:', errors
2014-01-03 13:32:13 -05:00
res.success (model, response, options) ->
2014-06-30 22:16:26 -04:00
#console.log 'Saved language:', newLang