index.jsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // @flow
  15. import React from 'react'
  16. import styled from 'styled-components'
  17. import type { Execution } from '../../../types/Execution'
  18. import Arrow from '../../atoms/Arrow'
  19. import StatusIcon from '../../atoms/StatusIcon'
  20. import Palette from '../../styleUtils/Palette'
  21. import StyleProps from '../../styleUtils/StyleProps'
  22. import DateUtils from '../../../utils/DateUtils'
  23. const ArrowStyled = styled(Arrow) `
  24. opacity: ${props => props.forceShow ? 1 : 0};
  25. position: absolute;
  26. top: 0;
  27. transition: all ${StyleProps.animations.swift};
  28. ${props => props.orientation === 'left' ? 'left: -19px;' : ''}
  29. ${props => props.orientation === 'right' ? 'right: -19px;' : ''}
  30. `
  31. const Wrapper = styled.div`
  32. position: relative;
  33. height: 30px;
  34. user-select: none;
  35. &:hover ${ArrowStyled} {
  36. opacity: 1;
  37. }
  38. `
  39. const MainLine = styled.div`
  40. width: 100%;
  41. padding-top: 7px;
  42. display: flex;
  43. `
  44. const ProgressLine = styled.div`
  45. border-bottom: 2px solid ${Palette.primary};
  46. transition: all ${StyleProps.animations.swift};
  47. `
  48. const EndLine = styled.div`
  49. border-bottom: 2px solid ${Palette.grayscale[2]};
  50. transition: all ${StyleProps.animations.swift};
  51. `
  52. const ItemsWrapper = styled.div`
  53. overflow: hidden;
  54. position: absolute;
  55. top: 0;
  56. left: 0;
  57. right: 0;
  58. `
  59. const Items = styled.div`
  60. display: flex;
  61. `
  62. const Item = styled.div`
  63. display: flex;
  64. flex-direction: column;
  65. align-items: center;
  66. margin-right: 90px;
  67. cursor: pointer;
  68. min-width: 75px;
  69. max-width: 75px;
  70. `
  71. const ItemLabel = styled.div`
  72. font-size: 12px;
  73. color: ${Palette.grayscale[4]};
  74. margin-top: 2px;
  75. ${props => props.selected ? `color: ${Palette.black};` : ''}
  76. ${props => props.selected ? `font-weight: ${StyleProps.fontWeights.medium};` : ''}
  77. `
  78. type Props = {
  79. items: Execution[],
  80. selectedItem: ?Execution,
  81. onPreviousClick: () => void,
  82. onNextClick: () => void,
  83. onItemClick: (item: Execution) => void,
  84. }
  85. class Timeline extends React.Component<Props> {
  86. itemsRef: HTMLElement
  87. progressLineRef: HTMLElement
  88. wrapperRef: HTMLElement
  89. itemRef: HTMLElement
  90. endLineRef: HTMLElement
  91. componentDidMount() {
  92. this.moveToSelectedItem()
  93. if (!this.itemsRef) {
  94. return
  95. }
  96. this.itemsRef.style.transition = `all ${StyleProps.animations.swift}`
  97. }
  98. componentDidUpdate() {
  99. if (this.itemsRef && !this.itemsRef.style.transition) {
  100. this.itemsRef.style.transition = `all ${StyleProps.animations.swift}`
  101. }
  102. this.moveToSelectedItem()
  103. }
  104. moveToSelectedItem() {
  105. if (!this.progressLineRef || !this.endLineRef) {
  106. return
  107. }
  108. if (!this.itemRef || !this.props.selectedItem || !this.itemsRef) {
  109. this.progressLineRef.style.width = '0'
  110. this.endLineRef.style.width = '100%'
  111. return
  112. }
  113. // $FlowIssue
  114. let itemIndex = this.props.items.findIndex(i => i.id === this.props.selectedItem.id)
  115. let halfWidth = this.wrapperRef.offsetWidth / 2
  116. let itemGap = this.itemRef.offsetWidth + 90
  117. let itemHalfWidth = this.itemRef.offsetWidth / 2
  118. let offset = (halfWidth - (itemGap * itemIndex)) - itemHalfWidth
  119. this.itemsRef.style.marginLeft = `${offset}px`
  120. let lastItemPos = (itemGap * (this.props.items.length - 1)) + offset + itemHalfWidth
  121. this.progressLineRef.style.width = `${lastItemPos}px`
  122. this.endLineRef.style.width = `${Math.max(this.wrapperRef.offsetWidth - lastItemPos, 0)}px`
  123. }
  124. renderMainLine() {
  125. return (
  126. <MainLine>
  127. <ProgressLine innerRef={line => { this.progressLineRef = line }} />
  128. <EndLine innerRef={line => { this.endLineRef = line }} />
  129. </MainLine>
  130. )
  131. }
  132. renderItems() {
  133. if (!this.props.items || !this.props.items.length) {
  134. return null
  135. }
  136. return (
  137. <ItemsWrapper>
  138. <Items innerRef={items => { this.itemsRef = items }}>
  139. {this.props.items.map(item => (
  140. <Item
  141. key={item.id}
  142. innerRef={item => { this.itemRef = item }}
  143. onClick={() => { this.props.onItemClick(item) }}
  144. >
  145. <StatusIcon status={item.status} useBackground />
  146. <ItemLabel selected={this.props.selectedItem && this.props.selectedItem.id === item.id}>
  147. {DateUtils.getLocalTime(item.created_at).format('DD MMM YYYY')}
  148. </ItemLabel>
  149. </Item>
  150. ))}
  151. </Items>
  152. </ItemsWrapper>
  153. )
  154. }
  155. render() {
  156. return (
  157. <Wrapper innerRef={w => { this.wrapperRef = w }}>
  158. <ArrowStyled
  159. orientation="left"
  160. forceShow={!this.props.items || !this.props.items.length}
  161. primary={Boolean(this.props.items && this.props.items.length)}
  162. onClick={this.props.onPreviousClick}
  163. />
  164. {this.renderMainLine()}
  165. {this.renderItems()}
  166. <ArrowStyled
  167. orientation="right"
  168. forceShow={!this.props.items || !this.props.items.length}
  169. onClick={this.props.onNextClick}
  170. />
  171. </Wrapper>
  172. )
  173. }
  174. }
  175. export default Timeline