2014-01-03 13:32:13 -05:00
|
|
|
gplusClientID = "800329290710-j9sivplv2gpcdgkrsis9rff3o417mlfa.apps.googleusercontent.com"
|
|
|
|
|
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-02-17 20:42:41 -05:00
|
|
|
# Experimenting with direct links
|
2014-06-05 19:55:49 -04:00
|
|
|
'test/*subpath': go('test')
|
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()
|
|
|
|
if view then @openView(view) else @showNotFound()
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
callback:"signinCallback",
|
|
|
|
clientid:gplusClientID,
|
|
|
|
cookiepolicy:"single_host_origin",
|
|
|
|
scope:"https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email",
|
2014-05-22 14:24:35 -04:00
|
|
|
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
|
|
|
|
console.warn "Didn't have gapi.signin to render G+ login button. (DoNotTrackMe extension?)"
|
|
|
|
|
|
|
|
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
|
|
|
|
return @showNotFound() if not ViewClass
|
|
|
|
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
|
|
|
|
# passing the broken off pieces as args. This way views like "resource/14394893"
|
|
|
|
# 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 @showNotFound() if not ViewClass
|
|
|
|
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
|
|
|
|
|
|
|
|
showNotFound: ->
|
|
|
|
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
|