TimePicker.tsx 8.9 KB

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