TimePicker.tsx 8.1 KB

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