timepicker.ts 4.8 KB

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