Skip to content

Commit

Permalink
cleanup the mess of resetting/reloading the tool
Browse files Browse the repository at this point in the history
  • Loading branch information
timonegk committed Mar 30, 2021
1 parent dd1962b commit 06a4e94
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function calculateImageScale() {
restoreSelection: function() {},
cancelSelection: function() {},
reset: function() {},
clear: function() {},
drawExistingAnnotations: function() {},
closeDrawing: function() {},
handleMousemove: function() {},
Expand Down Expand Up @@ -229,25 +230,19 @@ function calculateImageScale() {
$('#annotation_edit_button_' + gEditAnnotationId).parent().parent().fadeOut().remove();
} else {
displayFeedback($('#feedback_annotation_updated'));
displayExistingAnnotations(data.annotations);
tool.drawExistingAnnotations(globals.currentAnnotationsOfSelectedType);
}
} else {
displayFeedback($('#feedback_annotation_exists'));
}
} else if (response.status === 201) {
displayFeedback($('#feedback_annotation_created'));
displayExistingAnnotations(data.annotations);
}
// update current annotations
gCurrentAnnotations = data.annotations;
globals.currentAnnotationsOfSelectedType = gCurrentAnnotations.filter(function (e) {
return e.annotation_type.id === gAnnotationType;
});

tool.drawExistingAnnotations(globals.currentAnnotationsOfSelectedType);

resetSelection();
} catch (e) {
console.log(e);
displayFeedback($('#feedback_connection_error'));
Expand Down Expand Up @@ -513,8 +508,6 @@ function calculateImageScale() {
currentImage.replaceWith(newImage);
globals.image = newImage;
calculateImageScale();
tool.initSelection();
resetSelection();

if (currentImage.data('imageid') !== undefined) {
// add previous image to cache
Expand Down Expand Up @@ -612,9 +605,6 @@ function calculateImageScale() {
$('#edit_active').removeClass('hidden');

$('.js_feedback').stop().addClass('hidden');
let params = {
annotation_id: annotationId
};

let annotationData = annotationElem.data('vector');
if (annotationData === undefined) {
Expand All @@ -636,7 +626,6 @@ function calculateImageScale() {

notInImage.prop('checked', false).change();


tool.reloadSelection(annotationId, annotationData);
$('#concealed').prop('checked', annotationElem.data('concealed')).change();
$('#blurred').prop('checked', annotationElem.data('blurred')).change();
Expand Down Expand Up @@ -849,9 +838,6 @@ function calculateImageScale() {
loading.removeClass('hidden');
$('#annotation_type_id').val(gAnnotationType);

if (tool instanceof BoundingBoxes) {
tool.cancelSelection();
}
let loadImage = displayImage(imageId).then(scrollImageList);

if (!$('#keep_selection').prop('checked')) {
Expand All @@ -869,7 +855,13 @@ function calculateImageScale() {
}
$('#next-image-id').attr('value', next_image_id || '');
$('#delete-image-form').attr('action', DELETE_IMAGE_URL.replace('%s', imageId));
tool.restoreSelection(false);

tool.clear();
if (globals.restoreSelection !== undefined) {
tool.restoreSelection();
} else {
resetSelection();
}

if (fromHistory !== true) {
history.pushState({
Expand All @@ -884,12 +876,6 @@ function calculateImageScale() {
loading.addClass('hidden');
displayExistingAnnotations(gCurrentAnnotations);
tool.drawExistingAnnotations(globals.currentAnnotationsOfSelectedType);

if (globals.restoreSelection !== undefined) {
tool.restoreSelection();
} else {
resetSelection();
}
} catch {
console.log("Unable to load annotations for image" + imageId);
}
Expand Down Expand Up @@ -1151,7 +1137,6 @@ function calculateImageScale() {
// it from window (this should wait for all external sources including images)
$(window).on('load', function() {
setTool();
tool.initSelection();
loadAnnotateView(gImageId);
preloadImages();
}());
Expand Down Expand Up @@ -1180,7 +1165,11 @@ function calculateImageScale() {
event.preventDefault();
try {
let annotation = getValidAnnotation()
tool.clear();
resetSelection();
await createAnnotation(annotation);
tool.drawExistingAnnotations(globals.currentAnnotationsOfSelectedType);
displayExistingAnnotations(gCurrentAnnotations);
} catch (e) {
console.log(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ class BoundingBoxes {
this.setHighlightColor(annotationId);
this.selection = globals.image.imgAreaSelect({
instance: true,
show: true
show: true,
onSelectChange: this.updateAnnotationFields,
});
if (!annotationData) {
annotationData = {
Expand Down Expand Up @@ -158,7 +159,7 @@ class BoundingBoxes {
/**
* Restore the selection.
*/
restoreSelection(reset) {
restoreSelection() {
if (!$('#keep_selection').prop('checked')) {
return;
}
Expand All @@ -174,9 +175,6 @@ class BoundingBoxes {
this.reloadSelection(0, globals.restoreSelection);
}
}
if (reset !== false) {
globals.restoreSelection = undefined;
}
}

moveSelectionUp() {
Expand Down
25 changes: 15 additions & 10 deletions imagetagger/imagetagger/annotations/static/annotations/js/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ class Canvas {
this.updateAnnotationFields({x1: 0, x2: 0, y1: 0, y2: 0});
break;
}
this.currentDrawing = undefined;
}

reset() {
Expand Down Expand Up @@ -425,7 +426,8 @@ class Canvas {
}

drawExistingAnnotations(annotations, color) {
this.clear();
let currentDrawing = this.currentDrawing;
this.currentDrawing = undefined;
color = color || globals.stdColor;
let colors = [];
if (color.constructor === Array) {
Expand All @@ -445,8 +447,6 @@ class Canvas {
for (let i in annotations) {
let annotation = annotations[i];
let color = colors[i];
console.log(annotation);
console.log(color);
if (annotation.annotation_type.id !== this.annotationTypeId) {
continue;
}
Expand Down Expand Up @@ -485,7 +485,12 @@ class Canvas {
console.log("Unknown vector type: " + annotation.annotation_type.vector_type);
}
}
this.currentDrawing = undefined;
// completely restore the drawing from before drawing the existing annotations,
// especially the annotation fields are overwritten during the drawing
this.currentDrawing = currentDrawing;
if (currentDrawing) {
this.updateAnnotationFields(currentDrawing);
}
}

updateAnnotationFields(drawing) {
Expand Down Expand Up @@ -545,12 +550,12 @@ class Canvas {
drawing.setMutable(false);
}
if (this.old && this.currentDrawing) {
this.currentDrawing.setPoints(this.old);
this.old = undefined;
} else if (this.currentDrawing) {
this.currentDrawing.remove();
}
this.currentDrawing = undefined;
this.currentDrawing.setPoints(this.old);
this.old = undefined;
} else if (this.currentDrawing) {
this.currentDrawing.remove();
}
this.currentDrawing = undefined;
}

cancelSelection() {
Expand Down

0 comments on commit 06a4e94

Please sign in to comment.