2020-03-12 15:53:35 +00:00
|
|
|
|
// Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert balance in cents to a human readable amount
|
|
|
|
|
* @param value the balance, in cents
|
|
|
|
|
* @returns {string}
|
|
|
|
|
*/
|
|
|
|
|
function pretty_money(value) {
|
|
|
|
|
if (value % 100 === 0)
|
|
|
|
|
return (value < 0 ? "- " : "") + Math.floor(Math.abs(value) / 100) + " €";
|
|
|
|
|
else
|
2020-03-12 22:12:49 +00:00
|
|
|
|
return (value < 0 ? "- " : "") + Math.floor(Math.abs(value) / 100) + "."
|
|
|
|
|
+ (Math.abs(value) % 100 < 10 ? "0" : "") + (Math.abs(value) % 100) + " €";
|
2020-03-12 15:53:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 11:11:16 +00:00
|
|
|
|
/**
|
|
|
|
|
* Add a message on the top of the page.
|
|
|
|
|
* @param msg The message to display
|
|
|
|
|
* @param alert_type The type of the alert. Choices: info, success, warning, danger
|
2020-04-06 05:06:52 +00:00
|
|
|
|
* @param timeout The delay (in millis) after that the message is auto-closed. If negative, then it is ignored.
|
2020-03-16 11:11:16 +00:00
|
|
|
|
*/
|
2020-04-06 05:06:52 +00:00
|
|
|
|
function addMsg(msg, alert_type, timeout=-1) {
|
2020-03-16 11:11:16 +00:00
|
|
|
|
let msgDiv = $("#messages");
|
|
|
|
|
let html = msgDiv.html();
|
2020-04-06 05:06:52 +00:00
|
|
|
|
let id = Math.floor(10000 * Math.random() + 1);
|
2020-03-16 11:41:14 +00:00
|
|
|
|
html += "<div class=\"alert alert-" + alert_type + " alert-dismissible\">" +
|
2020-04-06 05:06:52 +00:00
|
|
|
|
"<button id=\"close-message-" + id + "\" class=\"close\" data-dismiss=\"alert\" href=\"#\"><span aria-hidden=\"true\">×</span></button>"
|
2020-03-16 11:41:14 +00:00
|
|
|
|
+ msg + "</div>\n";
|
2020-03-16 11:11:16 +00:00
|
|
|
|
msgDiv.html(html);
|
2020-04-06 05:06:52 +00:00
|
|
|
|
|
|
|
|
|
if (timeout > 0) {
|
|
|
|
|
setTimeout(function () {
|
|
|
|
|
$("#close-message-" + id).click();
|
|
|
|
|
}, timeout);
|
|
|
|
|
}
|
2020-03-16 11:11:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 22:04:51 +00:00
|
|
|
|
/**
|
|
|
|
|
* add Muliple error message from err_obj
|
2020-03-27 21:48:20 +00:00
|
|
|
|
* @param errs_obj [{error_code:erro_message}]
|
2020-04-06 05:06:52 +00:00
|
|
|
|
* @param timeout The delay (in millis) after that the message is auto-closed. If negative, then it is ignored.
|
2020-03-26 22:04:51 +00:00
|
|
|
|
*/
|
2020-04-06 05:06:52 +00:00
|
|
|
|
function errMsg(errs_obj, timeout=-1) {
|
2020-03-26 22:04:51 +00:00
|
|
|
|
for (const err_msg of Object.values(errs_obj)) {
|
2020-04-06 05:06:52 +00:00
|
|
|
|
addMsg(err_msg,'danger', timeout);
|
2020-03-26 22:04:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-27 21:48:20 +00:00
|
|
|
|
|
|
|
|
|
var reloadWithTurbolinks = (function () {
|
|
|
|
|
var scrollPosition;
|
|
|
|
|
|
|
|
|
|
function reload () {
|
|
|
|
|
scrollPosition = [window.scrollX, window.scrollY];
|
|
|
|
|
Turbolinks.visit(window.location.toString(), { action: 'replace' })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document.addEventListener('turbolinks:load', function () {
|
|
|
|
|
if (scrollPosition) {
|
|
|
|
|
window.scrollTo.apply(window, scrollPosition);
|
|
|
|
|
scrollPosition = null
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return reload;
|
|
|
|
|
})();
|
|
|
|
|
|
2020-03-12 15:53:35 +00:00
|
|
|
|
/**
|
|
|
|
|
* Reload the balance of the user on the right top corner
|
|
|
|
|
*/
|
|
|
|
|
function refreshBalance() {
|
|
|
|
|
$("#user_balance").load("/ #user_balance");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Query the 20 first matched notes with a given pattern
|
|
|
|
|
* @param pattern The pattern that is queried
|
|
|
|
|
* @param fun For each found note with the matched alias `alias`, fun(note, alias) is called.
|
|
|
|
|
*/
|
|
|
|
|
function getMatchedNotes(pattern, fun) {
|
2020-03-13 11:21:16 +00:00
|
|
|
|
$.getJSON("/api/note/alias/?format=json&alias=" + pattern + "&search=user|club&ordering=normalized_name", fun);
|
2020-03-12 15:53:35 +00:00
|
|
|
|
}
|
2020-03-12 20:05:43 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a <li> entry with a given id and text
|
|
|
|
|
*/
|
2020-03-28 16:44:22 +00:00
|
|
|
|
function li(id, text,extra_css) {
|
|
|
|
|
return "<li class=\"list-group-item py-1 d-flex justify-content-between align-items-center " + extra_css + "\"" +
|
2020-03-12 20:05:43 +00:00
|
|
|
|
" id=\"" + id + "\">" + text + "</li>\n";
|
|
|
|
|
}
|
2020-03-28 16:44:22 +00:00
|
|
|
|
/**
|
|
|
|
|
* Return style to apply according to the balance of the note
|
|
|
|
|
* @param balance The balance of the note.
|
|
|
|
|
*/
|
|
|
|
|
function displayStyle(balance){
|
|
|
|
|
if (balance < -5000){
|
|
|
|
|
return " text-danger bg-dark"
|
|
|
|
|
}
|
|
|
|
|
else if(balance <-1000){
|
|
|
|
|
return " text-danger"
|
|
|
|
|
}
|
|
|
|
|
else if(balance < 0){
|
|
|
|
|
return "text-warning"
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 20:05:43 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render note name and picture
|
|
|
|
|
* @param note The note to render
|
|
|
|
|
* @param alias The alias to be displayed
|
|
|
|
|
* @param user_note_field
|
|
|
|
|
* @param profile_pic_field
|
|
|
|
|
*/
|
|
|
|
|
function displayNote(note, alias, user_note_field=null, profile_pic_field=null) {
|
2020-03-20 01:05:41 +00:00
|
|
|
|
if (!note.display_image) {
|
2020-03-28 16:44:22 +00:00
|
|
|
|
note.display_image = '/media/pic/default.png';
|
2020-03-20 01:05:41 +00:00
|
|
|
|
}
|
|
|
|
|
let img = note.display_image;
|
2020-04-09 22:02:22 +00:00
|
|
|
|
if (alias !== note.name && note.name)
|
2020-03-12 20:05:43 +00:00
|
|
|
|
alias += " (aka. " + note.name + ")";
|
2020-03-20 01:05:41 +00:00
|
|
|
|
if (user_note_field !== null)
|
2020-04-09 20:49:52 +00:00
|
|
|
|
|
2020-03-28 16:44:22 +00:00
|
|
|
|
$("#" + user_note_field).addClass(displayStyle(note.balance));
|
2020-03-28 20:30:55 +00:00
|
|
|
|
$("#" + user_note_field).text(alias + (note.balance == null ? "" : (":\n" + pretty_money(note.balance))));
|
2020-03-28 16:44:22 +00:00
|
|
|
|
if (profile_pic_field != null){
|
2020-03-12 20:05:43 +00:00
|
|
|
|
$("#" + profile_pic_field).attr('src', img);
|
2020-03-28 16:44:22 +00:00
|
|
|
|
$("#" + profile_pic_field).click(function(){
|
|
|
|
|
console.log(note);
|
|
|
|
|
if(note.resourcetype == "NoteUser"){
|
|
|
|
|
document.location.href = "/accounts/user/" + note.user;
|
|
|
|
|
}
|
|
|
|
|
else if(note.resourcetype == "NoteClub"){
|
|
|
|
|
document.location.href = "/accounts/club/" + note.club;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-03-12 20:05:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove a note from the emitters.
|
|
|
|
|
* @param d The note to remove
|
|
|
|
|
* @param note_prefix The prefix of the identifiers of the <li> blocks of the emitters
|
|
|
|
|
* @param notes_display An array containing the infos of the buyers: [alias, note id, note object, quantity]
|
2020-03-12 22:12:49 +00:00
|
|
|
|
* @param note_list_id The div block identifier where the notes of the buyers are displayed
|
2020-03-12 20:05:43 +00:00
|
|
|
|
* @param user_note_field The identifier of the field that display the note of the hovered note (useful in
|
|
|
|
|
* consumptions, put null if not used)
|
|
|
|
|
* @param profile_pic_field The identifier of the field that display the profile picture of the hovered note
|
|
|
|
|
* (useful in consumptions, put null if not used)
|
|
|
|
|
* @returns an anonymous function to be compatible with jQuery events
|
|
|
|
|
*/
|
2020-03-12 22:12:49 +00:00
|
|
|
|
function removeNote(d, note_prefix="note", notes_display, note_list_id, user_note_field=null, profile_pic_field=null) {
|
2020-03-12 20:05:43 +00:00
|
|
|
|
return (function() {
|
|
|
|
|
let new_notes_display = [];
|
|
|
|
|
let html = "";
|
|
|
|
|
notes_display.forEach(function (disp) {
|
2020-03-16 08:32:39 +00:00
|
|
|
|
if (disp.quantity > 1 || disp.id !== d.id) {
|
|
|
|
|
disp.quantity -= disp.id === d.id ? 1 : 0;
|
2020-03-12 20:05:43 +00:00
|
|
|
|
new_notes_display.push(disp);
|
2020-03-16 08:32:39 +00:00
|
|
|
|
html += li(note_prefix + "_" + disp.id, disp.name
|
|
|
|
|
+ "<span class=\"badge badge-dark badge-pill\">" + disp.quantity + "</span>");
|
2020-03-12 20:05:43 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
2020-03-12 22:12:49 +00:00
|
|
|
|
|
|
|
|
|
notes_display.length = 0;
|
|
|
|
|
new_notes_display.forEach(function(disp) {
|
|
|
|
|
notes_display.push(disp);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$("#" + note_list_id).html(html);
|
2020-03-12 20:05:43 +00:00
|
|
|
|
notes_display.forEach(function (disp) {
|
2020-03-16 08:32:39 +00:00
|
|
|
|
let obj = $("#" + note_prefix + "_" + disp.id);
|
2020-03-12 22:12:49 +00:00
|
|
|
|
obj.click(removeNote(disp, note_prefix, notes_display, note_list_id, user_note_field, profile_pic_field));
|
2020-03-12 20:05:43 +00:00
|
|
|
|
obj.hover(function() {
|
2020-03-16 08:32:39 +00:00
|
|
|
|
if (disp.note)
|
|
|
|
|
displayNote(disp.note, disp.name, user_note_field, profile_pic_field);
|
2020-03-12 20:05:43 +00:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate an auto-complete field to query a note with its alias
|
|
|
|
|
* @param field_id The identifier of the text field where the alias is typed
|
|
|
|
|
* @param alias_matched_id The div block identifier where the matched aliases are displayed
|
|
|
|
|
* @param note_list_id The div block identifier where the notes of the buyers are displayed
|
|
|
|
|
* @param notes An array containing the note objects of the buyers
|
|
|
|
|
* @param notes_display An array containing the infos of the buyers: [alias, note id, note object, quantity]
|
|
|
|
|
* @param alias_prefix The prefix of the <li> blocks for the matched aliases
|
|
|
|
|
* @param note_prefix The prefix of the <li> blocks for the notes of the buyers
|
|
|
|
|
* @param user_note_field The identifier of the field that display the note of the hovered note (useful in
|
|
|
|
|
* consumptions, put null if not used)
|
|
|
|
|
* @param profile_pic_field The identifier of the field that display the profile picture of the hovered note
|
|
|
|
|
* (useful in consumptions, put null if not used)
|
2020-03-13 00:34:20 +00:00
|
|
|
|
* @param alias_click Function that is called when an alias is clicked. If this method exists and doesn't return true,
|
|
|
|
|
* the associated note is not displayed.
|
|
|
|
|
* Useful for a consumption if the item is selected before.
|
2020-03-12 20:05:43 +00:00
|
|
|
|
*/
|
|
|
|
|
function autoCompleteNote(field_id, alias_matched_id, note_list_id, notes, notes_display, alias_prefix="alias",
|
2020-03-13 00:34:20 +00:00
|
|
|
|
note_prefix="note", user_note_field=null, profile_pic_field=null, alias_click=null) {
|
2020-03-12 20:05:43 +00:00
|
|
|
|
let field = $("#" + field_id);
|
|
|
|
|
// When the user clicks on the search field, it is immediately cleared
|
|
|
|
|
field.click(function() {
|
|
|
|
|
field.val("");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let old_pattern = null;
|
|
|
|
|
|
2020-03-13 11:21:33 +00:00
|
|
|
|
// When the user type "Enter", the first alias is clicked
|
|
|
|
|
field.keypress(function(event) {
|
2020-03-28 16:44:22 +00:00
|
|
|
|
if (event.originalEvent.charCode === 13 && notes.length > 0) {
|
|
|
|
|
let li_obj = $("#" + alias_matched_id + " li").first();
|
|
|
|
|
displayNote(notes[0], li_obj.text(), user_note_field, profile_pic_field);
|
|
|
|
|
li_obj.trigger("click");
|
|
|
|
|
}
|
2020-03-13 11:21:33 +00:00
|
|
|
|
});
|
|
|
|
|
|
2020-03-12 20:05:43 +00:00
|
|
|
|
// When the user type something, the matched aliases are refreshed
|
2020-03-13 11:21:33 +00:00
|
|
|
|
field.keyup(function(e) {
|
|
|
|
|
if (e.originalEvent.charCode === 13)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-03-12 20:05:43 +00:00
|
|
|
|
let pattern = field.val();
|
2020-03-13 11:27:01 +00:00
|
|
|
|
// If the pattern is not modified, we don't query the API
|
2020-03-28 16:44:22 +00:00
|
|
|
|
if (pattern === old_pattern)
|
2020-03-12 20:05:43 +00:00
|
|
|
|
return;
|
|
|
|
|
old_pattern = pattern;
|
|
|
|
|
notes.length = 0;
|
|
|
|
|
|
|
|
|
|
let aliases_matched_obj = $("#" + alias_matched_id);
|
|
|
|
|
let aliases_matched_html = "";
|
2020-03-28 16:44:22 +00:00
|
|
|
|
// get matched Alias with note associated
|
|
|
|
|
if(pattern == ""){
|
|
|
|
|
aliases_matched_obj = $("#" + alias_matched_id);
|
|
|
|
|
aliases_matched_html = "";
|
|
|
|
|
aliases_matched_obj.html("")
|
|
|
|
|
notes.length = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-13 11:27:01 +00:00
|
|
|
|
|
2020-03-28 16:44:22 +00:00
|
|
|
|
$.getJSON("/api/note/consumer/?format=json&alias="
|
|
|
|
|
+ pattern
|
|
|
|
|
+ "&search=user|club&ordering=normalized_name",
|
|
|
|
|
function(consumers){
|
2020-03-13 11:21:16 +00:00
|
|
|
|
// The response arrived too late, we stop the request
|
|
|
|
|
if (pattern !== $("#" + field_id).val())
|
|
|
|
|
return;
|
2020-03-28 16:44:22 +00:00
|
|
|
|
consumers.results.forEach(function (consumer){
|
|
|
|
|
let note = consumer.note;
|
|
|
|
|
extra_css = displayStyle(note.balance);
|
|
|
|
|
aliases_matched_html += li(alias_prefix + '_' + consumer.id,
|
|
|
|
|
consumer.name,
|
|
|
|
|
extra_css);
|
2020-03-13 11:21:16 +00:00
|
|
|
|
notes.push(note);
|
2020-03-12 20:05:43 +00:00
|
|
|
|
});
|
2020-03-13 11:21:16 +00:00
|
|
|
|
aliases_matched_obj.html(aliases_matched_html);
|
2020-03-28 16:44:22 +00:00
|
|
|
|
consumers.results.forEach(function(consumer){
|
|
|
|
|
let note = consumer.note;
|
|
|
|
|
let consumer_obj = $("#" + alias_prefix+ "_" + consumer.id);
|
|
|
|
|
consumer_obj.hover(function(){
|
|
|
|
|
displayNote(consumer.note, consumer.name, user_note_field,profile_pic_field)
|
2020-03-13 11:21:16 +00:00
|
|
|
|
});
|
2020-03-28 16:44:22 +00:00
|
|
|
|
consumer_obj.click(function(){
|
|
|
|
|
field.val(""); old_pattern = ""; // reset input field
|
2020-03-13 11:21:16 +00:00
|
|
|
|
var disp = null;
|
|
|
|
|
notes_display.forEach(function (d) {
|
|
|
|
|
// We compare the note ids
|
2020-03-16 08:32:39 +00:00
|
|
|
|
if (d.id === note.id) {
|
|
|
|
|
d.quantity += 1;
|
2020-03-13 11:21:16 +00:00
|
|
|
|
disp = d;
|
|
|
|
|
}
|
2020-03-12 20:05:43 +00:00
|
|
|
|
});
|
2020-03-13 11:21:16 +00:00
|
|
|
|
// In the other case, we add a new emitter
|
2020-03-16 08:32:39 +00:00
|
|
|
|
if (disp == null) {
|
|
|
|
|
disp = {
|
2020-03-28 16:44:22 +00:00
|
|
|
|
name: consumer.name,
|
|
|
|
|
id: consumer.id,
|
2020-03-16 08:32:39 +00:00
|
|
|
|
note: note,
|
|
|
|
|
quantity: 1
|
|
|
|
|
};
|
|
|
|
|
notes_display.push(disp);
|
|
|
|
|
}
|
2020-03-13 11:21:16 +00:00
|
|
|
|
|
|
|
|
|
// If the function alias_click exists, it is called. If it doesn't return true, then the notes are
|
|
|
|
|
// note displayed. Useful for a consumption when a button is already clicked
|
|
|
|
|
if (alias_click && !alias_click())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
let note_list = $("#" + note_list_id);
|
|
|
|
|
let html = "";
|
|
|
|
|
notes_display.forEach(function (disp) {
|
2020-03-28 16:44:22 +00:00
|
|
|
|
html += li(note_prefix + "_" + disp.id,
|
|
|
|
|
disp.name
|
|
|
|
|
+ "<span class=\"badge badge-dark badge-pill\">"
|
|
|
|
|
+ disp.quantity + "</span>",
|
|
|
|
|
displayStyle(disp.note.balance));
|
2020-03-13 11:21:16 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Emitters are displayed
|
|
|
|
|
note_list.html(html);
|
2020-03-12 20:05:43 +00:00
|
|
|
|
|
2020-03-13 11:21:16 +00:00
|
|
|
|
notes_display.forEach(function (disp) {
|
2020-03-16 08:32:39 +00:00
|
|
|
|
let line_obj = $("#" + note_prefix + "_" + disp.id);
|
2020-03-13 11:21:16 +00:00
|
|
|
|
// Hover an emitter display also the profile picture
|
|
|
|
|
line_obj.hover(function () {
|
2020-03-16 08:32:39 +00:00
|
|
|
|
displayNote(disp.note, disp.name, user_note_field, profile_pic_field);
|
2020-03-13 11:21:16 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
});
|
2020-03-28 16:44:22 +00:00
|
|
|
|
})
|
2020-03-12 20:05:43 +00:00
|
|
|
|
});
|
2020-04-09 20:49:52 +00:00
|
|
|
|
|
2020-03-28 16:44:22 +00:00
|
|
|
|
});// end getJSON alias
|
2020-03-12 20:05:43 +00:00
|
|
|
|
});
|
2020-03-28 16:44:22 +00:00
|
|
|
|
}// end function autocomplete
|
2020-03-16 09:39:16 +00:00
|
|
|
|
|
|
|
|
|
// When a validate button is clicked, we switch the validation status
|
|
|
|
|
function de_validate(id, validated) {
|
|
|
|
|
$("#validate_" + id).html("<strong style=\"font-size: 16pt;\">⟳ ...</strong>");
|
|
|
|
|
|
|
|
|
|
// Perform a PATCH request to the API in order to update the transaction
|
|
|
|
|
// If the user has insuffisent rights, an error message will appear
|
|
|
|
|
$.ajax({
|
|
|
|
|
"url": "/api/note/transaction/transaction/" + id + "/",
|
|
|
|
|
type: "PATCH",
|
|
|
|
|
dataType: "json",
|
|
|
|
|
headers: {
|
|
|
|
|
"X-CSRFTOKEN": CSRF_TOKEN
|
|
|
|
|
},
|
|
|
|
|
data: {
|
2020-03-25 12:13:01 +00:00
|
|
|
|
resourcetype: "RecurrentTransaction",
|
|
|
|
|
valid: !validated,
|
|
|
|
|
invalidity_reason: invalidity_reason,
|
2020-03-16 09:39:16 +00:00
|
|
|
|
},
|
|
|
|
|
success: function () {
|
|
|
|
|
// Refresh jQuery objects
|
|
|
|
|
$(".validate").click(de_validate);
|
|
|
|
|
|
|
|
|
|
refreshBalance();
|
|
|
|
|
// error if this method doesn't exist. Please define it.
|
|
|
|
|
refreshHistory();
|
|
|
|
|
},
|
|
|
|
|
error: function(err) {
|
2020-03-16 11:11:16 +00:00
|
|
|
|
addMsg("Une erreur est survenue lors de la validation/dévalidation " +
|
|
|
|
|
"de cette transaction : " + err.responseText, "danger");
|
2020-03-16 09:39:16 +00:00
|
|
|
|
|
|
|
|
|
refreshBalance();
|
|
|
|
|
// error if this method doesn't exist. Please define it.
|
|
|
|
|
refreshHistory();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|