Files
threshold-ui/ui/admin/app/routes/application.js
T

118 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-05-19 18:10:39 -04:00
import Route from '@ember/routing/route';
2020-07-24 17:49:42 -04:00
import { getOwner } from '@ember/application';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
2022-01-03 09:28:24 -05:00
import { loading } from 'ember-loading';
import { A } from '@ember/array';
2020-05-19 18:10:39 -04:00
/**
* Entry route for the application.
*/
2021-02-26 12:04:12 -05:00
export default class ApplicationRoute extends Route {
2020-07-24 17:49:42 -04:00
// =services
@service session;
2020-10-15 16:45:38 -04:00
@service confirm;
@service router;
2020-07-24 17:49:42 -04:00
// =attributes
routeIfUnauthenticated = 'index';
// =methods
constructor() {
super(...arguments);
/**
* If user attempts to navigate away from unsaved changes, the user is
* asked to confirm that they would like to discard the changes. If the
* user chooses discard, changes on the model are rolled back and the
* transition is retried. If the user cancels discard, the transition is
* aborted.
*/
this.router.on('routeWillChange', async (transition) => {
const fromName = transition?.from?.name;
const toName = transition?.to?.name;
const maybeModel = transition?.from?.attributes;
if (fromName !== toName && maybeModel?.hasDirtyAttributes) {
transition.abort();
try {
await this.confirm.confirm('abandon', { isAbandonConfirm: true });
maybeModel?.rollbackAttributes();
transition.retry();
} catch (e) {
// if user denies, do nothing
}
}
});
}
2020-11-04 13:51:37 -05:00
beforeModel() {
const theme = this.session.get('data.theme');
this.toggleTheme(theme);
}
2020-07-24 17:49:42 -04:00
// =actions
/**
* Delegates invalidation to the session service.
*/
@action
invalidateSession() {
this.session.invalidate();
}
2020-09-08 17:35:02 -04:00
/**
* Hooks into ember-loading to kick off loading indicator in the
* application template.
2020-09-25 18:25:04 -04:00
* @return {boolean} always returns true
*/
@action
@loading
loading() {
return true;
}
/**
* Invalidates the session if a 401 error occurs and returns false to
* prevent further error handling.
* Returns true in all other cases, allowing error handling to occur (such
* as displaying the `error.hbs` template, if one exists).
* @param {Error} e
*/
2020-09-08 17:35:02 -04:00
@action
error(e) {
const isUnauthenticated = A(e?.errors)?.firstObject?.isUnauthenticated;
2020-09-08 17:35:02 -04:00
if (isUnauthenticated) {
this.session.invalidate();
return false;
}
return true;
2020-09-08 17:35:02 -04:00
}
2020-10-15 16:45:38 -04:00
2020-11-04 13:51:37 -05:00
/**
* Applies the specified color theme to the root ember element.
* @param {string} theme - "light", "dark", or nullish (system default)
*/
@action
toggleTheme(theme) {
const rootElementSelector = getOwner(this).rootElement;
2020-11-16 15:08:59 -05:00
const rootEl = getOwner(this)
.lookup('service:-document')
2020-11-04 13:51:37 -05:00
.querySelector(rootElementSelector);
this.session.set('data.theme', theme);
switch (theme) {
case 'light':
rootEl.classList.add('rose-theme-light');
rootEl.classList.remove('rose-theme-dark');
break;
case 'dark':
rootEl.classList.add('rose-theme-dark');
rootEl.classList.remove('rose-theme-light');
break;
default:
rootEl.classList.remove('rose-theme-dark');
rootEl.classList.remove('rose-theme-light');
}
}
2020-07-24 17:49:42 -04:00
}