TimePicker.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 } from 'app/types/series';
  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. }
  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. this.setState(
  121. {
  122. rangeString: rangeUtil.describeTimeRange(nextRange),
  123. fromRaw: nextRange.from.format(DATE_FORMAT),
  124. toRaw: nextRange.to.format(DATE_FORMAT),
  125. },
  126. () => {
  127. onChangeTime(nextRange, scanning);
  128. }
  129. );
  130. return nextRange;
  131. }
  132. handleChangeFrom = e => {
  133. this.setState({
  134. fromRaw: e.target.value,
  135. });
  136. };
  137. handleChangeTo = e => {
  138. this.setState({
  139. toRaw: e.target.value,
  140. });
  141. };
  142. handleClickApply = () => {
  143. const { onChangeTime } = this.props;
  144. let range;
  145. this.setState(
  146. state => {
  147. const { toRaw, fromRaw } = this.state;
  148. range = {
  149. from: dateMath.parse(fromRaw, false),
  150. to: dateMath.parse(toRaw, true),
  151. };
  152. const rangeString = rangeUtil.describeTimeRange(range);
  153. return {
  154. isOpen: false,
  155. rangeString,
  156. };
  157. },
  158. () => {
  159. if (onChangeTime) {
  160. onChangeTime(range);
  161. }
  162. }
  163. );
  164. };
  165. handleClickLeft = () => this.move(-1);
  166. handleClickPicker = () => {
  167. this.setState(state => ({
  168. isOpen: !state.isOpen,
  169. }));
  170. };
  171. handleClickRight = () => this.move(1);
  172. handleClickRefresh = () => {};
  173. handleClickRelativeOption = range => {
  174. const { onChangeTime } = this.props;
  175. const rangeString = rangeUtil.describeTimeRange(range);
  176. this.setState(
  177. {
  178. toRaw: range.to,
  179. fromRaw: range.from,
  180. isOpen: false,
  181. rangeString,
  182. },
  183. () => {
  184. if (onChangeTime) {
  185. onChangeTime(range);
  186. }
  187. }
  188. );
  189. };
  190. getTimeOptions() {
  191. return rangeUtil.getRelativeTimesList({}, this.state.rangeString);
  192. }
  193. dropdownRef = el => {
  194. this.dropdownEl = el;
  195. };
  196. renderDropdown() {
  197. const { fromRaw, isOpen, toRaw } = this.state;
  198. if (!isOpen) {
  199. return null;
  200. }
  201. const timeOptions = this.getTimeOptions();
  202. return (
  203. <div ref={this.dropdownRef} className="gf-timepicker-dropdown">
  204. <div className="gf-timepicker-absolute-section">
  205. <h3 className="section-heading">Custom range</h3>
  206. <label className="small">From:</label>
  207. <div className="gf-form-inline">
  208. <div className="gf-form max-width-28">
  209. <input
  210. type="text"
  211. className="gf-form-input input-large timepicker-from"
  212. value={fromRaw}
  213. onChange={this.handleChangeFrom}
  214. />
  215. </div>
  216. </div>
  217. <label className="small">To:</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-to"
  223. value={toRaw}
  224. onChange={this.handleChangeTo}
  225. />
  226. </div>
  227. </div>
  228. {/* <label className="small">Refreshing every:</label>
  229. <div className="gf-form-inline">
  230. <div className="gf-form max-width-28">
  231. <select className="gf-form-input input-medium" ng-options="f.value as f.text for f in ctrl.refresh.options"></select>
  232. </div>
  233. </div> */}
  234. <div className="gf-form">
  235. <button className="btn gf-form-btn btn-secondary" onClick={this.handleClickApply}>
  236. Apply
  237. </button>
  238. </div>
  239. </div>
  240. <div className="gf-timepicker-relative-section">
  241. <h3 className="section-heading">Quick ranges</h3>
  242. {Object.keys(timeOptions).map(section => {
  243. const group = timeOptions[section];
  244. return (
  245. <ul key={section}>
  246. {group.map(option => (
  247. <li className={option.active ? 'active' : ''} key={option.display}>
  248. <a onClick={() => this.handleClickRelativeOption(option)}>{option.display}</a>
  249. </li>
  250. ))}
  251. </ul>
  252. );
  253. })}
  254. </div>
  255. </div>
  256. );
  257. }
  258. render() {
  259. const { isUtc, rangeString, refreshInterval } = this.state;
  260. return (
  261. <div className="timepicker">
  262. <div className="navbar-buttons">
  263. <button className="btn navbar-button navbar-button--tight timepicker-left" onClick={this.handleClickLeft}>
  264. <i className="fa fa-chevron-left" />
  265. </button>
  266. <button className="btn navbar-button gf-timepicker-nav-btn" onClick={this.handleClickPicker}>
  267. <i className="fa fa-clock-o" />
  268. <span className="timepicker-rangestring">{rangeString}</span>
  269. {isUtc ? <span className="gf-timepicker-utc">UTC</span> : null}
  270. {refreshInterval ? <span className="text-warning">&nbsp; Refresh every {refreshInterval}</span> : null}
  271. </button>
  272. <button className="btn navbar-button navbar-button--tight timepicker-right" onClick={this.handleClickRight}>
  273. <i className="fa fa-chevron-right" />
  274. </button>
  275. </div>
  276. {this.renderDropdown()}
  277. </div>
  278. );
  279. }
  280. }