sidemenuCtrl.js 3.5 KB

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