2015-03-25 18:19:22 +09:00
|
|
|
getRoute = function (item) {
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2015-04-08 09:30:57 +09:00
|
|
|
// filter out admin-only items if needed
|
|
|
|
getMenuItems = function (menu) {
|
|
|
|
var menuItems = menu.menuItems;
|
|
|
|
|
|
|
|
if (!isAdmin(Meteor.user())) {
|
|
|
|
menuItems = _.reject(menuItems, function (item) {
|
|
|
|
return item.adminOnly;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return menuItems;
|
|
|
|
}
|
|
|
|
|
2015-03-28 10:54:41 +09:00
|
|
|
Template[getTemplate('menuComponent')].helpers({
|
2015-04-08 09:30:57 +09:00
|
|
|
getMenuItems: function () {
|
|
|
|
return getMenuItems(this);
|
|
|
|
},
|
2015-03-28 10:54:41 +09:00
|
|
|
menuClass: function () {
|
2015-03-23 10:32:56 +09:00
|
|
|
|
2015-03-28 10:54:41 +09:00
|
|
|
var classes = [this.menuName+"-menu"];
|
|
|
|
var mode = (typeof this.menuMode === "undefined") ? "list" : this.menuMode;
|
2015-04-08 09:30:57 +09:00
|
|
|
var count = getMenuItems(this).length;
|
2015-03-23 11:28:36 +09:00
|
|
|
|
2015-03-28 10:54:41 +09:00
|
|
|
classes.push("menu-"+mode);
|
2015-03-23 10:32:56 +09:00
|
|
|
|
2015-04-08 11:57:53 +09:00
|
|
|
if (!!this.menuClass) {
|
2015-03-28 10:54:41 +09:00
|
|
|
classes.push(this.menuClass)
|
2015-03-23 10:32:56 +09:00
|
|
|
}
|
|
|
|
|
2015-04-08 11:57:53 +09:00
|
|
|
if (this.menuCollapsed) {
|
|
|
|
classes.push("menu-collapsed");
|
|
|
|
classes.push("menu-show-more");
|
|
|
|
}
|
|
|
|
|
2015-03-23 11:28:36 +09:00
|
|
|
if (count) {
|
2015-03-28 10:54:41 +09:00
|
|
|
classes.push("menu-has-items");
|
2015-03-23 11:28:36 +09:00
|
|
|
if (count > 3) {
|
2015-04-08 11:57:53 +09:00
|
|
|
classes.push("menu-show-more");
|
2015-03-23 11:28:36 +09:00
|
|
|
}
|
2015-03-20 18:31:49 +09:00
|
|
|
} else {
|
2015-03-28 10:54:41 +09:00
|
|
|
classes.push("menu-no-items");
|
2015-03-20 18:31:49 +09:00
|
|
|
}
|
2015-03-23 11:28:36 +09:00
|
|
|
|
2015-04-08 11:57:53 +09:00
|
|
|
return _.unique(classes).join(" ");
|
2015-03-20 18:31:49 +09:00
|
|
|
},
|
2015-03-28 10:54:41 +09:00
|
|
|
menuLabel: function () {
|
|
|
|
// if label is defined, use this. Else default to menu name
|
|
|
|
return !!this.menuLabel ? this.menuLabel : i18n.t(this.menuName);
|
2015-03-20 18:31:49 +09:00
|
|
|
},
|
2015-03-22 12:27:05 +09:00
|
|
|
hasTemplate: function () {
|
|
|
|
return !!this.template;
|
|
|
|
},
|
2015-03-21 17:52:15 +09:00
|
|
|
itemClass: function () {
|
|
|
|
var itemClass = "";
|
2015-03-25 18:19:22 +09:00
|
|
|
var currentPath = Router.current().location.get().path ;
|
|
|
|
|
2015-03-21 17:52:15 +09:00
|
|
|
if (this.adminOnly) {
|
2015-03-25 18:19:22 +09:00
|
|
|
itemClass += " item-admin";
|
|
|
|
}
|
|
|
|
if (getRoute(this) === currentPath || getRoute(this) === Meteor.absoluteUrl() + currentPath.substr(1)) {
|
|
|
|
// substr(1) is to avoid having two "/" in the URL
|
|
|
|
itemClass += " item-active";
|
2015-03-20 18:31:49 +09:00
|
|
|
}
|
2015-03-25 18:19:22 +09:00
|
|
|
if (this.itemClass) {
|
|
|
|
itemClass += " "+this.itemClass;
|
|
|
|
}
|
|
|
|
|
2015-03-21 17:52:15 +09:00
|
|
|
return itemClass;
|
|
|
|
},
|
|
|
|
itemLabel: function () {
|
|
|
|
// if label is a Function return its result, else return i18n'd version of label
|
|
|
|
return typeof this.label == "function" ? this.label() : i18n.t(this.label);
|
|
|
|
},
|
|
|
|
itemRoute: function () {
|
2015-03-25 18:19:22 +09:00
|
|
|
return getRoute(this);
|
2015-03-21 17:52:15 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-03-28 10:54:41 +09:00
|
|
|
Template[getTemplate('menuComponent')].events({
|
2015-03-21 17:52:15 +09:00
|
|
|
'click .show-more': function (e, t) {
|
|
|
|
e.preventDefault();
|
2015-03-28 10:54:41 +09:00
|
|
|
$menu = t.$('.menu');
|
|
|
|
$menu.toggleClass('menu-open');
|
2015-03-20 18:31:49 +09:00
|
|
|
}
|
|
|
|
});
|