Make alias form a bit more HTML friendly

This commit is contained in:
Alexandre Iooss 2020-08-23 00:33:36 +02:00
parent 342d3910c7
commit 0ba77fb8f0
3 changed files with 55 additions and 49 deletions

View File

@ -1,37 +1,43 @@
/**
* On form submit, create a new alias
*/
function create_alias (e) {
// Do not submit HTML form
e.preventDefault();
$("#alias_input").on('keypress',function(e) { // Get data and send to API
if(e.which == 13) { const formData = new FormData(e.target);
$("#alias_submit").click(); $.post("/api/note/alias/", {
} "csrfmiddlewaretoken": formData.get("csrfmiddlewaretoken"),
}); "name": formData.get("name"),
"note": formData.get("note")
function create_alias(note_id){ }).done(function () {
$.post("/api/note/alias/", // Reload table
{
"csrfmiddlewaretoken": CSRF_TOKEN,
"name": $("#alias_input").val(),
"note": note_id
}
).done(function(){
$("#alias_table").load(location.pathname + " #alias_table"); $("#alias_table").load(location.pathname + " #alias_table");
addMsg("Alias ajouté", "success"); addMsg("Alias ajouté", "success");
}) }).fail(function (xhr, _textStatus, _error) {
.fail(function(xhr, textStatus, error){
errMsg(xhr.responseJSON); errMsg(xhr.responseJSON);
}); });
} }
// on click of button "delete" , call the API
/**
* On click of "delete", delete the alias
* @param Integer button_id Alias id to remove
*/
function delete_button (button_id) { function delete_button (button_id) {
$.ajax({ $.ajax({
url: "/api/note/alias/" + button_id + "/", url: "/api/note/alias/" + button_id + "/",
method: "DELETE", method: "DELETE",
headers: { "X-CSRFTOKEN": CSRF_TOKEN } headers: { "X-CSRFTOKEN": CSRF_TOKEN }
}) }).done(function () {
.done(function(){
addMsg('Alias supprimé', 'success'); addMsg('Alias supprimé', 'success');
$("#alias_table").load(location.pathname + " #alias_table"); $("#alias_table").load(location.pathname + " #alias_table");
}) }).fail(function (xhr, _textStatus, _error) {
.fail(function(xhr,textStatus, error){
errMsg(xhr.responseJSON); errMsg(xhr.responseJSON);
}); });
} }
$(document).ready(function () {
// Attach event
document.getElementById("form_alias").addEventListener("submit", create_alias);
})

View File

@ -6,15 +6,15 @@
<h3 class="card-header text-center"> <h3 class="card-header text-center">
{% trans "Note aliases" %} {% trans "Note aliases" %}
</h3> </h3>
<div class="card-body text-center"> <div class="card-body">
<div class="input-group"> <form class="input-group" method="POST" id="form_alias">
<input id="alias_input" type="text" class="form-control"> {% csrf_token %}
<input type="hidden" name="note" value="{{ object.note.pk }}">
<input type="text" name="name" class="form-control">
<div class="input-group-append"> <div class="input-group-append">
<button id="alias_submit" class="btn btn-success" onclick="create_alias({{ object.note.pk }})"> <input type="submit" class="btn btn-success" value="{% trans "Add" %}">
{% trans "Add" %}
</button>
</div>
</div> </div>
</form>
</div> </div>
{% render_table aliases %} {% render_table aliases %}
</div> </div>

View File

@ -6,15 +6,15 @@
<h3 class="card-header text-center"> <h3 class="card-header text-center">
{% trans "Note aliases" %} {% trans "Note aliases" %}
</h3> </h3>
<div class="card-body text-center"> <div class="card-body">
<div class="input-group"> <form class="input-group" method="POST" id="form_alias">
<input id="alias_input" type="text" class="form-control"> {% csrf_token %}
<input type="hidden" name="note" value="{{ object.note.pk }}">
<input type="text" name="name" class="form-control">
<div class="input-group-append"> <div class="input-group-append">
<button id="alias_submit" class="btn btn-success" onclick="create_alias({{ object.note.pk }})"> <input type="submit" class="btn btn-success" value="{% trans "Add" %}">
{% trans "Add" %}
</button>
</div>
</div> </div>
</form>
</div> </div>
{% render_table aliases %} {% render_table aliases %}
</div> </div>