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