-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_list.js
148 lines (130 loc) · 5.09 KB
/
image_list.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"use strict";
const loadAllImages = async () => {
try {
// Use a relative URL to fetch images.json
const response = await fetch("/images.json");
if (response.ok) {
const imageData = await response.json();
const images = imageData.images;
const tagsToSearch = getTagsFromStorage();
let count = 0;
for (const img of images) {
console.log(images.length - 1)
console.log(img.id);
// Check if the image matches the search criteria (tagsToSearch)
if (doesImageMatchSearch(img.tags, tagsToSearch)) {
if (count === 0) {
$("#one").append(`<img class="image_content" src="${img.path}" id="${img.id}">`);
count++;
} else if (count === 1) {
$("#two").append(`<img class="image_content" src="${img.path}" id="${img.id}">`);
count++;
} else if (count === 2) {
$("#three").append(`<img class="image_content" src="${img.path}" id="${img.id}">`);
count = 0;
}
}
else if (sessionStorage.getItem("tags") == '[""]' || sessionStorage.getItem("tags") == null) {
if (count == 0) {
$("#one").append(`<img class="image_content" src="${img.path}" id="${img.id}">`);
count++;
} else if (count == 1) {
$("#two").append(`<img class="image_content" src="${img.path}" id="${img.id}">`);
count++;
} else if (count == 2) {
$("#three").append(`<img class="image_content" src="${img.path}" id="${img.id}">`);
count = 0;
}
}
if (img.id == images.length) {
sessionStorage.setItem("largest_id", img.id);
}
}
} else {
console.error("Failed to fetch images:", response.status);
}
} catch (error) {
console.error("Error retrieving images: ", error);
}
};
const addNewImage = async (path, tags) => {
let new_id;
if (isNaN(parseInt(sessionStorage.getItem("largest_id")))) {
sessionStorage.setItem("largest_id", "1")
new_id = parseInt(sessionStorage.getItem("largest_id"))
}
else {
new_id = parseInt(sessionStorage.getItem("largest_id")) + 1
}
const bodyContents = JSON.stringify({
"id": '' + new_id,
"path": path,
"tags": tags.split(" ") // Convert the tags string to an array of tags
});
try {
const response = await fetch("add_image", {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "POST",
body: bodyContents
});
// Check if the image was successfully added
response.ok ? await loadAllImages() : console.error("Failed to add image: ", response.status);
} catch (error) {
console.error("Error adding new image: ", error);
}
};
const getTagsFromStorage = () => {
const tags = sessionStorage.getItem("tags");
return tags ? JSON.parse(tags) : [];
};
const doesImageMatchSearch = (imageTags, searchTags) => {
// Separate tags into included and excluded lists based on '-' prefix
const includedTags = [];
const excludedTags = [];
searchTags.forEach(tag => {
if (tag.startsWith("-")) {
excludedTags.push(tag.substring(1));
}
else {
includedTags.push(tag);
}
});
// Check if any of the excluded tags are present in the imageTags
if (excludedTags.some(tag => imageTags.includes(tag))) {
return false;
}
// Check if all included tags are present in the imageTags
return includedTags.every(tag => imageTags.includes(tag));
};
$("#lookup-button").click(async () => {
const tags = $("#topic-id-selection").val().split(" ");
const filteredTags = tags.map(tag => tag.trim().toLowerCase());
sessionStorage.setItem("tags", JSON.stringify(filteredTags));
location.replace('image_list.html');
});
$(document).ready(async () => {
const tagsToSearch = getTagsFromStorage().join(", ");
$("#search_criteria").append(`<b>Tags:</b> <i>${tagsToSearch}</i>`);
$("#add_image").click(async () => {
// Opens a window
try {
const image_link = $("#image_link").val();
const image_tags = $("#image_tags").val();
await addNewImage(image_link, image_tags);
await location.reload();
}
catch (error) {
console.error("Image failed to post: ", error);
}
});
$(".main_area").on("click",".image_content",async (e) => {
let image_id = e.target.id;
sessionStorage.clear();
sessionStorage.setItem("image_id", image_id);
location.replace("view_image.html");
});
await loadAllImages();
});