sidemenu_ctrl.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'jquery',
  5. '../core_module',
  6. 'app/core/config',
  7. ],
  8. function (angular, _, $, coreModule, config) {
  9. 'use strict';
  10. coreModule.default.controller('SideMenuCtrl', function($scope, $location, contextSrv, backendSrv) {
  11. $scope.getUrl = function(url) {
  12. return config.appSubUrl + url;
  13. };
  14. $scope.setupMainNav = function() {
  15. _.each(config.bootData.mainNavLinks, function(item) {
  16. $scope.mainLinks.push({
  17. text: item.text,
  18. icon: item.icon,
  19. href: $scope.getUrl(item.href)
  20. });
  21. });
  22. };
  23. $scope.openUserDropdown = function() {
  24. $scope.orgMenu = [
  25. {section: 'You', cssClass: 'dropdown-menu-title'},
  26. {text: 'Profile', url: $scope.getUrl('/profile')},
  27. ];
  28. if (contextSrv.hasRole('Admin')) {
  29. $scope.orgMenu.push({section: contextSrv.user.orgName, cssClass: 'dropdown-menu-title'});
  30. $scope.orgMenu.push({
  31. text: "Settings",
  32. url: $scope.getUrl("/org"),
  33. });
  34. $scope.orgMenu.push({
  35. text: "Users",
  36. url: $scope.getUrl("/org/users"),
  37. });
  38. $scope.orgMenu.push({
  39. text: "API Keys",
  40. url: $scope.getUrl("/org/apikeys"),
  41. });
  42. }
  43. $scope.orgMenu.push({cssClass: "divider"});
  44. if (config.allowOrgCreate) {
  45. $scope.orgMenu.push({text: "New organization", icon: "fa fa-fw fa-plus", url: $scope.getUrl('/org/new')});
  46. }
  47. backendSrv.get('/api/user/orgs').then(function(orgs) {
  48. _.each(orgs, function(org) {
  49. if (org.orgId === contextSrv.user.orgId) {
  50. return;
  51. }
  52. $scope.orgMenu.push({
  53. text: "Switch to " + org.name,
  54. icon: "fa fa-fw fa-random",
  55. click: function() {
  56. $scope.switchOrg(org.orgId);
  57. }
  58. });
  59. });
  60. $scope.orgMenu.push({cssClass: "divider"});
  61. if (contextSrv.isGrafanaAdmin) {
  62. $scope.orgMenu.push({text: "Server admin", url: $scope.getUrl("/admin/settings")});
  63. }
  64. if (contextSrv.isSignedIn) {
  65. $scope.orgMenu.push({text: "Sign out", url: $scope.getUrl("/logout"), target: "_self"});
  66. }
  67. });
  68. };
  69. $scope.switchOrg = function(orgId) {
  70. backendSrv.post('/api/user/using/' + orgId).then(function() {
  71. window.location.href = $scope.getUrl('/');
  72. });
  73. };
  74. $scope.setupAdminNav = function() {
  75. $scope.systemSection = true;
  76. $scope.grafanaVersion = config.buildInfo.version;
  77. $scope.mainLinks.push({
  78. text: "System info",
  79. icon: "fa fa-fw fa-info",
  80. href: $scope.getUrl("/admin/settings"),
  81. });
  82. $scope.mainLinks.push({
  83. text: "Global Users",
  84. icon: "fa fa-fw fa-user",
  85. href: $scope.getUrl("/admin/users"),
  86. });
  87. $scope.mainLinks.push({
  88. text: "Global Orgs",
  89. icon: "fa fa-fw fa-users",
  90. href: $scope.getUrl("/admin/orgs"),
  91. });
  92. };
  93. $scope.updateMenu = function() {
  94. $scope.systemSection = false;
  95. $scope.mainLinks = [];
  96. $scope.orgMenu = [];
  97. var currentPath = $location.path();
  98. if (currentPath.indexOf('/admin') === 0) {
  99. $scope.setupAdminNav();
  100. } else {
  101. $scope.setupMainNav();
  102. }
  103. };
  104. $scope.init = function() {
  105. $scope.showSignout = contextSrv.isSignedIn && !config['authProxyEnabled'];
  106. $scope.updateMenu();
  107. $scope.$on('$routeChangeSuccess', $scope.updateMenu);
  108. };
  109. });
  110. });