Begin working toward AnnotationSpan component
parent
36e55c5fb5
commit
377e715744
|
@ -16,40 +16,21 @@ export const getAnnotations = (graph, annotations = []) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const [xStart, xEnd] = graph.xAxisRange()
|
const [xStart, xEnd] = graph.xAxisRange()
|
||||||
return annotations.reduce((acc, a) => {
|
|
||||||
// Don't render if annotation.time is outside the graph
|
return annotations.filter(a => {
|
||||||
const annoStart = +a.startTime
|
const annoStart = +a.startTime
|
||||||
const annoEnd = +a.endTime
|
const annoEnd = +a.endTime
|
||||||
const endAnnotation = {
|
|
||||||
...a,
|
// If annotation is too far left
|
||||||
id: `${a.id}-end`,
|
if (annoEnd < xStart) {
|
||||||
startTime: `${annoEnd}`,
|
return false
|
||||||
endTime: `${annoEnd}`,
|
|
||||||
}
|
|
||||||
|
|
||||||
if (annoStart < xStart) {
|
|
||||||
if (annoEnd > xStart) {
|
|
||||||
return [...acc, a, endAnnotation]
|
|
||||||
}
|
|
||||||
|
|
||||||
return acc
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If annotation is too far right
|
||||||
if (annoStart > xEnd) {
|
if (annoStart > xEnd) {
|
||||||
return acc
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// If annotation does not have duration, include in array
|
return true
|
||||||
if (annoStart === annoEnd) {
|
})
|
||||||
return [...acc, a]
|
|
||||||
}
|
|
||||||
|
|
||||||
// If annoEnd is out of bounds, just render the start point
|
|
||||||
if (annoEnd > xEnd) {
|
|
||||||
return [...acc, a]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Render both the start and end point
|
|
||||||
return [...acc, a, endAnnotation]
|
|
||||||
}, [])
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
import React, {Component, PropTypes} from 'react'
|
import React, {Component, PropTypes} from 'react'
|
||||||
|
|
||||||
import AnnotationTooltip from 'src/shared/components/AnnotationTooltip'
|
import AnnotationTooltip from 'src/shared/components/AnnotationTooltip'
|
||||||
|
import AnnotationWindow from 'src/shared/components/AnnotationWindow'
|
||||||
|
|
||||||
import {ADDING, EDITING, TEMP_ANNOTATION} from 'src/shared/annotations/helpers'
|
import {ADDING, EDITING} from 'src/shared/annotations/helpers'
|
||||||
import * as schema from 'shared/schemas'
|
import * as schema from 'shared/schemas'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
@ -11,26 +12,12 @@ import {
|
||||||
annotationStyle,
|
annotationStyle,
|
||||||
} from 'src/shared/annotations/styles'
|
} from 'src/shared/annotations/styles'
|
||||||
|
|
||||||
const idAppendage = '-end'
|
|
||||||
|
|
||||||
class Annotation extends Component {
|
class Annotation extends Component {
|
||||||
state = {
|
state = {
|
||||||
isDragging: false,
|
isDragging: false,
|
||||||
isMouseOver: false,
|
isMouseOver: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
isEndpoint = () => {
|
|
||||||
const {annotation: {id}} = this.props
|
|
||||||
|
|
||||||
return id.substring(id.length - idAppendage.length) === idAppendage
|
|
||||||
}
|
|
||||||
|
|
||||||
getStartID = () => {
|
|
||||||
const {annotation: {id}} = this.props
|
|
||||||
|
|
||||||
return id.substring(0, id.length - idAppendage.length)
|
|
||||||
}
|
|
||||||
|
|
||||||
handleStartDrag = () => {
|
handleStartDrag = () => {
|
||||||
const {mode} = this.props
|
const {mode} = this.props
|
||||||
if (mode === ADDING || mode === null) {
|
if (mode === ADDING || mode === null) {
|
||||||
|
@ -63,7 +50,7 @@ class Annotation extends Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
const {pageX} = e
|
const {pageX} = e
|
||||||
const {annotation, annotations, dygraph, onUpdateAnnotation} = this.props
|
const {annotation, dygraph, onUpdateAnnotation} = this.props
|
||||||
const {startTime} = annotation
|
const {startTime} = annotation
|
||||||
const {left} = dygraph.graphDiv.getBoundingClientRect()
|
const {left} = dygraph.graphDiv.getBoundingClientRect()
|
||||||
const [startX, endX] = dygraph.xAxisRange()
|
const [startX, endX] = dygraph.xAxisRange()
|
||||||
|
@ -92,36 +79,12 @@ class Annotation extends Component {
|
||||||
newTime = startX
|
newTime = startX
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isEndpoint()) {
|
|
||||||
const startAnnotation = annotations.find(a => a.id === this.getStartID())
|
|
||||||
if (!startAnnotation) {
|
|
||||||
return console.error('Start annotation does not exist')
|
|
||||||
}
|
|
||||||
|
|
||||||
this.counter = this.counter + 1
|
|
||||||
return onUpdateAnnotation({
|
|
||||||
...startAnnotation,
|
|
||||||
endTime: newTime,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
onUpdateAnnotation({...annotation, startTime: `${newTime}`})
|
onUpdateAnnotation({...annotation, startTime: `${newTime}`})
|
||||||
|
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
}
|
}
|
||||||
|
|
||||||
handleConfirmUpdate = annotation => {
|
|
||||||
const {onUpdateAnnotation} = this.props
|
|
||||||
|
|
||||||
if (this.isEndpoint()) {
|
|
||||||
const id = this.getStartID()
|
|
||||||
return onUpdateAnnotation({...annotation, id})
|
|
||||||
}
|
|
||||||
|
|
||||||
onUpdateAnnotation(annotation)
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const {dygraph, annotation, mode} = this.props
|
const {dygraph, annotation, mode} = this.props
|
||||||
const {isDragging, isMouseOver} = this.state
|
const {isDragging, isMouseOver} = this.state
|
||||||
|
@ -129,13 +92,10 @@ class Annotation extends Component {
|
||||||
const humanTime = `${new Date(+annotation.startTime)}`
|
const humanTime = `${new Date(+annotation.startTime)}`
|
||||||
const hasDuration = annotation.starTime !== annotation.endTime
|
const hasDuration = annotation.starTime !== annotation.endTime
|
||||||
|
|
||||||
if (annotation.id === TEMP_ANNOTATION.id) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
const isEditing = mode === EDITING
|
const isEditing = mode === EDITING
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<div>
|
||||||
<div
|
<div
|
||||||
className="dygraph-annotation"
|
className="dygraph-annotation"
|
||||||
style={annotationStyle(annotation, dygraph, isMouseOver, isDragging)}
|
style={annotationStyle(annotation, dygraph, isMouseOver, isDragging)}
|
||||||
|
@ -150,31 +110,29 @@ class Annotation extends Component {
|
||||||
onMouseEnter={this.handleMouseEnter}
|
onMouseEnter={this.handleMouseEnter}
|
||||||
onMouseLeave={this.handleMouseLeave}
|
onMouseLeave={this.handleMouseLeave}
|
||||||
/>
|
/>
|
||||||
<div
|
<div style={flagStyle(isMouseOver, isDragging, hasDuration, false)} />
|
||||||
style={flagStyle(
|
|
||||||
isMouseOver,
|
|
||||||
isDragging,
|
|
||||||
hasDuration,
|
|
||||||
this.isEndpoint()
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<AnnotationTooltip
|
<AnnotationTooltip
|
||||||
isEditing={isEditing}
|
isEditing={isEditing}
|
||||||
annotation={annotation}
|
annotation={annotation}
|
||||||
onMouseLeave={this.handleMouseLeave}
|
onMouseLeave={this.handleMouseLeave}
|
||||||
annotationState={this.state}
|
annotationState={this.state}
|
||||||
onConfirmUpdate={this.handleConfirmUpdate}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
{annotation.startTime !== annotation.endTime &&
|
||||||
|
<AnnotationWindow
|
||||||
|
key={annotation.id}
|
||||||
|
annotation={annotation}
|
||||||
|
dygraph={dygraph}
|
||||||
|
/>}
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const {arrayOf, func, shape, string} = PropTypes
|
const {func, shape, string} = PropTypes
|
||||||
|
|
||||||
Annotation.propTypes = {
|
Annotation.propTypes = {
|
||||||
mode: string,
|
mode: string,
|
||||||
annotations: arrayOf(schema.annotation).isRequired,
|
|
||||||
annotation: schema.annotation.isRequired,
|
annotation: schema.annotation.isRequired,
|
||||||
dygraph: shape({}).isRequired,
|
dygraph: shape({}).isRequired,
|
||||||
onUpdateAnnotation: func.isRequired,
|
onUpdateAnnotation: func.isRequired,
|
||||||
|
|
|
@ -30,7 +30,7 @@ class AnnotationTooltip extends Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
handleConfirmUpdate = () => {
|
handleConfirmUpdate = () => {
|
||||||
this.props.onConfirmUpdate(this.state.annotation)
|
this.props.updateAnnotation(this.state.annotation)
|
||||||
}
|
}
|
||||||
|
|
||||||
handleRejectUpdate = () => {
|
handleRejectUpdate = () => {
|
||||||
|
@ -94,10 +94,11 @@ AnnotationTooltip.propTypes = {
|
||||||
annotation: schema.annotation.isRequired,
|
annotation: schema.annotation.isRequired,
|
||||||
onMouseLeave: func.isRequired,
|
onMouseLeave: func.isRequired,
|
||||||
annotationState: shape({}),
|
annotationState: shape({}),
|
||||||
onConfirmUpdate: func.isRequired,
|
|
||||||
deleteAnnotationAsync: func.isRequired,
|
deleteAnnotationAsync: func.isRequired,
|
||||||
|
updateAnnotation: func.isRequired,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default connect(null, {
|
export default connect(null, {
|
||||||
deleteAnnotationAsync: actions.deleteAnnotationAsync,
|
deleteAnnotationAsync: actions.deleteAnnotationAsync,
|
||||||
|
updateAnnotation: actions.updateAnnotation,
|
||||||
})(AnnotationTooltip)
|
})(AnnotationTooltip)
|
||||||
|
|
|
@ -3,7 +3,6 @@ import {connect} from 'react-redux'
|
||||||
import {bindActionCreators} from 'redux'
|
import {bindActionCreators} from 'redux'
|
||||||
|
|
||||||
import Annotation from 'src/shared/components/Annotation'
|
import Annotation from 'src/shared/components/Annotation'
|
||||||
import AnnotationWindow from 'src/shared/components/AnnotationWindow'
|
|
||||||
import NewAnnotation from 'src/shared/components/NewAnnotation'
|
import NewAnnotation from 'src/shared/components/NewAnnotation'
|
||||||
import * as schema from 'src/shared/schemas'
|
import * as schema from 'src/shared/schemas'
|
||||||
|
|
||||||
|
@ -68,16 +67,9 @@ class Annotations extends Component {
|
||||||
mode={mode}
|
mode={mode}
|
||||||
annotation={a}
|
annotation={a}
|
||||||
dygraph={dygraph}
|
dygraph={dygraph}
|
||||||
annotations={annotations}
|
|
||||||
onUpdateAnnotation={handleUpdateAnnotation}
|
onUpdateAnnotation={handleUpdateAnnotation}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{annotations.filter(a => !a.id.includes('-end')).map((a, i) => {
|
|
||||||
return (
|
|
||||||
a.startTime !== a.endTime &&
|
|
||||||
<AnnotationWindow key={i} annotation={a} dygraph={dygraph} />
|
|
||||||
)
|
|
||||||
})}
|
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue