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';
|
2018-03-08 18:39:22 +00:00
|
|
|
import { AbortError } from 'ember-data/adapters/errors';
|
2018-08-09 02:34:56 +00:00
|
|
|
import RSVP from 'rsvp';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2017-09-26 23:37:08 +00:00
|
|
|
export default Route.extend({
|
2017-12-15 21:39:18 +00:00
|
|
|
config: service(),
|
2018-08-02 22:56:11 +00:00
|
|
|
system: service(),
|
2018-08-10 01:12:26 +00:00
|
|
|
store: service(),
|
2017-10-23 17:21:44 +00:00
|
|
|
|
2018-08-04 01:18:51 +00:00
|
|
|
queryParams: {
|
|
|
|
region: {
|
|
|
|
refreshModel: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2017-09-28 17:04:33 +00:00
|
|
|
resetController(controller, isExiting) {
|
|
|
|
if (isExiting) {
|
|
|
|
controller.set('error', null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-08-08 20:49:04 +00:00
|
|
|
beforeModel(transition) {
|
2018-08-09 02:34:56 +00:00
|
|
|
return RSVP.all([this.get('system.regions'), this.get('system.defaultRegion')]).then(
|
|
|
|
promises => {
|
2018-08-09 02:39:32 +00:00
|
|
|
if (!this.get('system.shouldShowRegions')) return promises;
|
|
|
|
|
2018-08-09 02:34:56 +00:00
|
|
|
const queryParam = transition.queryParams.region;
|
|
|
|
const defaultRegion = this.get('system.defaultRegion.region');
|
2018-08-10 01:12:26 +00:00
|
|
|
const currentRegion = this.get('system.activeRegion') || defaultRegion;
|
2018-08-02 22:56:11 +00:00
|
|
|
|
2018-08-10 01:12:26 +00:00
|
|
|
// Only reset the store if the region actually changed
|
|
|
|
if (
|
|
|
|
(queryParam && queryParam !== currentRegion) ||
|
|
|
|
(!queryParam && currentRegion !== defaultRegion)
|
|
|
|
) {
|
|
|
|
this.get('system').reset();
|
|
|
|
this.get('store').unloadAll();
|
2018-08-09 02:34:56 +00:00
|
|
|
}
|
2018-08-08 20:49:04 +00:00
|
|
|
|
2018-08-10 01:12:26 +00:00
|
|
|
this.set('system.activeRegion', queryParam || defaultRegion);
|
|
|
|
|
2018-08-09 02:34:56 +00:00
|
|
|
return promises;
|
|
|
|
}
|
|
|
|
);
|
2018-08-02 22:56:11 +00:00
|
|
|
},
|
|
|
|
|
2018-08-09 02:34:56 +00:00
|
|
|
// setupController doesn't refire when the model hook refires as part of
|
|
|
|
// a query param change
|
2018-08-10 01:12:26 +00:00
|
|
|
afterModel(model, transition) {
|
|
|
|
const queryParam = transition.queryParams.region;
|
2018-08-09 02:34:56 +00:00
|
|
|
const controller = this.controllerFor('application');
|
2018-08-10 01:12:26 +00:00
|
|
|
|
|
|
|
// The default region shouldn't show up as a query param since
|
|
|
|
// it's superfluous.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-02 22:56:11 +00:00
|
|
|
return this._super(...arguments);
|
|
|
|
},
|
|
|
|
|
2017-09-26 23:37:08 +00:00
|
|
|
actions: {
|
|
|
|
didTransition() {
|
2017-10-23 17:21:44 +00:00
|
|
|
if (!this.get('config.isTest')) {
|
|
|
|
window.scrollTo(0, 0);
|
|
|
|
}
|
2017-09-26 23:37:08 +00:00
|
|
|
},
|
2017-09-28 17:04:33 +00:00
|
|
|
|
2017-10-12 23:56:20 +00:00
|
|
|
willTransition() {
|
|
|
|
this.controllerFor('application').set('error', null);
|
|
|
|
},
|
|
|
|
|
2017-09-28 17:04:33 +00:00
|
|
|
error(error) {
|
2018-03-08 18:39:22 +00:00
|
|
|
if (!(error instanceof AbortError)) {
|
|
|
|
this.controllerFor('application').set('error', error);
|
|
|
|
}
|
2017-09-28 17:04:33 +00:00
|
|
|
},
|
2017-09-26 23:37:08 +00:00
|
|
|
},
|
|
|
|
});
|