timepicker.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import angular from 'angular';
  4. import moment from 'moment';
  5. import * as rangeUtil from 'app/core/utils/rangeutil';
  6. export class TimePickerCtrl {
  7. static tooltipFormat = 'MMM D, YYYY HH:mm:ss';
  8. static defaults = {
  9. time_options: ['5m', '15m', '1h', '6h', '12h', '24h', '2d', '7d', '30d'],
  10. refresh_intervals: ['5s', '10s', '30s', '1m', '5m', '15m', '30m', '1h', '2h', '1d'],
  11. };
  12. dashboard: any;
  13. panel: any;
  14. absolute: any;
  15. timeRaw: any;
  16. tooltip: string;
  17. rangeString: string;
  18. timeOptions: any;
  19. refresh: any;
  20. isOpen: boolean;
  21. isUtc: boolean;
  22. /** @ngInject */
  23. constructor(private $scope, private $rootScope, private timeSrv) {
  24. $scope.ctrl = this;
  25. $rootScope.onAppEvent('zoom-out', () => this.zoom(2), $scope);
  26. $rootScope.onAppEvent('refresh', () => this.init(), $scope);
  27. $rootScope.onAppEvent('dash-editor-hidden', () => this.isOpen = false, $scope);
  28. this.init();
  29. }
  30. init() {
  31. this.panel = this.dashboard.timepicker;
  32. _.defaults(this.panel, TimePickerCtrl.defaults);
  33. var time = angular.copy(this.timeSrv.timeRange());
  34. var timeRaw = angular.copy(this.timeSrv.timeRange(false));
  35. if (this.dashboard.timezone === 'browser') {
  36. time.from.local();
  37. time.to.local();
  38. if (moment.isMoment(timeRaw.from)) {
  39. timeRaw.from.local();
  40. }
  41. if (moment.isMoment(timeRaw.to)) {
  42. timeRaw.to.local();
  43. }
  44. } else {
  45. this.isUtc = true;
  46. }
  47. this.rangeString = rangeUtil.describeTimeRange(timeRaw);
  48. this.absolute = {fromJs: time.from.toDate(), toJs: time.to.toDate()};
  49. this.tooltip = this.dashboard.formatDate(time.from) + ' <br>to<br>';
  50. this.tooltip += this.dashboard.formatDate(time.to);
  51. // do not update time raw when dropdown is open
  52. // as auto refresh will reset the from/to input fields
  53. if (!this.isOpen) {
  54. this.timeRaw = timeRaw;
  55. }
  56. }
  57. zoom(factor) {
  58. var range = this.timeSrv.timeRange();
  59. var timespan = (range.to.valueOf() - range.from.valueOf());
  60. var center = range.to.valueOf() - timespan/2;
  61. var to = (center + (timespan*factor)/2);
  62. var from = (center - (timespan*factor)/2);
  63. if (to > Date.now() && range.to <= Date.now()) {
  64. var offset = to - Date.now();
  65. from = from - offset;
  66. to = Date.now();
  67. }
  68. this.timeSrv.setTime({from: moment.utc(from), to: moment.utc(to) });
  69. }
  70. openDropdown() {
  71. this.init();
  72. this.isOpen = true;
  73. this.timeOptions = rangeUtil.getRelativeTimesList(this.panel, this.rangeString);
  74. this.refresh = {
  75. value: this.dashboard.refresh,
  76. options: _.map(this.panel.refresh_intervals, (interval: any) => {
  77. return {text: interval, value: interval};
  78. })
  79. };
  80. this.refresh.options.unshift({text: 'off'});
  81. this.$rootScope.appEvent('show-dash-editor', {
  82. src: 'app/features/dashboard/timepicker/dropdown.html',
  83. scope: this.$scope,
  84. cssClass: 'gf-timepicker-dropdown',
  85. });
  86. }
  87. applyCustom() {
  88. if (this.refresh.value !== this.dashboard.refresh) {
  89. this.timeSrv.setAutoRefresh(this.refresh.value);
  90. }
  91. this.timeSrv.setTime(this.timeRaw, true);
  92. this.$rootScope.appEvent('hide-dash-editor');
  93. }
  94. absoluteFromChanged() {
  95. this.timeRaw.from = this.getAbsoluteMomentForTimezone(this.absolute.fromJs);
  96. }
  97. absoluteToChanged() {
  98. this.timeRaw.to = this.getAbsoluteMomentForTimezone(this.absolute.toJs);
  99. }
  100. getAbsoluteMomentForTimezone(jsDate) {
  101. return this.dashboard.timezone === 'browser' ? moment(jsDate) : moment(jsDate).utc();
  102. }
  103. setRelativeFilter(timespan) {
  104. this.panel.now = true;
  105. var range = {from: timespan.from, to: timespan.to};
  106. if (this.panel.nowDelay && range.to === 'now') {
  107. range.to = 'now-' + this.panel.nowDelay;
  108. }
  109. this.timeSrv.setTime(range);
  110. this.$rootScope.appEvent('hide-dash-editor');
  111. }
  112. }
  113. export function settingsDirective() {
  114. 'use strict';
  115. return {
  116. restrict: 'E',
  117. templateUrl: 'app/features/dashboard/timepicker/settings.html',
  118. controller: TimePickerCtrl,
  119. bindToController: true,
  120. controllerAs: 'ctrl',
  121. scope: {
  122. dashboard: "="
  123. }
  124. };
  125. }
  126. export function timePickerDirective() {
  127. 'use strict';
  128. return {
  129. restrict: 'E',
  130. templateUrl: 'app/features/dashboard/timepicker/timepicker.html',
  131. controller: TimePickerCtrl,
  132. bindToController: true,
  133. controllerAs: 'ctrl',
  134. scope: {
  135. dashboard: "="
  136. }
  137. };
  138. }
  139. angular.module('grafana.directives').directive('gfTimePickerSettings', settingsDirective);
  140. angular.module('grafana.directives').directive('gfTimePicker', timePickerDirective);
  141. import {inputDateDirective} from './input_date';
  142. angular.module("grafana.directives").directive('inputDatetime', inputDateDirective);