2017-12-15 21:39:18 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2018-08-02 22:56:11 +00:00
|
|
|
import { next } from '@ember/runloop';
|
2018-08-08 20:49:04 +00:00
|
|
|
import Route from '@ember/routing/route';
|
2019-10-15 18:32:58 +00:00
|
|
|
import { AbortError } from '@ember-data/adapter/error';
|
2018-08-09 02:34:56 +00:00
|
|
|
import RSVP from 'rsvp';
|
2020-06-11 21:23:00 +00:00
|
|
|
import { action } from '@ember/object';
|
|
|
|
import classic from 'ember-classic-decorator';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2020-06-11 21:23:00 +00:00
|
|
|
@classic
|
|
|
|
export default class ApplicationRoute extends Route {
|
|
|
|
@service config;
|
|
|
|
@service system;
|
|
|
|
@service store;
|
|
|
|
@service token;
|
2017-10-23 17:21:44 +00:00
|
|
|
|
2020-06-11 21:23:00 +00:00
|
|
|
queryParams = {
|
2018-08-04 01:18:51 +00:00
|
|
|
region: {
|
|
|
|
refreshModel: true,
|
|
|
|
},
|
2020-06-11 21:23:00 +00:00
|
|
|
};
|
2018-08-04 01:18:51 +00:00
|
|
|
|
2017-09-28 17:04:33 +00:00
|
|
|
resetController(controller, isExiting) {
|
|
|
|
if (isExiting) {
|
|
|
|
controller.set('error', null);
|
|
|
|
}
|
2020-06-11 21:23:00 +00:00
|
|
|
}
|
2017-09-28 17:04:33 +00:00
|
|
|
|
2018-08-08 20:49:04 +00:00
|
|
|
beforeModel(transition) {
|
2020-01-20 20:57:01 +00:00
|
|
|
const fetchSelfTokenAndPolicies = this.get('token.fetchSelfTokenAndPolicies')
|
|
|
|
.perform()
|
|
|
|
.catch();
|
|
|
|
|
|
|
|
return RSVP.all([
|
|
|
|
this.get('system.regions'),
|
|
|
|
this.get('system.defaultRegion'),
|
|
|
|
fetchSelfTokenAndPolicies,
|
|
|
|
]).then(promises => {
|
|
|
|
if (!this.get('system.shouldShowRegions')) return promises;
|
|
|
|
|
|
|
|
const queryParam = transition.to.queryParams.region;
|
|
|
|
const defaultRegion = this.get('system.defaultRegion.region');
|
|
|
|
const currentRegion = this.get('system.activeRegion') || defaultRegion;
|
|
|
|
|
|
|
|
// Only reset the store if the region actually changed
|
|
|
|
if (
|
|
|
|
(queryParam && queryParam !== currentRegion) ||
|
|
|
|
(!queryParam && currentRegion !== defaultRegion)
|
|
|
|
) {
|
|
|
|
this.system.reset();
|
|
|
|
this.store.unloadAll();
|
2018-08-09 02:34:56 +00:00
|
|
|
}
|
2020-01-20 20:57:01 +00:00
|
|
|
|
|
|
|
this.set('system.activeRegion', queryParam || defaultRegion);
|
|
|
|
|
|
|
|
return promises;
|
|
|
|
});
|
2020-06-11 21:23:00 +00:00
|
|
|
}
|
2018-08-02 22:56:11 +00:00
|
|
|
|
2018-08-13 23:18:06 +00:00
|
|
|
// Model is being used as a way to transfer the provided region
|
|
|
|
// query param to update the controller state.
|
|
|
|
model(params) {
|
|
|
|
return params.region;
|
2020-06-11 21:23:00 +00:00
|
|
|
}
|
2018-08-13 23:18:06 +00:00
|
|
|
|
|
|
|
setupController(controller, model) {
|
|
|
|
const queryParam = model;
|
2018-08-10 01:12:26 +00:00
|
|
|
|
|
|
|
if (queryParam === this.get('system.defaultRegion.region')) {
|
|
|
|
next(() => {
|
|
|
|
controller.set('region', null);
|
2018-08-08 20:49:04 +00:00
|
|
|
});
|
2018-08-10 01:12:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 21:23:00 +00:00
|
|
|
return super.setupController(...arguments);
|
|
|
|
}
|
2018-08-02 22:56:11 +00:00
|
|
|
|
2020-06-11 21:23:00 +00:00
|
|
|
@action
|
|
|
|
didTransition() {
|
|
|
|
if (!this.get('config.isTest')) {
|
|
|
|
window.scrollTo(0, 0);
|
|
|
|
}
|
|
|
|
}
|
2017-09-28 17:04:33 +00:00
|
|
|
|
2020-06-11 21:23:00 +00:00
|
|
|
@action
|
|
|
|
willTransition() {
|
|
|
|
this.controllerFor('application').set('error', null);
|
|
|
|
}
|
2017-10-12 23:56:20 +00:00
|
|
|
|
2020-06-11 21:23:00 +00:00
|
|
|
@action
|
|
|
|
error(error) {
|
|
|
|
if (!(error instanceof AbortError)) {
|
|
|
|
this.controllerFor('application').set('error', error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|