grafana_app.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. // 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. pageClass = data.$$route.pageClass;
  89. if (pageClass) {
  90. body.addClass(pageClass);
  91. }
  92. $("#tooltip, .tooltip").remove();
  93. // check for kiosk url param
  94. if (data.params.kiosk) {
  95. appEvents.emit('toggle-kiosk-mode');
  96. }
  97. });
  98. // handle kiosk mode
  99. appEvents.on('toggle-kiosk-mode', () => {
  100. body.toggleClass('page-kiosk-mode');
  101. });
  102. // handle in active view state class
  103. var lastActivity = new Date().getTime();
  104. var activeUser = true;
  105. var inActiveTimeLimit = 60 * 1000;
  106. function checkForInActiveUser() {
  107. if (!activeUser) {
  108. return;
  109. }
  110. // only go to activity low mode on dashboard page
  111. if (!body.hasClass('page-dashboard')) {
  112. return;
  113. }
  114. if ((new Date().getTime() - lastActivity) > inActiveTimeLimit) {
  115. activeUser = false;
  116. body.addClass('user-activity-low');
  117. }
  118. }
  119. function userActivityDetected() {
  120. lastActivity = new Date().getTime();
  121. if (!activeUser) {
  122. activeUser = true;
  123. body.removeClass('user-activity-low');
  124. }
  125. }
  126. body.mousemove(userActivityDetected);
  127. body.keydown(userActivityDetected);
  128. setInterval(checkForInActiveUser, 1000);
  129. appEvents.on('toggle-view-mode', () => {
  130. lastActivity = 0;
  131. checkForInActiveUser();
  132. });
  133. // handle document clicks that should hide things
  134. body.click(function(evt) {
  135. var target = $(evt.target);
  136. if (target.parents().length === 0) {
  137. return;
  138. }
  139. // for stuff that animates, slides out etc, clicking it needs to
  140. // hide it right away
  141. var clickAutoHide = target.closest('[data-click-hide]');
  142. if (clickAutoHide.length) {
  143. var clickAutoHideParent = clickAutoHide.parent();
  144. clickAutoHide.detach();
  145. setTimeout(function() {
  146. clickAutoHideParent.append(clickAutoHide);
  147. }, 100);
  148. }
  149. if (target.parents('.dash-playlist-actions').length === 0) {
  150. playlistSrv.stop();
  151. }
  152. // hide search
  153. if (body.find('.search-container').length > 0) {
  154. if (target.parents('.search-container').length === 0) {
  155. scope.$apply(function() {
  156. scope.appEvent('hide-dash-search');
  157. });
  158. }
  159. }
  160. // hide sidemenu
  161. if (!ignoreSideMenuHide && !contextSrv.pinned && body.find('.sidemenu').length > 0) {
  162. if (target.parents('.sidemenu').length === 0) {
  163. scope.$apply(function() {
  164. scope.contextSrv.toggleSideMenu();
  165. });
  166. }
  167. }
  168. // hide popovers
  169. var popover = elem.find('.popover');
  170. if (popover.length > 0 && target.parents('.graph-legend').length === 0) {
  171. popover.hide();
  172. }
  173. });
  174. }
  175. };
  176. }
  177. coreModule.directive('grafanaApp', grafanaAppDirective);