grafana_app.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import store from 'app/core/store';
  4. import _ from 'lodash';
  5. import angular from 'angular';
  6. import $ from 'jquery';
  7. import coreModule from 'app/core/core_module';
  8. import {profiler} from 'app/core/profiler';
  9. import appEvents from 'app/core/app_events';
  10. export class GrafanaCtrl {
  11. /** @ngInject */
  12. constructor($scope, alertSrv, utilSrv, $rootScope, $controller, contextSrv) {
  13. $scope.init = function() {
  14. $scope.contextSrv = contextSrv;
  15. $rootScope.appSubUrl = config.appSubUrl;
  16. $scope._ = _;
  17. profiler.init(config, $rootScope);
  18. alertSrv.init();
  19. utilSrv.init();
  20. $scope.dashAlerts = alertSrv;
  21. };
  22. $scope.initDashboard = function(dashboardData, viewScope) {
  23. $scope.appEvent("dashboard-fetch-end", dashboardData);
  24. $controller('DashboardCtrl', { $scope: viewScope }).init(dashboardData);
  25. };
  26. $rootScope.onAppEvent = function(name, callback, localScope) {
  27. var unbind = $rootScope.$on(name, callback);
  28. var callerScope = this;
  29. if (callerScope.$id === 1 && !localScope) {
  30. console.log('warning rootScope onAppEvent called without localscope');
  31. }
  32. if (localScope) {
  33. callerScope = localScope;
  34. }
  35. callerScope.$on('$destroy', unbind);
  36. };
  37. $rootScope.appEvent = function(name, payload) {
  38. $rootScope.$emit(name, payload);
  39. appEvents.emit(name, payload);
  40. };
  41. $rootScope.colors = [
  42. "#7EB26D","#EAB839","#6ED0E0","#EF843C","#E24D42","#1F78C1","#BA43A9","#705DA0",
  43. "#508642","#CCA300","#447EBC","#C15C17","#890F02","#0A437C","#6D1F62","#584477",
  44. "#B7DBAB","#F4D598","#70DBED","#F9BA8F","#F29191","#82B5D8","#E5A8E2","#AEA2E0",
  45. "#629E51","#E5AC0E","#64B0C8","#E0752D","#BF1B00","#0A50A1","#962D82","#614D93",
  46. "#9AC48A","#F2C96D","#65C5DB","#F9934E","#EA6460","#5195CE","#D683CE","#806EB7",
  47. "#3F6833","#967302","#2F575E","#99440A","#58140C","#052B51","#511749","#3F2B5B",
  48. "#E0F9D7","#FCEACA","#CFFAFF","#F9E2D2","#FCE2DE","#BADFF4","#F9D9F9","#DEDAF7"
  49. ];
  50. $scope.init();
  51. }
  52. }
  53. /** @ngInject */
  54. export function grafanaAppDirective(playlistSrv, contextSrv) {
  55. return {
  56. restrict: 'E',
  57. controller: GrafanaCtrl,
  58. link: (scope, elem) => {
  59. var ignoreSideMenuHide;
  60. var body = $('body');
  61. $.fn.modal.Constructor.prototype.enforceFocus = function() {}; // see https://github.com/zenorocha/clipboard.js/issues/155
  62. // handle sidemenu open state
  63. scope.$watch('contextSrv.sidemenu', newVal => {
  64. if (newVal !== undefined) {
  65. body.toggleClass('sidemenu-open', scope.contextSrv.sidemenu);
  66. if (!newVal) {
  67. contextSrv.setPinnedState(false);
  68. }
  69. }
  70. if (contextSrv.sidemenu) {
  71. ignoreSideMenuHide = true;
  72. setTimeout(() => {
  73. ignoreSideMenuHide = false;
  74. }, 300);
  75. }
  76. });
  77. scope.$watch('contextSrv.pinned', newVal => {
  78. if (newVal !== undefined) {
  79. body.toggleClass('sidemenu-pinned', newVal);
  80. }
  81. });
  82. // tooltip removal fix
  83. // manage page classes
  84. var pageClass;
  85. scope.$on("$routeChangeSuccess", function(evt, data) {
  86. if (pageClass) {
  87. body.removeClass(pageClass);
  88. }
  89. pageClass = data.$$route.pageClass;
  90. if (pageClass) {
  91. body.addClass(pageClass);
  92. }
  93. $("#tooltip, .tooltip").remove();
  94. // check for kiosk url param
  95. if (data.params.kiosk) {
  96. appEvents.emit('toggle-kiosk-mode');
  97. }
  98. });
  99. // handle kiosk mode
  100. appEvents.on('toggle-kiosk-mode', () => {
  101. body.toggleClass('page-kiosk-mode');
  102. });
  103. // handle in active view state class
  104. var lastActivity = new Date().getTime();
  105. var activeUser = true;
  106. var inActiveTimeLimit = 60 * 1000;
  107. function checkForInActiveUser() {
  108. if (!activeUser) {
  109. return;
  110. }
  111. // only go to activity low mode on dashboard page
  112. if (!body.hasClass('page-dashboard')) {
  113. return;
  114. }
  115. if ((new Date().getTime() - lastActivity) > inActiveTimeLimit) {
  116. activeUser = false;
  117. body.addClass('user-activity-low');
  118. }
  119. }
  120. function userActivityDetected() {
  121. lastActivity = new Date().getTime();
  122. if (!activeUser) {
  123. activeUser = true;
  124. body.removeClass('user-activity-low');
  125. }
  126. }
  127. // mouse and keyboard is user activity
  128. body.mousemove(userActivityDetected);
  129. body.keydown(userActivityDetected);
  130. // treat tab change as activity
  131. document.addEventListener('visibilitychange', userActivityDetected);
  132. // check every 2 seconds
  133. setInterval(checkForInActiveUser, 2000);
  134. appEvents.on('toggle-view-mode', () => {
  135. lastActivity = 0;
  136. checkForInActiveUser();
  137. });
  138. // handle document clicks that should hide things
  139. body.click(function(evt) {
  140. var target = $(evt.target);
  141. if (target.parents().length === 0) {
  142. return;
  143. }
  144. // for stuff that animates, slides out etc, clicking it needs to
  145. // hide it right away
  146. var clickAutoHide = target.closest('[data-click-hide]');
  147. if (clickAutoHide.length) {
  148. var clickAutoHideParent = clickAutoHide.parent();
  149. clickAutoHide.detach();
  150. setTimeout(function() {
  151. clickAutoHideParent.append(clickAutoHide);
  152. }, 100);
  153. }
  154. if (target.parents('.dash-playlist-actions').length === 0) {
  155. playlistSrv.stop();
  156. }
  157. // hide search
  158. if (body.find('.search-container').length > 0) {
  159. if (target.parents('.search-container').length === 0) {
  160. scope.$apply(function() {
  161. scope.appEvent('hide-dash-search');
  162. });
  163. }
  164. }
  165. // hide sidemenu
  166. if (!ignoreSideMenuHide && !contextSrv.pinned && body.find('.sidemenu').length > 0) {
  167. if (target.parents('.sidemenu').length === 0) {
  168. scope.$apply(function() {
  169. scope.contextSrv.toggleSideMenu();
  170. });
  171. }
  172. }
  173. // hide popovers
  174. var popover = elem.find('.popover');
  175. if (popover.length > 0 && target.parents('.graph-legend').length === 0) {
  176. popover.hide();
  177. }
  178. });
  179. }
  180. };
  181. }
  182. coreModule.directive('grafanaApp', grafanaAppDirective);