TimePicker.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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, isUtc = false): string {
  16. if (moment.isMoment(value)) {
  17. return value;
  18. }
  19. if (value.indexOf('now') !== -1) {
  20. return value;
  21. }
  22. let time: any = value;
  23. // Possible epoch
  24. if (!isNaN(time)) {
  25. time = parseInt(time, 10);
  26. }
  27. time = isUtc ? moment.utc(time) : moment(time);
  28. return time.format(DATE_FORMAT);
  29. }
  30. interface TimePickerProps {
  31. isOpen?: boolean;
  32. isUtc?: boolean;
  33. range?: RawTimeRange;
  34. onChangeTime?: (Range) => void;
  35. }
  36. interface TimePickerState {
  37. isOpen: boolean;
  38. isUtc: boolean;
  39. rangeString: string;
  40. refreshInterval?: string;
  41. initialRange?: RawTimeRange;
  42. // Input-controlled text, keep these in a shape that is human-editable
  43. fromRaw: string;
  44. toRaw: string;
  45. }
  46. export default class TimePicker extends PureComponent<TimePickerProps, TimePickerState> {
  47. dropdownEl: any;
  48. constructor(props) {
  49. super(props);
  50. this.state = {
  51. isOpen: props.isOpen,
  52. isUtc: props.isUtc,
  53. rangeString: '',
  54. fromRaw: '',
  55. toRaw: '',
  56. initialRange: DEFAULT_RANGE,
  57. refreshInterval: '',
  58. };
  59. }
  60. static getDerivedStateFromProps(props, state) {
  61. if (state.initialRange && state.initialRange === props.range) {
  62. return state;
  63. }
  64. const from = props.range ? props.range.from : DEFAULT_RANGE.from;
  65. const to = props.range ? props.range.to : DEFAULT_RANGE.to;
  66. // Ensure internal format
  67. const fromRaw = parseTime(from, props.isUtc);
  68. const toRaw = parseTime(to, props.isUtc);
  69. const range = {
  70. from: fromRaw,
  71. to: toRaw,
  72. };
  73. return {
  74. ...state,
  75. fromRaw,
  76. toRaw,
  77. initialRange: props.range,
  78. rangeString: rangeUtil.describeTimeRange(range),
  79. };
  80. }
  81. move(direction: number) {
  82. const { onChangeTime } = this.props;
  83. const { fromRaw, toRaw } = this.state;
  84. const from = dateMath.parse(fromRaw, false);
  85. const to = dateMath.parse(toRaw, true);
  86. const timespan = (to.valueOf() - from.valueOf()) / 2;
  87. let nextTo, nextFrom;
  88. if (direction === -1) {
  89. nextTo = to.valueOf() - timespan;
  90. nextFrom = from.valueOf() - timespan;
  91. } else if (direction === 1) {
  92. nextTo = to.valueOf() + timespan;
  93. nextFrom = from.valueOf() + timespan;
  94. if (nextTo > Date.now() && to < Date.now()) {
  95. nextTo = Date.now();
  96. nextFrom = from.valueOf();
  97. }
  98. } else {
  99. nextTo = to.valueOf();
  100. nextFrom = from.valueOf();
  101. }
  102. const nextRange = {
  103. from: moment(nextFrom),
  104. to: moment(nextTo),
  105. };
  106. this.setState(
  107. {
  108. rangeString: rangeUtil.describeTimeRange(nextRange),
  109. fromRaw: nextRange.from.format(DATE_FORMAT),
  110. toRaw: nextRange.to.format(DATE_FORMAT),
  111. },
  112. () => {
  113. onChangeTime(nextRange);
  114. }
  115. );
  116. }
  117. handleChangeFrom = e => {
  118. this.setState({
  119. fromRaw: e.target.value,
  120. });
  121. };
  122. handleChangeTo = e => {
  123. this.setState({
  124. toRaw: e.target.value,
  125. });
  126. };
  127. handleClickApply = () => {
  128. const { onChangeTime } = this.props;
  129. let range;
  130. this.setState(
  131. state => {
  132. const { toRaw, fromRaw } = this.state;
  133. range = {
  134. from: dateMath.parse(fromRaw, false),
  135. to: dateMath.parse(toRaw, true),
  136. };
  137. const rangeString = rangeUtil.describeTimeRange(range);
  138. return {
  139. isOpen: false,
  140. rangeString,
  141. };
  142. },
  143. () => {
  144. if (onChangeTime) {
  145. onChangeTime(range);
  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. this.setState(
  162. {
  163. toRaw: range.to,
  164. fromRaw: range.from,
  165. isOpen: false,
  166. rangeString,
  167. },
  168. () => {
  169. if (onChangeTime) {
  170. onChangeTime(range);
  171. }
  172. }
  173. );
  174. };
  175. getTimeOptions() {
  176. return rangeUtil.getRelativeTimesList({}, this.state.rangeString);
  177. }
  178. dropdownRef = el => {
  179. this.dropdownEl = el;
  180. };
  181. renderDropdown() {
  182. const { fromRaw, isOpen, toRaw } = this.state;
  183. if (!isOpen) {
  184. return null;
  185. }
  186. const timeOptions = this.getTimeOptions();
  187. return (
  188. <div ref={this.dropdownRef} className="gf-timepicker-dropdown">
  189. <div className="gf-timepicker-absolute-section">
  190. <h3 className="section-heading">Custom range</h3>
  191. <label className="small">From:</label>
  192. <div className="gf-form-inline">
  193. <div className="gf-form max-width-28">
  194. <input
  195. type="text"
  196. className="gf-form-input input-large timepicker-from"
  197. value={fromRaw}
  198. onChange={this.handleChangeFrom}
  199. />
  200. </div>
  201. </div>
  202. <label className="small">To:</label>
  203. <div className="gf-form-inline">
  204. <div className="gf-form max-width-28">
  205. <input
  206. type="text"
  207. className="gf-form-input input-large timepicker-to"
  208. value={toRaw}
  209. onChange={this.handleChangeTo}
  210. />
  211. </div>
  212. </div>
  213. {/* <label className="small">Refreshing every:</label>
  214. <div className="gf-form-inline">
  215. <div className="gf-form max-width-28">
  216. <select className="gf-form-input input-medium" ng-options="f.value as f.text for f in ctrl.refresh.options"></select>
  217. </div>
  218. </div> */}
  219. <div className="gf-form">
  220. <button className="btn gf-form-btn btn-secondary" onClick={this.handleClickApply}>
  221. Apply
  222. </button>
  223. </div>
  224. </div>
  225. <div className="gf-timepicker-relative-section">
  226. <h3 className="section-heading">Quick ranges</h3>
  227. {Object.keys(timeOptions).map(section => {
  228. const group = timeOptions[section];
  229. return (
  230. <ul key={section}>
  231. {group.map(option => (
  232. <li className={option.active ? 'active' : ''} key={option.display}>
  233. <a onClick={() => this.handleClickRelativeOption(option)}>{option.display}</a>
  234. </li>
  235. ))}
  236. </ul>
  237. );
  238. })}
  239. </div>
  240. </div>
  241. );
  242. }
  243. render() {
  244. const { isUtc, rangeString, refreshInterval } = this.state;
  245. return (
  246. <div className="timepicker">
  247. <div className="navbar-buttons">
  248. <button className="btn navbar-button navbar-button--tight timepicker-left" onClick={this.handleClickLeft}>
  249. <i className="fa fa-chevron-left" />
  250. </button>
  251. <button className="btn navbar-button gf-timepicker-nav-btn" onClick={this.handleClickPicker}>
  252. <i className="fa fa-clock-o" />
  253. <span className="timepicker-rangestring">{rangeString}</span>
  254. {isUtc ? <span className="gf-timepicker-utc">UTC</span> : null}
  255. {refreshInterval ? <span className="text-warning">&nbsp; Refresh every {refreshInterval}</span> : null}
  256. </button>
  257. <button className="btn navbar-button navbar-button--tight timepicker-right" onClick={this.handleClickRight}>
  258. <i className="fa fa-chevron-right" />
  259. </button>
  260. </div>
  261. {this.renderDropdown()}
  262. </div>
  263. );
  264. }
  265. }