grafana_app.ts 6.8 KB

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