sidemenuCtrl.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'jquery',
  5. 'config',
  6. ],
  7. function (angular, _, $, config) {
  8. 'use strict';
  9. var module = angular.module('grafana.controllers');
  10. module.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.isUsing) {
  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({
  61. text: "New Organization",
  62. icon: "fa fa-fw fa-plus",
  63. href: $scope.getUrl('/org/new')
  64. });
  65. });
  66. };
  67. $scope.switchOrg = function(orgId) {
  68. backendSrv.post('/api/user/using/' + orgId).then(function() {
  69. window.location.href = $scope.getUrl('/');
  70. });
  71. };
  72. $scope.setupAdminNav = function() {
  73. $scope.systemSection = true;
  74. $scope.grafanaVersion = config.buildInfo.version;
  75. $scope.mainLinks.push({
  76. text: "System info",
  77. icon: "fa fa-fw fa-info",
  78. href: $scope.getUrl("/admin/settings"),
  79. });
  80. $scope.mainLinks.push({
  81. text: "Global Users",
  82. icon: "fa fa-fw fa-user",
  83. href: $scope.getUrl("/admin/users"),
  84. });
  85. $scope.mainLinks.push({
  86. text: "Global Orgs",
  87. icon: "fa fa-fw fa-users",
  88. href: $scope.getUrl("/admin/orgs"),
  89. });
  90. };
  91. $scope.updateMenu = function() {
  92. $scope.systemSection = false;
  93. $scope.mainLinks = [];
  94. $scope.orgMenu = [];
  95. var currentPath = $location.path();
  96. if (currentPath.indexOf('/admin') === 0) {
  97. $scope.setupAdminNav();
  98. } else {
  99. $scope.setupMainNav();
  100. }
  101. };
  102. $scope.init = function() {
  103. $scope.updateMenu();
  104. $scope.$on('$routeChangeSuccess', $scope.updateMenu);
  105. };
  106. });
  107. });