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

140 lines
3.4 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
2020-11-12 13:22:16 -05:00
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
2020-11-13 12:11:12 -05:00
import { A } from '@ember/array';
import { getOwner } from '@ember/application';
2022-01-03 09:28:24 -05:00
import { loading } from 'ember-loading';
2020-11-12 13:22:16 -05:00
2021-02-26 12:04:12 -05:00
export default class ApplicationRoute extends Route {
2020-11-12 13:22:16 -05:00
// =services
@service session;
2022-07-27 10:25:01 -07:00
@service clusterUrl;
2021-02-12 13:52:58 -05:00
@service ipc;
2022-11-07 16:18:02 -08:00
@service intl;
2020-11-12 13:22:16 -05:00
// =attributes
2020-11-13 15:20:26 -05:00
/**
* @type {string}
*/
2020-11-12 13:22:16 -05:00
routeIfUnauthenticated = 'index';
2020-11-13 15:20:26 -05:00
/**
2022-07-27 10:25:01 -07:00
* Check that the clusterUrl specified in the renderer matches the clusterUrl
2020-11-13 15:20:26 -05:00
* reported by the main process. If they differ, update the main process
2022-07-27 10:25:01 -07:00
* clusterUrl so that the renderer's CSP can be rewritten to allow requests.
2020-11-13 15:20:26 -05:00
*/
async beforeModel() {
2022-11-07 16:18:02 -08:00
this.intl.setLocale(['en-us']);
await this.session.setup();
2021-02-12 15:42:22 -05:00
const theme = this.session.get('data.theme');
this.toggleTheme(theme);
2022-07-27 10:25:01 -07:00
return this.clusterUrl.updateClusterUrl();
2020-11-13 15:20:26 -05:00
}
/**
* Add window frame config on controller.
*/
2021-06-06 11:29:23 -04:00
async setupController(controller) {
2021-09-16 10:33:56 -04:00
controller.set('hasMacOSChrome', await this.ipc.invoke('hasMacOSChrome'));
controller.set(
'showWindowActions',
await this.ipc.invoke('showWindowActions')
2021-09-16 10:33:56 -04:00
);
}
2020-11-12 13:22:16 -05:00
// =actions
/**
* Delegates invalidation to the session service.
*/
@action
invalidateSession() {
this.session.invalidate();
}
2021-02-11 19:01:45 -05:00
/**
2022-07-27 10:25:01 -07:00
* Disconnects from clusterUrl and invalidates session, thereby resetting
* the client and reloading to the onboarding clusterUrl screen.
2021-02-11 19:01:45 -05:00
*/
@action
disconnect() {
2022-07-27 10:25:01 -07:00
this.clusterUrl.resetClusterUrl();
2021-02-11 19:01:45 -05:00
this.invalidateSession();
}
2020-11-12 15:13:28 -05:00
/**
* Hooks into ember-loading to kick off loading indicator in the
* application template.
* @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
*/
@action
error(e) {
const isUnauthenticated = A(e?.errors)?.firstObject?.isUnauthenticated;
if (isUnauthenticated) {
this.session.invalidate();
return false;
}
return true;
}
2021-02-09 11:16:03 -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;
const rootEl = getOwner(this)
.lookup('service:-document')
.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');
}
}
@action
minimize() {
this.ipc.invoke('minimizeWindow');
}
@action
toggleFullScreen() {
this.ipc.invoke('toggleFullscreenWindow');
}
@action
close() {
this.ipc.invoke('closeWindow');
}
2020-11-12 13:22:16 -05:00
}