DEV: Convert nav-item to gjs (#24076)

This commit is contained in:
Jarek Radosz 2023-10-24 17:55:09 +02:00 committed by GitHub
parent 9a497d9e22
commit 930dc38500
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 46 deletions

View File

@ -0,0 +1,47 @@
/* You might be looking for navigation-item. */
import Component from "@glimmer/component";
import { LinkTo } from "@ember/routing";
import { inject as service } from "@ember/service";
import { htmlSafe } from "@ember/template";
import getURL from "discourse-common/lib/get-url";
import { iconHTML } from "discourse-common/lib/icon-library";
import I18n from "discourse-i18n";
export default class NavItem extends Component {
@service router;
get contents() {
const text = this.args.i18nLabel || I18n.t(this.args.label);
if (this.args.icon) {
return htmlSafe(`${iconHTML(this.args.icon)} ${text}`);
}
return text;
}
get active() {
if (!this.args.route) {
return;
}
if (this.args.routeParam && this.router.currentRoute) {
return this.router.currentRoute.params.filter === this.args.routeParam;
}
return this.router.isActive(this.args.route);
}
<template>
<li class={{if this.active "active"}}>
{{#if @routeParam}}
<LinkTo
@route={{@route}}
@model={{@routeParam}}
>{{this.contents}}</LinkTo>
{{else if @route}}
<LinkTo @route={{@route}}>{{this.contents}}</LinkTo>
{{else}}
<a href={{getURL @path}} data-auto-route="true">{{this.contents}}</a>
{{/if}}
</li>
</template>
}

View File

@ -1,10 +0,0 @@
{{#if this.routeParam}}
<LinkTo
@route={{this.route}}
@model={{this.routeParam}}
>{{this.contents}}</LinkTo>
{{else if this.route}}
<LinkTo @route={{this.route}}>{{this.contents}}</LinkTo>
{{else}}
<a href={{get-url this.path}} data-auto-route="true">{{this.contents}}</a>
{{/if}}

View File

@ -1,36 +0,0 @@
import Component from "@ember/component";
/* You might be looking for navigation-item. */
import { inject as service } from "@ember/service";
import { htmlSafe } from "@ember/template";
import { iconHTML } from "discourse-common/lib/icon-library";
import discourseComputed from "discourse-common/utils/decorators";
import I18n from "discourse-i18n";
export default Component.extend({
tagName: "li",
classNameBindings: ["active"],
router: service(),
@discourseComputed("label", "i18nLabel", "icon")
contents(label, i18nLabel, icon) {
let text = i18nLabel || I18n.t(label);
if (icon) {
return htmlSafe(`${iconHTML(icon)} ${text}`);
}
return text;
},
@discourseComputed("route", "router.currentRoute")
active(route, currentRoute) {
if (!route) {
return;
}
const routeParam = this.routeParam;
if (routeParam && currentRoute) {
return currentRoute.params["filter"] === routeParam;
}
return this.router.isActive(route);
},
});