Skip to content

A platform for community discussion. Free, open, simple.

Give `ExtraNavItem` more control over when it can be displayed.

Guo Xiang Tanef78268c01ed
diff --git a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 b/app/assets/javascripts/discourse/lib/plugin-api.js.es6
index cfff459ed93..6c20b968f56 100644
--- a/app/assets/javascripts/discourse/lib/plugin-api.js.es6
+++ b/app/assets/javascripts/discourse/lib/plugin-api.js.es6
@@ -668,6 +668,18 @@ class PluginApi {
* displayName: "Discourse"
* href: "https://www.discourse.org",
* })
+ *
+ * An optional `customFilter` callback can be included to not display the
+ * nav item on certain routes
+ *
+ * Example:
+ *
+ * addNavigationBarItem({
+ * name: "link-to-bugs-category",
+ * displayName: "bugs"
+ * href: "/c/bugs",
+ * customFilter: (category, args) => { category && category.get('name') !== 'bug' }
+ * })
*/
addNavigationBarItem(item) {
if (!item["name"]) {
diff --git a/app/assets/javascripts/discourse/models/nav-item.js.es6 b/app/assets/javascripts/discourse/models/nav-item.js.es6
index 4587183ed62..e6ad1f91029 100644
--- a/app/assets/javascripts/discourse/models/nav-item.js.es6
+++ b/app/assets/javascripts/discourse/models/nav-item.js.es6
@@ -102,7 +102,8 @@ const NavItem = Discourse.Model.extend({
});
const ExtraNavItem = NavItem.extend({
- @computed("href") href: href => href
+ @computed("href") href: href => href,
+ customFilter: null
});
NavItem.reopenClass({
@@ -169,7 +170,11 @@ NavItem.reopenClass({
i => i !== null && !(category && i.get("name").indexOf("categor") === 0)
);
- return items.concat(NavItem.extraNavItems);
+ const extraItems = NavItem.extraNavItems.filter(item => {
+ return item.customFilter && item.customFilter.call(this, category, args);
+ });
+
+ return items.concat(extraItems);
}
});