timepicker.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.zoomOut(), $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. zoomOut() {
  42. }
  43. openDropdown() {
  44. this.timeOptions = rangeUtil.getRelativeTimesList(this.panel, this.rangeString);
  45. this.refresh = {
  46. value: this.dashboard.refresh,
  47. options: _.map(this.panel.refresh_intervals, (interval: any) => {
  48. return {text: interval, value: interval};
  49. })
  50. };
  51. this.refresh.options.unshift({text: 'off'});
  52. this.$rootScope.appEvent('show-dash-editor', {
  53. src: 'app/features/dashboard/timepicker/dropdown.html',
  54. scope: this.$scope,
  55. cssClass: 'gf-timepicker-dropdown',
  56. });
  57. }
  58. applyCustom() {
  59. if (this.refresh.value !== this.dashboard.refresh) {
  60. this.timeSrv.setAutoRefresh(this.refresh.value);
  61. }
  62. this.timeSrv.setTime(this.timeRaw);
  63. this.$rootScope.appEvent('hide-dash-editor');
  64. }
  65. absoluteFromChanged() {
  66. this.timeRaw.from = moment(this.absolute.fromJs);
  67. }
  68. absoluteToChanged() {
  69. this.timeRaw.to = moment(this.absolute.toJs);
  70. }
  71. setRelativeFilter(timespan) {
  72. this.panel.now = true;
  73. var range = {from: timespan.from, to: timespan.to};
  74. if (this.panel.nowDelay) {
  75. range.to = 'now-' + this.panel.nowDelay;
  76. }
  77. this.timeSrv.setTime(range);
  78. this.$rootScope.appEvent('hide-dash-editor');
  79. }
  80. }
  81. export function settingsDirective() {
  82. 'use strict';
  83. return {
  84. restrict: 'E',
  85. templateUrl: 'app/features/dashboard/timepicker/settings.html',
  86. controller: TimePickerCtrl,
  87. scope: true,
  88. };
  89. }
  90. export function timePickerDirective() {
  91. 'use strict';
  92. return {
  93. restrict: 'E',
  94. templateUrl: 'app/features/dashboard/timepicker/timepicker.html',
  95. controller: TimePickerCtrl,
  96. bindToController: true,
  97. controllerAs: 'ctrl',
  98. scope: {
  99. dashboard: "="
  100. }
  101. };
  102. }
  103. angular.module('grafana.directives').directive('gfTimePickerSettings', settingsDirective);
  104. angular.module('grafana.directives').directive('gfTimePicker', timePickerDirective);