Commit b1431779 authored by Mat's avatar Mat

ajaxDisplay, factorise au maximum les fonctions réutilisables

parent d7d6493d
......@@ -247,7 +247,7 @@
let href = Routing.generate('potage_offre_legumes_ajax_display', {'id': jsonRow.id });
referenceBtn.setAttribute('href', href);
referenceBtn.setAttribute('title', 'Éditer');
referenceBtn.classList.add('btn', 'btn-outline-dark', 'btn-sm', 'mb-2', 'mr-2');
referenceBtn.classList.add('btn', 'btn-outline-dark', 'btn-sm', 'mb-1', 'mr-1');
referenceBtn.textContent = jsonRow.reference;
td6.appendChild(referenceBtn);
if (jsonRow.status === 'draft')
......@@ -349,10 +349,7 @@
*/
function displayDeleteOffre(jsonRow)
{
let deleteBtn = document.createElement('button');
deleteBtn.setAttribute('title', 'Supprimer');
deleteBtn.innerHTML = '<i class="fas fa-trash fa-fw"></i>';
deleteBtn.classList.add('btn', 'btn-danger', 'btn-sm', 'mb-2', 'btn-delete');
let deleteBtn = buildDeleteBtn();
deleteBtn.dataset.offreId = jsonRow.id;
deleteBtn.addEventListener('click', function()
{
......@@ -396,30 +393,13 @@
reloadTables();
/*
* Bouton pour recharger la page
*/
let reloadBtns = document.querySelectorAll('.btn-reload');
reloadBtns.forEach(function(reloadBtn) {
reloadBtn.addEventListener('click', function()
{
reloadTables();
});
reloadButton(function(){
reloadTables();
});
/*
* Ajouter une nouvelle offre
*/
let createBtns = document.querySelectorAll('.btn-create');
createBtns.forEach(function(createBtn) {
createBtn.addEventListener('click', function()
{
titleForm.textContent = 'Nouvelle offre';
submitURL = Routing.generate('api_offre_create');
});
createButton(function(){
titleForm.textContent = 'Nouvelle offre';
submitURL = Routing.generate('api_offre_create');
});
......
......@@ -34,11 +34,11 @@
<div class="col right hd-1">
<div style="margin-top: 0.65em;">
{#
<a href="javascript: void(0);" class="btn btn-outline-dark mb-2 btn-reload">
<i class="fas fa-sync-alt"></i>
Rafraîchir la liste
</a>
{#
<a href="javascript: void(0);" class="btn btn-dark mb-2 btn-create"
data-toggle="modal" data-target=".form-offre-modal">
<i class="fas fa-plus"></i>
......@@ -209,19 +209,10 @@
reloadGridLegumes();
/*
* Bouton pour recharger la page
*/
let reloadBtns = document.querySelectorAll('.btn-reload');
reloadBtns.forEach(function(reloadBtn) {
reloadBtn.addEventListener('click', function()
{
reloadGridLegumes();
});
reloadButton(function(){
reloadGridLegumes();
});
/*
* Un form 'sorted' affiche en console
* un tableau avec l'ordre souhaité
......@@ -235,7 +226,6 @@
{
list[i] = items[i].dataset.legumeId;
}
//console.log(list);
let input = document.querySelector('input#offre_sorted_sorted');
input.value = list;
......
/*
* Bouton pour recharger la page
*/
function reloadButton(callback)
{
let reloadBtns = document.querySelectorAll('.btn-reload');
reloadBtns.forEach(function(reloadBtn) {
reloadBtn.addEventListener('click', function()
{
callback();
});
});
}
/*
* Bouton pour ajouter un nouvel objet
*/
function createButton(callback)
{
let createBtns = document.querySelectorAll('.btn-create');
createBtns.forEach(function(createBtn) {
createBtn.addEventListener('click', function()
{
callback();
});
});
}
/*
* Construit les boutons
*/
function buildEditBtn()
{
let btn = document.createElement('button');
btn.innerHTML = '<i class="fas fa-edit fa-fw"></i> Editer';
btn.classList.add('btn', 'btn-dark', 'btn-sm', 'mb-1', 'mr-1');
btn.dataset.toggle = "modal";
return btn;
}
function buildCloneBtn()
{
let btn = document.createElement('button');
btn.setAttribute('title', 'Dupliquer');
btn.innerHTML = '<i class="fas fa-clone fa-fw"></i>';
btn.classList.add('btn', 'btn-secondary', 'btn-sm', 'mb-1', 'mr-1');
return btn;
}
function buildDeleteBtn()
{
let btn = document.createElement('button');
btn.setAttribute('title', 'Supprimer');
btn.innerHTML = '<i class="fas fa-trash fa-fw"></i>';
btn.classList.add('btn', 'btn-danger', 'btn-sm', 'mb-1', 'mr-1', 'btn-delete');
return btn;
}
function buildInsertBtn()
{
let btn = document.createElement('button');
btn.innerHTML = '<i class="fas fa-clipboard-check fa-fw"></i> Insérer <i class="fas fa-chevron-right fa-fw"></i>';
btn.classList.add('btn', 'btn-info', 'btn-sm', 'mb-1', 'mr-1');
btn.dataset.toggle = "modal";
return btn;
}
/*
* Compte les rangées de la table
*/
let totalRows = document.querySelector('#totalRows');
function refreshTotalRowsStats(countURL)
{
AJAX('GET', countURL, function(request)
{
let json = JSON.parse(request.responseText);
totalRows.dataset.total = json;
totalRows.textContent = json + (json > 1 ? ' légumes' : ' légume');
});
}
/*
* Compte les résultats de la recherche
*/
let totalPages = document.querySelector('#totalPages');
function refreshSearchStats(searchURL)
{
AJAX('GET', searchURL, function(request)
{
let json = JSON.parse(request.responseText);
totalPages.innerHTML = ''+ json.length +' résultats';
});
}
/*
* Définit le pas de la pagination
*/
let stepform = document.querySelector('form#pagination select');
function selectPaginateStep(paginate, callback)
{
stepform.addEventListener('change', function()
{
let step = (this.value === '-1') ? 10 : this.value;
paginate.step = parseInt(step);
paginate.page = 1;
callback();
});
}
/*
* Affiche le bloc de pagination
*/
let pagiNav = document.querySelector('nav#pagenav ul.pagination');
function displayPagination(URL, paginate, callback)
{
AJAX('GET', URL, function()
{
totalPages.innerHTML = null;
pagiNav.innerHTML = null;
let perPage = paginate.step;
if (totalRows.dataset.total > perPage)
{
let nbPages = Math.ceil(totalRows.dataset.total / perPage);
let currentPage = paginate.page;
for (let i = 1; i <= nbPages; i++)
{
let pageItem = document.createElement('li');
pageItem.classList.add('page-item', (i === currentPage) ? 'active' : null);
let pageItemChild = document.createElement((i === currentPage) ? 'span' : 'a');
pageItemChild.classList.add('page-link');
pageItemChild.textContent = i.toString();
if (i !== currentPage)
{
pageItemChild.addEventListener('click', function()
{
paginate.page = i;
callback();
});
}
pageItem.appendChild(pageItemChild);
pagiNav.appendChild(pageItem);
}
totalPages.innerHTML = '— page '+ currentPage + ' sur '+ nbPages;
}
});
}
/*
* Enlève le bloc de pagination
*/
function removePagination()
{
totalPages.innerHTML = null;
pagiNav.innerHTML = null;
}
/*
* Interrupteur de tri par colonne
*/
function sortToggleByColumn(paginate, callback)
{
let columns = document.querySelectorAll('th[scope="col"] a.btn-sort');
columns.forEach(function(column)
{
let col = column.getAttribute('id');
column.addEventListener('click', function()
{
columns.forEach(function(column) {
let oldCaret = column.parentNode.querySelector('i.fas');
if (oldCaret !== null) {
oldCaret.remove();
}
});
paginate.column = col;
paginate.order = (paginate.order === 'ASC') ? 'DESC' : 'ASC';
callback();
let th = this.parentNode;
let newCaret = document.createElement('i');
newCaret.classList.add('fas', (paginate.order === 'DESC') ? 'fa-caret-down' : 'fa-caret-up');
th.appendChild(newCaret);
});
});
}
/*
* F O N C T I O N S A J A X
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment