timepicker.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ///<reference path="../../../headers/common.d.ts" />
  2. ///<amd-dependency path="./input_date" name="inputDate" />
  3. import angular = require('angular');
  4. import _ = require('lodash');
  5. import moment = require('moment');
  6. import kbn = require('kbn');
  7. import dateMath = require('app/core/utils/datemath');
  8. import rangeUtil = require('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. constructor(private $scope, private $rootScope, private timeSrv) {
  25. $scope.ctrl = this;
  26. $rootScope.onAppEvent('refresh', () => this.init(), $scope);
  27. $rootScope.onAppEvent('zoom-out', () => this.zoom(2), $scope);
  28. this.init();
  29. }
  30. init() {
  31. this.panel = this.dashboard.timepicker;
  32. _.defaults(this.panel, TimePickerCtrl.defaults);
  33. var time = this.timeSrv.timeRange();
  34. var timeRaw = this.timeSrv.timeRange(false);
  35. this.rangeString = rangeUtil.describeTimeRange(timeRaw);
  36. this.absolute = {fromJs: time.from.toDate(), toJs: time.to.toDate()};
  37. this.timeRaw = timeRaw;
  38. this.tooltip = this.dashboard.formatDate(time.from) + ' <br>to<br>';
  39. this.tooltip += this.dashboard.formatDate(time.to);
  40. }
  41. zoom(factor) {
  42. var range = this.timeSrv.timeRange();
  43. var timespan = (range.to.valueOf() - range.from.valueOf());
  44. var center = range.to.valueOf() - timespan/2;
  45. var to = (center + (timespan*factor)/2);
  46. var from = (center - (timespan*factor)/2);
  47. if (to > Date.now() && range.to <= Date.now()) {
  48. var offset = to - Date.now();
  49. from = from - offset;
  50. to = Date.now();
  51. }
  52. this.timeSrv.setTime({from: moment.utc(from), to: moment.utc(to) });
  53. }
  54. openDropdown() {
  55. this.timeOptions = rangeUtil.getRelativeTimesList(this.panel, this.rangeString);
  56. this.refresh = {
  57. value: this.dashboard.refresh,
  58. options: _.map(this.panel.refresh_intervals, (interval: any) => {
  59. return {text: interval, value: interval};
  60. })
  61. };
  62. this.refresh.options.unshift({text: 'off'});
  63. this.$rootScope.appEvent('show-dash-editor', {
  64. src: 'app/features/dashboard/timepicker/dropdown.html',
  65. scope: this.$scope,
  66. cssClass: 'gf-timepicker-dropdown',
  67. });
  68. }
  69. applyCustom() {
  70. if (this.refresh.value !== this.dashboard.refresh) {
  71. this.timeSrv.setAutoRefresh(this.refresh.value);
  72. }
  73. this.timeSrv.setTime(this.timeRaw);
  74. this.$rootScope.appEvent('hide-dash-editor');
  75. }
  76. absoluteFromChanged() {
  77. this.timeRaw.from = moment(this.absolute.fromJs);
  78. }
  79. absoluteToChanged() {
  80. this.timeRaw.to = moment(this.absolute.toJs);
  81. }
  82. setRelativeFilter(timespan) {
  83. this.panel.now = true;
  84. var range = {from: timespan.from, to: timespan.to};
  85. if (this.panel.nowDelay) {
  86. range.to = 'now-' + this.panel.nowDelay;
  87. }
  88. this.timeSrv.setTime(range);
  89. this.$rootScope.appEvent('hide-dash-editor');
  90. }
  91. }
  92. export function settingsDirective() {
  93. 'use strict';
  94. return {
  95. restrict: 'E',
  96. templateUrl: 'app/features/dashboard/timepicker/settings.html',
  97. controller: TimePickerCtrl,
  98. scope: true,
  99. };
  100. }
  101. export function timePickerDirective() {
  102. 'use strict';
  103. return {
  104. restrict: 'E',
  105. templateUrl: 'app/features/dashboard/timepicker/timepicker.html',
  106. controller: TimePickerCtrl,
  107. bindToController: true,
  108. controllerAs: 'ctrl',
  109. scope: {
  110. dashboard: "="
  111. }
  112. };
  113. }
  114. angular.module('grafana.directives').directive('gfTimePickerSettings', settingsDirective);
  115. angular.module('grafana.directives').directive('gfTimePicker', timePickerDirective);