From 589d02a5d2a26465175892382c89b1bda07c5434 Mon Sep 17 00:00:00 2001 From: Pedro Dinis Date: Wed, 22 Jan 2025 22:02:06 +0000 Subject: [PATCH] Refactor annotation list accordions to solve issues with opening annotations --- .../DocumentAnnotations.vue | 107 ++++++++---------- src/store/document.js | 6 +- 2 files changed, 53 insertions(+), 60 deletions(-) diff --git a/src/components/DocumentAnnotations/DocumentAnnotations.vue b/src/components/DocumentAnnotations/DocumentAnnotations.vue index d5a80b7e..fae5cf44 100644 --- a/src/components/DocumentAnnotations/DocumentAnnotations.vue +++ b/src/components/DocumentAnnotations/DocumentAnnotations.vue @@ -51,17 +51,14 @@ :key="indexGroup" :class="[ 'annotation-set-group', - !annotationSetsAccordion[indexGroup] === true && - 'annotation-set-collapsed', + !isAccordionOpen(annotationSet) && 'annotation-set-collapsed', ]" > -
+
@@ -80,7 +77,7 @@ class="labelset-action-buttons" >
- +
annotationSetToFind.id === annotationSet.id - ); - const newAnnotationSetsAccordion = [...this.annotationSetsAccordion]; - newAnnotationSetsAccordion[index] = true; + const newAnnotationSetsAccordion = { + ...this.annotationSetsAccordion, + }; + newAnnotationSetsAccordion[ + annotationSet.id || annotationSet.label_set.id + ] = true; this.annotationSetsAccordion = newAnnotationSetsAccordion; } } @@ -272,51 +270,38 @@ export default { annotationSet.labels.length === 0 && this.isSearchingAnnotationList ); }, - - toggleAccordion(index) { - const newAnnotationSetsAccordion = [...this.annotationSetsAccordion]; - newAnnotationSetsAccordion[index] = !newAnnotationSetsAccordion[index]; + isAccordionOpen(annotationSet) { + return ( + this.isSearchingAnnotationList || + this.annotationSetsAccordion[ + annotationSet.id || annotationSet.label_set.id + ] === true + ); + }, + toggleAccordion(annotationSet) { + const newAnnotationSetsAccordion = { ...this.annotationSetsAccordion }; + newAnnotationSetsAccordion[ + annotationSet.id || annotationSet.label_set.id + ] = + !newAnnotationSetsAccordion[ + annotationSet.id || annotationSet.label_set.id + ]; this.annotationSetsAccordion = newAnnotationSetsAccordion; }, openAllAccordions() { - const newAnnotationSetsAccordion = [...this.annotationSetsAccordion]; - newAnnotationSetsAccordion.forEach((_, index) => { - newAnnotationSetsAccordion[index] = true; - }); + const newAnnotationSetsAccordion = { ...this.annotationSetsAccordion }; + for (var key in newAnnotationSetsAccordion) { + newAnnotationSetsAccordion[key] = true; + } this.annotationSetsAccordion = newAnnotationSetsAccordion; }, loadAccordions(newAnnotationSets, oldAnnotationSets = null) { if (newAnnotationSets) { const isFirstTime = this.annotationSetsAccordion === null; - const newAnnotationSetsAccordion = []; + const newAnnotationSetsAccordion = {}; const annotationSetsOpened = []; const annotationSetsCreated = []; - if (!isFirstTime) { - // when annotation sets changed, restore old state - // and check if new ones were created to be open by default - - this.annotationSetsAccordion.forEach((isOpen, index) => { - if (isOpen) { - annotationSetsOpened.push(oldAnnotationSets[index]); - } - }); - - newAnnotationSets.forEach((newAnnotationSet) => { - const existed = oldAnnotationSets.find( - (oldAnnotationSet) => - oldAnnotationSet && - newAnnotationSet && - oldAnnotationSet.id && - newAnnotationSet.id && - oldAnnotationSet.id === newAnnotationSet.id - ); - if (!existed && newAnnotationSet && newAnnotationSet.id !== null) { - annotationSetsCreated.push(newAnnotationSet); - } - }); - } - newAnnotationSets.forEach((newAnnotationSet, index) => { const wasOpen = annotationSetsOpened.find( (annotationSetOpened) => @@ -326,19 +311,25 @@ export default { newAnnotationSet.id === annotationSetOpened.id ); if (isFirstTime && this.annotationSetId) { - newAnnotationSetsAccordion[index] = - newAnnotationSet.id == this.annotationSetId; + newAnnotationSetsAccordion[ + newAnnotationSet.id || newAnnotationSet.label_set.id + ] = newAnnotationSet.id == this.annotationSetId; } else if (isFirstTime && this.annotationId) { - newAnnotationSetsAccordion[index] = - this.isAnnotationInAnnotationSet( - newAnnotationSet, - this.annotationId - ); + newAnnotationSetsAccordion[ + newAnnotationSet.id || newAnnotationSet.label_set.id + ] = this.isAnnotationInAnnotationSet( + newAnnotationSet, + this.annotationId + ); } else if (isFirstTime && index === 0) { // open first one by default - newAnnotationSetsAccordion[index] = true; + newAnnotationSetsAccordion[ + newAnnotationSet.id || newAnnotationSet.label_set.id + ] = true; } else if (wasOpen) { - newAnnotationSetsAccordion[index] = wasOpen !== undefined; + newAnnotationSetsAccordion[ + newAnnotationSet.id || newAnnotationSet.label_set.id + ] = wasOpen !== undefined; } else { const wasCreated = annotationSetsCreated.find( (annotationSetCreated) => @@ -346,7 +337,9 @@ export default { newAnnotationSet.id && newAnnotationSet.id === annotationSetCreated.id ); - newAnnotationSetsAccordion[index] = wasCreated !== undefined; + newAnnotationSetsAccordion[ + newAnnotationSet.id || newAnnotationSet.label_set.id + ] = wasCreated !== undefined; } }); this.annotationSetsAccordion = newAnnotationSetsAccordion; diff --git a/src/store/document.js b/src/store/document.js index 0a3a4c95..5172dfd1 100644 --- a/src/store/document.js +++ b/src/store/document.js @@ -1243,11 +1243,11 @@ const actions = { } } else { commit("ADD_ANNOTATION", response.data); - if (response.data && response.data.id) { - dispatch("setAnnotationId", response.data.id); - } } + if (response.data && response.data.id) { + dispatch("setAnnotationId", response.data.id); + } resolve(response); } })