codecombat/app/core/Router.coffee

246 lines
9 KiB
CoffeeScript
Raw Normal View History

2014-06-30 22:16:26 -04:00
gplusClientID = '800329290710-j9sivplv2gpcdgkrsis9rff3o417mlfa.apps.googleusercontent.com'
# TODO: Move to GPlusHandler
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
initialize: ->
# http://nerds.airbnb.com/how-to-add-google-analytics-page-tracking-to-57536
@bind 'route', @_trackPageView
Backbone.Mediator.subscribe 'auth:gplus-api-loaded', @onGPlusAPILoaded, @
2014-01-03 13:32:13 -05:00
Backbone.Mediator.subscribe 'router:navigate', @onNavigate, @
@initializeSocialMediaServices = _.once @initializeSocialMediaServices
2014-01-03 13:32:13 -05:00
routes:
2014-11-27 12:44:08 -05:00
'': go('HomeView')
'about': go('AboutView')
2014-07-29 11:28:13 -04:00
'account': go('account/MainAccountView')
'account/settings': go('account/AccountSettingsRootView')
'account/unsubscribe': go('account/UnsubscribeView')
#'account/profile': go('user/JobProfileView') # legacy URL, sent in emails
'account/profile': go('EmployersView') # Show the not-recruiting-now screen
2014-11-25 14:09:29 -05:00
'account/payments': go('account/PaymentsView')
'account/subscription': go('account/SubscriptionView')
'account/subscription/sale': go('account/SubscriptionSaleView')
'account/invoices': go('account/InvoicesView')
2015-09-25 13:03:44 -04:00
'account/prepaid': go('account/PrepaidView')
'admin': go('admin/MainAdminView')
'admin/candidates': go('admin/CandidatesView')
'admin/clas': go('admin/CLAsView')
'admin/employers': go('admin/EmployersListView')
'admin/files': go('admin/FilesView')
'admin/analytics': go('admin/AnalyticsView')
'admin/analytics/subscriptions': go('admin/AnalyticsSubscriptionsView')
'admin/level-sessions': go('admin/LevelSessionsView')
'admin/users': go('admin/UsersView')
2014-08-30 23:27:58 -04:00
'admin/base': go('admin/BaseView')
'admin/trial-requests': go('admin/TrialRequestsView')
'admin/user-code-problems': go('admin/UserCodeProblemsView')
'admin/pending-patches': go('admin/PendingPatchesView')
'beta': go('HomeView')
2015-09-25 18:49:00 -04:00
'careers': => window.location.href = 'https://jobs.lever.co/codecombat'
2015-10-26 23:39:36 -04:00
'Careers': => window.location.href = 'https://jobs.lever.co/codecombat'
2015-09-14 20:38:18 -04:00
'cla': go('CLAView')
'clans': go('clans/ClansView')
'clans/:clanID': go('clans/ClanDetailsView')
'community': go('CommunityView')
'contribute': go('contribute/MainContributeView')
'contribute/adventurer': go('contribute/AdventurerView')
'contribute/ambassador': go('contribute/AmbassadorView')
'contribute/archmage': go('contribute/ArchmageView')
'contribute/artisan': go('contribute/ArtisanView')
'contribute/diplomat': go('contribute/DiplomatView')
'contribute/scribe': go('contribute/ScribeView')
'courses/mock1': go('courses/mock1/CoursesView')
'courses/mock1/enroll/:courseID': go('courses/mock1/CourseEnrollView')
'courses/mock1/:courseID': go('courses/mock1/CourseDetailsView')
'courses': go('courses/CoursesView')
2015-12-03 14:39:11 -05:00
'Courses': go('courses/CoursesView')
2015-11-03 19:41:06 -05:00
'courses/students': go('courses/StudentCoursesView')
'courses/teachers': go('courses/TeacherCoursesView')
2015-11-05 18:30:57 -05:00
'courses/purchase': go('courses/PurchaseCoursesView')
'courses/enroll(/:courseID)': go('courses/CourseEnrollView')
2015-11-30 14:14:27 -05:00
'courses/:classroomID': go('courses/ClassroomView')
'courses/:courseID/:courseInstanceID': go('courses/CourseDetailsView')
'db/*path': 'routeToServer'
'demo(/*subpath)': go('DemoView')
'docs/components': go('docs/ComponentsDocumentationView')
'docs/systems': go('docs/SystemsDocumentationView')
2014-08-26 20:34:00 -04:00
'editor': go('CommunityView')
'editor/achievement': go('editor/achievement/AchievementSearchView')
'editor/achievement/:articleID': go('editor/achievement/AchievementEditView')
'editor/article': go('editor/article/ArticleSearchView')
'editor/article/preview': go('editor/article/ArticlePreviewView')
'editor/article/:articleID': go('editor/article/ArticleEditView')
'editor/level': go('editor/level/LevelSearchView')
'editor/level/:levelID': go('editor/level/LevelEditView')
'editor/thang': go('editor/thang/ThangTypeSearchView')
'editor/thang/:thangID': go('editor/thang/ThangTypeEditView')
'editor/campaign/:campaignID': go('editor/campaign/CampaignEditorView')
2015-03-07 19:30:25 -05:00
'editor/poll': go('editor/poll/PollSearchView')
'editor/poll/:articleID': go('editor/poll/PollEditView')
'employers': go('EmployersView')
'file/*path': 'routeToServer'
2014-08-15 10:20:45 -04:00
'github/*path': 'routeToServer'
2015-11-03 19:41:06 -05:00
'hoc': go('courses/HourOfCodeView')
'i18n': go('i18n/I18NHomeView')
'i18n/thang/:handle': go('i18n/I18NEditThangTypeView')
'i18n/component/:handle': go('i18n/I18NEditComponentView')
'i18n/level/:handle': go('i18n/I18NEditLevelView')
'i18n/achievement/:handle': go('i18n/I18NEditAchievementView')
'i18n/campaign/:handle': go('i18n/I18NEditCampaignView')
2015-03-07 19:30:25 -05:00
'i18n/poll/:handle': go('i18n/I18NEditPollView')
2015-04-30 16:35:21 -04:00
'identify': go('user/IdentifyView')
'legal': go('LegalView')
'multiplayer': go('MultiplayerView')
'play(/)': go('play/CampaignView') # extra slash is to get Facebook app to work
'play/ladder/:levelID/:leagueType/:leagueID': go('ladder/LadderView')
'play/ladder/:levelID': go('ladder/LadderView')
'play/ladder': go('ladder/MainLadderView')
'play/level/:levelID': go('play/level/PlayLevelView')
'play/spectate/:levelID': go('play/SpectateView')
'play/:map': go('play/CampaignView')
'preview': go('HomeView')
'teachers': go('TeachersView')
'teachers/freetrial': go('TeachersFreeTrialView')
'test(/*subpath)': go('TestView')
'user/:slugOrID': go('user/MainUserView')
#'user/:slugOrID/profile': go('user/JobProfileView')
'user/:slugOrID/profile': go('EmployersView') # Show the not-recruiting-now screen
'*name/': 'removeTrailingSlash'
2015-06-24 17:46:59 -04:00
'*name': go('NotFoundView')
routeToServer: (e) ->
window.location.href = window.location.href
removeTrailingSlash: (e) ->
@navigate e, {trigger: true}
routeDirectly: (path, args) ->
path = "views/#{path}" if not _.string.startsWith(path, 'views/')
ViewClass = @tryToLoadModule path
if not ViewClass and application.moduleLoader.load(path)
@listenToOnce application.moduleLoader, 'load-complete', ->
@routeDirectly(path, args)
return
return @openView @notFoundView() if not ViewClass
view = new ViewClass({}, args...) # options, then any path fragment args
view.render()
@openView(view)
tryToLoadModule: (path) ->
try
return require(path)
catch error
if error.toString().search('Cannot find module "' + path + '" from') is -1
throw error
2014-01-03 13:32:13 -05:00
openView: (view) ->
@closeCurrentView()
$('#page-container').empty().append view.el
window.currentView = view
@activateTab()
@renderLoginButtons() if view.usesSocialMedia
2014-01-03 13:32:13 -05:00
view.afterInsert()
view.didReappear()
closeCurrentView: ->
if window.currentView?.reloadOnClose
return document.location.reload()
window.currentModal?.hide?()
return unless window.currentView?
window.currentView.destroy()
$('.popover').popover 'hide'
2014-01-03 13:32:13 -05:00
onGPlusAPILoaded: =>
@renderLoginButtons()
initializeSocialMediaServices: ->
return if application.testing or application.demoing
require('core/services/facebook')()
require('core/services/google')()
require('core/services/twitter')()
2014-01-03 13:32:13 -05:00
renderLoginButtons: =>
@initializeSocialMediaServices()
$('.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',
2015-02-13 14:18:00 -05:00
scope: 'https://www.googleapis.com/auth/plus.login email',
2014-06-30 22:16:26 -04:00
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
activateTab: ->
base = _.string.words(document.location.pathname[1..], '/')[0]
$("ul.nav li.#{base}").addClass('active')
_trackPageView: ->
window.tracker?.trackPageView()
2014-01-03 13:32:13 -05:00
onNavigate: (e) ->
if _.isString e.viewClass
ViewClass = @tryToLoadModule e.viewClass
if not ViewClass and application.moduleLoader.load(e.viewClass)
@listenToOnce application.moduleLoader, 'load-complete', ->
@onNavigate(e)
return
e.viewClass = ViewClass
2014-01-03 13:32:13 -05:00
manualView = e.view or e.viewClass
if (e.route is document.location.pathname) and not manualView
return document.location.reload()
@navigate e.route, {trigger: not manualView}
@_trackPageView()
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
navigate: (fragment, options) ->
super fragment, options
Backbone.Mediator.publish 'router:navigated', route: fragment