filters.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. getChronologyRange() {
  43. return { min: this.#minDate, max: this.#maxDate };
  44. }
  45. isFiltering() {
  46. if (window.chronology.isInitialized()) {
  47. let chronoRange = window.chronology.getRange();
  48. if (this.#minDate > chronoRange.min || this.#maxDate < chronoRange.max)
  49. return true;
  50. }
  51. for (let i in this.#excludeFilters) {
  52. if (this.#excludeFilters[i]?.length)
  53. return true;
  54. }
  55. for (let i in this.#filters) {
  56. if (this.#filters[i]?.length)
  57. return true;
  58. }
  59. return false;
  60. }
  61. // Returns false if image doesnt match
  62. #matchTags(mediaTags, tags) {
  63. for (let i of tags)
  64. if (mediaTags.find(x => x === i || x.startsWith(`${i}/`)))
  65. return true;
  66. return false;
  67. }
  68. match(mediaItem) {
  69. const dateTime = mediaItem.getDateTs();
  70. if (dateTime < this.#minDate || dateTime > this.#maxDate)
  71. return false;
  72. for (let i in this.#filters) {
  73. if (!this.#filters[i].length)
  74. continue;
  75. if (i === "Tags" && this.#filters[i]) {
  76. const mediaTags = mediaItem.allTags();
  77. if (this.#filters[i].indexOf("") !== -1 && !mediaTags.length)
  78. continue;
  79. if (this.#matchTags(mediaTags, this.#filters[i].filter(i => !!i)))
  80. continue;
  81. return false;
  82. }
  83. if (this.#filters[i].indexOf("") >= 0 && !mediaItem.meta[i]?.value)
  84. continue;
  85. if (this.#filters[i].indexOf(""+mediaItem.meta[i]?.value) === -1)
  86. return false;
  87. }
  88. for (let i in this.#excludeFilters) {
  89. if (!this.#excludeFilters[i].length)
  90. continue;
  91. if (i === "Tags" && this.#excludeFilters[i]) {
  92. const mediaTags = mediaItem.allTags();
  93. if (this.#excludeFilters[i].indexOf("") !== -1 && !mediaTags.length)
  94. return false;
  95. if (this.#matchTags(mediaTags, this.#excludeFilters[i].filter(i => !!i)))
  96. return false;
  97. continue;
  98. }
  99. if (this.#excludeFilters[i].indexOf("") >= 0 && !mediaItem.meta[i]?.value)
  100. return false;
  101. if (this.#excludeFilters[i].indexOf(""+mediaItem.meta[i]?.value) >= 0)
  102. return false;;
  103. }
  104. return true;
  105. }
  106. #filters = {};
  107. #excludeFilters = {};
  108. #minDate = 0;
  109. #maxDate = Infinity;
  110. }
  111. return new FilterManager();
  112. })();