2020-10-01 08:33:22 +00:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import { tracked } from '@glimmer/tracking';
|
|
|
|
import { action } from '@ember/object';
|
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
|
|
|
|
class State {
|
|
|
|
constructor(name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
matches(match) {
|
|
|
|
return this.name === match;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Outlet extends Component {
|
2021-03-08 12:15:54 +00:00
|
|
|
@service('routlet') routlet;
|
2020-10-01 08:33:22 +00:00
|
|
|
@service('router') router;
|
|
|
|
|
2021-03-08 12:15:54 +00:00
|
|
|
@tracked element;
|
|
|
|
@tracked routeName;
|
2020-10-01 08:33:22 +00:00
|
|
|
@tracked state;
|
|
|
|
@tracked previousState;
|
2021-03-08 12:15:54 +00:00
|
|
|
@tracked endTransition;
|
2020-10-01 08:33:22 +00:00
|
|
|
|
2021-03-08 12:15:54 +00:00
|
|
|
get model() {
|
|
|
|
return this.args.model || {};
|
2020-10-01 08:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setAppRoute(name) {
|
2020-10-19 16:29:09 +00:00
|
|
|
const nspace = 'nspace.';
|
|
|
|
if (name.startsWith(nspace)) {
|
|
|
|
name = name.substr(nspace.length);
|
2020-10-01 08:33:22 +00:00
|
|
|
}
|
|
|
|
if (name !== 'loading') {
|
2021-03-08 12:15:54 +00:00
|
|
|
const doc = this.element.ownerDocument.documentElement;
|
2020-10-01 08:33:22 +00:00
|
|
|
if (doc.classList.contains('ember-loading')) {
|
|
|
|
doc.classList.remove('ember-loading');
|
|
|
|
}
|
|
|
|
doc.dataset.route = name;
|
|
|
|
this.setAppState('idle');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setAppState(state) {
|
2021-03-08 12:15:54 +00:00
|
|
|
const doc = this.element.ownerDocument.documentElement;
|
|
|
|
doc.dataset.state = state;
|
2020-10-01 08:33:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 12:15:54 +00:00
|
|
|
@action
|
|
|
|
attributeChanged(prop, value) {
|
|
|
|
switch (prop) {
|
|
|
|
case 'element':
|
|
|
|
this.element = value;
|
|
|
|
if (this.args.name === 'application') {
|
|
|
|
this.setAppState('loading');
|
|
|
|
this.setAppRoute(this.router.currentRouteName);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-10-01 08:33:22 +00:00
|
|
|
|
2021-03-08 12:15:54 +00:00
|
|
|
@action transitionEnd($el) {
|
|
|
|
if (typeof this.endTransition === 'function') {
|
|
|
|
this.endTransition();
|
|
|
|
}
|
2020-10-01 08:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
startLoad(transition) {
|
2021-03-08 12:15:54 +00:00
|
|
|
const outlet = this.routlet.findOutlet(transition.to.name) || 'application';
|
2020-10-01 08:33:22 +00:00
|
|
|
if (this.args.name === outlet) {
|
|
|
|
this.previousState = this.state;
|
|
|
|
this.state = new State('loading');
|
2021-03-08 12:15:54 +00:00
|
|
|
this.endTransition = this.routlet.transition();
|
|
|
|
// if we have no transition-duration set immediately end the transition
|
|
|
|
const duration = window
|
|
|
|
.getComputedStyle(this.element)
|
|
|
|
.getPropertyValue('transition-duration');
|
|
|
|
if (parseFloat(duration) === 0) {
|
|
|
|
this.endTransition();
|
|
|
|
}
|
2020-10-01 08:33:22 +00:00
|
|
|
}
|
|
|
|
if (this.args.name === 'application') {
|
|
|
|
this.setAppState('loading');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
endLoad(transition) {
|
|
|
|
if (this.state.matches('loading')) {
|
|
|
|
this.previousState = this.state;
|
|
|
|
this.state = new State('idle');
|
|
|
|
}
|
|
|
|
if (this.args.name === 'application') {
|
|
|
|
this.setAppRoute(this.router.currentRouteName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
connect() {
|
2021-03-08 12:15:54 +00:00
|
|
|
this.routlet.addOutlet(this.args.name, this);
|
2020-10-01 08:33:22 +00:00
|
|
|
this.previousState = this.state = new State('idle');
|
|
|
|
this.router.on('routeWillChange', this.startLoad);
|
|
|
|
this.router.on('routeDidChange', this.endLoad);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
disconnect() {
|
2021-03-08 12:15:54 +00:00
|
|
|
this.routlet.removeOutlet(this.args.name);
|
2020-10-01 08:33:22 +00:00
|
|
|
this.router.off('routeWillChange', this.startLoad);
|
|
|
|
this.router.off('routeDidChange', this.endLoad);
|
|
|
|
}
|
|
|
|
}
|