Skip to content

Commit

Permalink
clean up some deep and confusing call stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
timonegk committed Mar 30, 2021
1 parent 06a4e94 commit 75a9b39
Showing 1 changed file with 51 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -563,14 +563,7 @@ function calculateImageScale() {
result.attr('id', 'image_list');
oldImageList.replaceWith(result);

gImageList = getImageList();

// load first image if current image is not within image set
if (!imageContained) {
await loadAnnotateView(imageList[0].id);
}

scrollImageList();
gImageList = imageList.map(image => { return image.id });
}

/**
Expand Down Expand Up @@ -631,21 +624,6 @@ function calculateImageScale() {
$('#blurred').prop('checked', annotationElem.data('blurred')).change();
}

/**
* Get the image list from all .annotate_image_link within #image_list.
*/
function getImageList() {
let imageList = [];
$('#image_list').find('.annotate_image_link').each(function(key, elem) {
let imageId = parseInt($(elem).data('imageid'));
if (imageList.indexOf(imageId) === -1) {
imageList.push(imageId);
}
});

return imageList;
}

/**
* Validate a vector.
*
Expand Down Expand Up @@ -819,6 +797,11 @@ function calculateImageScale() {
* @param fromHistory
*/
async function loadAnnotateView(imageId, fromHistory) {
// load first image if current image is not within image set
if (!gImageList.includes(imageId)) {
console.log('Loading first image because ' + imageId + ' is not contained in the selection');
imageId = gImageList[0];
}
gEditAnnotationId = undefined;

imageId = parseInt(imageId);
Expand All @@ -838,7 +821,7 @@ function calculateImageScale() {
loading.removeClass('hidden');
$('#annotation_type_id').val(gAnnotationType);

let loadImage = displayImage(imageId).then(scrollImageList);
let loadImage = displayImage(imageId);

if (!$('#keep_selection').prop('checked')) {
$('#concealed').prop('checked', false);
Expand Down Expand Up @@ -883,9 +866,9 @@ function calculateImageScale() {
}

/**
* Load the image list from tye server applying a new filter.
* Get the image list from the server applying a new filter.
*/
async function loadImageList() {
async function getImageList() {
let filterElem = $('#filter_annotation_type');
let filter = filterElem.val();
let params = {
Expand All @@ -911,7 +894,7 @@ function calculateImageScale() {
displayFeedback($('#feedback_image_set_empty'));
filterElem.val('').change();
} else {
await displayImageList(data.image_set.images);
return data.image_set.images;
}
} catch {
displayFeedback($('#feedback_connection_error'));
Expand Down Expand Up @@ -979,7 +962,7 @@ function calculateImageScale() {
* Load the previous or the next image
*
* @param offset integer to add to the current image index
* @return index of the requested image in gImageList
* @return id of the requested image
*/
async function loadAdjacentImage(offset) {
let imageIndex = gImageList.indexOf(gImageId);
Expand All @@ -994,7 +977,8 @@ function calculateImageScale() {
while (imageIndex > imageIndex.length) {
imageIndex -= imageIndex.length;
}
await loadAnnotateView(gImageList[imageIndex]);
gImageId = gImageList[imageIndex];
return gImageList[imageIndex];
}

/**
Expand Down Expand Up @@ -1029,9 +1013,10 @@ function calculateImageScale() {

/**
* Scroll image list to make current image visible.
* @param imageId id of the image to scroll to
*/
function scrollImageList() {
let imageLink = $('#annotate_image_link_' + gImageId);
function scrollImageList(imageId) {
let imageLink = $('#annotate_image_link_' + imageId);
let list = $('#image_list');

let offset = list.offset().top;
Expand Down Expand Up @@ -1105,6 +1090,17 @@ function calculateImageScale() {
handleAnnotationTypeChange();
}

async function updateFilter() {
handleAnnotationTypeChange();
let imageList = await getImageList();
await displayImageList(imageList);
if (!gImageList.includes(gImageId)) {
gImageId = gImageList[0];
}
scrollImageList(gImageId);
await loadAnnotateView(gImageId);
}


$(document).ready(function() {
let get_params = decodeURIComponent(window.location.search.substring(1)).split('&');
Expand All @@ -1129,25 +1125,26 @@ function calculateImageScale() {
"Content-Type": 'application/json',
"X-CSRFTOKEN": gCsrfToken
};
gImageList = getImageList();
scrollImageList();
loadAnnotationTypeList();

// W3C standards do not define the load event on images, we therefore need to use
// it from window (this should wait for all external sources including images)
$(window).on('load', function() {
setTool();
loadAnnotateView(gImageId);
preloadImages();
getImageList()
.then(displayImageList)
.then(r => { scrollImageList(gImageId) })
.then(r => { loadAnnotateView(gImageId) })
.then(r => { preloadImages() });
loadAnnotationTypeList();
}());

$('.annotation_value').on('input', function() {
tool.reloadSelection();
});
$('#not_in_image').on('change', handleNotInImageToggle);
handleNotInImageToggle();
$('select#filter_annotation_type').on('change', loadImageList);
$('#filter_update_btn').on('click', loadImageList);
$('select#filter_annotation_type').on('change', updateFilter);
$('#filter_update_btn').on('click', updateFilter);
$('select').on('change', function() {
document.activeElement.blur();
});
Expand Down Expand Up @@ -1180,29 +1177,33 @@ function calculateImageScale() {
$('#last_button').click(async function (event) {
try {
let annotation = getValidAnnotation(true);
let annotationPromise = createAnnotation(annotation);
let imagePromise = loadImageList().then(r => {
return loadAdjacentImage(-1);
})
await Promise.all([annotationPromise, imagePromise]);
await createAnnotation(annotation);
let imageId = await loadAdjacentImage(-1);
let imageList = await getImageList();
await displayImageList(imageList);
scrollImageList(imageId);
await loadAnnotateView(imageId);
} catch (e) {
console.log(e);
}
});
$('#back_button').click(async function (event) {
await loadAdjacentImage(-1);
let imageId = await loadAdjacentImage(-1);
await loadAnnotateView(imageId);
});
$('#skip_button').click(async function (event) {
await loadAdjacentImage(1)
let imageId = await loadAdjacentImage(1);
await loadAnnotateView(imageId);
});
$('#next_button').click(async function (event) {
try {
let annotation = getValidAnnotation(true);
let annotationPromise = createAnnotation(annotation);
let imagePromise = loadImageList().then(r => {
return loadAdjacentImage(1);
})
await Promise.all([annotationPromise, imagePromise]);
await createAnnotation(annotation);
let imageId = await loadAdjacentImage(1);
let imageList = await getImageList();
await displayImageList(imageList);
scrollImageList(imageId);
await loadAnnotateView(imageId);
} catch (e) {
console.log(e);
}
Expand Down

0 comments on commit 75a9b39

Please sign in to comment.