grafana_app.ts 7.4 KB

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