sidemenu.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import _ from 'lodash';
  2. import config from 'app/core/config';
  3. import $ from 'jquery';
  4. import coreModule from '../../core_module';
  5. import appEvents from 'app/core/app_events';
  6. export class SideMenuCtrl {
  7. user: any;
  8. mainLinks: any;
  9. bottomNav: any;
  10. loginUrl: string;
  11. isSignedIn: boolean;
  12. isOpenMobile: boolean;
  13. /** @ngInject */
  14. constructor(private $scope, private $rootScope, private $location, private contextSrv, private $timeout) {
  15. this.isSignedIn = contextSrv.isSignedIn;
  16. this.user = contextSrv.user;
  17. let navTree = _.cloneDeep(config.bootData.navTree);
  18. this.mainLinks = _.filter(navTree, item => !item.hideFromMenu);
  19. this.bottomNav = _.filter(navTree, item => item.hideFromMenu);
  20. this.loginUrl = 'login?redirect=' + encodeURIComponent(this.$location.path());
  21. if (contextSrv.user.orgCount > 1) {
  22. let profileNode = _.find(this.bottomNav, { id: 'profile' });
  23. if (profileNode) {
  24. profileNode.showOrgSwitcher = true;
  25. }
  26. }
  27. this.$scope.$on('$routeChangeSuccess', () => {
  28. this.loginUrl = 'login?redirect=' + encodeURIComponent(this.$location.path());
  29. });
  30. }
  31. toggleSideMenu() {
  32. this.contextSrv.toggleSideMenu();
  33. appEvents.emit('toggle-sidemenu');
  34. this.$timeout(() => {
  35. this.$rootScope.$broadcast('render');
  36. });
  37. }
  38. toggleSideMenuSmallBreakpoint() {
  39. appEvents.emit('toggle-sidemenu-mobile');
  40. }
  41. switchOrg() {
  42. this.$rootScope.appEvent('show-modal', {
  43. templateHtml: '<org-switcher dismiss="dismiss()"></org-switcher>',
  44. });
  45. }
  46. itemClicked(item, evt) {
  47. if (item.url === '/shortcuts') {
  48. appEvents.emit('show-modal', {
  49. templateHtml: '<help-modal></help-modal>',
  50. });
  51. evt.preventDefault();
  52. }
  53. }
  54. }
  55. export function sideMenuDirective() {
  56. return {
  57. restrict: 'E',
  58. templateUrl: 'public/app/core/components/sidemenu/sidemenu.html',
  59. controller: SideMenuCtrl,
  60. bindToController: true,
  61. controllerAs: 'ctrl',
  62. scope: {},
  63. link: function(scope, elem) {
  64. // hack to hide dropdown menu
  65. elem.on('click.dropdown', '.dropdown-menu a', function(evt) {
  66. var menu = $(evt.target).parents('.dropdown-menu');
  67. var parent = menu.parent();
  68. menu.detach();
  69. setTimeout(function() {
  70. parent.append(menu);
  71. }, 100);
  72. });
  73. },
  74. };
  75. }
  76. coreModule.directive('sidemenu', sideMenuDirective);