TimePicker.tsx 8.1 KB

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