GrafanaCtrl.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { IRootScopeService, IAngularEvent, auto } from 'angular';
  2. import $ from 'jquery';
  3. import _ from 'lodash'; // eslint-disable-line lodash/import-scope
  4. import { AppEvent } from '@grafana/data';
  5. import { setLegacyAngularInjector, setAppEvents, setAngularLoader } from '@grafana/runtime';
  6. import { colors } from '@grafana/ui';
  7. import coreModule from 'app/angular/core_module';
  8. import { AngularLoader } from 'app/angular/services/AngularLoader';
  9. import appEvents from 'app/core/app_events';
  10. import config from 'app/core/config';
  11. import { ContextSrv } from 'app/core/services/context_srv';
  12. import { initGrafanaLive } from 'app/features/live';
  13. import { CoreEvents, AppEventEmitter, AppEventConsumer } from 'app/types';
  14. import { UtilSrv } from './services/UtilSrv';
  15. export type GrafanaRootScope = IRootScopeService & AppEventEmitter & AppEventConsumer & { colors: string[] };
  16. export class GrafanaCtrl {
  17. /** @ngInject */
  18. constructor(
  19. $scope: any,
  20. utilSrv: UtilSrv,
  21. $rootScope: GrafanaRootScope,
  22. contextSrv: ContextSrv,
  23. angularLoader: AngularLoader,
  24. $injector: auto.IInjectorService
  25. ) {
  26. // make angular loader service available to react components
  27. setAngularLoader(angularLoader);
  28. setLegacyAngularInjector($injector);
  29. setAppEvents(appEvents);
  30. initGrafanaLive();
  31. $scope.init = () => {
  32. $scope.contextSrv = contextSrv;
  33. $scope.appSubUrl = config.appSubUrl;
  34. $scope._ = _;
  35. utilSrv.init();
  36. };
  37. $rootScope.colors = colors;
  38. $rootScope.onAppEvent = function <T>(
  39. event: AppEvent<T> | string,
  40. callback: (event: IAngularEvent, ...args: any[]) => void,
  41. localScope?: any
  42. ) {
  43. let unbind;
  44. if (typeof event === 'string') {
  45. unbind = $rootScope.$on(event, callback);
  46. } else {
  47. unbind = $rootScope.$on(event.name, callback);
  48. }
  49. let callerScope = this;
  50. if (callerScope.$id === 1 && !localScope) {
  51. console.warn('warning rootScope onAppEvent called without localscope');
  52. }
  53. if (localScope) {
  54. callerScope = localScope;
  55. }
  56. callerScope.$on('$destroy', unbind);
  57. };
  58. $rootScope.appEvent = <T>(event: AppEvent<T> | string, payload?: T | any) => {
  59. if (typeof event === 'string') {
  60. $rootScope.$emit(event, payload);
  61. appEvents.emit(event, payload);
  62. } else {
  63. $rootScope.$emit(event.name, payload);
  64. appEvents.emit(event, payload);
  65. }
  66. };
  67. $scope.init();
  68. }
  69. }
  70. /** @ngInject */
  71. export function grafanaAppDirective() {
  72. return {
  73. restrict: 'E',
  74. controller: GrafanaCtrl,
  75. link: (scope: IRootScopeService & AppEventEmitter, elem: JQuery) => {
  76. const body = $('body');
  77. // see https://github.com/zenorocha/clipboard.js/issues/155
  78. $.fn.modal.Constructor.prototype.enforceFocus = () => {};
  79. appEvents.on(CoreEvents.toggleSidemenuHidden, () => {
  80. body.toggleClass('sidemenu-hidden');
  81. });
  82. // handle in active view state class
  83. let lastActivity = new Date().getTime();
  84. let activeUser = true;
  85. const inActiveTimeLimit = 60 * 5000;
  86. function checkForInActiveUser() {
  87. if (!activeUser) {
  88. return;
  89. }
  90. // only go to activity low mode on dashboard page
  91. if (!body.hasClass('page-dashboard')) {
  92. return;
  93. }
  94. if (new Date().getTime() - lastActivity > inActiveTimeLimit) {
  95. activeUser = false;
  96. body.addClass('view-mode--inactive');
  97. }
  98. }
  99. function userActivityDetected() {
  100. lastActivity = new Date().getTime();
  101. if (!activeUser) {
  102. activeUser = true;
  103. body.removeClass('view-mode--inactive');
  104. }
  105. }
  106. // mouse and keyboard is user activity
  107. body.mousemove(userActivityDetected);
  108. body.keydown(userActivityDetected);
  109. // set useCapture = true to catch event here
  110. document.addEventListener('wheel', userActivityDetected, { capture: true, passive: true });
  111. // treat tab change as activity
  112. document.addEventListener('visibilitychange', userActivityDetected);
  113. // check every 2 seconds
  114. setInterval(checkForInActiveUser, 2000);
  115. // handle document clicks that should hide things
  116. body.click((evt) => {
  117. const target = $(evt.target);
  118. if (target.parents().length === 0) {
  119. return;
  120. }
  121. // ensure dropdown menu doesn't impact on z-index
  122. body.find('.dropdown-menu-open').removeClass('dropdown-menu-open');
  123. // for stuff that animates, slides out etc, clicking it needs to
  124. // hide it right away
  125. const clickAutoHide = target.closest('[data-click-hide]');
  126. if (clickAutoHide.length) {
  127. const clickAutoHideParent = clickAutoHide.parent();
  128. clickAutoHide.detach();
  129. setTimeout(() => {
  130. clickAutoHideParent.append(clickAutoHide);
  131. }, 100);
  132. }
  133. // hide popovers
  134. const popover = elem.find('.popover');
  135. if (popover.length > 0 && target.parents('.graph-legend').length === 0) {
  136. popover.hide();
  137. }
  138. });
  139. },
  140. };
  141. }
  142. coreModule.directive('grafanaApp', grafanaAppDirective);