Created
October 3, 2017 19:59
-
-
Save jgerigmeyer/2dd9113b72b1254b27dcc8ccafb14726 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import BB from 'backbone'; | |
import Mnt from 'backbone.marionette'; | |
require('_404.njk'); | |
export default function(App) { | |
const Controller = (App.Controller = {}); | |
Controller.URLS = { | |
home: () => '/', | |
login: () => '/accounts/login', | |
register: () => '/accounts/register', | |
reset: () => '/accounts/reset', | |
accounts: () => '/accounts/manage', | |
organization: org => `/orgs/${org}`, | |
project: (org, project) => `/orgs/${org}/projects/${project}`, | |
scenario: (org, project, scenario) => | |
`/orgs/${org}/projects/${project}/scenarios/${scenario}`, | |
}; | |
Controller.Router = Mnt.AppRouter.extend({ | |
appRoutes: { | |
'': 'showDefault', | |
'accounts/login(/)': 'showLogin', | |
'accounts/register(/)': 'showRegister', | |
'accounts/reset(/)': 'showReset', | |
'accounts/reset-confirm/:uid/:token(/)': 'confirmReset', | |
'accounts/confirm/:email/:token(/)': 'confirmEmail', | |
'accounts/manage(/)': 'showAccounts', | |
'orgs/:orgSlug(/)': 'showOrganization', | |
'orgs/:orgSlug/projects/:projectId(/)': 'showProject', | |
'orgs/:orgSlug/projects/:projectId/scenarios/:scenarioId(/)': | |
'showScenario', | |
'*path': 'show404', | |
}, | |
}); | |
/* eslint-disable class-methods-use-this */ | |
Controller.Controller = class { | |
isAuthenticated() { | |
return typeof App.Auth.me !== 'undefined'; | |
} | |
goToHome() { | |
BB.history.navigate(Controller.URLS.home(), { trigger: true }); | |
return false; | |
} | |
goToLogin() { | |
BB.history.navigate(Controller.URLS.login(), { trigger: true }); | |
return false; | |
} | |
getOrg(slug) { | |
const dfd = $.Deferred(); | |
if (slug) { | |
const org = App.Auth.me.organization; | |
org.fetched.done(() => { | |
if (slug === org.get('slug')) { | |
dfd.resolve(org); | |
} else { | |
// If we don't recognize the requested org slug, | |
// try to fetch it to see if the ID matches the known org. | |
// This can happen if the slug has changed, | |
// and we're using an old-slug URL. | |
$.get(window.URLS['organization-detail'](slug)) | |
.done(resp => { | |
if (resp.id === org.id) { | |
dfd.resolve(org); | |
} else { | |
dfd.reject(); | |
} | |
}) | |
.fail(() => { | |
dfd.reject(); | |
}); | |
} | |
}); | |
} else { | |
dfd.reject(); | |
} | |
return dfd.promise(); | |
} | |
getProject(orgSlug, projectId) { | |
const dfd = $.Deferred(); | |
this.getOrg(orgSlug) | |
.done(org => { | |
if (projectId) { | |
org.projects.fetched.done(() => { | |
const project = org.projects.get(projectId); | |
if (project) { | |
if (project.scenarios.fetched.state() !== 'resolved') { | |
// Fetch scenarios | |
project.fetchNestedCollection('scenarios'); | |
} | |
if (project.categories.fetched.state() !== 'resolved') { | |
// Fetch categories | |
project.fetchNestedCollection('categories'); | |
} | |
dfd.resolve(org, project); | |
} else { | |
dfd.reject(); | |
} | |
}); | |
} else { | |
dfd.reject(); | |
} | |
}) | |
.fail(() => { | |
dfd.reject(); | |
}); | |
return dfd.promise(); | |
} | |
getScenario(orgSlug, projectId, scenarioId) { | |
const dfd = $.Deferred(); | |
this.getProject(orgSlug, projectId) | |
.done((org, project) => { | |
if (scenarioId) { | |
project.scenarios.fetched.done(() => { | |
const scenario = project.scenarios.get(scenarioId); | |
if (scenario) { | |
if (scenario.experiences.fetched.state() !== 'resolved') { | |
// Fetch experiences | |
scenario.fetchNestedCollection('experiences'); | |
} | |
if (scenario.schedules.fetched.state() !== 'resolved') { | |
// Fetch schedules | |
scenario.fetchNestedCollection('schedules'); | |
} | |
dfd.resolve(org, project, scenario); | |
} else { | |
dfd.reject(); | |
} | |
}); | |
} else { | |
dfd.reject(); | |
} | |
}) | |
.fail(() => { | |
dfd.reject(); | |
}); | |
return dfd.promise(); | |
} | |
show404() { | |
App.showView(new App.BaseViews.StaticView({ tpl: '_404', title: '404' })); | |
} | |
showDefault() { | |
if (!this.isAuthenticated()) { | |
return this.goToLogin(); | |
} | |
const org = App.Auth.me.organization; | |
org.fetched.done(() => { | |
BB.history.navigate(Controller.URLS.organization(org.get('slug')), { | |
trigger: true, | |
}); | |
}); | |
return true; | |
} | |
showLogin() { | |
if (this.isAuthenticated()) { | |
return this.goToHome(); | |
} | |
App.showView(new App.Login.View()); | |
return true; | |
} | |
showRegister() { | |
if (this.isAuthenticated()) { | |
return this.goToHome(); | |
} | |
App.showView(new App.Register.View()); | |
return true; | |
} | |
showReset() { | |
if (this.isAuthenticated()) { | |
return this.goToHome(); | |
} | |
App.showView(new App.Reset.Request()); | |
return true; | |
} | |
confirmReset(uid, token) { | |
const url = window.URLS['users:password-reset-confirm'](uid, token); | |
$.get(url) | |
.done(() => { | |
App.showView(new App.Reset.Confirm({ uid, token })); | |
}) | |
.fail(xhr => { | |
if (xhr.status === 400) { | |
App.addMsg(App.Reset.invalid_token_msg, 'error'); | |
} else { | |
App.ajaxError(xhr); | |
} | |
BB.history.navigate(Controller.URLS.reset(), { trigger: true }); | |
}); | |
} | |
confirmEmail(email, token) { | |
const url = window.URLS['users:confirm'](email, token); | |
$.post(url) | |
.done(() => { | |
App.addMsg( | |
'Success! Your email address has been verified.', | |
'success', | |
); | |
if (App.Auth.me) { | |
App.Auth.me.set('email_confirmed', true); | |
} | |
this.goToHome(); | |
}) | |
.fail(xhr => { | |
if (xhr.status === 400) { | |
App.addMsg( | |
'Your confirmation link is invalid or expired.', | |
'error', | |
); | |
} else { | |
App.ajaxError(xhr); | |
} | |
BB.history.navigate(Controller.URLS.register(), { trigger: true }); | |
}); | |
} | |
showAccounts() { | |
if (!this.isAuthenticated()) { | |
return this.goToLogin(); | |
} | |
App.showView(new App.Accounts.Layout()); | |
return true; | |
} | |
showOrganization(orgSlug) { | |
if (!this.isAuthenticated()) { | |
return this.goToLogin(); | |
} | |
this.getOrg(orgSlug) | |
.done(org => { | |
App.showView(new App.Organizations.Layout({ model: org })); | |
}) | |
.fail(this.show404); | |
return true; | |
} | |
showProject(orgSlug, projectId) { | |
if (!this.isAuthenticated()) { | |
return this.goToLogin(); | |
} | |
this.getProject(orgSlug, projectId) | |
.done((org, project) => { | |
App.showView(new App.Organizations.Layout({ model: org, project })); | |
}) | |
.fail(this.show404); | |
return true; | |
} | |
showScenario(orgSlug, projectId, scenarioId) { | |
if (!this.isAuthenticated()) { | |
return this.goToLogin(); | |
} | |
this.getScenario(orgSlug, projectId, scenarioId) | |
.done((org, project, scenario) => { | |
App.showView( | |
new App.Scenarios.DetailLayout({ | |
model: scenario, | |
collection: project.scenarios, | |
organization: org, | |
project, | |
}), | |
); | |
}) | |
.fail(this.show404); | |
return true; | |
} | |
}; | |
/* eslint-enable class-methods-use-this */ | |
const controller = new Controller.Controller(); | |
controller.router = new Controller.Router({ controller }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment