2020-06-14 23:42:44 -04:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-11-16 14:19:52 -05:00
|
|
|
import PropTypes from 'prop-types';
|
2020-06-15 23:38:48 -04:00
|
|
|
import {
|
|
|
|
|
LayoutSelector as OHIFLayoutSelector,
|
|
|
|
|
ToolbarButton,
|
|
|
|
|
useViewportGrid,
|
|
|
|
|
} from '@ohif/ui';
|
2020-06-14 23:42:44 -04:00
|
|
|
|
2023-01-06 15:00:23 -05:00
|
|
|
function LayoutSelector({
|
|
|
|
|
rows,
|
|
|
|
|
columns,
|
|
|
|
|
className,
|
|
|
|
|
servicesManager,
|
|
|
|
|
...rest
|
|
|
|
|
}) {
|
2020-06-14 23:42:44 -04:00
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
2022-11-16 14:19:52 -05:00
|
|
|
const [disableSelector, setDisableSelector] = useState(false);
|
2020-06-26 13:28:30 -04:00
|
|
|
const [viewportGridState, viewportGridService] = useViewportGrid();
|
2020-06-14 23:42:44 -04:00
|
|
|
|
2023-01-06 15:00:23 -05:00
|
|
|
const { HangingProtocolService, ToolBarService } = servicesManager.services;
|
2022-11-16 14:19:52 -05:00
|
|
|
|
2020-06-26 10:40:03 -03:00
|
|
|
const closeOnOutsideClick = () => {
|
|
|
|
|
if (isOpen) {
|
|
|
|
|
setIsOpen(false);
|
2020-06-14 23:42:44 -04:00
|
|
|
}
|
2020-06-26 10:40:03 -03:00
|
|
|
};
|
|
|
|
|
|
2022-11-16 14:19:52 -05:00
|
|
|
useEffect(() => {
|
|
|
|
|
const { unsubscribe } = HangingProtocolService.subscribe(
|
|
|
|
|
HangingProtocolService.EVENTS.PROTOCOL_CHANGED,
|
|
|
|
|
evt => {
|
|
|
|
|
const { protocol } = evt;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
unsubscribe();
|
|
|
|
|
};
|
|
|
|
|
}, [HangingProtocolService]);
|
|
|
|
|
|
2020-06-26 10:40:03 -03:00
|
|
|
useEffect(() => {
|
2020-06-16 00:43:16 -04:00
|
|
|
window.addEventListener('click', closeOnOutsideClick);
|
2020-06-14 23:42:44 -04:00
|
|
|
return () => {
|
2020-06-16 00:43:16 -04:00
|
|
|
window.removeEventListener('click', closeOnOutsideClick);
|
2020-06-14 23:42:44 -04:00
|
|
|
};
|
|
|
|
|
}, [isOpen]);
|
|
|
|
|
|
2020-06-26 10:40:03 -03:00
|
|
|
useEffect(() => {
|
|
|
|
|
/* Reset to default layout when component unmounts */
|
|
|
|
|
return () => {
|
2020-06-29 16:12:42 -03:00
|
|
|
viewportGridService.setLayout({ numCols: 1, numRows: 1 });
|
2020-06-26 10:40:03 -03:00
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
2020-09-14 21:18:46 -04:00
|
|
|
const onInteractionHandler = () => setIsOpen(!isOpen);
|
2020-06-15 23:38:48 -04:00
|
|
|
const DropdownContent = isOpen ? OHIFLayoutSelector : null;
|
2020-06-14 23:42:44 -04:00
|
|
|
|
2023-01-06 15:00:23 -05:00
|
|
|
const onSelectionHandler = ({ numRows, numCols }) => {
|
|
|
|
|
// TODO Introduce a service to persist the state of the current hanging protocol/app.
|
|
|
|
|
|
|
|
|
|
// TODO Here the layout change will amount to a change of hanging protocol as specified by the extension for this layout selector tool
|
|
|
|
|
// followed by the change of the grid itself.
|
|
|
|
|
if (HangingProtocolService.getActiveProtocol().protocol.id === 'mpr') {
|
|
|
|
|
ToolBarService.recordInteraction({
|
|
|
|
|
groupId: 'MPR',
|
|
|
|
|
itemId: 'MPR',
|
|
|
|
|
interactionType: 'toggle',
|
|
|
|
|
commands: [
|
|
|
|
|
{
|
|
|
|
|
commandName: 'toggleMPR',
|
|
|
|
|
commandOptions: {},
|
|
|
|
|
context: 'CORNERSTONE',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
viewportGridService.setLayout({ numRows, numCols });
|
|
|
|
|
};
|
|
|
|
|
|
2020-06-14 23:42:44 -04:00
|
|
|
return (
|
|
|
|
|
<ToolbarButton
|
2020-06-15 00:51:36 -04:00
|
|
|
id="Layout"
|
|
|
|
|
label="Grid Layout"
|
|
|
|
|
icon="tool-layout"
|
2020-09-14 21:18:46 -04:00
|
|
|
onInteraction={onInteractionHandler}
|
2023-01-06 15:00:23 -05:00
|
|
|
className={className}
|
|
|
|
|
rounded={rest.rounded}
|
2020-06-15 23:38:48 -04:00
|
|
|
dropdownContent={
|
2020-06-16 00:43:16 -04:00
|
|
|
DropdownContent !== null && (
|
|
|
|
|
<DropdownContent
|
2022-09-20 17:43:23 -04:00
|
|
|
rows={rows}
|
|
|
|
|
columns={columns}
|
2023-01-06 15:00:23 -05:00
|
|
|
onSelection={onSelectionHandler}
|
2020-06-16 00:43:16 -04:00
|
|
|
/>
|
|
|
|
|
)
|
2020-06-15 23:38:48 -04:00
|
|
|
}
|
2022-11-16 14:19:52 -05:00
|
|
|
isActive={disableSelector ? false : isOpen}
|
2021-03-01 16:52:54 +01:00
|
|
|
type="toggle"
|
2020-06-14 23:42:44 -04:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-16 14:19:52 -05:00
|
|
|
LayoutSelector.propTypes = {
|
|
|
|
|
rows: PropTypes.number,
|
|
|
|
|
columns: PropTypes.number,
|
|
|
|
|
onLayoutChange: PropTypes.func,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LayoutSelector.defaultProps = {
|
|
|
|
|
rows: 3,
|
|
|
|
|
columns: 3,
|
|
|
|
|
onLayoutChange: () => {},
|
|
|
|
|
};
|
|
|
|
|
|
2020-06-15 00:51:36 -04:00
|
|
|
export default LayoutSelector;
|