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

Refactor annotation list accordions to solve issues with opening annotations #447

Merged
merged 1 commit into from
Jan 24, 2025
Merged
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
107 changes: 50 additions & 57 deletions src/components/DocumentAnnotations/DocumentAnnotations.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,14 @@
:key="indexGroup"
:class="[
'annotation-set-group',
!annotationSetsAccordion[indexGroup] === true &&
'annotation-set-collapsed',
!isAccordionOpen(annotationSet) && 'annotation-set-collapsed',
]"
>
<div class="label-set-header" @click="toggleAccordion(indexGroup)">
<div class="label-set-header" @click="toggleAccordion(annotationSet)">
<div class="label-set-name">
<b-icon
:icon="
annotationSetsAccordion[indexGroup]
? 'angle-up'
: 'angle-down'
isAccordionOpen(annotationSet) ? 'angle-up' : 'angle-down'
"
size="is-12"
/>
Expand All @@ -80,7 +77,7 @@
class="labelset-action-buttons"
>
<AnnotationSetActionButtons
:is-placeholder="annotationSetsAccordion[indexGroup] === false"
:is-placeholder="!isAccordionOpen(annotationSet)"
:number-of-empty-labels-in-annotation-set="
emptyLabels(annotationSet).length
"
Expand All @@ -107,7 +104,7 @@
</div>
</div>

<b-collapse :open="annotationSetsAccordion[indexGroup] === true">
<b-collapse :open="isAccordionOpen(annotationSet)">
<div v-if="annotationSet.labels.length > 0">
<div v-for="label in annotationSet.labels" :key="label.id">
<div
Expand Down Expand Up @@ -242,11 +239,12 @@ export default {
if (newAnnotationId) {
const annotationSet = this.annotationSetOfAnnotation(newAnnotationId);
if (annotationSet) {
const index = this.annotationSets.findIndex(
(annotationSetToFind) => 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;
}
}
Expand All @@ -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) =>
Expand All @@ -326,27 +311,35 @@ 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) =>
annotationSetCreated.id &&
newAnnotationSet.id &&
newAnnotationSet.id === annotationSetCreated.id
);
newAnnotationSetsAccordion[index] = wasCreated !== undefined;
newAnnotationSetsAccordion[
newAnnotationSet.id || newAnnotationSet.label_set.id
] = wasCreated !== undefined;
}
});
this.annotationSetsAccordion = newAnnotationSetsAccordion;
Expand Down
6 changes: 3 additions & 3 deletions src/store/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
})
Expand Down
Loading