sidemenu_ctrl.js 3.5 KB

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