MediaWiki:Script/Filter.js : Différence entre versions

Ligne 15 : Ligne 15 :
  
 
function toNormalForm(str) {
 
function toNormalForm(str) {
     return removeAccent(str).replace(/[^a-zA-Z0-9 ]/g, '');;
+
     return removeAccent(str).replace(/[^a-zA-Z0-9 ]/g, "");;
 +
}
 +
 
 +
 
 +
function addValueToObject(object, key, value) {
 +
         
 +
  if (object[key]) {
 +
    object[key].push(value);
 +
  } else {
 +
    object[key] = [value];
 +
  }
 
}
 
}
  
Ligne 36 : Ligne 46 :
 
   function toggleDropdown(button, dropdownMenu) {
 
   function toggleDropdown(button, dropdownMenu) {
 
     if (button === activeButton) {
 
     if (button === activeButton) {
       dropdownMenu.classList.add('tabber-noactive');
+
       hideElement(dropdownMenu);
 
       activeButton = null;
 
       activeButton = null;
 
     } else {
 
     } else {
 
       if (activeButton) {
 
       if (activeButton) {
         activeButton.nextElementSibling.classList.add('tabber-noactive');
+
         hideElement(activeButton.nextElementSibling);
 
       }
 
       }
       dropdownMenu.classList.remove('tabber-noactive');
+
       showElement(dropdownMenu);
 
       activeButton = button;
 
       activeButton = button;
 
     }
 
     }
Ligne 50 : Ligne 60 :
 
   function closeDropdowns() {
 
   function closeDropdowns() {
 
     if (activeButton) {
 
     if (activeButton) {
       activeButton.nextElementSibling.classList.add('tabber-noactive');
+
       hideElement(activeButton.nextElementSibling);
 
       activeButton = null;
 
       activeButton = null;
 
     }
 
     }
Ligne 76 : Ligne 86 :
 
     }
 
     }
 
   });
 
   });
 +
}
 +
 +
 +
function incrementCounter(counterElement, counterValue) {
 +
 +
  if (counterValue === 0) {
 +
    showElement(counterElement);
 +
  }
 +
 +
  counterElement.textContent = counterValue + 1;
 +
}
 +
 +
 +
function decrementCounter(counterElement, counterValue) {
 +
 +
  if (counterValue === 1) {
 +
    hideElement(counterElement);
 +
  }
 +
 +
  counterElement.textContent = counterValue - 1;
 
}
 
}
  
  
 
function filterItems(filterInformation, cardInformation) {
 
function filterItems(filterInformation, cardInformation) {
 
+
 
   function isObjectValuesIncludedInFilter(parameters, filter) {
+
 
     return Object.keys(filter).every(function(property) {
+
   function showCounter() {
      return parameters[property].split(' ').some(function(value) {
+
     var checkbox = filterInformation.checkbox
        return filter[property].indexOf(value) !== -1;
+
   
      });
+
    for (var filterName in checkbox) {
 +
      var values = checkbox[filterName].values
 +
     
 +
      for (var filterValue in values) {
 +
        var span = values[filterValue].span;
 +
       
 +
        span.textContent = " (" + values[filterValue].value + ")";
 +
        values[filterValue].value = 0;
 +
      }
 +
    }
 +
  }
 +
 
 +
 
 +
  function addCount(cardParameters, checkboxNames, parameter) {
 +
    cardParameters[parameter].split(' ').forEach(function(cardValue) {
 +
      checkboxNames[parameter].values[cardValue].value += 1;
 +
    });
 +
  }
 +
 +
 
 +
  function isObjectValuesInRangeFilter(parameters, rangeFilter) {
 +
    return Object.keys(rangeFilter).every(function(property) {
 +
      var propertyValue = parameters[property];
 +
      return rangeFilter[property].min <= propertyValue && rangeFilter[property].max >= propertyValue;
 
     });
 
     });
 
   }
 
   }
 +
 
 
    
 
    
 
   function filterByName(parameters, filterName) {
 
   function filterByName(parameters, filterName) {
Ligne 96 : Ligne 150 :
 
   }
 
   }
 
    
 
    
   function isObjectValuesIncludedInFilterWithRange(parameters, filter, rangeFilter, filterName) {
+
 
     return isObjectValuesIncludedInFilter(parameters, filter) && filterByName(parameters, filterName) && Object.keys(rangeFilter).every(function(property) {
+
   function isObjectValuesInFilter(parameters, filter) {
       var propertyValue = parameters[property];
+
     return Object.keys(filter).every(function(property) {
      return rangeFilter[property].min <= propertyValue && rangeFilter[property].max >= propertyValue;
+
       return parameters[property].split(' ').some(function(value) {
 +
        return filter[property].indexOf(value) !== -1;
 +
      });
 
     });
 
     });
 
   }
 
   }
 
+
 
 +
 
 +
  function isObjectValuesInFilters(parameters, filter, rangeFilter, filterName) {
 +
    return isObjectValuesInFilter(parameters, filter) && filterByName(parameters, filterName) && isObjectValuesInRangeFilter(parameters, rangeFilter);
 +
  }
 +
 
 
   var filter = filterInformation.filters.filter;
 
   var filter = filterInformation.filters.filter;
 
   var rangeFilter = filterInformation.filters.rangeFilter;
 
   var rangeFilter = filterInformation.filters.rangeFilter;
 
   var filterName = filterInformation.filters.filterName;
 
   var filterName = filterInformation.filters.filterName;
   var listToFilter = cardInformation.listToFilter;
+
  var checkboxNames = filterInformation.checkbox;
 +
   var listToFilter = cardInformation.listToFilter.children;
 
   var cardData = cardInformation.data;
 
   var cardData = cardInformation.data;
 +
 
 +
  if (filterInformation.reverse) {
 +
    var parent = cardInformation.listToFilter;
 +
    for (var reverseIndex = 1; reverseIndex < listToFilter.length; reverseIndex++) {
 +
      parent.insertBefore(listToFilter[reverseIndex], parent.firstChild);
 +
    }
 +
    cardInformation.data.reverse();
 +
  }
 
    
 
    
 
   for (var cardIndex = 0; cardIndex < listToFilter.length; cardIndex++) {
 
   for (var cardIndex = 0; cardIndex < listToFilter.length; cardIndex++) {
 
     var card = listToFilter[cardIndex];
 
     var card = listToFilter[cardIndex];
 
     var cardParameters = cardData[cardIndex];
 
     var cardParameters = cardData[cardIndex];
     if (isObjectValuesIncludedInFilterWithRange(cardParameters, filter, rangeFilter, filterName)) {
+
     if (isObjectValuesInFilters(cardParameters, filter, rangeFilter, filterName)) {
 
       showElement(card);
 
       showElement(card);
 +
      for (var parameter in checkboxNames) {
 +
        addCount(cardParameters, checkboxNames, parameter);
 +
      }
 
     } else {
 
     } else {
 
       hideElement(card);
 
       hideElement(card);
 
     }
 
     }
 
   }
 
   }
 +
  showCounter();
 
}
 
}
  
  
 
function updateFilterObject(filterInformation, cardInformation) {
 
function updateFilterObject(filterInformation, cardInformation) {
 
+
    
   function incrementCounter(counterElement, counterValue) {
 
   
 
    if (counterValue === 0) {
 
      showElement(counterElement);
 
    }
 
   
 
    counterElement.textContent = counterValue + 1;
 
  }
 
 
 
  function decrementCounter(counterElement, counterValue) {
 
   
 
    if (counterValue === 1) {
 
      hideElement(counterElement);
 
    }
 
   
 
    counterElement.textContent = counterValue - 1;
 
  }
 
 
    
 
    
 
   function updateFilter(event, filterByName = false) {
 
   function updateFilter(event, filterByName = false) {
  
 +
   
 
     function handleNumberType(target) {
 
     function handleNumberType(target) {
 
        
 
        
Ligne 155 : Ligne 213 :
 
         rangeFilter[filterName][extremum] = currentValue;
 
         rangeFilter[filterName][extremum] = currentValue;
 
       } else {
 
       } else {
        var range = {min: initialRange.min, max: initialRange.max};
 
       
 
 
         incrementCounter(counterElement, counterValue);
 
         incrementCounter(counterElement, counterValue);
         range[extremum] = currentValue;
+
         rangeFilter[filterName] = {min: initialRange.min, max: initialRange.max};
         rangeFilter[filterName] = range;
+
         rangeFilter[filterName][extremum] = currentValue;
 
       }
 
       }
 
       if ((rangeFilter[filterName].max === initialRange.max) && (rangeFilter[filterName].min === initialRange.min)) {
 
       if ((rangeFilter[filterName].max === initialRange.max) && (rangeFilter[filterName].min === initialRange.min)) {
Ligne 166 : Ligne 222 :
 
       }
 
       }
 
     }
 
     }
 +
   
 
      
 
      
 
     function handleCheckbox(target) {
 
     function handleCheckbox(target) {
Ligne 177 : Ligne 234 :
 
       if (target.checked) {
 
       if (target.checked) {
 
         incrementCounter(counterElement, counterValue);
 
         incrementCounter(counterElement, counterValue);
         if (filter[filterName]) {
+
         addValueToObject(filter, filterName, filterValue);
          filter[filterName].push(filterValue);
+
          
         } else {
 
          filter[filterName] = [filterValue];
 
        }
 
 
       } else {
 
       } else {
 
         decrementCounter(counterElement, counterValue);
 
         decrementCounter(counterElement, counterValue);
Ligne 205 : Ligne 259 :
 
         handleNumberType(target);
 
         handleNumberType(target);
 
       } else if (type === "checkbox") {
 
       } else if (type === "checkbox") {
         handleCheckbox(target);
+
         if (target.id === "filter-reverse") {
 +
          filterInformation.reverse = true;
 +
        } else {
 +
          handleCheckbox(target);
 +
        }
 
       }
 
       }
 
     }
 
     }
Ligne 234 : Ligne 292 :
  
 
function filterInitialization() {
 
function filterInitialization() {
 +
 
 
   function createSpan() {
 
   function createSpan() {
 
     var span = document.createElement("span");
 
     var span = document.createElement("span");
Ligne 239 : Ligne 298 :
 
     span.style.fontSize = '12px';
 
     span.style.fontSize = '12px';
 
     return span;
 
     return span;
 +
  }
 +
 
 +
 
 +
  function handlefilterParameters(filterParameters, param, filterName) {
 +
   
 +
    if (param === "levels") {
 +
      addValueToObject(filterParameters, "levels", filterName)
 +
     
 +
    } else if (param.startsWith("replace_")) {
 +
     
 +
      var replace = param.split("replace_")[1].replace("_", " ");
 +
     
 +
      if (!filterParameters["replace"]) {
 +
        filterParameters["replace"] = {};
 +
      }
 +
      filterParameters["replace"][filterName] = {"value": replace};
 +
     
 +
    } else if (param === "element") {
 +
      filterParameters["element"] = filterName;
 +
    }
 
   }
 
   }
 
    
 
    
 
   var form = document.getElementById("filter-form");
 
   var form = document.getElementById("filter-form");
 
   var filters = { filterName: "", filter: {}, rangeFilter: {} };
 
   var filters = { filterName: "", filter: {}, rangeFilter: {} };
   var filterInformation = { form: form, filters: filters, range: {}, checkbox: {} };
+
   var filterInformation = { form: form, filters: filters, range: {}, checkbox: {}, reverse: false };
 
   var allButton = form.querySelectorAll("button");
 
   var allButton = form.querySelectorAll("button");
 +
  var filterParameters = {};
  
 
   allButton.forEach(function(button) {
 
   allButton.forEach(function(button) {
 +
   
 
     var buttonSibling = button.nextElementSibling;
 
     var buttonSibling = button.nextElementSibling;
 +
   
 
     if (!buttonSibling) return;
 
     if (!buttonSibling) return;
 
+
   
 
     var filterName = buttonSibling.id;
 
     var filterName = buttonSibling.id;
 
     var counter = buttonSibling.nextElementSibling;
 
     var counter = buttonSibling.nextElementSibling;
     if (!filterName || !counter) return;
+
    var param = buttonSibling.dataset.param;
 +
   
 +
     if (!filterName || !counter || !param) return;
  
 
     var infoType = filterName.endsWith("-range") ? "range" : "checkbox";
 
     var infoType = filterName.endsWith("-range") ? "range" : "checkbox";
     var filterObj = filterInformation[infoType][filterName.replace("-range", "")] = { counter: counter };
+
     var filterName = filterName.replace("-range", "")
 
+
    var filterObj = filterInformation[infoType][filterName] = { counter: counter };
 +
   
 +
    handlefilterParameters(filterParameters, param, filterName);
 +
   
 
     if (infoType === "checkbox") {
 
     if (infoType === "checkbox") {
 
       filterObj.values = {};
 
       filterObj.values = {};
Ligne 262 : Ligne 349 :
 
       allInput.forEach(function(input) {
 
       allInput.forEach(function(input) {
 
         var filterValue = input.dataset.filter;
 
         var filterValue = input.dataset.filter;
 +
       
 
         if (!filterValue) return;
 
         if (!filterValue) return;
  
 
         var span = createSpan();
 
         var span = createSpan();
 
         input.parentElement.appendChild(span);
 
         input.parentElement.appendChild(span);
         filterObj.values[filterValue] = { value: 0, span: span };
+
         filterObj.values[filterValue] = { value: 0, span: span};
 
       });
 
       });
 
     } else if (infoType === "range") {
 
     } else if (infoType === "range") {
Ligne 275 : Ligne 363 :
 
   });
 
   });
  
   return filterInformation;
+
   return [filterInformation, filterParameters];
 
}
 
}
  
  
function getCardInformation(filterInformation) {
+
function getCardInformation(filterParameters) {
 
    
 
    
   var listToFilter = document.getElementById("list-to-filter").children;
+
  function createDataSelector(name) {
 +
    return "[data-" + name + "]"
 +
  }
 +
 
 +
   var listToFilter = document.getElementById("list-to-filter");
 +
  var listToFilterChildren = listToFilter.children;
 
   var allCardInformation = [];
 
   var allCardInformation = [];
   var filterNames = Object.keys(filterInformation.checkbox).concat(Object.keys(filterInformation.range));
+
   var filterLevelsNames = filterParameters["levels"];
 +
  var filterWithReplace = filterParameters["replace"];
 +
  var filterElementName = filterParameters["element"];
 +
 
 +
  if (filterLevelsNames) {
 +
    var [level0, level1] = filterLevelsNames;
 +
    var levelsSelector = createDataSelector(level0);
 +
  }
 +
 
 +
  if (filterWithReplace) {
 +
    for (var replaceName in filterWithReplace) {
 +
      filterWithReplace[replaceName].selector = createDataSelector(replaceName);
 +
    }
 +
  }
 +
 
 +
  if (filterElementName) {
 +
    var elementSelector = createDataSelector(filterElementName);
 +
  }
  
   for (var cardIndex = 0; cardIndex < listToFilter.length; cardIndex++) {
+
   for (var cardIndex = 0; cardIndex < listToFilterChildren.length; cardIndex++) {
     var card = listToFilter[cardIndex];
+
   
 +
     var card = listToFilterChildren[cardIndex];
 
     var cardInformation = {};
 
     var cardInformation = {};
 
     var cardName = card.querySelector("[data-name]").textContent;
 
     var cardName = card.querySelector("[data-name]").textContent;
 
     cardInformation.name = toNormalForm(cardName);
 
     cardInformation.name = toNormalForm(cardName);
 
      
 
      
     if (filterNames.indexOf("e0") !== -1) {
+
     if (filterLevelsNames) {
       var e0Element = card.querySelector("[data-e0]");
+
       var levelsElement = card.querySelector(levelsSelector);
       var [e0Value, e1Value] = e0Element.textContent.slice(1, -1).split(", ");
+
       var [levelValue0, levelValue1] = levelsElement.textContent.slice(1, -1).split(", ");
       cardInformation.e0 = Number(e0Value);
+
       cardInformation[level0] = Number(levelValue0);
       cardInformation.e1 = removeAccent(e1Value);
+
       cardInformation[level1] = removeAccent(levelValue1);
 
     }
 
     }
 
      
 
      
     if (filterNames.indexOf("e2") !== -1) {
+
     if (filterElementName) {
       var e2Element = card.querySelector("[data-e2]").children;
+
       var elementChild = card.querySelector(elementSelector).children;
       var e2Value = "";
+
       var elementValue = "";
       if (e2Element.length) {
+
       if (elementChild.length) {
         for (var e2ValueIndex = 0; e2ValueIndex < e2Element.length; e2ValueIndex++) {
+
         for (var elementIndex = 0; elementIndex < elementChild.length; elementIndex++) {
           e2Value += e2Element[e2ValueIndex].title + " ";
+
           elementValue += elementChild[elementIndex].title + " ";
 
         }
 
         }
 
       }
 
       }
       e2Value = e2Value.trim() || "Aucun";
+
       elementValue = elementValue.trim() || "Aucun";
       cardInformation.e2 = removeAccent(e2Value);
+
       cardInformation[filterElementName] = removeAccent(elementValue);
 
     }
 
     }
 
        
 
        
     if (filterNames.indexOf("e3") !== -1) {
+
     if (filterWithReplace) {
      var e3Value = card.querySelector("[data-e3]").textContent.replace(" type", "");
+
      for (var replaceName in filterWithReplace) {
      cardInformation.e3 = removeAccent(e3Value);
+
        var replaceInformation = filterWithReplace[replaceName];
    }
+
        var replaceValue = card.querySelector(replaceInformation.selector).textContent.replace(replaceInformation.value, '');
     
+
        cardInformation[replaceName] = removeAccent(replaceValue)
    if (filterNames.indexOf("e4") !== -1) {
+
      }
      var e4Value = card.querySelector("[data-e4]").textContent.replace('+ ', '');
 
      cardInformation.e4 = removeAccent(e4Value);
 
 
     }
 
     }
 
        
 
        
Ligne 325 : Ligne 434 :
 
    
 
    
 
   return allCardInformation;
 
   return allCardInformation;
 +
}
 +
 +
 +
function filterWithUrl(filterInformation) {
 +
 
 +
  function processParameter(key, value, filters) {
 +
 +
    if (key === "name") {
 +
      var filterName = document.getElementById("filter-name");
 +
      filterName.value = toNormalForm(value);
 +
      filters.filterName = value;
 +
    } else if (Object.keys(filterInformation.checkbox).indexOf(key) !== -1) {
 +
      var checkboxElement = document.getElementById(key + "-" + value);
 +
      if (!checkboxElement) return;
 +
      var counterElement = filterInformation.checkbox[key].counter;
 +
      var counterValue = Number(counterElement.textContent);
 +
      incrementCounter(counterElement, counterValue);
 +
      checkboxElement.checked = true;
 +
      addValueToObject(filters.filter, key, value);
 +
    } else if (key === "reverse") {
 +
      if (value === "1") {
 +
        filterInformation.reverse = true;
 +
        var reverseElement = document.getElementById("filter-reverse");
 +
        if (reverseElement) {
 +
          reverseElement.checked = true;
 +
        }
 +
      }
 +
    } else {
 +
      var splitKey = key.split("-")[0];
 +
      if (Object.keys(filterInformation.range).indexOf(splitKey) !== -1) {
 +
        var rangeElement = document.getElementById(key);
 +
        if (!rangeElement) return;
 +
        rangeElement.value = value;
 +
        var rangeInformation = filterInformation.range[splitKey];
 +
        incrementCounter(rangeInformation.counter, 0);
 +
        var extremum = key.split("-")[1];
 +
        var rangeFilter = filters.rangeFilter;
 +
        if (rangeFilter[splitKey]) {
 +
          rangeFilter[splitKey][extremum] = value;
 +
        } else {
 +
          rangeFilter[splitKey] = { min: rangeInformation.init.min, max: rangeInformation.init.max };
 +
          rangeFilter[splitKey][extremum] = value;
 +
        }
 +
      }
 +
    }
 +
  }
 +
 
 +
  var url = new URL(window.location.href);
 +
  var urlHash = url.hash;
 +
  var useParams = true;
 +
  var filters = filterInformation.filters;
 +
 +
  if (urlHash) {
 +
   
 +
    var start = ".3F";
 +
    var equal = '.3D';
 +
    var and = '.26';
 +
    var startParametersIndex = urlHash.indexOf(start);
 +
 +
    if (startParametersIndex !== -1) {
 +
     
 +
      useParams = false;
 +
      var parameters = urlHash.slice(startParametersIndex + start.length);
 +
 +
      parameters.split(and).forEach(function(keyValue) {
 +
        var [key, value] = keyValue.split(equal);
 +
        processParameter(key, value, filters);
 +
      });
 +
    }
 +
  }
 +
 
 +
  if (useParams) {
 +
    var URLparams = new URLSearchParams(url.search);
 +
 +
    for (var [key, value] of URLparams.entries()) {
 +
      processParameter(key, value, filters);
 +
    }
 +
  }
 
}
 
}
  
 
      
 
      
 
(function(){
 
(function(){
  var filterInformation = filterInitialization();
 
  var cardInformation = getCardInformation(filterInformation);
 
 
    
 
    
   handleDropdowns(filterInformation)
+
  var [filterInformation, filterParameters] = filterInitialization();
 +
  var cardInformation = getCardInformation(filterParameters);
 +
 
 +
   handleDropdowns(filterInformation);
 +
  filterWithUrl(filterInformation);
 +
  filterItems(filterInformation, cardInformation);
 
   updateFilterObject(filterInformation, cardInformation);
 
   updateFilterObject(filterInformation, cardInformation);
 
   loading();
 
   loading();
 
})();
 
})();

Version du 31 août 2023 à 23:02

function hideElement(element) {
  element.classList.add("tabber-noactive");
}


function showElement(element) {
  element.classList.remove("tabber-noactive");
}


function removeAccent(str) {
  return str.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase()
}


function toNormalForm(str) {
    return removeAccent(str).replace(/[^a-zA-Z0-9 ]/g, "");;
}


function addValueToObject(object, key, value) {
          
  if (object[key]) {
    object[key].push(value);
  } else {
    object[key] = [value];
  }
}


function loading() {
  
  var mainContainer = document.getElementById("hide-all");
  var loadingAnimation = document.getElementById("loading-animation");

  mainContainer.classList.remove("tabber-noactive");
  loadingAnimation.classList.add("tabber-noactive");
}


function handleDropdowns(filterInformation) {
  
  var form = filterInformation.form;
  var activeButton = null;

  function toggleDropdown(button, dropdownMenu) {
    if (button === activeButton) {
      hideElement(dropdownMenu);
      activeButton = null;
    } else {
      if (activeButton) {
        hideElement(activeButton.nextElementSibling);
      }
      showElement(dropdownMenu);
      activeButton = button;
    }
  }

  
  function closeDropdowns() {
    if (activeButton) {
      hideElement(activeButton.nextElementSibling);
      activeButton = null;
    }
  }

  form.addEventListener('click', function(event) {

    var target = event.target;
    var button = target.closest('button');
    
    if (button) {
      
      var dropdownMenu = button.nextElementSibling;
      
      toggleDropdown(button, dropdownMenu);
      
    } else if (!target.closest('.dropdown-menu')) {
      closeDropdowns();
    }
  });

  document.addEventListener('click', function(event) {
    if (!form.contains(event.target)) {
      closeDropdowns();
    }
  });
}


function incrementCounter(counterElement, counterValue) {

  if (counterValue === 0) {
    showElement(counterElement);
  }

  counterElement.textContent = counterValue + 1;
}


function decrementCounter(counterElement, counterValue) {

  if (counterValue === 1) {
    hideElement(counterElement);
  }

  counterElement.textContent = counterValue - 1;
}


function filterItems(filterInformation, cardInformation) {
  
  
  function showCounter() {
    var checkbox = filterInformation.checkbox
    
    for (var filterName in checkbox) {
      var values = checkbox[filterName].values
      
      for (var filterValue in values) {
        var span = values[filterValue].span;
        
        span.textContent = " (" + values[filterValue].value + ")";
        values[filterValue].value = 0;
      }
    }
  }
  
  
  function addCount(cardParameters, checkboxNames, parameter) {
    cardParameters[parameter].split(' ').forEach(function(cardValue) {
      checkboxNames[parameter].values[cardValue].value += 1;
    });
  }
 
  
  function isObjectValuesInRangeFilter(parameters, rangeFilter) {
    return Object.keys(rangeFilter).every(function(property) {
      var propertyValue = parameters[property];
      return rangeFilter[property].min <= propertyValue && rangeFilter[property].max >= propertyValue;
    });
  }
  
  
  function filterByName(parameters, filterName) {
    if (filterName) {
      return parameters.name.indexOf(filterName) !== -1;
    }
    return true;
  }
  
  
  function isObjectValuesInFilter(parameters, filter) {
    return Object.keys(filter).every(function(property) {
      return parameters[property].split(' ').some(function(value) {
        return filter[property].indexOf(value) !== -1;
      });
    });
  }
  
  
  function isObjectValuesInFilters(parameters, filter, rangeFilter, filterName) {
    return isObjectValuesInFilter(parameters, filter) && filterByName(parameters, filterName) && isObjectValuesInRangeFilter(parameters, rangeFilter);
  }
  
  var filter = filterInformation.filters.filter;
  var rangeFilter = filterInformation.filters.rangeFilter;
  var filterName = filterInformation.filters.filterName;
  var checkboxNames = filterInformation.checkbox;
  var listToFilter = cardInformation.listToFilter.children;
  var cardData = cardInformation.data;
  
  if (filterInformation.reverse) {
    var parent = cardInformation.listToFilter;
    for (var reverseIndex = 1; reverseIndex < listToFilter.length; reverseIndex++) {
      parent.insertBefore(listToFilter[reverseIndex], parent.firstChild);
    }
    cardInformation.data.reverse();
  }
  
  for (var cardIndex = 0; cardIndex < listToFilter.length; cardIndex++) {
    var card = listToFilter[cardIndex];
    var cardParameters = cardData[cardIndex];
    if (isObjectValuesInFilters(cardParameters, filter, rangeFilter, filterName)) {
      showElement(card);
      for (var parameter in checkboxNames) {
        addCount(cardParameters, checkboxNames, parameter);
      }
    } else {
      hideElement(card);
    }
  }
  showCounter();
}


function updateFilterObject(filterInformation, cardInformation) {
  
  
  function updateFilter(event, filterByName = false) {

    
    function handleNumberType(target) {
      
      var [filterName, extremum] = target.id.split("-");
      var currentValue = Number(target.value);
      var counterElement = filterInformation.range[filterName].counter;
      var counterValue = Number(counterElement.textContent);
      var initialRange = filterInformation.range[filterName].init;
      var rangeFilter = filterInformation.filters.rangeFilter;
      
      if (rangeFilter[filterName]) {
        rangeFilter[filterName][extremum] = currentValue;
      } else {
        incrementCounter(counterElement, counterValue);
        rangeFilter[filterName] = {min: initialRange.min, max: initialRange.max};
        rangeFilter[filterName][extremum] = currentValue;
      }
      if ((rangeFilter[filterName].max === initialRange.max) && (rangeFilter[filterName].min === initialRange.min)) {
        decrementCounter(counterElement, counterValue);
        delete rangeFilter[filterName];
      }
    }
    
    
    function handleCheckbox(target) {
      
      var filterName = target.id.split("-")[0];
      var filter = filterInformation.filters.filter;
      var filterValue = target.dataset.filter;
      var counterElement = filterInformation.checkbox[filterName].counter;
      var counterValue = Number(counterElement.textContent);
      
      if (target.checked) {
        incrementCounter(counterElement, counterValue);
        addValueToObject(filter, filterName, filterValue);
        
      } else {
        decrementCounter(counterElement, counterValue);
        var index = filter[filterName].indexOf(filterValue);
        if (index !== -1) {
          if (filter[filterName].length === 1) {
            delete filter[filterName];
          } else {
            filter[filterName].splice(index, 1);
          }
        }
      }
    }
    
    var target = event.target;
    
    if (filterByName) {
      filterInformation.filters.filterName = toNormalForm(target.value);
    } else {
      var type = target.type;
      
      if (type === "number") {
        handleNumberType(target);
      } else if (type === "checkbox") {
        if (target.id === "filter-reverse") {
          filterInformation.reverse = true;
        } else {
          handleCheckbox(target);
        }
      }
    }
    filterItems(filterInformation, cardInformation);
  }
  
  var form = filterInformation.form;
  var debounceTimer;
  
  form.addEventListener("submit", function(event) {
    event.preventDefault();
  });
  
  form.addEventListener("change", function(event) {
    updateFilter(event);
  });
  
  form.addEventListener("input", function(event) {
    if (event.target.id === "filter-name") {
      clearTimeout(debounceTimer);
      debounceTimer = setTimeout(function() {
        updateFilter(event, true);
      }, 500);
    }
  });
}


function filterInitialization() {
  
  function createSpan() {
    var span = document.createElement("span");
    span.style.color = '#9F9F9F';
    span.style.fontSize = '12px';
    return span;
  }
  
  
  function handlefilterParameters(filterParameters, param, filterName) {
    
    if (param === "levels") {
      addValueToObject(filterParameters, "levels", filterName)
      
    } else if (param.startsWith("replace_")) {
      
      var replace = param.split("replace_")[1].replace("_", " ");
      
      if (!filterParameters["replace"]) {
        filterParameters["replace"] = {};
      }
      filterParameters["replace"][filterName] = {"value": replace};
      
    } else if (param === "element") {
      filterParameters["element"] = filterName;
    }
  }
  
  var form = document.getElementById("filter-form");
  var filters = { filterName: "", filter: {}, rangeFilter: {} };
  var filterInformation = { form: form, filters: filters, range: {}, checkbox: {}, reverse: false };
  var allButton = form.querySelectorAll("button");
  var filterParameters = {};

  allButton.forEach(function(button) {
    
    var buttonSibling = button.nextElementSibling;
    
    if (!buttonSibling) return;
    
    var filterName = buttonSibling.id;
    var counter = buttonSibling.nextElementSibling;
    var param = buttonSibling.dataset.param;
    
    if (!filterName || !counter || !param) return;

    var infoType = filterName.endsWith("-range") ? "range" : "checkbox";
    var filterName = filterName.replace("-range", "")
    var filterObj = filterInformation[infoType][filterName] = { counter: counter };
    
    handlefilterParameters(filterParameters, param, filterName);
    
    if (infoType === "checkbox") {
      filterObj.values = {};
      var allInput = buttonSibling.querySelectorAll("input");
      allInput.forEach(function(input) {
        var filterValue = input.dataset.filter;
        
        if (!filterValue) return;

        var span = createSpan();
        input.parentElement.appendChild(span);
        filterObj.values[filterValue] = { value: 0, span: span};
      });
    } else if (infoType === "range") {
      filterObj.init = {}
      filterObj.init.min = Number(buttonSibling.children[0].firstChild.min);
      filterObj.init.max = Number(buttonSibling.children[0].firstChild.max);
    }
  });

  return [filterInformation, filterParameters];
}


function getCardInformation(filterParameters) {
  
  function createDataSelector(name) {
    return "[data-" + name + "]"
  }
  
  var listToFilter = document.getElementById("list-to-filter");
  var listToFilterChildren = listToFilter.children;
  var allCardInformation = [];
  var filterLevelsNames = filterParameters["levels"];
  var filterWithReplace = filterParameters["replace"];
  var filterElementName = filterParameters["element"];
  
  if (filterLevelsNames) {
    var [level0, level1] = filterLevelsNames;
    var levelsSelector = createDataSelector(level0);
  }
  
  if (filterWithReplace) {
    for (var replaceName in filterWithReplace) {
      filterWithReplace[replaceName].selector = createDataSelector(replaceName);
    }
  }
  
  if (filterElementName) {
    var elementSelector = createDataSelector(filterElementName);
  }

  for (var cardIndex = 0; cardIndex < listToFilterChildren.length; cardIndex++) {
    
    var card = listToFilterChildren[cardIndex];
    var cardInformation = {};
    var cardName = card.querySelector("[data-name]").textContent;
    cardInformation.name = toNormalForm(cardName);
    
    if (filterLevelsNames) {
      var levelsElement = card.querySelector(levelsSelector);
      var [levelValue0, levelValue1] = levelsElement.textContent.slice(1, -1).split(", ");
      cardInformation[level0] = Number(levelValue0);
      cardInformation[level1] = removeAccent(levelValue1);
    }
    
    if (filterElementName) {
      var elementChild = card.querySelector(elementSelector).children;
      var elementValue = "";
      if (elementChild.length) {
        for (var elementIndex = 0; elementIndex < elementChild.length; elementIndex++) {
          elementValue += elementChild[elementIndex].title + " ";
        }
      }
      elementValue = elementValue.trim() || "Aucun";
      cardInformation[filterElementName] = removeAccent(elementValue);
    }
      
    if (filterWithReplace) {
      for (var replaceName in filterWithReplace) {
        var replaceInformation = filterWithReplace[replaceName];
        var replaceValue = card.querySelector(replaceInformation.selector).textContent.replace(replaceInformation.value, '');
        cardInformation[replaceName] = removeAccent(replaceValue)
      }
    }
      
    allCardInformation.push(cardInformation);
  }
  allCardInformation = { listToFilter: listToFilter, data: allCardInformation };
  
  return allCardInformation;
}


function filterWithUrl(filterInformation) {
  
  function processParameter(key, value, filters) {

    if (key === "name") {
      var filterName = document.getElementById("filter-name");
      filterName.value = toNormalForm(value);
      filters.filterName = value;
    } else if (Object.keys(filterInformation.checkbox).indexOf(key) !== -1) {
      var checkboxElement = document.getElementById(key + "-" + value);
      if (!checkboxElement) return;
      var counterElement = filterInformation.checkbox[key].counter;
      var counterValue = Number(counterElement.textContent);
      incrementCounter(counterElement, counterValue);
      checkboxElement.checked = true;
      addValueToObject(filters.filter, key, value);
    } else if (key === "reverse") {
      if (value === "1") {
        filterInformation.reverse = true;
        var reverseElement = document.getElementById("filter-reverse");
        if (reverseElement) {
          reverseElement.checked = true;
        }
      }
    } else {
      var splitKey = key.split("-")[0];
      if (Object.keys(filterInformation.range).indexOf(splitKey) !== -1) {
        var rangeElement = document.getElementById(key);
        if (!rangeElement) return;
        rangeElement.value = value;
        var rangeInformation = filterInformation.range[splitKey];
        incrementCounter(rangeInformation.counter, 0);
        var extremum = key.split("-")[1];
        var rangeFilter = filters.rangeFilter;
        if (rangeFilter[splitKey]) {
          rangeFilter[splitKey][extremum] = value;
        } else {
          rangeFilter[splitKey] = { min: rangeInformation.init.min, max: rangeInformation.init.max };
          rangeFilter[splitKey][extremum] = value;
        }
      }
    }
  }
  
  var url = new URL(window.location.href);
  var urlHash = url.hash;
  var useParams = true;
  var filters = filterInformation.filters;

  if (urlHash) {
    
    var start = ".3F";
    var equal = '.3D';
    var and = '.26';
    var startParametersIndex = urlHash.indexOf(start);

    if (startParametersIndex !== -1) {
      
      useParams = false;
      var parameters = urlHash.slice(startParametersIndex + start.length);

      parameters.split(and).forEach(function(keyValue) {
        var [key, value] = keyValue.split(equal);
        processParameter(key, value, filters);
      });
    }
  }
  
  if (useParams) {
    var URLparams = new URLSearchParams(url.search);

    for (var [key, value] of URLparams.entries()) {
      processParameter(key, value, filters);
    }
  }
}

    
(function(){
  
  var [filterInformation, filterParameters] = filterInitialization();
  var cardInformation = getCardInformation(filterParameters);

  handleDropdowns(filterInformation);
  filterWithUrl(filterInformation);
  filterItems(filterInformation, cardInformation);
  updateFilterObject(filterInformation, cardInformation);
  loading();
})();