Aliases are now asynchronously queried

This commit is contained in:
Yohann D'ANELLO 2020-03-13 12:21:16 +01:00
parent a1f7f8f5b7
commit 1e6891e1c7
1 changed files with 57 additions and 69 deletions

View File

@ -1,21 +1,6 @@
// Copyright (C) 2018-2020 by BDE ENS Paris-Saclay // Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
/**
* Perform a request on an URL and get result
* @param url The url where the request is performed
* @param success The function to call with the request
* @param data The data for the request (optional)
*/
function getJSONSync(url, success, data) {
$.ajax({
url: url,
dataType: 'json',
data: data,
async: false,
success: success
});
}
/** /**
* Convert balance in cents to a human readable amount * Convert balance in cents to a human readable amount
@ -41,14 +26,9 @@ function refreshBalance() {
* Query the 20 first matched notes with a given pattern * Query the 20 first matched notes with a given pattern
* @param pattern The pattern that is queried * @param pattern The pattern that is queried
* @param fun For each found note with the matched alias `alias`, fun(note, alias) is called. * @param fun For each found note with the matched alias `alias`, fun(note, alias) is called.
* This function is synchronous.
*/ */
function getMatchedNotes(pattern, fun) { function getMatchedNotes(pattern, fun) {
getJSONSync("/api/note/alias/?format=json&alias=" + pattern + "&search=user|club&ordering=normalized_name", function(aliases) { $.getJSON("/api/note/alias/?format=json&alias=" + pattern + "&search=user|club&ordering=normalized_name", fun);
aliases.results.forEach(function(alias) {
fun(alias.note, alias);
});
});
} }
/** /**
@ -161,8 +141,14 @@ function autoCompleteNote(field_id, alias_matched_id, note_list_id, notes, notes
let aliases_matched_obj = $("#" + alias_matched_id); let aliases_matched_obj = $("#" + alias_matched_id);
let aliases_matched_html = ""; let aliases_matched_html = "";
// Get matched notes with the given pattern // Get matched notes with the given pattern
getMatchedNotes(pattern, function(note, alias) { getMatchedNotes(pattern, function(aliases) {
aliases_matched_html += li(alias_prefix + "_" + alias.normalized_name, alias.name); // The response arrived too late, we stop the request
if (pattern !== $("#" + field_id).val())
return;
aliases.results.forEach(function (alias) {
let note = alias.note;
aliases_matched_html += li(alias_prefix + "_" + alias.id, alias.name);
note.alias = alias; note.alias = alias;
notes.push(note); notes.push(note);
}); });
@ -172,7 +158,7 @@ function autoCompleteNote(field_id, alias_matched_id, note_list_id, notes, notes
notes.forEach(function (note) { notes.forEach(function (note) {
let alias = note.alias; let alias = note.alias;
let alias_obj = $("#" + alias_prefix + "_" + alias.normalized_name); let alias_obj = $("#" + alias_prefix + "_" + alias.id);
// When an alias is hovered, the profile picture and the balance are displayed at the right place // When an alias is hovered, the profile picture and the balance are displayed at the right place
alias_obj.hover(function () { alias_obj.hover(function () {
displayNote(note, alias.name, user_note_field, profile_pic_field); displayNote(note, alias.name, user_note_field, profile_pic_field);
@ -217,7 +203,9 @@ function autoCompleteNote(field_id, alias_matched_id, note_list_id, notes, notes
}); });
// When an emitter is clicked, it is removed // When an emitter is clicked, it is removed
line_obj.click(removeNote(disp, note_prefix, notes_display, note_list_id, user_note_field, profile_pic_field)); line_obj.click(removeNote(disp, note_prefix, notes_display, note_list_id, user_note_field,
profile_pic_field));
});
}); });
}); });
}); });