diff --git a/static/recherche.js b/static/recherche.js index 5e126fe..6589275 100644 --- a/static/recherche.js +++ b/static/recherche.js @@ -1,17 +1,104 @@ +var listCategories = []; +var listTags = []; + +function getListTags(element) { + let arrayTags = []; + let listLi = element.getElementsByTagName("li"); + for (let i = 0; i < listLi.length; i++) { + arrayTags.push(listLi[i].textContent.toLowerCase()); + } + return arrayTags; +} + function recherche() { - let categorie = document.getElementById("categorie").value; let listElem = document.getElementsByClassName("elem"); - if (categorie == "tout") { + + if (!listCategories.length && !listTags.length) { for (let element of listElem) { element.classList.remove("hide"); } - } else { + } else if (listCategories.length && listTags.length) { for (let element of listElem) { - if (element.classList.contains(categorie)) { + let arrayTags = getListTags(element); + if (listTags.some(r => arrayTags.includes(r)) && + listCategories.some(r => element.classList.contains(r))) { element.classList.remove("hide"); } else { element.classList.add("hide"); } } + } else if (listCategories.length) { + for (let element of listElem) { + if (listCategories.some(r => element.classList.contains(r))) { + element.classList.remove("hide"); + } else { + element.classList.add("hide"); + } + } + } else if (listTags.length) { + for (let element of listElem) { + let arrayTags = getListTags(element); + if (listTags.some(r => arrayTags.includes(r))) { + element.classList.remove("hide"); + } else { + element.classList.add("hide"); + } + } + } +} + +function addCat() { + let categorie = document.getElementById("categorie").value; + if (!listCategories.includes(categorie)) { + listCategories.push(categorie); + let divListCat = document.getElementById("listCategorie"); + let newElem = document.createElement('p'); + newElem.innerHTML = categorie; + newElem.id = categorie; + newElem.onclick = function() {removeCat(this)}; + divListCat.appendChild(newElem); + + recherche(); + } +} + +function removeCat(elem) { + let categorie = elem.id; + if (listCategories.includes(categorie)) { + let idCat = listCategories.indexOf(categorie); + listCategories.splice(idCat, 1); + + let divListCat = document.getElementById("listCategorie"); + divListCat.removeChild(elem); + + recherche(); + } +} + +function addTag() { + let tag = document.getElementById("tag").value; + if (!listTags.includes(tag.toLowerCase())) { + listTags.push(tag.toLowerCase()); + let divListTag = document.getElementById("listTags"); + let newElem = document.createElement('p'); + newElem.innerHTML = tag; + newElem.id = tag; + newElem.onclick = function() {removeTag(this)}; + divListTag.appendChild(newElem); + + recherche(); + } +} + +function removeTag(elem) { + let tag = elem.id; + if (listTags.includes(tag)) { + let idTag = listTags.indexOf(tag); + listTags.splice(idTag, 1); + + let divListTag = document.getElementById("listTags"); + divListTag.removeChild(elem); + + recherche(); } } diff --git a/templates/index.html b/templates/index.html index ad43a07..d2c02e2 100644 --- a/templates/index.html +++ b/templates/index.html @@ -15,7 +15,25 @@ A propos - {% if not recherche %} + {% if recherche %} +
+

Recherche

+
+
+
+
+
+ + + + +
+
+ {% else %}