do not use global for storing all menu items anymore; instead decorate each menu item with reference pointing back to all menu itemms

This commit is contained in:
Sacha Greif 2015-08-22 09:45:34 +09:00
parent cb47ac773b
commit c174b208cf

View file

@ -1,54 +1,71 @@
menuItemsGlobal= []; var getRoute = function (item) {
getRoute = function (item) {
// if route is a Function return its result, else apply Router.path() to it // if route is a Function return its result, else apply Router.path() to it
return typeof item.route === "function" ? item.route() : Router.path(item.route); return typeof item.route === "function" ? item.route() : Router.path(item.route);
}; };
var filterMenuItems = function (menuItems) {
filterMenuItems = function (menuItems, level, parentId) {
var childLevel = level + 1;
// filter out admin-only items if needed // filter out admin-only items if needed
if (!Users.is.admin(Meteor.user())) { if (!Users.is.admin(Meteor.user())) {
menuItems = _.reject(menuItems, function (item) { menuItems = _.reject(menuItems, function (item) {
return item.adminOnly; return item.adminOnly;
}); });
} }
return menuItems;
};
var getChildMenuItems = function (node) {
var level = node.level;
var childLevel = level + 1;
var menuItems = node.allMenuItems;
menuItems = filterMenuItems(menuItems);
menuItems = _.filter(menuItems, function (item) { menuItems = _.filter(menuItems, function (item) {
if (level === 0) { // return elements with the correct parentId
// if this is the root level, return elements with no parentId return item.parentId === node._id;
return typeof item.parentId === "undefined";
} else {
// else, return elements with the correct parentId
return item.parentId === parentId;
}
}); });
// decorate child item with their level // decorate child item with their level and a reference to the root level
menuItems = _.map(menuItems, function (item) { menuItems = _.map(menuItems, function (item) {
item.level = childLevel; item.level = childLevel;
item.allMenuItems = node.allMenuItems
return item; return item;
}); });
return menuItems; return menuItems;
}; };
Template.menuComponent.onCreated(function () { // Template.menuComponent.onCreated(function () {
menuItemsGlobal = this.data.menuItems; // menuItemsGlobal = this.data.menuItems;
}); // });
Template.menuComponent.helpers({ Template.menuComponent.helpers({
rootMenuItems: function () { rootMenuItems: function () {
return filterMenuItems(this.menuItems, 0);
var allMenuItems = this.menuItems;
var menuItems = filterMenuItems(allMenuItems); // filter out admin items if needed
// get root elements
menuItems = _.filter(menuItems, function(item) {
return typeof item.parentId === "undefined";
});
menuItems = _.map(menuItems, function (item) {
item.level = 0;
item.allMenuItems = allMenuItems;
return item;
});
return menuItems;
}, },
showMenuLabel: function () { showMenuLabel: function () {
return !this.hideMenuLabel; return !this.hideMenuLabel;
}, },
menuClass: function () { menuClass: function () {
var classes = [this.menuName+"-menu"]; var classes = [this.menuName+"-menu"];
var count = filterMenuItems(this.menuItems, 0).length; var count = filterMenuItems(this.menuItems).length;
if (!!this.menuClass) { if (!!this.menuClass) {
classes.push(this.menuClass) classes.push(this.menuClass)
@ -121,7 +138,8 @@ Template.menuItem.helpers({
childMenuItems: function () { childMenuItems: function () {
var currentLevel = this.level; var currentLevel = this.level;
if (this._id) { // don't try to find child menu items if current element doesn't have an id if (this._id) { // don't try to find child menu items if current element doesn't have an id
return filterMenuItems(menuItemsGlobal, currentLevel, this._id); var childMenuItems = getChildMenuItems(this);
return childMenuItems;
} }
} }
}); });