DashNavTimeControls.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React, { Component } from 'react';
  2. import { Unsubscribable } from 'rxjs';
  3. import { dateMath, TimeRange, TimeZone } from '@grafana/data';
  4. import { TimeRangeUpdatedEvent } from '@grafana/runtime';
  5. import { defaultIntervals, RefreshPicker, ToolbarButtonRow } from '@grafana/ui';
  6. import { TimePickerWithHistory } from 'app/core/components/TimePicker/TimePickerWithHistory';
  7. import { appEvents } from 'app/core/core';
  8. import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
  9. import { ShiftTimeEvent, ShiftTimeEventDirection, ZoomOutEvent } from '../../../../types/events';
  10. import { DashboardModel } from '../../state';
  11. export interface Props {
  12. dashboard: DashboardModel;
  13. onChangeTimeZone: (timeZone: TimeZone) => void;
  14. }
  15. export class DashNavTimeControls extends Component<Props> {
  16. private sub?: Unsubscribable;
  17. componentDidMount() {
  18. this.sub = this.props.dashboard.events.subscribe(TimeRangeUpdatedEvent, () => this.forceUpdate());
  19. }
  20. componentWillUnmount() {
  21. this.sub?.unsubscribe();
  22. }
  23. onChangeRefreshInterval = (interval: string) => {
  24. getTimeSrv().setAutoRefresh(interval);
  25. this.forceUpdate();
  26. };
  27. onRefresh = () => {
  28. getTimeSrv().refreshTimeModel();
  29. return Promise.resolve();
  30. };
  31. onMoveBack = () => {
  32. appEvents.publish(new ShiftTimeEvent({ direction: ShiftTimeEventDirection.Left }));
  33. };
  34. onMoveForward = () => {
  35. appEvents.publish(new ShiftTimeEvent({ direction: ShiftTimeEventDirection.Right }));
  36. };
  37. onChangeTimePicker = (timeRange: TimeRange) => {
  38. const { dashboard } = this.props;
  39. const panel = dashboard.timepicker;
  40. const hasDelay = panel.nowDelay && timeRange.raw.to === 'now';
  41. const adjustedFrom = dateMath.isMathString(timeRange.raw.from) ? timeRange.raw.from : timeRange.from;
  42. const adjustedTo = dateMath.isMathString(timeRange.raw.to) ? timeRange.raw.to : timeRange.to;
  43. const nextRange = {
  44. from: adjustedFrom,
  45. to: hasDelay ? 'now-' + panel.nowDelay : adjustedTo,
  46. };
  47. getTimeSrv().setTime(nextRange);
  48. };
  49. onChangeTimeZone = (timeZone: TimeZone) => {
  50. this.props.dashboard.timezone = timeZone;
  51. this.props.onChangeTimeZone(timeZone);
  52. this.onRefresh();
  53. };
  54. onChangeFiscalYearStartMonth = (month: number) => {
  55. this.props.dashboard.fiscalYearStartMonth = month;
  56. this.onRefresh();
  57. };
  58. onZoom = () => {
  59. appEvents.publish(new ZoomOutEvent({ scale: 2 }));
  60. };
  61. render() {
  62. const { dashboard } = this.props;
  63. const { refresh_intervals } = dashboard.timepicker;
  64. const intervals = getTimeSrv().getValidIntervals(refresh_intervals || defaultIntervals);
  65. const timePickerValue = getTimeSrv().timeRange();
  66. const timeZone = dashboard.getTimezone();
  67. const fiscalYearStartMonth = dashboard.fiscalYearStartMonth;
  68. const hideIntervalPicker = dashboard.panelInEdit?.isEditing;
  69. return (
  70. <ToolbarButtonRow>
  71. <TimePickerWithHistory
  72. value={timePickerValue}
  73. onChange={this.onChangeTimePicker}
  74. timeZone={timeZone}
  75. fiscalYearStartMonth={fiscalYearStartMonth}
  76. onMoveBackward={this.onMoveBack}
  77. onMoveForward={this.onMoveForward}
  78. onZoom={this.onZoom}
  79. onChangeTimeZone={this.onChangeTimeZone}
  80. onChangeFiscalYearStartMonth={this.onChangeFiscalYearStartMonth}
  81. />
  82. <RefreshPicker
  83. onIntervalChanged={this.onChangeRefreshInterval}
  84. onRefresh={this.onRefresh}
  85. value={dashboard.refresh}
  86. intervals={intervals}
  87. tooltip="Refresh dashboard"
  88. noIntervalPicker={hideIntervalPicker}
  89. />
  90. </ToolbarButtonRow>
  91. );
  92. }
  93. }