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'
|
2014-03-17 00:33:46 -04:00
|
|
|
|
2014-07-06 14:45:27 -04:00
|
|
|
# user views
|
|
|
|
'user/:nameOrID(/:subview)': 'userView'
|
|
|
|
|
2014-06-14 10:37:31 -04:00
|
|
|
# Direct links
|
|
|
|
'test/*subpath': go('TestView')
|
|
|
|
'demo/*subpath': go('DemoView')
|
2014-05-19 14:58:45 -04:00
|
|
|
'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-03-17 00:33:46 -04:00
|
|
|
|
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()
|
2014-07-06 14:01:47 -04:00
|
|
|
@openView if view then view else @notFoundView()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-07-06 14:45:27 -04:00
|
|
|
userView: (nameOrID, subview) ->
|
|
|
|
modulePrefix = 'views/user/'
|
|
|
|
suffix = subview or 'home'
|
|
|
|
ViewClass = @tryToLoadModule modulePrefix + suffix
|
|
|
|
if ViewClass
|
|
|
|
view = new ViewClass {}, nameOrID
|
|
|
|
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: ->
|
2014-01-07 02:45:33 -05:00
|
|
|
$('.share-buttons, .partner-badges').addClass('fade-in').delay(10000).removeClass('fade-in', 5000)
|
2014-01-09 13:48:51 -05:00
|
|
|
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?
|
2014-01-09 13:48:51 -05:00
|
|
|
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
|
|
|
}
|
2014-01-09 13:48:51 -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)
|
2014-03-07 14:57:15 -05:00
|
|
|
@cache[route] = view if view?.cache
|
2014-01-03 13:32:13 -05:00
|
|
|
return view
|
2014-03-17 00:33:46 -04:00
|
|
|
|
2014-02-17 20:42:41 -05:00
|
|
|
routeDirectly: (path, args) ->
|
2014-06-05 19:55:49 -04:00
|
|
|
if window.currentView?.reloadOnClose
|
|
|
|
return document.location.reload()
|
2014-02-17 20:42:41 -05:00
|
|
|
path = "views/#{path}"
|
|
|
|
ViewClass = @tryToLoadModule path
|
2014-07-06 14:01:47 -04:00
|
|
|
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
|
|
|
|
|
2014-07-06 14:01:47 -04:00
|
|
|
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
|
|
|
|
|
2014-07-06 14:01:47 -04:00
|
|
|
notFoundView: ->
|
2014-01-03 13:32:13 -05:00
|
|
|
NotFoundView = require('views/not_found')
|
|
|
|
view = new NotFoundView()
|
|
|
|
view.render()
|
|
|
|
|
|
|
|
closeCurrentView: ->
|
2014-02-16 21:29:24 -05:00
|
|
|
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
|
2014-03-17 00:33:46 -04:00
|
|
|
@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
|