Skip to content

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

FEATURE: add an "init" method for extra nav items

Sam Saffronf66e5d38d3ae
diff --git a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 b/app/assets/javascripts/discourse/lib/plugin-api.js.es6
index 9658a959e28..88f2d7381de 100644
--- a/app/assets/javascripts/discourse/lib/plugin-api.js.es6
+++ b/app/assets/javascripts/discourse/lib/plugin-api.js.es6
@@ -46,7 +46,7 @@ import { queryRegistry } from "discourse/widgets/widget";
import Composer from "discourse/models/composer";
// If you add any methods to the API ensure you bump up this number
-const PLUGIN_API_VERSION = "0.8.35";
+const PLUGIN_API_VERSION = "0.8.36";
class PluginApi {
constructor(version, container) {
@@ -716,7 +716,7 @@ class PluginApi {
/**
*
- * Adds a new item in the navigation bar.
+ * Adds a new item in the navigation bar. Returns the NavItem object created.
*
* Example:
*
@@ -729,12 +729,16 @@ class PluginApi {
* An optional `customFilter` callback can be included to not display the
* nav item on certain routes
*
+ * An optional `init` callback can be included to run custom code on menu
+ * init
+ *
* Example:
*
* addNavigationBarItem({
* name: "link-to-bugs-category",
* displayName: "bugs"
* href: "/c/bugs",
+ * init: (navItem, category) => { if (category) { navItem.set("category", category) } }
* customFilter: (category, args, router) => { category && category.name !== 'bug' }
* customHref: (category, args, router) => { if (category && category.name) === 'not-a-bug') "/a-feature"; },
* before: "top",
@@ -773,7 +777,15 @@ class PluginApi {
};
}
- addNavItem(item);
+ const init = item.init;
+ if (init) {
+ const router = this.container.lookup("service:router");
+ item.init = function(navItem, category, args) {
+ init(navItem, category, args, router);
+ };
+ }
+
+ return addNavItem(item);
}
}
diff --git a/app/assets/javascripts/discourse/models/nav-item.js.es6 b/app/assets/javascripts/discourse/models/nav-item.js.es6
index f03a5fc131b..5b36d3b4e4d 100644
--- a/app/assets/javascripts/discourse/models/nav-item.js.es6
+++ b/app/assets/javascripts/discourse/models/nav-item.js.es6
@@ -118,6 +118,8 @@ const ExtraNavItem = NavItem.extend({
}
}),
+ count: 0,
+
customFilter: null
});
@@ -196,6 +198,10 @@ NavItem.reopenClass({
let forceActive = false;
extraItems.forEach(item => {
+ if (item.init) {
+ item.init.call(this, item, category, args);
+ }
+
const before = item.before;
if (before) {
let i = 0;
@@ -243,5 +249,7 @@ export function customNavItemHref(cb) {
}
export function addNavItem(item) {
- NavItem.extraNavItems.push(ExtraNavItem.create(item));
+ const navItem = ExtraNavItem.create(item);
+ NavItem.extraNavItems.push(navItem);
+ return navItem;
}