codecombat/app/lib/Router.coffee

176 lines
5.3 KiB
CoffeeScript
Raw Normal View History

2014-06-30 22:16:26 -04:00
gplusClientID = '800329290710-j9sivplv2gpcdgkrsis9rff3o417mlfa.apps.googleusercontent.com'
2014-01-03 13:32:13 -05:00
2014-02-17 20:42:41 -05:00
go = (path) -> -> @routeDirectly path, arguments
2014-01-03 13:32:13 -05:00
module.exports = class CocoRouter extends Backbone.Router
subscribe: ->
Backbone.Mediator.subscribe 'gapi-loaded', @onGPlusAPILoaded, @
Backbone.Mediator.subscribe 'router:navigate', @onNavigate, @
routes:
# every abnormal view gets listed here
'': 'home'
'preview': 'home'
'beta': 'home'
# editor views tend to have the same general structure
'editor/:model(/:slug_or_id)(/:subview)': 'editorModelView'
# Direct links
'test': go('TestView')
'test/*subpath': go('TestView')
'demo': go('DemoView')
'demo/*subpath': go('DemoView')
'play/ladder/:levelID': go('play/ladder/ladder_view')
'play/ladder': go('play/ladder_home')
2014-01-03 13:32:13 -05:00
# db and file urls call the server directly
'db/*path': 'routeToServer'
'file/*path': 'routeToServer'
# most go through here
'*name': 'general'
home: -> @openRoute('home')
general: (name) ->
@openRoute(name)
2014-01-03 13:32:13 -05:00
editorModelView: (modelName, slugOrId, subview) ->
modulePrefix = "views/editor/#{modelName}/"
suffix = subview or (if slugOrId then 'edit' else 'home')
ViewClass = @tryToLoadModule(modulePrefix + suffix)
unless ViewClass
#console.log('could not hack it', modulePrefix + suffix)
args = (a for a in arguments when a)
args.splice(0, 0, 'editor')
return @openRoute(args.join('/'))
view = new ViewClass({}, slugOrId)
view.render()
@openView if view then view else @notFoundView()
2014-01-03 13:32:13 -05:00
cache: {}
openRoute: (route) ->
route = route.split('?')[0]
route = route.split('#')[0]
view = @getViewFromCache(route)
@openView(view)
openView: (view) ->
@closeCurrentView()
$('#page-container').empty().append view.el
window.currentView = view
@activateTab()
@renderLoginButtons()
window.scrollTo(0, view.scrollY) if view.scrollY?
view.afterInsert()
view.didReappear() if view.fromCache
onGPlusAPILoaded: =>
@renderLoginButtons()
renderLoginButtons: ->
$('.share-buttons, .partner-badges').addClass('fade-in').delay(10000).removeClass('fade-in', 5000)
setTimeout(FB.XFBML.parse, 10) if FB?.XFBML?.parse # Handles FB login and Like
twttr?.widgets?.load?()
2014-01-03 13:32:13 -05:00
return unless gapi?.plusone?
gapi.plusone.go?() # Handles +1 button
2014-01-03 13:32:13 -05:00
for gplusButton in $('.gplus-login-button')
params = {
2014-06-30 22:16:26 -04:00
callback: 'signinCallback',
clientid: gplusClientID,
cookiepolicy: 'single_host_origin',
scope: 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email',
height: 'short',
2014-01-03 13:32:13 -05:00
}
if gapi.signin?.render
2014-01-03 13:32:13 -05:00
gapi.signin.render(gplusButton, params)
else
2014-06-30 22:16:26 -04:00
console.warn 'Didn\'t have gapi.signin to render G+ login button. (DoNotTrackMe extension?)'
2014-01-03 13:32:13 -05:00
getViewFromCache: (route) ->
if route of @cache
@cache[route].fromCache = true
return @cache[route]
view = @getView(route)
@cache[route] = view if view?.cache
2014-01-03 13:32:13 -05:00
return view
2014-02-17 20:42:41 -05:00
routeDirectly: (path, args) ->
if window.currentView?.reloadOnClose
return document.location.reload()
2014-02-17 20:42:41 -05:00
path = "views/#{path}"
ViewClass = @tryToLoadModule path
return @openView @notFoundView() if not ViewClass
2014-02-17 20:42:41 -05:00
view = new ViewClass({}, args...) # options, then any path fragment args
view.render()
@openView(view)
2014-01-03 13:32:13 -05:00
getView: (route, suffix='_view') ->
# iteratively breaks down the url pieces looking for the view
2014-06-30 22:16:26 -04:00
# passing the broken off pieces as args. This way views like 'resource/14394893'
2014-01-03 13:32:13 -05:00
# will get passed to the resource view with arg '14394893'
pieces = _.string.words(route, '/')
split = Math.max(1, pieces.length-1)
while split > -1
sub_route = _.string.join('/', pieces[0..split]...)
path = "views/#{sub_route}#{suffix}"
ViewClass = @tryToLoadModule(path)
break if ViewClass
split -= 1
return @notFoundView() if not ViewClass
2014-01-03 13:32:13 -05:00
args = pieces[split+1..]
view = new ViewClass({}, args...) # options, then any path fragment args
view.render()
tryToLoadModule: (path) ->
try
return require(path)
catch error
if error.toString().search('Cannot find module "' + path + '" from') is -1
throw error
notFoundView: ->
2014-01-03 13:32:13 -05:00
NotFoundView = require('views/not_found')
view = new NotFoundView()
view.render()
closeCurrentView: ->
window.currentModal?.hide?()
2014-01-03 13:32:13 -05:00
return unless window.currentView?
if window.currentView.cache
window.currentView.scrollY = window.scrollY
window.currentView.willDisappear()
else
window.currentView.destroy()
activateTab: ->
base = _.string.words(document.location.pathname[1..], '/')[0]
$("ul.nav li.#{base}").addClass('active')
initialize: ->
@cache = {}
# http://nerds.airbnb.com/how-to-add-google-analytics-page-tracking-to-57536
@bind 'route', @_trackPageView
_trackPageView: ->
window.tracker?.trackPageView()
onNavigate: (e) ->
manualView = e.view or e.viewClass
@navigate e.route, {trigger: not manualView}
2014-01-03 13:32:13 -05:00
return unless manualView
if e.viewClass
args = e.viewArgs or []
view = new e.viewClass(args...)
view.render()
@openView view
else
@openView e.view
routeToServer: (e) ->
window.location.href = window.location.href