grafana/public/app/core/controllers/sidemenu_ctrl.js

132 lines
3.3 KiB
JavaScript
Raw Normal View History

2015-01-21 21:44:34 +08:00
define([
'angular',
'lodash',
'jquery',
'../core_module',
2015-10-30 21:19:02 +08:00
'app/core/config',
2015-01-21 21:44:34 +08:00
],
function (angular, _, $, coreModule, config) {
2015-01-21 21:44:34 +08:00
'use strict';
2015-12-16 19:21:13 +08:00
coreModule.default.controller('SideMenuCtrl', function($scope, $location, contextSrv, backendSrv) {
2015-01-21 21:44:34 +08:00
$scope.getUrl = function(url) {
return config.appSubUrl + url;
};
2015-02-25 23:01:37 +08:00
$scope.setupMainNav = function() {
_.each(config.bootData.mainNavLinks, function(item) {
2015-02-25 23:01:37 +08:00
$scope.mainLinks.push({
text: item.text,
icon: item.icon,
href: $scope.getUrl(item.href)
2015-02-25 23:01:37 +08:00
});
});
};
$scope.loadOrgs = function() {
$scope.orgMenu = [];
2015-02-25 23:01:37 +08:00
if (contextSrv.hasRole('Admin')) {
$scope.orgMenu.push({
text: "Organization settings",
2015-02-25 23:01:37 +08:00
href: $scope.getUrl("/org"),
});
$scope.orgMenu.push({
text: "Users",
href: $scope.getUrl("/org/users"),
});
$scope.orgMenu.push({
text: "API Keys",
href: $scope.getUrl("/org/apikeys"),
});
$scope.orgMenu.push({
text: "Datasources",
href: $scope.getUrl("/datasources"),
});
$scope.orgMenu.push({
text: "Apps",
href: $scope.getUrl("/org/apps"),
});
}
if ($scope.orgMenu.length > 0) {
$scope.orgMenu.push({ cssClass: 'divider' });
}
2015-02-25 23:01:37 +08:00
backendSrv.get('/api/user/orgs').then(function(orgs) {
_.each(orgs, function(org) {
if (org.orgId === contextSrv.user.orgId) {
return;
}
$scope.orgMenu.push({
text: "Switch to " + org.name,
icon: "fa fa-fw fa-random",
click: function() {
$scope.switchOrg(org.orgId);
}
2015-02-25 23:01:37 +08:00
});
});
if (config.allowOrgCreate) {
$scope.orgMenu.push({
text: "New Organization",
icon: "fa fa-fw fa-plus",
href: $scope.getUrl('/org/new')
});
}
});
};
$scope.switchOrg = function(orgId) {
backendSrv.post('/api/user/using/' + orgId).then(function() {
window.location.href = $scope.getUrl('/');
});
2015-02-25 23:01:37 +08:00
};
$scope.setupAdminNav = function() {
$scope.systemSection = true;
$scope.grafanaVersion = config.buildInfo.version;
$scope.mainLinks.push({
text: "System info",
icon: "fa fa-fw fa-info",
href: $scope.getUrl("/admin/settings"),
});
$scope.mainLinks.push({
text: "Global Users",
icon: "fa fa-fw fa-user",
href: $scope.getUrl("/admin/users"),
});
$scope.mainLinks.push({
text: "Global Orgs",
icon: "fa fa-fw fa-users",
href: $scope.getUrl("/admin/orgs"),
});
};
2015-01-21 21:44:34 +08:00
2015-02-25 23:01:37 +08:00
$scope.updateMenu = function() {
$scope.systemSection = false;
$scope.mainLinks = [];
$scope.orgMenu = [];
2015-02-25 23:01:37 +08:00
var currentPath = $location.path();
if (currentPath.indexOf('/admin') === 0) {
$scope.setupAdminNav();
} else {
$scope.setupMainNav();
}
2015-01-21 21:44:34 +08:00
};
$scope.init = function() {
$scope.showSignout = contextSrv.isSignedIn && !config['authProxyEnabled'];
2015-02-25 23:01:37 +08:00
$scope.updateMenu();
$scope.$on('$routeChangeSuccess', $scope.updateMenu);
2015-01-21 21:44:34 +08:00
};
});
});