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

274 lines
6.9 KiB
JavaScript
Raw Normal View History

2019-05-30 00:46:45 -04:00
import React, { Component } from 'react';
import PropTypes from 'prop-types';
2019-05-02 17:12:06 -04:00
2019-05-30 00:46:45 -04:00
import cloneDeep from 'lodash.clonedeep';
import bounding from '../../lib/utils/bounding.js';
2019-05-02 17:12:06 -04:00
2019-05-30 00:46:45 -04:00
import LabellingTransition from './LabellingTransition.js';
import { SelectTree, Icon } from 'react-viewerbase';
import { getAddLabelButtonStyle } from './labellingPositionUtils.js';
2019-05-02 17:12:06 -04:00
2019-05-30 00:46:45 -04:00
import OHIFLabellingData from './OHIFLabellingData.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,
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();
};
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) {
2019-05-30 00:46:45 -04:00
style.left -= 160;
2019-05-02 17:12:06 -04:00
}
return (
<LabellingTransition
displayComponent={this.state.displayComponent}
onTransitionExit={this.props.labellingDoneCallback}
>
<div
className={mainElementClassName}
style={style}
ref={this.mainElement}
onMouseLeave={this.fadeOutAndLeave}
onMouseEnter={this.clearFadeOutTimer}
>
{this.labellingStateFragment()}
</div>
</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-28 14:57:07 -04:00
<Icon name="check-solid" 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 = () => {
this.setState({
editLocation: true,
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: this.state.componentStyle.left,
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
repositionComponent = () => {
// SetTimeout for the css animation to end.
setTimeout(() => {
2019-05-30 00:46:45 -04:00
bounding(this.mainElement);
}, 200);
};
2019-05-02 17:12:06 -04:00
}