codecombat/app/views/core/RootView.coffee

192 lines
6.9 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('core/auth')
2014-01-03 13:32:13 -05:00
locale = require 'locale/locale'
2014-08-15 13:27:36 -04:00
Achievement = require 'models/Achievement'
AchievementPopup = require 'views/core/AchievementPopup'
utils = require 'core/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'
'click .signup-button': 'onClickSignupButton'
'click .login-button': 'onClickLoginButton'
'click a': 'onClickAnchor'
2014-06-13 16:35:57 -04:00
'click button': 'toggleModal'
'click li': 'toggleModal'
2016-07-26 19:47:55 -04:00
'treema-error': 'onTreemaError'
subscriptions:
'achievements:new': 'handleNewAchievements'
'modal:open-modal-view': 'onOpenModalView'
shortcuts:
'ctrl+shift+a': 'navigateToAdmin'
2014-06-17 15:42:27 -04:00
showNewAchievement: (achievement, earnedAchievement) ->
earnedAchievement.set('notified', true)
earnedAchievement.patch()
return if achievement.get('collection') is 'level.sessions' and not achievement.get('query')?.team
#return if @isIE() # Some bugs in IE right now, TODO fix soon! # Maybe working now with not caching achievement fetches in CocoModel?
return if window.serverConfig.picoCTF
2016-08-24 10:37:08 -04:00
return if achievement.get('hidden')
2015-01-08 14:57:24 -05:00
new AchievementPopup achievement: achievement, earnedAchievement: earnedAchievement
handleNewAchievements: (e) ->
_.each e.earnedAchievements.models, (earnedAchievement) =>
achievement = new Achievement(_id: earnedAchievement.get('achievement'))
achievement.fetch
success: (achievement) => @showNewAchievement?(achievement, earnedAchievement)
cache: false
2014-01-03 13:32:13 -05:00
logoutAccount: ->
2015-10-16 18:02:33 -04:00
window?.webkit?.messageHandlers?.notification?.postMessage(name: "signOut") if window.application.isIPadApp
Backbone.Mediator.publish("auth:logging-out", {})
window.tracker?.trackEvent 'Log Out', category:'Homepage', ['Google Analytics'] if @id is 'home-view'
2014-01-03 13:32:13 -05:00
logoutUser($('#login-email').val())
onClickSignupButton: ->
CreateAccountModal = require 'views/core/CreateAccountModal'
switch @id
when 'home-view'
window.tracker?.trackEvent 'Started Signup', category: 'Homepage', label: 'Homepage'
when 'world-map-view'
# TODO: add campaign data
window.tracker?.trackEvent 'Started Signup', category: 'World Map', label: 'World Map'
else
2014-12-08 16:45:01 -05:00
window.tracker?.trackEvent 'Started Signup', label: @id
@openModalView new CreateAccountModal()
onClickLoginButton: ->
AuthModal = require 'views/core/AuthModal'
window.tracker?.trackEvent 'Login', category: 'Homepage', ['Google Analytics'] if @id is 'home-view'
@openModalView new AuthModal()
onClickAnchor: (e) ->
return if @destroyed
anchorText = e?.currentTarget?.text
window.tracker?.trackEvent anchorText, category: 'Homepage', ['Google Analytics'] if @id is 'home-view' and anchorText
@toggleModal e
onOpenModalView: (e) ->
return console.error "Couldn't find modalPath #{e.modalPath}" unless e.modalPath and ModalClass = require e.modalPath
@openModalView new ModalClass {}
2014-01-03 13:32:13 -05:00
showLoading: ($el) ->
$el ?= @$el.find('#site-content-area')
2014-01-03 13:32:13 -05:00
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()
afterRender: ->
if @$el.find('#site-nav').length # hack...
@$el.addClass('site-chrome')
if @showBackground
@$el.addClass('show-background')
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 title = @getTitle() then title += ' | CodeCombat'
else title = 'CodeCombat - Learn how to code by playing a game'
$('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()
2014-08-23 18:51:59 -04:00
preferred = me.get('preferredLanguage', true)
@addLanguagesToSelect($select, preferred)
$('body').attr('lang', preferred)
addLanguagesToSelect: ($select, initialVal) ->
initialVal ?= 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)
2015-02-20 18:20:44 -05:00
for code, localeInfo of locale when code isnt 'update' and (not (code in genericCodes) or code is initialVal)
2014-01-03 13:32:13 -05:00
$select.append(
2014-06-30 22:16:26 -04:00
$('<option></option>').val(code).text(localeInfo.nativeDescription))
if code is 'fr'
$select.append(
$('<option class="select-dash" disabled="disabled"></option>').text('----------------------------------'))
$select.val(initialVal)
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)
loading = application.moduleLoader.loadLanguage(me.get('preferredLanguage', true))
if loading
@listenToOnce application.moduleLoader, 'load-complete', @onLanguageLoaded
else
@onLanguageLoaded()
onLanguageLoaded: ->
2014-01-03 13:32:13 -05:00
@render()
unless me.get('preferredLanguage').split('-')[0] is 'en'
DiplomatModal = require 'views/core/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
isOldBrowser: ->
if $.browser
majorVersion = $.browser.versionNumber
return true if $.browser.mozilla && majorVersion < 25
return true if $.browser.chrome && majorVersion < 31 # Noticed Gems in the Deep not loading with 30
return true if $.browser.safari && majorVersion < 6 # 6 might have problems with Aether, or maybe just old minors of 6: https://errorception.com/projects/51a79585ee207206390002a2/errors/547a202e1ead63ba4e4ac9fd
else
console.warn 'no more jquery browser version...'
return false
logoutRedirectURL: '/'
navigateToAdmin: ->
if window.amActually or me.isAdmin()
application.router.navigate('/admin', {trigger: true})
2016-07-26 19:47:55 -04:00
onTreemaError: (e) ->
noty text: e.message, layout: 'topCenter', type: 'error', killer: false, timeout: 5000, dismissQueue: true