TimePicker.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import React, { PureComponent } from 'react';
  2. import * as rangeUtil from '@grafana/ui/src/utils/rangeutil';
  3. import { Input, RawTimeRange, TimeRange, TIME_FORMAT } from '@grafana/ui';
  4. import { toUtc, isDateTime, dateTime } from '@grafana/ui/src/utils/moment_wrapper';
  5. interface TimePickerProps {
  6. isOpen?: boolean;
  7. isUtc?: boolean;
  8. range: TimeRange;
  9. onChangeTime?: (range: RawTimeRange, scanning?: boolean) => void;
  10. }
  11. interface TimePickerState {
  12. isOpen: boolean;
  13. isUtc: boolean;
  14. rangeString: string;
  15. refreshInterval?: string;
  16. initialRange: RawTimeRange;
  17. // Input-controlled text, keep these in a shape that is human-editable
  18. fromRaw: string;
  19. toRaw: string;
  20. }
  21. const getRaw = (isUtc: boolean, range: any) => {
  22. const rawRange = {
  23. from: range.raw.from,
  24. to: range.raw.to,
  25. };
  26. if (isDateTime(rawRange.from)) {
  27. if (!isUtc) {
  28. rawRange.from = rawRange.from.local();
  29. }
  30. rawRange.from = rawRange.from.format(TIME_FORMAT);
  31. }
  32. if (isDateTime(rawRange.to)) {
  33. if (!isUtc) {
  34. rawRange.to = rawRange.to.local();
  35. }
  36. rawRange.to = rawRange.to.format(TIME_FORMAT);
  37. }
  38. return rawRange;
  39. };
  40. /**
  41. * TimePicker with dropdown menu for relative dates.
  42. *
  43. * Initialize with a range that is either based on relative rawRange.strings,
  44. * or on Moment objects.
  45. * Internally the component needs to keep a string representation in `fromRaw`
  46. * and `toRaw` for the controlled inputs.
  47. * When a time is picked, `onChangeTime` is called with the new range that
  48. * is again based on relative time strings or Moment objects.
  49. */
  50. export default class TimePicker extends PureComponent<TimePickerProps, TimePickerState> {
  51. dropdownEl: any;
  52. constructor(props) {
  53. super(props);
  54. const { range, isUtc, isOpen } = props;
  55. const rawRange = getRaw(props.isUtc, range);
  56. this.state = {
  57. isOpen: isOpen,
  58. isUtc: isUtc,
  59. rangeString: rangeUtil.describeTimeRange(range.raw),
  60. fromRaw: rawRange.from,
  61. toRaw: rawRange.to,
  62. initialRange: range.raw,
  63. refreshInterval: '',
  64. };
  65. } //Temp solution... How do detect if ds supports table format?
  66. static getDerivedStateFromProps(props: TimePickerProps, state: TimePickerState) {
  67. if (
  68. state.initialRange &&
  69. state.initialRange.from === props.range.raw.from &&
  70. state.initialRange.to === props.range.raw.to
  71. ) {
  72. return state;
  73. }
  74. const { range } = props;
  75. const rawRange = getRaw(props.isUtc, range);
  76. return {
  77. ...state,
  78. fromRaw: rawRange.from,
  79. toRaw: rawRange.to,
  80. initialRange: range.raw,
  81. rangeString: rangeUtil.describeTimeRange(range.raw),
  82. };
  83. }
  84. move(direction: number, scanning?: boolean): RawTimeRange {
  85. const { onChangeTime, range: origRange } = this.props;
  86. const range = {
  87. from: toUtc(origRange.from),
  88. to: toUtc(origRange.to),
  89. };
  90. const timespan = (range.to.valueOf() - range.from.valueOf()) / 2;
  91. let to, from;
  92. if (direction === -1) {
  93. to = range.to.valueOf() - timespan;
  94. from = range.from.valueOf() - timespan;
  95. } else if (direction === 1) {
  96. to = range.to.valueOf() + timespan;
  97. from = range.from.valueOf() + timespan;
  98. } else {
  99. to = range.to.valueOf();
  100. from = range.from.valueOf();
  101. }
  102. const nextTimeRange = {
  103. from: this.props.isUtc ? toUtc(from) : dateTime(from),
  104. to: this.props.isUtc ? toUtc(to) : dateTime(to),
  105. };
  106. if (onChangeTime) {
  107. onChangeTime(nextTimeRange);
  108. }
  109. return nextTimeRange;
  110. }
  111. handleChangeFrom = e => {
  112. this.setState({
  113. fromRaw: e.target.value,
  114. });
  115. };
  116. handleChangeTo = e => {
  117. this.setState({
  118. toRaw: e.target.value,
  119. });
  120. };
  121. handleClickApply = () => {
  122. const { onChangeTime, isUtc } = this.props;
  123. let rawRange;
  124. this.setState(
  125. state => {
  126. const { toRaw, fromRaw } = this.state;
  127. rawRange = {
  128. from: fromRaw,
  129. to: toRaw,
  130. };
  131. if (rawRange.from.indexOf('now') === -1) {
  132. rawRange.from = isUtc ? toUtc(rawRange.from, TIME_FORMAT) : dateTime(rawRange.from, TIME_FORMAT);
  133. }
  134. if (rawRange.to.indexOf('now') === -1) {
  135. rawRange.to = isUtc ? toUtc(rawRange.to, TIME_FORMAT) : dateTime(rawRange.to, TIME_FORMAT);
  136. }
  137. const rangeString = rangeUtil.describeTimeRange(rawRange);
  138. return {
  139. isOpen: false,
  140. rangeString,
  141. };
  142. },
  143. () => {
  144. if (onChangeTime) {
  145. onChangeTime(rawRange);
  146. }
  147. }
  148. );
  149. };
  150. handleClickLeft = () => this.move(-1);
  151. handleClickPicker = () => {
  152. this.setState(state => ({
  153. isOpen: !state.isOpen,
  154. }));
  155. };
  156. handleClickRight = () => this.move(1);
  157. handleClickRefresh = () => {};
  158. handleClickRelativeOption = range => {
  159. const { onChangeTime } = this.props;
  160. const rangeString = rangeUtil.describeTimeRange(range);
  161. const rawRange = {
  162. from: range.from,
  163. to: range.to,
  164. };
  165. this.setState(
  166. {
  167. toRaw: rawRange.to,
  168. fromRaw: rawRange.from,
  169. isOpen: false,
  170. rangeString,
  171. },
  172. () => {
  173. if (onChangeTime) {
  174. onChangeTime(rawRange);
  175. }
  176. }
  177. );
  178. };
  179. getTimeOptions() {
  180. return rangeUtil.getRelativeTimesList({}, this.state.rangeString);
  181. }
  182. dropdownRef = el => {
  183. this.dropdownEl = el;
  184. };
  185. renderDropdown() {
  186. const { fromRaw, isOpen, toRaw } = this.state;
  187. if (!isOpen) {
  188. return null;
  189. }
  190. const timeOptions = this.getTimeOptions();
  191. return (
  192. <div ref={this.dropdownRef} className="gf-timepicker-dropdown">
  193. <div className="popover-box">
  194. <div className="popover-box__header">
  195. <span className="popover-box__title">Quick ranges</span>
  196. </div>
  197. <div className="popover-box__body gf-timepicker-relative-section">
  198. {Object.keys(timeOptions).map(section => {
  199. const group = timeOptions[section];
  200. return (
  201. <ul key={section}>
  202. {group.map((option: any) => (
  203. <li className={option.active ? 'active' : ''} key={option.display}>
  204. <a onClick={() => this.handleClickRelativeOption(option)}>{option.display}</a>
  205. </li>
  206. ))}
  207. </ul>
  208. );
  209. })}
  210. </div>
  211. </div>
  212. <div className="popover-box">
  213. <div className="popover-box__header">
  214. <span className="popover-box__title">Custom range</span>
  215. </div>
  216. <div className="popover-box__body gf-timepicker-absolute-section">
  217. <label className="small">From:</label>
  218. <div className="gf-form-inline">
  219. <div className="gf-form max-width-28">
  220. <Input
  221. type="text"
  222. className="gf-form-input input-large timepicker-from"
  223. value={fromRaw}
  224. onChange={this.handleChangeFrom}
  225. />
  226. </div>
  227. </div>
  228. <label className="small">To:</label>
  229. <div className="gf-form-inline">
  230. <div className="gf-form max-width-28">
  231. <Input
  232. type="text"
  233. className="gf-form-input input-large timepicker-to"
  234. value={toRaw}
  235. onChange={this.handleChangeTo}
  236. />
  237. </div>
  238. </div>
  239. <div className="gf-form">
  240. <button className="btn gf-form-btn btn-secondary" onClick={this.handleClickApply}>
  241. Apply
  242. </button>
  243. </div>
  244. </div>
  245. </div>
  246. </div>
  247. );
  248. }
  249. render() {
  250. const { isUtc, rangeString, refreshInterval } = this.state;
  251. return (
  252. <div className="timepicker">
  253. <div className="navbar-buttons">
  254. <button className="btn navbar-button navbar-button--tight timepicker-left" onClick={this.handleClickLeft}>
  255. <i className="fa fa-chevron-left" />
  256. </button>
  257. <button className="btn navbar-button gf-timepicker-nav-btn" onClick={this.handleClickPicker}>
  258. <i className="fa fa-clock-o" />
  259. <span className="timepicker-rangestring">{rangeString}</span>
  260. {isUtc ? <span className="gf-timepicker-utc">UTC</span> : null}
  261. {refreshInterval ? <span className="text-warning">&nbsp; Refresh every {refreshInterval}</span> : null}
  262. </button>
  263. <button className="btn navbar-button navbar-button--tight timepicker-right" onClick={this.handleClickRight}>
  264. <i className="fa fa-chevron-right" />
  265. </button>
  266. </div>
  267. {this.renderDropdown()}
  268. </div>
  269. );
  270. }
  271. }