ExploreTimeControls.tsx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import React, { Component } from 'react';
  2. import { TimeRange, TimeZone, RawTimeRange, dateTimeForTimeZone, dateMath } from '@grafana/data';
  3. import { TimePickerWithHistory } from 'app/core/components/TimePicker/TimePickerWithHistory';
  4. import { getShiftedTimeRange, getZoomedTimeRange } from 'app/core/utils/timePicker';
  5. import { ExploreId } from 'app/types';
  6. import { TimeSyncButton } from './TimeSyncButton';
  7. export interface Props {
  8. exploreId: ExploreId;
  9. hideText?: boolean;
  10. range: TimeRange;
  11. timeZone: TimeZone;
  12. fiscalYearStartMonth: number;
  13. splitted: boolean;
  14. syncedTimes: boolean;
  15. onChangeTimeSync: () => void;
  16. onChangeTime: (range: RawTimeRange) => void;
  17. onChangeTimeZone: (timeZone: TimeZone) => void;
  18. onChangeFiscalYearStartMonth: (fiscalYearStartMonth: number) => void;
  19. }
  20. export class ExploreTimeControls extends Component<Props> {
  21. onMoveTimePicker = (direction: number) => {
  22. const { range, onChangeTime, timeZone } = this.props;
  23. const { from, to } = getShiftedTimeRange(direction, range);
  24. const nextTimeRange = {
  25. from: dateTimeForTimeZone(timeZone, from),
  26. to: dateTimeForTimeZone(timeZone, to),
  27. };
  28. onChangeTime(nextTimeRange);
  29. };
  30. onMoveForward = () => this.onMoveTimePicker(1);
  31. onMoveBack = () => this.onMoveTimePicker(-1);
  32. onChangeTimePicker = (timeRange: TimeRange) => {
  33. const adjustedFrom = dateMath.isMathString(timeRange.raw.from) ? timeRange.raw.from : timeRange.from;
  34. const adjustedTo = dateMath.isMathString(timeRange.raw.to) ? timeRange.raw.to : timeRange.to;
  35. this.props.onChangeTime({
  36. from: adjustedFrom,
  37. to: adjustedTo,
  38. });
  39. };
  40. onZoom = () => {
  41. const { range, onChangeTime, timeZone } = this.props;
  42. const { from, to } = getZoomedTimeRange(range, 2);
  43. const nextTimeRange = {
  44. from: dateTimeForTimeZone(timeZone, from),
  45. to: dateTimeForTimeZone(timeZone, to),
  46. };
  47. onChangeTime(nextTimeRange);
  48. };
  49. render() {
  50. const {
  51. range,
  52. timeZone,
  53. fiscalYearStartMonth,
  54. splitted,
  55. syncedTimes,
  56. onChangeTimeSync,
  57. hideText,
  58. onChangeTimeZone,
  59. onChangeFiscalYearStartMonth,
  60. } = this.props;
  61. const timeSyncButton = splitted ? <TimeSyncButton onClick={onChangeTimeSync} isSynced={syncedTimes} /> : undefined;
  62. const timePickerCommonProps = {
  63. value: range,
  64. timeZone,
  65. fiscalYearStartMonth,
  66. onMoveBackward: this.onMoveBack,
  67. onMoveForward: this.onMoveForward,
  68. onZoom: this.onZoom,
  69. hideText,
  70. };
  71. return (
  72. <TimePickerWithHistory
  73. {...timePickerCommonProps}
  74. timeSyncButton={timeSyncButton}
  75. isSynced={syncedTimes}
  76. widthOverride={splitted ? window.innerWidth / 2 : undefined}
  77. onChange={this.onChangeTimePicker}
  78. onChangeTimeZone={onChangeTimeZone}
  79. onChangeFiscalYearStartMonth={onChangeFiscalYearStartMonth}
  80. />
  81. );
  82. }
  83. }