grafana_app.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. sidemenuOpen = scope.contextSrv.sidemenu;
  72. body.toggleClass('sidemenu-open', sidemenuOpen);
  73. appEvents.on('toggle-sidemenu', () => {
  74. sidemenuOpen = scope.contextSrv.sidemenu;
  75. body.toggleClass('sidemenu-open');
  76. });
  77. appEvents.on('toggle-sidemenu-mobile', () => {
  78. body.toggleClass('sidemenu-open--xs');
  79. });
  80. appEvents.on('toggle-sidemenu-hidden', () => {
  81. body.toggleClass('sidemenu-hidden');
  82. });
  83. scope.$watch(() => playlistSrv.isPlaying, function(newValue) {
  84. elem.toggleClass('playlist-active', newValue === true);
  85. });
  86. // check if we are in server side render
  87. if (document.cookie.indexOf('renderKey') !== -1) {
  88. body.addClass('body--phantomjs');
  89. }
  90. // tooltip removal fix
  91. // manage page classes
  92. var pageClass;
  93. scope.$on('$routeChangeSuccess', function(evt, data) {
  94. if (pageClass) {
  95. body.removeClass(pageClass);
  96. }
  97. if (data.$$route) {
  98. pageClass = data.$$route.pageClass;
  99. if (pageClass) {
  100. body.addClass(pageClass);
  101. }
  102. }
  103. // clear body class sidemenu states
  104. body.removeClass('sidemenu-open--xs');
  105. $('#tooltip, .tooltip').remove();
  106. // check for kiosk url param
  107. if (data.params.kiosk) {
  108. appEvents.emit('toggle-kiosk-mode');
  109. }
  110. // check for 'inactive' url param for clean looks like kiosk, but with title
  111. if (data.params.inactive) {
  112. body.addClass('user-activity-low');
  113. // for some reason, with this class it looks cleanest
  114. body.addClass('sidemenu-open');
  115. }
  116. // close all drops
  117. for (const drop of Drop.drops) {
  118. drop.destroy();
  119. }
  120. });
  121. // handle kiosk mode
  122. appEvents.on('toggle-kiosk-mode', () => {
  123. body.toggleClass('page-kiosk-mode');
  124. });
  125. // handle in active view state class
  126. var lastActivity = new Date().getTime();
  127. var activeUser = true;
  128. var inActiveTimeLimit = 60 * 1000;
  129. var sidemenuHidden = false;
  130. function checkForInActiveUser() {
  131. if (!activeUser) {
  132. return;
  133. }
  134. // only go to activity low mode on dashboard page
  135. if (!body.hasClass('page-dashboard')) {
  136. return;
  137. }
  138. if (new Date().getTime() - lastActivity > inActiveTimeLimit) {
  139. activeUser = false;
  140. body.addClass('user-activity-low');
  141. // hide sidemenu
  142. if (sidemenuOpen) {
  143. sidemenuHidden = true;
  144. body.removeClass('sidemenu-open');
  145. $timeout(function() {
  146. $rootScope.$broadcast('render');
  147. }, 100);
  148. }
  149. }
  150. }
  151. function userActivityDetected() {
  152. lastActivity = new Date().getTime();
  153. if (!activeUser) {
  154. activeUser = true;
  155. body.removeClass('user-activity-low');
  156. // restore sidemenu
  157. if (sidemenuHidden) {
  158. sidemenuHidden = false;
  159. body.addClass('sidemenu-open');
  160. appEvents.emit('toggle-inactive-mode');
  161. $timeout(function() {
  162. $rootScope.$broadcast('render');
  163. }, 100);
  164. }
  165. }
  166. }
  167. // mouse and keyboard is user activity
  168. body.mousemove(userActivityDetected);
  169. body.keydown(userActivityDetected);
  170. // set useCapture = true to catch event here
  171. document.addEventListener('wheel', userActivityDetected, { capture: true, passive: true });
  172. // treat tab change as activity
  173. document.addEventListener('visibilitychange', userActivityDetected);
  174. // check every 2 seconds
  175. setInterval(checkForInActiveUser, 2000);
  176. appEvents.on('toggle-view-mode', () => {
  177. lastActivity = 0;
  178. checkForInActiveUser();
  179. });
  180. // handle document clicks that should hide things
  181. body.click(function(evt) {
  182. var target = $(evt.target);
  183. if (target.parents().length === 0) {
  184. return;
  185. }
  186. // for stuff that animates, slides out etc, clicking it needs to
  187. // hide it right away
  188. var clickAutoHide = target.closest('[data-click-hide]');
  189. if (clickAutoHide.length) {
  190. var clickAutoHideParent = clickAutoHide.parent();
  191. clickAutoHide.detach();
  192. setTimeout(function() {
  193. clickAutoHideParent.append(clickAutoHide);
  194. }, 100);
  195. }
  196. if (target.parents('.navbar-buttons--playlist').length === 0) {
  197. playlistSrv.stop();
  198. }
  199. // hide search
  200. if (body.find('.search-container').length > 0) {
  201. if (target.parents('.search-results-container, .search-field-wrapper').length === 0) {
  202. scope.$apply(function() {
  203. scope.appEvent('hide-dash-search');
  204. });
  205. }
  206. }
  207. // hide popovers
  208. var popover = elem.find('.popover');
  209. if (popover.length > 0 && target.parents('.graph-legend').length === 0) {
  210. popover.hide();
  211. }
  212. });
  213. },
  214. };
  215. }
  216. coreModule.directive('grafanaApp', grafanaAppDirective);