2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2021-03-08 12:15:54 +00:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { action } from '@ember/object';
|
|
|
|
import { tracked } from '@glimmer/tracking';
|
|
|
|
|
2022-02-18 17:16:03 +00:00
|
|
|
const templateRe = /\${([A-Za-z.0-9_-]+)}/g;
|
2021-03-08 12:15:54 +00:00
|
|
|
export default class RouteComponent extends Component {
|
|
|
|
@service('routlet') routlet;
|
2021-09-15 18:50:11 +00:00
|
|
|
@service('router') router;
|
2022-02-18 17:16:03 +00:00
|
|
|
@service('intl') intl;
|
|
|
|
@service('encoder') encoder;
|
2021-03-08 12:15:54 +00:00
|
|
|
|
2021-10-07 11:38:04 +00:00
|
|
|
@tracked _model;
|
2021-03-08 12:15:54 +00:00
|
|
|
|
2022-02-18 17:16:03 +00:00
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
2022-09-15 08:43:17 +00:00
|
|
|
this.intlKey = this.encoder.createRegExpEncoder(templateRe, (_) => _);
|
2022-02-18 17:16:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 16:43:46 +00:00
|
|
|
get params() {
|
|
|
|
return this.routlet.paramsFor(this.args.name);
|
|
|
|
}
|
|
|
|
|
2021-10-07 11:38:04 +00:00
|
|
|
get model() {
|
2022-02-18 17:16:03 +00:00
|
|
|
if (this._model) {
|
2022-01-07 19:16:21 +00:00
|
|
|
return this._model;
|
2021-10-07 11:38:04 +00:00
|
|
|
}
|
2022-01-07 19:16:21 +00:00
|
|
|
if (this.args.name) {
|
|
|
|
const outlet = this.routlet.outletFor(this.args.name);
|
2022-09-15 08:43:17 +00:00
|
|
|
|
|
|
|
if (outlet) {
|
|
|
|
return this.routlet.modelFor(outlet.name);
|
|
|
|
}
|
2022-01-07 19:16:21 +00:00
|
|
|
}
|
|
|
|
return undefined;
|
2021-10-07 11:38:04 +00:00
|
|
|
}
|
2022-06-09 17:15:52 +00:00
|
|
|
|
|
|
|
@action
|
|
|
|
exists(str) {
|
|
|
|
return this.routlet.exists(`${this.args.name}.${str}`);
|
|
|
|
}
|
|
|
|
|
2022-02-18 17:16:03 +00:00
|
|
|
@action
|
|
|
|
t(str, options) {
|
|
|
|
if (str.includes('${')) {
|
|
|
|
str = this.intlKey(str, options);
|
|
|
|
}
|
|
|
|
return this.intl.t(`routes.${this.args.name}.${str}`, options);
|
|
|
|
}
|
2021-10-07 11:38:04 +00:00
|
|
|
|
2021-03-08 12:15:54 +00:00
|
|
|
@action
|
|
|
|
connect() {
|
|
|
|
this.routlet.addRoute(this.args.name, this);
|
|
|
|
}
|
2021-04-06 12:40:40 +00:00
|
|
|
|
2021-03-08 12:15:54 +00:00
|
|
|
@action
|
|
|
|
disconnect() {
|
|
|
|
this.routlet.removeRoute(this.args.name, this);
|
|
|
|
}
|
|
|
|
}
|