filters.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. window.FilterManager = (() => {
  2. class FilterManager extends EventTarget {
  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. this.dispatchEvent(new CustomEvent("filterUpdated"));
  13. if (this.isFiltering())
  14. document.body.classList.add("filter-active");
  15. else
  16. document.body.classList.remove("filter-active");
  17. }
  18. setFilterValue(key, val) {
  19. this.#filters[key] = val;
  20. }
  21. setExclusionFilter(key, val) {
  22. this.#excludeFilters[key] = val;
  23. }
  24. setChronologyRange(min, max) {
  25. let updated = false;
  26. if (this.#minDate != min) {
  27. this.#minDate = min;
  28. updated = true;
  29. }
  30. if (this.#maxDate != max) {
  31. this.#maxDate = max;
  32. updated = true;
  33. }
  34. updated && this.updateFilter();
  35. }
  36. isFiltering() {
  37. if (window.chronology.isInitialized()) {
  38. let chronoRange = window.chronology.getRange();
  39. if (this.#minDate > chronoRange.min || this.#maxDate < chronoRange.max)
  40. return true;
  41. }
  42. for (let i in this.#excludeFilters) {
  43. if (this.#excludeFilters[i]?.length)
  44. return true;
  45. }
  46. for (let i in this.#filters) {
  47. if (this.#filters[i]?.length)
  48. return true;
  49. }
  50. return false;
  51. }
  52. // Returns false if image doesnt match
  53. #matchTags(mediaTags, tags) {
  54. for (let i of tags)
  55. if (mediaTags.find(x => x === i || x.startsWith(`${i}/`)))
  56. return true;
  57. return false;
  58. }
  59. match(mediaItem) {
  60. const dateTime = mediaItem.getDateTs();
  61. if (dateTime < this.#minDate || dateTime > this.#maxDate)
  62. return false;
  63. for (let i in this.#filters) {
  64. if (!this.#filters[i].length)
  65. continue;
  66. if (i === "Tags" && this.#filters[i]) {
  67. const mediaTags = mediaItem.allTags();
  68. if (this.#filters[i].indexOf("") !== -1 && !mediaTags.length)
  69. continue;
  70. if (this.#matchTags(mediaTags, this.#filters[i].filter(i => !!i)))
  71. continue;
  72. return false;
  73. }
  74. if (this.#filters[i].indexOf("") >= 0 && !mediaItem.meta[i]?.value)
  75. continue;
  76. if (this.#filters[i].indexOf(""+mediaItem.meta[i]?.value) === -1)
  77. return false;
  78. }
  79. for (let i in this.#excludeFilters) {
  80. if (!this.#excludeFilters[i].length)
  81. continue;
  82. if (i === "Tags" && this.#excludeFilters[i]) {
  83. const mediaTags = mediaItem.allTags();
  84. if (this.#excludeFilters[i].indexOf("") !== -1 && !mediaTags.length)
  85. return false;
  86. if (this.#matchTags(mediaTags, this.#excludeFilters[i].filter(i => !!i)))
  87. return false;
  88. continue;
  89. }
  90. if (this.#excludeFilters[i].indexOf("") >= 0 && !mediaItem.meta[i]?.value)
  91. return false;
  92. if (this.#excludeFilters[i].indexOf(""+mediaItem.meta[i]?.value) >= 0)
  93. return false;;
  94. }
  95. return true;
  96. }
  97. #filters = {};
  98. #excludeFilters = {};
  99. #minDate = 0;
  100. #maxDate = Infinity;
  101. }
  102. return new FilterManager();
  103. })();