通过查看 https://meta.discourse.org/t/custom-top-navigation-links/87225,我基本上已经完成了大部分工作。
<script type="text/discourse-plugin" version="0.8.18">
if (I18n.translations.en) {
I18n.translations.en.js.filters.needsreply = {title: "需要回复", help: "未回复的主题"};
}
// 自定义以下值
// 设置搜索查询
let search_query = '?max_posts=1';
// 从“需要回复”菜单项中排除特定分类
let excludeList = ['pitches', 'weekly-recap', 'staff'];
api.addNavigationBarItem({
name: "needsreply",
displayName:"需要回复",
title: "需要回复",
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 和 tags 过滤器
api.modifyClass('controller:discovery/topics', {
resetParams: function () {
this.setProperties({max_posts: null});
this.setProperties({tags: null});
this._super();
}
});
</script>
唯一我还没弄明白的是如何在此脚本中访问当前分类。
在原始代码中,Discourse.NavItem 允许你访问当前分类。根据这段代码,我们可以动态地改变链接的行为(隐藏它,或者修改 href 使其仅查看该分类下的帖子)。
有人能推荐一种解决方案,将当前分类注入到上面的脚本中吗?这样我就可以根据用户正在查看的分类来定制链接的行为。