2020-06-14 23:42:44 -04:00
|
|
|
import React, { useEffect, useState } from 'react';
|
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
|
|
|
|
2020-06-16 00:43:16 -04:00
|
|
|
function LayoutSelector() {
|
2020-06-14 23:42:44 -04:00
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
2020-06-26 13:28:30 -04:00
|
|
|
const [viewportGridState, viewportGridService] = useViewportGrid();
|
2020-06-14 23:42:44 -04: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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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}
|
2020-06-15 23:38:48 -04:00
|
|
|
dropdownContent={
|
2020-06-16 00:43:16 -04:00
|
|
|
DropdownContent !== null && (
|
|
|
|
|
<DropdownContent
|
|
|
|
|
onSelection={({ numRows, numCols }) => {
|
2020-06-26 13:28:30 -04:00
|
|
|
viewportGridService.setLayout({ numCols, numRows });
|
2020-06-16 00:43:16 -04:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)
|
2020-06-15 23:38:48 -04:00
|
|
|
}
|
2020-06-14 23:42:44 -04:00
|
|
|
isActive={isOpen}
|
2021-03-01 16:52:54 +01:00
|
|
|
type="toggle"
|
2020-06-14 23:42:44 -04:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-15 00:51:36 -04:00
|
|
|
export default LayoutSelector;
|