Files
ohif-viewer/src/components/Labelling/LabellingFlow.js
T

317 lines
8.3 KiB
JavaScript
Raw Normal View History

2019-05-30 12:08:46 -04:00
import { Icon, SelectTree } from 'react-viewerbase';
2019-05-30 00:46:45 -04:00
import React, { Component } from 'react';
2019-05-02 17:12:06 -04:00
2019-05-30 00:46:45 -04:00
import LabellingTransition from './LabellingTransition.js';
import OHIFLabellingData from './OHIFLabellingData.js';
2019-05-30 12:08:46 -04:00
import PropTypes from 'prop-types';
import bounding from '../../lib/utils/bounding.js';
import cloneDeep from 'lodash.clonedeep';
import { getAddLabelButtonStyle } from './labellingPositionUtils.js';
2019-05-02 17:12:06 -04:00
export default class LabellingFlow extends Component {
static propTypes = {
eventData: PropTypes.object.isRequired,
measurementData: PropTypes.object.isRequired,
labellingDoneCallback: PropTypes.func.isRequired,
updateLabelling: PropTypes.func.isRequired,
initialTopDistance: PropTypes.number,
2019-05-02 17:12:06 -04:00
skipAddLabelButton: PropTypes.bool,
editLocation: PropTypes.bool,
editDescription: PropTypes.bool,
2019-05-30 00:46:45 -04:00
};
2019-05-02 17:12:06 -04:00
constructor(props) {
2019-05-30 00:46:45 -04:00
super(props);
2019-05-02 17:12:06 -04:00
2019-05-30 00:46:45 -04:00
const { location, locationLabel, description } = props.measurementData;
2019-05-02 17:12:06 -04:00
2019-05-30 00:46:45 -04:00
let style = props.componentStyle;
2019-05-02 17:12:06 -04:00
if (!props.skipAddLabelButton) {
2019-05-30 00:46:45 -04:00
style = getAddLabelButtonStyle(props.measurementData, props.eventData);
2019-05-02 17:12:06 -04:00
}
this.state = {
location,
locationLabel,
description,
skipAddLabelButton: props.skipAddLabelButton,
editDescription: props.editDescription,
editLocation: props.editLocation,
componentStyle: style,
confirmationState: false,
displayComponent: true,
2019-05-30 00:46:45 -04:00
};
2019-05-02 17:12:06 -04:00
2019-05-30 00:46:45 -04:00
this.mainElement = React.createRef();
this.descriptionInput = React.createRef();
2019-05-02 17:12:06 -04:00
2019-05-30 00:46:45 -04:00
this.initialItems = OHIFLabellingData;
this.currentItems = cloneDeep(this.initialItems);
2019-05-02 17:12:06 -04:00
}
componentDidUpdate = () => {
2019-05-30 00:46:45 -04:00
this.repositionComponent();
if (this.state.editDescription) {
this.descriptionInput.current.focus();
}
2019-05-30 00:46:45 -04:00
};
2019-05-02 17:12:06 -04:00
render() {
2019-05-30 00:46:45 -04:00
let mainElementClassName = 'labellingComponent';
2019-05-02 17:12:06 -04:00
if (this.state.editDescription) {
2019-05-30 00:46:45 -04:00
mainElementClassName += ' editDescription';
2019-05-02 17:12:06 -04:00
}
2019-05-30 00:46:45 -04:00
const style = Object.assign({}, this.state.componentStyle);
2019-05-02 17:12:06 -04:00
if (this.state.skipAddLabelButton) {
if (style.left - 160 < 0) {
style.left = 0;
} else {
style.left -= 160;
}
}
if (this.state.editLocation) {
style.maxHeight = '70vh';
if (!this.initialTopDistance) {
this.initialTopDistance = window.innerHeight - window.innerHeight * 0.3;
style.top = `${this.state.componentStyle.top -
this.initialTopDistance / 2}px`;
} else {
style.top = `${this.state.componentStyle.top}px`;
}
2019-05-02 17:12:06 -04:00
}
return (
<LabellingTransition
displayComponent={this.state.displayComponent}
onTransitionExit={this.props.labellingDoneCallback}
>
<>
<div className="labellingComponent-overlay"></div>
<div
className={mainElementClassName}
style={style}
ref={this.mainElement}
onMouseLeave={this.fadeOutAndLeave}
onMouseEnter={this.clearFadeOutTimer}
>
{this.labellingStateFragment()}
</div>
</>
2019-05-02 17:12:06 -04:00
</LabellingTransition>
2019-05-30 00:46:45 -04:00
);
2019-05-02 17:12:06 -04:00
}
labellingStateFragment = () => {
const {
skipAddLabelButton,
editLocation,
description,
locationLabel,
2019-05-30 00:46:45 -04:00
} = this.state;
2019-05-02 17:12:06 -04:00
if (!skipAddLabelButton) {
return (
<>
<button
type="button"
className="addLabelButton"
onClick={this.showLabelling}
>
{this.state.location ? 'Edit' : 'Add'} Label
</button>
</>
2019-05-30 00:46:45 -04:00
);
2019-05-02 17:12:06 -04:00
} else {
if (editLocation) {
return (
<SelectTree
items={this.currentItems}
columns={1}
onSelected={this.selectTreeSelectCalback}
selectTreeFirstTitle="Assign Label"
onComponentChange={this.repositionComponent}
/>
2019-05-30 00:46:45 -04:00
);
2019-05-02 17:12:06 -04:00
} else {
return (
<>
<div
className="checkIconWrapper"
onClick={this.fadeOutAndLeaveFast}
>
2019-05-30 12:08:46 -04:00
<Icon name="check" className="checkIcon" />
2019-05-02 17:12:06 -04:00
</div>
<div className="locationDescriptionWrapper">
<div className="location">{locationLabel}</div>
<div className="description">
<input
id="descriptionInput"
ref={this.descriptionInput}
defaultValue={description || ''}
autoComplete="off"
/>
</div>
</div>
<div className="commonButtons">
<button
type="button"
className="commonButton left"
onClick={this.relabel}
>
Relabel
</button>
<button
type="button"
className="commonButton right"
onClick={this.setDescriptionUpdateMode}
>
{description ? 'Edit ' : 'Add '}
Description
</button>
</div>
<div className="editDescriptionButtons">
<button
type="button"
className="commonButton left"
onClick={this.descriptionCancel}
>
Cancel
</button>
<button
type="button"
className="commonButton right"
onClick={this.descriptionSave}
>
Save
</button>
</div>
</>
2019-05-30 00:46:45 -04:00
);
2019-05-02 17:12:06 -04:00
}
}
2019-05-30 00:46:45 -04:00
};
2019-05-02 17:12:06 -04:00
relabel = event => {
const viewportTopPosition = this.mainElement.current.offsetParent.offsetTop;
const componentStyle = {
top: event.nativeEvent.y - viewportTopPosition - 55,
left: event.nativeEvent.x,
};
2019-05-02 17:12:06 -04:00
this.setState({
editLocation: true,
componentStyle,
2019-05-30 00:46:45 -04:00
});
};
2019-05-02 17:12:06 -04:00
setDescriptionUpdateMode = () => {
2019-05-30 00:46:45 -04:00
this.descriptionInput.current.focus();
2019-05-02 17:12:06 -04:00
this.setState({
editDescription: true,
2019-05-30 00:46:45 -04:00
});
};
2019-05-02 17:12:06 -04:00
descriptionCancel = () => {
2019-05-30 00:46:45 -04:00
const { description = '' } = cloneDeep(this.state);
this.descriptionInput.current.value = description;
2019-05-02 17:12:06 -04:00
this.setState({
editDescription: false,
2019-05-30 00:46:45 -04:00
});
};
2019-05-02 17:12:06 -04:00
descriptionSave = () => {
2019-05-30 00:46:45 -04:00
const description = this.descriptionInput.current.value;
this.props.updateLabelling({ description });
2019-05-02 17:12:06 -04:00
this.setState({
description,
editDescription: false,
2019-05-30 00:46:45 -04:00
});
};
2019-05-02 17:12:06 -04:00
selectTreeSelectCalback = (event, itemSelected) => {
2019-05-30 00:46:45 -04:00
const location = itemSelected.value;
this.props.updateLabelling({ location });
2019-05-02 17:12:06 -04:00
2019-05-30 00:46:45 -04:00
const viewportTopPosition = this.mainElement.current.offsetParent.offsetTop;
2019-05-02 17:12:06 -04:00
const componentStyle = {
top: event.nativeEvent.y - viewportTopPosition - 25,
left: event.nativeEvent.x,
2019-05-30 00:46:45 -04:00
};
2019-05-02 17:12:06 -04:00
this.setState({
editLocation: false,
confirmationState: true,
location: itemSelected.value,
locationLabel: itemSelected.label,
componentStyle,
2019-05-30 00:46:45 -04:00
});
2019-05-02 17:12:06 -04:00
if (this.isTouchScreen) {
this.setTimeout = setTimeout(() => {
this.setState({
displayComponent: false,
2019-05-30 00:46:45 -04:00
});
}, 2000);
2019-05-02 17:12:06 -04:00
}
2019-05-30 00:46:45 -04:00
};
2019-05-02 17:12:06 -04:00
showLabelling = () => {
this.setState({
skipAddLabelButton: true,
editLocation: false,
2019-05-30 00:46:45 -04:00
});
};
2019-05-02 17:12:06 -04:00
fadeOutAndLeave = () => {
// Wait for 1 sec to dismiss the labelling component
this.fadeOutTimer = setTimeout(() => {
this.setState({
displayComponent: false,
2019-05-30 00:46:45 -04:00
});
}, 1000);
};
2019-05-02 17:12:06 -04:00
fadeOutAndLeaveFast = () => {
this.setState({
displayComponent: false,
2019-05-30 00:46:45 -04:00
});
};
2019-05-02 17:12:06 -04:00
clearFadeOutTimer = () => {
if (!this.fadeOutTimer) {
2019-05-30 00:46:45 -04:00
return;
2019-05-02 17:12:06 -04:00
}
2019-05-30 00:46:45 -04:00
clearTimeout(this.fadeOutTimer);
};
2019-05-02 17:12:06 -04:00
calculateTopDistance = () => {
const height = window.innerHeight - window.innerHeight * 0.3;
let top = this.state.componentStyle.top - height / 2 + 55;
if (top < 0) {
top = 0;
} else {
if (top + height > window.innerHeight) {
top -= top + height - window.innerHeight;
}
}
return top;
};
2019-05-02 17:12:06 -04:00
repositionComponent = () => {
// SetTimeout for the css animation to end.
setTimeout(() => {
2019-05-30 00:46:45 -04:00
bounding(this.mainElement);
if (this.state.editLocation) {
this.mainElement.current.style.maxHeight = '70vh';
const top = this.calculateTopDistance();
this.mainElement.current.style.top = `${top}px`;
}
2019-05-30 00:46:45 -04:00
}, 200);
};
2019-05-02 17:12:06 -04:00
}