|
1 | | -import { getOwner } from '@ember/application'; |
2 | | -import Transition from '@ember/routing/-private/transition'; |
3 | 1 | import EmberRouter from '@ember/routing/router'; |
4 | | -import { inject as service } from '@ember/service'; |
5 | | -import Ember from 'ember'; |
6 | 2 | import config from 'ember-get-config'; |
7 | 3 |
|
8 | | -import { Blocker } from 'ember-osf-web/services/ready'; |
9 | | -import transitionTargetURL from 'ember-osf-web/utils/transition-target-url'; |
10 | | - |
11 | 4 | const { |
12 | 5 | engines: { |
13 | 6 | collections, |
14 | 7 | registries, |
15 | 8 | }, |
16 | | - featureFlagNames: { |
17 | | - routes: routeFlags, |
18 | | - }, |
19 | 9 | } = config; |
20 | 10 |
|
21 | | -/* eslint-disable ember/no-get */ |
22 | 11 | const Router = EmberRouter.extend({ |
23 | | - currentUser: service('current-user'), |
24 | | - features: service('features'), |
25 | | - statusMessages: service('status-messages'), |
26 | | - ready: service('ready'), |
27 | | - |
28 | | - // eslint-disable-next-line ember/avoid-leaking-state-in-ember-objects |
29 | | - readyBlocker: null as Blocker | null, |
30 | 12 | location: config.locationType, |
31 | 13 | rootURL: config.rootURL, |
32 | | - shouldScrollTop: true, |
33 | | - |
34 | | - willTransition(oldInfo: any, newInfo: any, transition: { targetName: string }) { |
35 | | - if (!this.readyBlocker || this.readyBlocker.isDone()) { |
36 | | - this.readyBlocker = this.get('ready').getBlocker(); |
37 | | - } |
38 | | - |
39 | | - this._super(oldInfo, newInfo, transition); |
40 | | - }, |
41 | | - |
42 | | - didTransition(...args: any[]) { |
43 | | - this._super(...args); |
44 | | - |
45 | | - this.get('currentUser').checkShowTosConsentBanner(); |
46 | | - this.get('statusMessages').updateMessages(); |
47 | | - |
48 | | - if (this.shouldScrollTop) { |
49 | | - const { application: { rootElement: rootElementSelector } } = getOwner(this); |
50 | | - const rootElement = document.querySelector(rootElementSelector); |
51 | | - rootElement.scrollIntoView(); |
52 | | - } |
53 | | - |
54 | | - if (this.readyBlocker && !this.readyBlocker.isDone()) { |
55 | | - this.readyBlocker.done(); |
56 | | - } |
57 | | - }, |
58 | | - |
59 | | - _doTransition(...args: any[]) { |
60 | | - const transition = this._super(...args); |
61 | | - return this._beforeTransition(transition); |
62 | | - }, |
63 | | - |
64 | | - _doURLTransition(...args: any[]) { |
65 | | - const transition = this._super(...args); |
66 | | - return this._beforeTransition(transition); |
67 | | - }, |
68 | | - |
69 | | - _beforeTransition(transition: Transition) { |
70 | | - // Don't snap the page to the top if it's just a query param change |
71 | | - // IE registries, preprints, collections, etc |
72 | | - // There doesn't appear to be a good way to access the transition |
73 | | - // inside of didTransition, so the state is just plucked here for future reference. |
74 | | - this.shouldScrollTop = !transition.queryParamsOnly; |
75 | | - |
76 | | - const isInitialTransition = transition.sequence === 0; |
77 | | - if (!isInitialTransition) { |
78 | | - const flag = routeFlags[transition.targetName]; |
79 | | - if (flag && !this.get('features').isEnabled(flag)) { |
80 | | - if (!Ember.testing) { |
81 | | - try { |
82 | | - window.location.assign(transitionTargetURL(transition)); |
83 | | - } catch (e) { |
84 | | - window.location.reload(); |
85 | | - } |
86 | | - } |
87 | | - transition.abort(); |
88 | | - } |
89 | | - } |
90 | | - return transition; |
91 | | - }, |
92 | 14 | }); |
93 | | -/* eslint-enable ember/no-get */ |
94 | 15 |
|
95 | 16 | /* eslint-disable array-callback-return */ |
96 | 17 |
|
|
0 commit comments