grafana_app.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. // see https://github.com/zenorocha/clipboard.js/issues/155
  62. $.fn.modal.Constructor.prototype.enforceFocus = function() {};
  63. // handle sidemenu open state
  64. scope.$watch('contextSrv.sidemenu', newVal => {
  65. if (newVal !== undefined) {
  66. body.toggleClass('sidemenu-open', scope.contextSrv.sidemenu);
  67. if (!newVal) {
  68. contextSrv.setPinnedState(false);
  69. }
  70. }
  71. if (contextSrv.sidemenu) {
  72. ignoreSideMenuHide = true;
  73. setTimeout(() => {
  74. ignoreSideMenuHide = false;
  75. }, 300);
  76. }
  77. });
  78. scope.$watch('contextSrv.pinned', newVal => {
  79. if (newVal !== undefined) {
  80. body.toggleClass('sidemenu-pinned', newVal);
  81. }
  82. });
  83. // tooltip removal fix
  84. // manage page classes
  85. var pageClass;
  86. scope.$on("$routeChangeSuccess", function(evt, data) {
  87. if (pageClass) {
  88. body.removeClass(pageClass);
  89. }
  90. if (data.$$route) {
  91. pageClass = data.$$route.pageClass;
  92. if (pageClass) {
  93. body.addClass(pageClass);
  94. }
  95. }
  96. $("#tooltip, .tooltip").remove();
  97. // check for kiosk url param
  98. if (data.params.kiosk) {
  99. appEvents.emit('toggle-kiosk-mode');
  100. }
  101. });
  102. // handle kiosk mode
  103. appEvents.on('toggle-kiosk-mode', () => {
  104. body.toggleClass('page-kiosk-mode');
  105. });
  106. // handle in active view state class
  107. var lastActivity = new Date().getTime();
  108. var activeUser = true;
  109. var inActiveTimeLimit = 60 * 1000;
  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. }
  122. }
  123. function userActivityDetected() {
  124. lastActivity = new Date().getTime();
  125. if (!activeUser) {
  126. activeUser = true;
  127. body.removeClass('user-activity-low');
  128. }
  129. }
  130. // mouse and keyboard is user activity
  131. body.mousemove(userActivityDetected);
  132. body.keydown(userActivityDetected);
  133. // treat tab change as activity
  134. document.addEventListener('visibilitychange', userActivityDetected);
  135. // check every 2 seconds
  136. setInterval(checkForInActiveUser, 2000);
  137. appEvents.on('toggle-view-mode', () => {
  138. lastActivity = 0;
  139. checkForInActiveUser();
  140. });
  141. // handle document clicks that should hide things
  142. body.click(function(evt) {
  143. var target = $(evt.target);
  144. if (target.parents().length === 0) {
  145. return;
  146. }
  147. // for stuff that animates, slides out etc, clicking it needs to
  148. // hide it right away
  149. var clickAutoHide = target.closest('[data-click-hide]');
  150. if (clickAutoHide.length) {
  151. var clickAutoHideParent = clickAutoHide.parent();
  152. clickAutoHide.detach();
  153. setTimeout(function() {
  154. clickAutoHideParent.append(clickAutoHide);
  155. }, 100);
  156. }
  157. if (target.parents('.dash-playlist-actions').length === 0) {
  158. playlistSrv.stop();
  159. }
  160. // hide search
  161. if (body.find('.search-container').length > 0) {
  162. if (target.parents('.search-container').length === 0) {
  163. scope.$apply(function() {
  164. scope.appEvent('hide-dash-search');
  165. });
  166. }
  167. }
  168. // hide menus
  169. var openMenus = body.find('.navbar-page-btn--open');
  170. if (openMenus.length > 0) {
  171. if (target.parents('.navbar-page-btn--open').length === 0) {
  172. openMenus.removeClass('navbar-page-btn--open');
  173. }
  174. }
  175. // hide sidemenu
  176. if (!ignoreSideMenuHide && !contextSrv.pinned && body.find('.sidemenu').length > 0) {
  177. if (target.parents('.sidemenu').length === 0) {
  178. scope.$apply(function() {
  179. scope.contextSrv.toggleSideMenu();
  180. });
  181. }
  182. }
  183. // hide popovers
  184. var popover = elem.find('.popover');
  185. if (popover.length > 0 && target.parents('.graph-legend').length === 0) {
  186. popover.hide();
  187. }
  188. });
  189. }
  190. };
  191. }
  192. coreModule.directive('grafanaApp', grafanaAppDirective);