timepicker.ts 4.8 KB

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