filters.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. window.FilterManager = (() => {
  2. class FilterManager {
  3. #updateFilter() {
  4. for (let i of MediaStorage.Instance.medias) {
  5. if (!i.ui)
  6. continue;
  7. if (this.match(i))
  8. i.ui.root.classList.remove("filtered");
  9. else
  10. i.ui.root.classList.add("filtered");
  11. }
  12. MediaStorage.Instance.onFilterUpdated();
  13. }
  14. setFilterValue(key, val) {
  15. this.#filters[key] = val;
  16. this.#updateFilter();
  17. }
  18. // Returns false if image doesnt match
  19. #matchTags(mediaTags, tags) {
  20. for (let i of tags)
  21. if (mediaTags.find(x => x === i || x.startsWith(`${i}/`)))
  22. return true;
  23. return false;
  24. }
  25. match(mediaItem) {
  26. for (let i in this.#filters) {
  27. if (!this.#filters[i] || !this.#filters[i].length)
  28. continue;
  29. if (i === "Tags") {
  30. const mediaTags = mediaItem.allTags();
  31. if (this.#filters[i].indexOf(undefined) !== -1 && !mediaTags.length)
  32. continue;
  33. if (this.#matchTags(mediaTags, this.#filters[i].filter(i => !!i)))
  34. continue;
  35. return false;
  36. }
  37. if (this.#filters[i].indexOf(mediaItem.meta[i]?.value) === -1)
  38. return false;
  39. }
  40. return true;
  41. }
  42. #filters = {};
  43. }
  44. return new FilterManager();
  45. })();