grafana_app.ts 6.7 KB

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