sidemenu_ctrl.js 3.0 KB

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