Custom Top Navigation Links を参考にしながら、ほぼ完成させることができました。
<script type="text/discourse-plugin" version="0.8.18">
if (I18n.translations.en) {
I18n.translations.en.js.filters.needsreply = {title: "Needs Reply", help: "Unanswered Topics"};
}
// これらの値をカスタマイズしてください
// 検索クエリを設定
let search_query = '?max_posts=1';
// 特定のカテゴリから「返信が必要」メニュー項目を除外
let excludeList = ['pitches', 'weekly-recap', 'staff'];
api.addNavigationBarItem({
name: "needsreply",
displayName:"Needs Reply",
title: "Needs Reply",
href: "/latest" + search_query
});
// 「返信が必要」ボタンにアクティブクラスを追加
api.modifyClass('component:navigation-item', {
active: Ember.computed("contentFilterMode", "filterMode", function () {
let contentFilterMode = this.get('content').get('filterMode');
console.log(window.location.search.split('&')[0], search_query.split('&')[0])
if (window.location.search && window.location.search.split('&')[0] === search_query.split('&')[0]) {
return contentFilterMode === "needsreply";
} else {
return this._super(contentFilterMode, this.get('filterMode'));
}
}),
});
// max_posts フィルタとタグフィルタを削除
api.modifyClass('controller:discovery/topics', {
resetParams: function () {
this.setProperties({max_posts: null});
this.setProperties({tags: null});
this._super();
}
});
</script>
唯一、このスクリプト内で現在のカテゴリにアクセスする方法がわからず、困っています。
元のコードでは、Discourse.NavItem を使用して現在のカテゴリにアクセスできます。このコードに基づき、リンクの動作を動的に変更(非表示にする、またはこのカテゴリ内の投稿のみを対象とするように href を変更するなど)しています。
上記のスクリプトに現在のカテゴリを注入し、ユーザーが閲覧しているカテゴリに応じてリンクの動作をカスタマイズできるような解決策をご提案いただけないでしょうか?