sidemenu_ctrl.js 3.2 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.controller('SideMenuCtrl', function($scope, $location, contextSrv, backendSrv) {
  11. $scope.getUrl = function(url) {
  12. return config.appSubUrl + url;
  13. };
  14. $scope.setupMainNav = function() {
  15. $scope.mainLinks.push({
  16. text: "Dashboards",
  17. icon: "fa fa-fw fa-th-large",
  18. href: $scope.getUrl("/"),
  19. });
  20. if (contextSrv.hasRole('Admin')) {
  21. $scope.mainLinks.push({
  22. text: "Data Sources",
  23. icon: "fa fa-fw fa-database",
  24. href: $scope.getUrl("/datasources"),
  25. });
  26. }
  27. };
  28. $scope.loadOrgs = function() {
  29. $scope.orgMenu = [];
  30. if (contextSrv.hasRole('Admin')) {
  31. $scope.orgMenu.push({
  32. text: "Organization settings",
  33. href: $scope.getUrl("/org"),
  34. });
  35. $scope.orgMenu.push({
  36. text: "Users",
  37. href: $scope.getUrl("/org/users"),
  38. });
  39. $scope.orgMenu.push({
  40. text: "API Keys",
  41. href: $scope.getUrl("/org/apikeys"),
  42. });
  43. }
  44. if ($scope.orgMenu.length > 0) {
  45. $scope.orgMenu.push({ cssClass: 'divider' });
  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. if (config.allowOrgCreate) {
  61. $scope.orgMenu.push({
  62. text: "New Organization",
  63. icon: "fa fa-fw fa-plus",
  64. href: $scope.getUrl('/org/new')
  65. });
  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. });