| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- window.FilterManager = (() => {
- class FilterManager extends EventTarget {
- updateFilter() {
- for (let i of MediaStorage.Instance.medias) {
- if (!i.ui)
- continue;
- if (this.match(i))
- i.ui.root.classList.remove("filtered");
- else
- i.ui.root.classList.add("filtered");
- }
- this.dispatchEvent(new CustomEvent("filterUpdated"));
- if (this.isFiltering())
- document.body.classList.add("filter-active");
- else
- document.body.classList.remove("filter-active");
- }
- getFilterValues(key) {
- return {
- include: this.#filters[key] || [],
- exclude: this.#excludeFilters[key] || []
- };
- }
- setFilterValue(key, val) {
- this.#filters[key] = val;
- }
- setExclusionFilter(key, val) {
- this.#excludeFilters[key] = val;
- }
- setChronologyRange(min, max) {
- let updated = false;
- if (this.#minDate != min) {
- this.#minDate = min;
- updated = true;
- }
- if (this.#maxDate != max) {
- this.#maxDate = max;
- updated = true;
- }
- updated && this.updateFilter();
- }
- getChronologyRange() {
- return { min: this.#minDate, max: this.#maxDate };
- }
- isFiltering() {
- if (window.chronology.isInitialized()) {
- let chronoRange = window.chronology.getRange();
- if (this.#minDate > chronoRange.min || this.#maxDate < chronoRange.max)
- return true;
- }
- for (let i in this.#excludeFilters) {
- if (this.#excludeFilters[i]?.length)
- return true;
- }
- for (let i in this.#filters) {
- if (this.#filters[i]?.length)
- return true;
- }
- return false;
- }
- // Returns false if image doesnt match
- #matchTags(mediaTags, tags) {
- for (let i of tags)
- if (mediaTags.find(x => x === i || x.startsWith(`${i}/`)))
- return true;
- return false;
- }
- match(mediaItem) {
- const dateTime = mediaItem.getDateTs();
- if (dateTime < this.#minDate || dateTime > this.#maxDate)
- return false;
- for (let i in this.#filters) {
- if (!this.#filters[i].length)
- continue;
- if (i === "Tags" && this.#filters[i]) {
- const mediaTags = mediaItem.allTags();
- if (this.#filters[i].indexOf("") !== -1 && !mediaTags.length)
- continue;
- if (this.#matchTags(mediaTags, this.#filters[i].filter(i => !!i)))
- continue;
- return false;
- }
- if (this.#filters[i].indexOf("") >= 0 && !mediaItem.meta[i]?.value)
- continue;
- if (this.#filters[i].indexOf(""+mediaItem.meta[i]?.value) === -1)
- return false;
- }
- for (let i in this.#excludeFilters) {
- if (!this.#excludeFilters[i].length)
- continue;
- if (i === "Tags" && this.#excludeFilters[i]) {
- const mediaTags = mediaItem.allTags();
- if (this.#excludeFilters[i].indexOf("") !== -1 && !mediaTags.length)
- return false;
- if (this.#matchTags(mediaTags, this.#excludeFilters[i].filter(i => !!i)))
- return false;
- continue;
- }
- if (this.#excludeFilters[i].indexOf("") >= 0 && !mediaItem.meta[i]?.value)
- return false;
- if (this.#excludeFilters[i].indexOf(""+mediaItem.meta[i]?.value) >= 0)
- return false;;
- }
- return true;
- }
- #filters = {};
- #excludeFilters = {};
- #minDate = 0;
- #maxDate = Infinity;
- }
- return new FilterManager();
- })();
|