filters.js 4.1 KB

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