grafana_app.ts 6.7 KB

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