Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotations functionalities #761

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 83 additions & 49 deletions src/components/MarkersDisplay/Annotations/AnnotationLayerSelect.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
import React, { useEffect, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import {
parseExternalAnnotationPage,
parseExternalAnnotationResource
} from '@Services/annotations-parser';

const AnnotationLayerSelect = ({ annotationLayers = [], duration = 0, setDisplayedAnnotationLayers }) => {
const AnnotationLayerSelect = ({
canvasAnnotationLayers = [],
duration = 0,
setDisplayedAnnotationLayers,
setAutoScrollEnabled,
autoScrollEnabled,
}) => {
const [selectedAnnotationLayers, setSelectedAnnotationLayers] = useState([]);
const [isOpen, setIsOpen] = useState(false);
const [selectedAll, setSelectedAll] = useState(false);

useEffect(() => {
if (annotationLayers?.length > 0) {
// Reset state when Canvas changes
setSelectedAnnotationLayers([]);
setDisplayedAnnotationLayers([]);
setSelectedAll(false);
setIsOpen(false);

if (canvasAnnotationLayers?.length > 0) {
// Sort annotation sets alphabetically
annotationLayers.sort((a, b) => a.label.localeCompare(b.label));
canvasAnnotationLayers.sort((a, b) => a.label.localeCompare(b.label));
// Select the first annotation set on page load
findOrFetchandParseLinkedAnnotations(annotationLayers[0]);
findOrFetchandParseLinkedAnnotations(canvasAnnotationLayers[0]);
}
}, [annotationLayers]);
}, [canvasAnnotationLayers]);

const isSelected = (layer) => selectedAnnotationLayers.includes(layer.label);
const isSelected = useCallback((layer) => {
return selectedAnnotationLayers.includes(layer.label);
}, [selectedAnnotationLayers]);
const toggleDropdown = () => setIsOpen((prev) => !prev);

/**
Expand Down Expand Up @@ -68,7 +82,7 @@ const AnnotationLayerSelect = ({ annotationLayers = [], duration = 0, setDisplay
setSelectedAll(selectAllUpdated);
if (selectAllUpdated) {
await Promise.all(
annotationLayers.map((annotationLayer) => {
canvasAnnotationLayers.map((annotationLayer) => {
findOrFetchandParseLinkedAnnotations(annotationLayer);
})
);
Expand Down Expand Up @@ -104,52 +118,72 @@ const AnnotationLayerSelect = ({ annotationLayers = [], duration = 0, setDisplay
setDisplayedAnnotationLayers((prev) => [...prev, annotationLayer]);
};

return (
<div className="ramp--annotatations__multi-select">
<div className="ramp--annotations__multi-select-header" onClick={toggleDropdown}>
{selectedAnnotationLayers.length > 0
? `${selectedAnnotationLayers.length} of ${annotationLayers.length} layers selected`
: "Select Annotation layer(s)"}
<span className={`annotations-dropdown-arrow ${isOpen ? "open" : ""}`}>▼</span>
if (canvasAnnotationLayers?.length > 0) {
return (
<div className="ramp--annotations__multi-select" data-testid="annotation-multi-select">
<div className="ramp--annotations__multi-select-header" onClick={toggleDropdown}>
{selectedAnnotationLayers.length > 0
? `${selectedAnnotationLayers.length} of ${canvasAnnotationLayers.length} layers selected`
: "Select Annotation layer(s)"}
<span className={`annotations-dropdown-arrow ${isOpen ? "open" : ""}`}>▼</span>
</div>
{isOpen && (
<ul className="annotations-dropdown-menu">
{
// Only show select all option when there's more than one annotation layer
canvasAnnotationLayers?.length > 1 &&
<li key="select-all" className="annotations-dropdown-item">
<label>
<input
type="checkbox"
checked={selectedAll}
onChange={handleSelectAll}
/>
Show all Annotation layers
</label>
</li>
}
{canvasAnnotationLayers.map((annotationLayer, index) => (
<li key={`annotaion-layer-${index}`} className="annotations-dropdown-item">
<label>
<input
type="checkbox"
checked={isSelected(annotationLayer)}
onChange={() => handleSelect(annotationLayer)}
/>
{annotationLayer.label}
</label>
</li>
))}
</ul>
)}
<div className="ramp--annotations__scroll" data-testid="annotations-scroll">
<input
type="checkbox"
id="scroll-check"
name="scrollcheck"
aria-checked={autoScrollEnabled}
title='Auto-scroll with media'
checked={autoScrollEnabled}
onChange={() => { setAutoScrollEnabled(!autoScrollEnabled); }}
/>
<label htmlFor="scroll-check" title='Auto-scroll with media'>
Auto-scroll with media
</label>
</div>
</div>
{isOpen && (
<ul className="annotations-dropdown-menu">
{
// Only show select all option when there's more than one annotation layer
annotationLayers?.length > 1 &&
<li key="select-all" className="annotations-dropdown-item">
<label>
<input
type="checkbox"
checked={selectedAll}
onChange={handleSelectAll}
/>
Show all Annotation layers
</label>
</li>
}
{annotationLayers.map((annotationLayer, index) => (
<li key={`annotaion-layer-${index}`} className="annotations-dropdown-item">
<label>
<input
type="checkbox"
checked={isSelected(annotationLayer)}
onChange={() => handleSelect(annotationLayer)}
/>
{annotationLayer.label}
</label>
</li>
))}
</ul>
)}
</div>
);
);
} else {
return null;
};
};

AnnotationLayerSelect.propTypes = {
annotationLayers: PropTypes.array.isRequired,
canvasAnnotationLayers: PropTypes.array.isRequired,
duration: PropTypes.number.isRequired,
setDisplayedAnnotationLayers: PropTypes.func.isRequired
setDisplayedAnnotationLayers: PropTypes.func.isRequired,
setAutoScrollEnabled: PropTypes.func.isRequired,
autoScrollEnabled: PropTypes.bool.isRequired,
};

export default AnnotationLayerSelect;
Loading