grafana_app.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import config from 'app/core/config';
  2. import _ from 'lodash';
  3. import $ from 'jquery';
  4. import coreModule from 'app/core/core_module';
  5. import { profiler } from 'app/core/profiler';
  6. import appEvents from 'app/core/app_events';
  7. import Drop from 'tether-drop';
  8. import { createStore } from 'app/stores/store';
  9. import colors from 'app/core/utils/colors';
  10. export class GrafanaCtrl {
  11. /** @ngInject */
  12. constructor($scope, alertSrv, utilSrv, $rootScope, $controller, contextSrv, bridgeSrv, backendSrv) {
  13. createStore(backendSrv);
  14. $scope.init = function() {
  15. $scope.contextSrv = contextSrv;
  16. $scope.appSubUrl = config.appSubUrl;
  17. $scope._ = _;
  18. profiler.init(config, $rootScope);
  19. alertSrv.init();
  20. utilSrv.init();
  21. bridgeSrv.init();
  22. $scope.dashAlerts = alertSrv;
  23. };
  24. $rootScope.colors = colors;
  25. $scope.initDashboard = function(dashboardData, viewScope) {
  26. $scope.appEvent('dashboard-fetch-end', dashboardData);
  27. $controller('DashboardCtrl', { $scope: viewScope }).init(dashboardData);
  28. };
  29. $rootScope.onAppEvent = function(name, callback, localScope) {
  30. var unbind = $rootScope.$on(name, callback);
  31. var callerScope = this;
  32. if (callerScope.$id === 1 && !localScope) {
  33. console.log('warning rootScope onAppEvent called without localscope');
  34. }
  35. if (localScope) {
  36. callerScope = localScope;
  37. }
  38. callerScope.$on('$destroy', unbind);
  39. };
  40. $rootScope.appEvent = function(name, payload) {
  41. $rootScope.$emit(name, payload);
  42. appEvents.emit(name, payload);
  43. };
  44. $scope.init();
  45. }
  46. }
  47. /** @ngInject */
  48. export function grafanaAppDirective(playlistSrv, contextSrv, $timeout, $rootScope, $location) {
  49. return {
  50. restrict: 'E',
  51. controller: GrafanaCtrl,
  52. link: (scope, elem) => {
  53. var sidemenuOpen;
  54. var body = $('body');
  55. // see https://github.com/zenorocha/clipboard.js/issues/155
  56. $.fn.modal.Constructor.prototype.enforceFocus = function() {};
  57. sidemenuOpen = scope.contextSrv.sidemenu;
  58. body.toggleClass('sidemenu-open', sidemenuOpen);
  59. appEvents.on('toggle-sidemenu', () => {
  60. sidemenuOpen = scope.contextSrv.sidemenu;
  61. body.toggleClass('sidemenu-open');
  62. });
  63. appEvents.on('toggle-sidemenu-mobile', () => {
  64. body.toggleClass('sidemenu-open--xs');
  65. });
  66. appEvents.on('toggle-sidemenu-hidden', () => {
  67. body.toggleClass('sidemenu-hidden');
  68. });
  69. scope.$watch(() => playlistSrv.isPlaying, function(newValue) {
  70. elem.toggleClass('playlist-active', newValue === true);
  71. });
  72. // tooltip removal fix
  73. // manage page classes
  74. var pageClass;
  75. scope.$on('$routeChangeSuccess', function(evt, data) {
  76. if (pageClass) {
  77. body.removeClass(pageClass);
  78. }
  79. if (data.$$route) {
  80. pageClass = data.$$route.pageClass;
  81. if (pageClass) {
  82. body.addClass(pageClass);
  83. }
  84. }
  85. // clear body class sidemenu states
  86. body.removeClass('sidemenu-open--xs');
  87. $('#tooltip, .tooltip').remove();
  88. // check for kiosk url param
  89. if (data.params.kiosk) {
  90. appEvents.emit('toggle-kiosk-mode');
  91. }
  92. // close all drops
  93. for (let drop of Drop.drops) {
  94. drop.destroy();
  95. }
  96. });
  97. // handle kiosk mode
  98. appEvents.on('toggle-kiosk-mode', () => {
  99. body.toggleClass('page-kiosk-mode');
  100. });
  101. // handle in active view state class
  102. var lastActivity = new Date().getTime();
  103. var activeUser = true;
  104. var inActiveTimeLimit = 60 * 1000;
  105. var sidemenuHidden = false;
  106. function checkForInActiveUser() {
  107. if (!activeUser) {
  108. return;
  109. }
  110. // only go to activity low mode on dashboard page
  111. if (!body.hasClass('page-dashboard')) {
  112. return;
  113. }
  114. if (new Date().getTime() - lastActivity > inActiveTimeLimit) {
  115. activeUser = false;
  116. body.addClass('user-activity-low');
  117. // hide sidemenu
  118. if (sidemenuOpen) {
  119. sidemenuHidden = true;
  120. body.removeClass('sidemenu-open');
  121. $timeout(function() {
  122. $rootScope.$broadcast('render');
  123. }, 100);
  124. }
  125. }
  126. }
  127. function userActivityDetected() {
  128. lastActivity = new Date().getTime();
  129. if (!activeUser) {
  130. activeUser = true;
  131. body.removeClass('user-activity-low');
  132. // restore sidemenu
  133. if (sidemenuHidden) {
  134. sidemenuHidden = false;
  135. body.addClass('sidemenu-open');
  136. $timeout(function() {
  137. $rootScope.$broadcast('render');
  138. }, 100);
  139. }
  140. }
  141. }
  142. // mouse and keyboard is user activity
  143. body.mousemove(userActivityDetected);
  144. body.keydown(userActivityDetected);
  145. // set useCapture = true to catch event here
  146. document.addEventListener('wheel', userActivityDetected, true);
  147. // treat tab change as activity
  148. document.addEventListener('visibilitychange', userActivityDetected);
  149. // check every 2 seconds
  150. setInterval(checkForInActiveUser, 2000);
  151. appEvents.on('toggle-view-mode', () => {
  152. lastActivity = 0;
  153. checkForInActiveUser();
  154. });
  155. // handle document clicks that should hide things
  156. body.click(function(evt) {
  157. var target = $(evt.target);
  158. if (target.parents().length === 0) {
  159. return;
  160. }
  161. // for stuff that animates, slides out etc, clicking it needs to
  162. // hide it right away
  163. var clickAutoHide = target.closest('[data-click-hide]');
  164. if (clickAutoHide.length) {
  165. var clickAutoHideParent = clickAutoHide.parent();
  166. clickAutoHide.detach();
  167. setTimeout(function() {
  168. clickAutoHideParent.append(clickAutoHide);
  169. }, 100);
  170. }
  171. if (target.parents('.navbar-buttons--playlist').length === 0) {
  172. playlistSrv.stop();
  173. }
  174. // hide search
  175. if (body.find('.search-container').length > 0) {
  176. if (target.parents('.search-results-container, .search-field-wrapper').length === 0) {
  177. scope.$apply(function() {
  178. scope.appEvent('hide-dash-search');
  179. });
  180. }
  181. }
  182. // hide popovers
  183. var popover = elem.find('.popover');
  184. if (popover.length > 0 && target.parents('.graph-legend').length === 0) {
  185. popover.hide();
  186. }
  187. });
  188. },
  189. };
  190. }
  191. coreModule.directive('grafanaApp', grafanaAppDirective);