sidemenu.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import _ from 'lodash';
  4. import $ from 'jquery';
  5. import coreModule from '../../core_module';
  6. export class SideMenuCtrl {
  7. isSignedIn: boolean;
  8. showSignout: boolean;
  9. user: any;
  10. mainLinks: any;
  11. orgMenu: any;
  12. appSubUrl: string;
  13. loginUrl: string;
  14. orgFilter: string;
  15. orgItems: any;
  16. orgs: any;
  17. maxShownOrgs: number;
  18. /** @ngInject */
  19. constructor(private $scope, private $location, private contextSrv, private backendSrv, private $element) {
  20. this.isSignedIn = contextSrv.isSignedIn;
  21. this.user = contextSrv.user;
  22. this.appSubUrl = config.appSubUrl;
  23. this.showSignout = this.contextSrv.isSignedIn && !config['authProxyEnabled'];
  24. this.maxShownOrgs = 10;
  25. this.mainLinks = config.bootData.mainNavLinks;
  26. this.openUserDropdown();
  27. this.loginUrl = 'login?redirect=' + encodeURIComponent(this.$location.path());
  28. this.$scope.$on('$routeChangeSuccess', () => {
  29. if (!this.contextSrv.pinned) {
  30. this.contextSrv.sidemenu = false;
  31. }
  32. this.loginUrl = 'login?redirect=' + encodeURIComponent(this.$location.path());
  33. });
  34. this.orgFilter = '';
  35. }
  36. getUrl(url) {
  37. return config.appSubUrl + url;
  38. }
  39. openUserDropdown() {
  40. this.orgMenu = [
  41. {section: 'You', cssClass: 'dropdown-menu-title'},
  42. {text: 'Profile', url: this.getUrl('/profile')},
  43. ];
  44. if (this.isSignedIn) {
  45. this.orgMenu.push({text: "Sign out", url: this.getUrl("/logout"), target: "_self"});
  46. }
  47. if (this.contextSrv.hasRole('Admin')) {
  48. this.orgMenu.push({section: this.user.orgName, cssClass: 'dropdown-menu-title'});
  49. this.orgMenu.push({
  50. text: "Preferences",
  51. url: this.getUrl("/org")
  52. });
  53. this.orgMenu.push({
  54. text: "Users",
  55. url: this.getUrl("/org/users")
  56. });
  57. this.orgMenu.push({
  58. text: "API Keys",
  59. url: this.getUrl("/org/apikeys")
  60. });
  61. }
  62. this.orgMenu.push({cssClass: "divider"});
  63. this.backendSrv.get('/api/user/orgs').then(orgs => {
  64. this.orgs = orgs;
  65. this.loadOrgsItems();
  66. });
  67. }
  68. loadOrgsItems(){
  69. this.orgItems = [];
  70. this.orgs.forEach(org => {
  71. if (org.orgId === this.contextSrv.user.orgId) {
  72. return;
  73. }
  74. if (this.orgItems.length < this.maxShownOrgs && (this.orgFilter === '' || org.name.indexOf(this.orgFilter) !== -1)){
  75. this.orgItems.push({
  76. text: "Switch to " + org.name,
  77. icon: "fa fa-fw fa-random",
  78. url: this.getUrl('/profile/switch-org/' + org.orgId),
  79. target: '_self'
  80. });
  81. }
  82. });
  83. if (config.allowOrgCreate) {
  84. this.orgItems.push({text: "New organization", icon: "fa fa-fw fa-plus", url: this.getUrl('/org/new')});
  85. }
  86. }
  87. }
  88. export function sideMenuDirective() {
  89. return {
  90. restrict: 'E',
  91. templateUrl: 'public/app/core/components/sidemenu/sidemenu.html',
  92. controller: SideMenuCtrl,
  93. bindToController: true,
  94. controllerAs: 'ctrl',
  95. scope: {},
  96. link: function(scope, elem) {
  97. // hack to hide dropdown menu
  98. elem.on('click.dropdown', '.dropdown-menu a', function(evt) {
  99. var menu = $(evt.target).parents('.dropdown-menu');
  100. var parent = menu.parent();
  101. menu.detach();
  102. setTimeout(function() {
  103. parent.append(menu);
  104. }, 100);
  105. });
  106. scope.$on("$destory", function() {
  107. elem.off('click.dropdown');
  108. });
  109. }
  110. };
  111. }
  112. coreModule.directive('sidemenu', sideMenuDirective);