grafana_app.ts 7.6 KB

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