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

This allows us to trigger special initialization code prior to adding an
extra nav item to the navigation menu.
This commit is contained in:
Sam Saffron 2019-11-04 19:07:11 +11:00
parent 72bc0f82b9
commit f66e5d38d3
2 changed files with 24 additions and 4 deletions

View File

@ -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);
}
}

View File

@ -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;
}