import _ from "lodash" import React from "react" import { Image } from "@mwater/expressions" import ImageThumbnailComponent from "../ImageThumbnailComponent" import ImagePopupComponent from "../ImagePopupComponent" import AquagenxCBTPopupComponent from "./AquagenxCBTPopupComponent" import AquagenxCBTDisplayComponent from "./AquagenxCBTDisplayComponent" import { InnerFormContext } from "../formContext" export interface AquagenxCBTAnswerComponentProps { /** Value contains two entries: image and cbt */ value?: any onValueChange: any questionId: string formCtx: InnerFormContext } interface AquagenxCBTAnswerComponentState { imageModal: any aquagenxModal: any } // Based on https://www.aquagenx.com/wp-content/uploads/2013/12/Aquagenx-CBT-Instructions-v3.pdf export default class AquagenxCBTAnswerComponent extends React.Component< AquagenxCBTAnswerComponentProps, AquagenxCBTAnswerComponentState > { static defaultProps = { value: { image: null, cbt: null } } constructor(props: AquagenxCBTAnswerComponentProps) { super(props) this.state = { imageModal: null, aquagenxModal: null } } focus() { return null } handleClickImage = () => { const modal = React.createElement(ImagePopupComponent, { imageManager: this.props.formCtx.imageManager, image: this.props.value.image, T: this.props.formCtx.T, onRemove: () => { this.setState({ imageModal: null }) const value = _.clone(this.props.value) value.image = null return this.props.onValueChange(value) }, onClose: () => { return this.setState({ imageModal: null }) }, onRotate: () => { const value = _.clone(this.props.value) value.image = _.extend({}, value.image, { rotation: ((value.image.rotation || 0) + 90) % 360 }) return this.props.onValueChange(value) } }) return this.setState({ imageModal: modal }) } handleAdd = () => { // Call imageAcquirer return this.props.formCtx.imageAcquirer!.acquire( {}, (image: Image) => { // Add to model const value = _.clone(this.props.value) value.image = image return this.props.onValueChange(value) }, (err: any) => { throw err } ) } handleEditClick = () => { const modal = React.createElement(AquagenxCBTPopupComponent, { value: this.props.value, questionId: this.props.questionId, onSave: (value: any) => { this.setState({ aquagenxModal: null }) return this.props.onValueChange(value) }, onClose: () => { return this.setState({ aquagenxModal: null }) }, formCtx: this.props.formCtx }) return this.setState({ aquagenxModal: modal }) } handleClearClick = () => { const value = _.clone(this.props.value) value.cbt = null return this.props.onValueChange(value) } renderImage() { return (