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-05-24 14:45:53 -04:00
Achievement = require ' ../../models/Achievement '
2014-05-26 12:21:56 -04:00
User = require ' ../../models/User '
2014-05-24 14:45:53 -04:00
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
events:
" click # logout-button " : " logoutAccount "
2014-02-22 21:06:37 -05:00
' change .language-dropdown ' : ' onLanguageChanged '
2014-02-20 16:19:49 -05:00
' click .toggle-fullscreen ' : ' toggleFullscreen '
2014-05-22 14:24:35 -04:00
' click .auth-button ' : ' onClickAuthbutton '
2014-01-06 19:58:50 -05:00
2014-05-24 14:45:53 -04:00
subscriptions:
' achievements:new ' : ' handleNewAchievements '
initialize: ->
2014-05-26 12:21:56 -04:00
$ =>
2014-05-29 05:37:35 -04:00
# TODO Ruben remove this. Allows for easy testing right now though
2014-05-26 14:45:12 -04:00
#test = new Achievement(_id:'537ce4855c91b8d1dda7fda8')
#test.fetch(success:@showNewAchievement)
2014-05-26 12:21:56 -04:00
2014-05-24 14:45:53 -04:00
showNewAchievement: (achievement) ->
2014-05-26 12:21:56 -04:00
currentLevel = me . level ( )
nextLevel = currentLevel + 1
currentLevelExp = User . expForLevel ( currentLevel )
nextLevelExp = User . expForLevel ( nextLevel )
totalExpNeeded = nextLevelExp - currentLevelExp
currentExp = me . get ( ' points ' )
worth = achievement . get ( ' worth ' )
2014-05-31 17:19:55 -04:00
leveledUp = currentExp - worth < currentLevelExp
alreadyAchievedPercentage = 100 * ( currentExp - currentLevelExp - worth ) / totalExpNeeded
newlyAchievedPercentage = if currentLevelExp is currentExp then 0 else 100 * worth / totalExpNeeded
2014-05-26 12:21:56 -04:00
console . debug " Current level is #{ currentLevel } ( #{ currentLevelExp } xp), next level is #{ nextLevel } ( #{ nextLevelExp } xp). "
2014-05-31 17:19:55 -04:00
console . debug " Need a total of #{ nextLevelExp - currentLevelExp } , already had #{ currentExp - currentLevelExp - worth } and just now earned #{ worth } totalling on #{ currentExp } "
2014-05-26 12:21:56 -04:00
alreadyAchievedBar = $ ( " <div class= ' progress-bar progress-bar-warning ' style= ' width: #{ alreadyAchievedPercentage } % ' ></div> " )
2014-05-31 17:19:55 -04:00
newlyAchievedBar = $ ( " <div data-toggle= ' tooltip ' class= ' progress-bar progress-bar-success ' style= ' width: #{ newlyAchievedPercentage } % ' ></div> " )
emptyBar = $ ( " <div data-toggle= ' tooltip ' class= ' progress-bar progress-bar-white ' style= ' width: #{ 100 - newlyAchievedPercentage - alreadyAchievedPercentage } % ' ></div> " )
progressBar = $ ( ' <div class= " progress " data-toggle= " tooltip " ></div> ' ) . append ( alreadyAchievedBar ) . append ( newlyAchievedBar ) . append ( emptyBar )
message = if ( currentLevel isnt 1 ) and leveledUp then " Reached level #{ currentLevel } ! " else null
2014-05-26 12:21:56 -04:00
2014-05-31 17:19:55 -04:00
alreadyAchievedBar . tooltip ( title: " #{ currentExp } XP in total " )
newlyAchievedBar . tooltip ( title: " #{ worth } XP earned " )
emptyBar . tooltip ( title: " #{ nextLevelExp - currentExp } XP until level #{ nextLevel } " )
# TODO a default should be linked here
2014-05-24 14:45:53 -04:00
imageURL = ' /file/ ' + achievement . get ( ' icon ' )
data =
2014-05-26 12:21:56 -04:00
title: achievement . get ( ' name ' )
2014-05-24 14:45:53 -04:00
image: $ ( " <img src= ' #{ imageURL } ' /> " )
description: achievement . get ( ' description ' )
2014-05-26 12:21:56 -04:00
progressBar: progressBar
earnedExp: " + #{ worth } XP "
message: message
2014-05-24 14:45:53 -04:00
options =
2014-05-31 17:19:55 -04:00
autoHideDelay: 10000
2014-05-24 14:45:53 -04:00
globalPosition: ' bottom right '
showDuration: 400
style: ' achievement '
2014-05-31 17:19:55 -04:00
autoHide: true
2014-05-24 14:45:53 -04:00
clickToHide: true
$ . notify ( data , options )
handleNewAchievements: (earnedAchievements) ->
2014-05-26 12:21:56 -04:00
console . debug ' Got new earned achievements '
2014-05-24 14:45:53 -04:00
# TODO performance?
_ . each ( earnedAchievements . models , (earnedAchievement) =>
2014-05-26 12:21:56 -04:00
achievement = new Achievement ( _id: earnedAchievement . get ( ' achievement ' ) )
console . log achievement
2014-05-24 14:45:53 -04:00
achievement . fetch (
success: @ showNewAchievement
)
)
2014-01-03 13:32:13 -05:00
logoutAccount: ->
logoutUser ( $ ( ' # login-email ' ) . val ( ) )
showWizardSettingsModal: ->
2014-02-24 12:46:40 -05:00
WizardSettingsModal = require ( ' views/modal/wizard_settings_modal ' )
subview = new WizardSettingsModal { }
2014-01-03 13:32:13 -05:00
@ openModalView subview
2014-05-22 14:24:35 -04:00
onClickAuthbutton: ->
AuthModal = require ' views/modal/auth_modal '
@ openModalView new AuthModal { }
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 ( )
2014-05-08 14:58:44 -04:00
#hash = location.hash
#location.hash = ''
#location.hash = hash
2014-04-07 17:34:36 -04:00
@ renderScrollbar ( )
2014-03-21 01:00:34 -04:00
#@$('.antiscroll-wrap').antiscroll() # not yet, buggy
2014-03-03 16:21:05 -05:00
afterRender: ->
super ( arguments . . . )
@ chooseTab ( location . hash . replace ( ' # ' , ' ' ) ) if location . hash
2014-04-12 15:35:45 -04:00
@ buildLanguages ( )
2014-04-11 19:15:26 -04:00
$ ( ' body ' ) . removeClass ( ' is-playing ' )
2014-01-03 13:32:13 -05:00
2014-03-03 16:21:05 -05:00
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: ->
$select = @ $el . find ( " .language-dropdown " ) . empty ( )
2014-02-21 07:42:52 -05:00
if $select . hasClass ( " fancified " )
2014-04-12 15:35:45 -04:00
$select . parent ( ) . find ( ' .options, .trigger ' ) . remove ( )
2014-02-21 07:42:52 -05:00
$select . unwrap ( ) . removeClass ( " fancified " )
2014-01-03 13:32:13 -05:00
preferred = me . lang ( )
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-01-06 19:58:50 -05: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
2014-02-22 21:06:37 -05:00
onLanguageChanged: ->
2014-01-03 13:32:13 -05:00
newLang = $ ( " .language-dropdown " ) . val ( )
$ . i18n . setLng ( newLang , { } )
@ saveLanguage ( newLang )
@ render ( )
unless newLang . split ( ' - ' ) [ 0 ] is " en "
@ openModalView ( application . router . getView ( " modal/diplomat_suggestion " , " _modal " ) )
saveLanguage: (newLang) ->
me . set ( ' preferredLanguage ' , newLang )
res = me . save ( )
return unless res
res . error ->
errors = JSON . parse ( res . responseText )
console . warn " Error saving language: " , errors
res . success (model, response, options) ->
2014-01-06 19:58:50 -05:00
#console.log "Saved language:", newLang
2014-02-20 16:19:49 -05:00
toggleFullscreen: (e) ->
# https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Using_full_screen_mode?redirectlocale=en-US&redirectslug=Web/Guide/DOM/Using_full_screen_mode
# Whoa, even cooler: https://developer.mozilla.org/en-US/docs/WebAPI/Pointer_Lock
full = document . fullscreenElement or
document . mozFullScreenElement or
document . mozFullscreenElement or
document . webkitFullscreenElement or
document . msFullscreenElement
d = document . documentElement
if not full
req = d . requestFullScreen or
d . mozRequestFullScreen or
d . mozRequestFullscreen or
d . msRequestFullscreen or
( if d . webkitRequestFullscreen then -> d . webkitRequestFullscreen Element . ALLOW_KEYBOARD_INPUT else null )
req ? . call d
else
nah = document . exitFullscreen or
document . mozCancelFullScreen or
document . mozCancelFullscreen or
document . msExitFullscreen or
document . webkitExitFullscreen
nah ? . call document
return