|
1 | 1 | (async () => { |
2 | 2 | const localStorageGalleryKey = "imageGalleryNavigation-GalleryID"; |
| 3 | + const localStorageGalleryParams = "imageGalleryNavigation-GalleryParams"; |
| 4 | + |
| 5 | + const defaultSearchParams = new URLSearchParams({ |
| 6 | + sortby: "title", |
| 7 | + sortdir: "asc", |
| 8 | + }); |
3 | 9 |
|
4 | 10 | // In order to handle scenarios where an image is in multiple galleries, capture ID of gallery the user is navigating from. |
5 | 11 | // If user navigates directly to an image URL and image is in multiple galleries, we will just use the first gallery in list. |
6 | 12 | // This may break if user jumps around in browser history, in which case we will fall back to basic scenario of assuming first gallery in list. |
7 | 13 | async function setupGalleryImageLinks() { |
8 | 14 | document.querySelectorAll("a[href*='/images/']").forEach(function (link) { |
9 | 15 | link.addEventListener("click", () => { |
| 16 | + // Parse Gallery URL. |
10 | 17 | var galleryID = window.location.pathname.split("/")[2]; |
| 18 | + |
| 19 | + // Save Gallery Info. |
11 | 20 | localStorage.setItem(localStorageGalleryKey, galleryID); |
| 21 | + localStorage.setItem(localStorageGalleryParams, window.location.search); |
12 | 22 | }); |
13 | 23 | }); |
14 | 24 | } |
|
22 | 32 | if (imageGalleries != null && imageGalleries.length > 0) { |
23 | 33 | // Get first entry in galleries list. |
24 | 34 | var galleryID = imageGalleries[0]; |
| 35 | + var galleryParams = defaultSearchParams; |
25 | 36 |
|
26 | 37 | // Check if there is a saved gallery ID and it is in gallery list. If true, use saved ID. |
27 | 38 | var savedGalleryId = localStorage.getItem(localStorageGalleryKey); |
| 39 | + var savedGalleryParamsStr = localStorage.getItem( |
| 40 | + localStorageGalleryParams |
| 41 | + ); |
| 42 | + var savedGalleryParams = savedGalleryParamsStr |
| 43 | + ? new URLSearchParams(savedGalleryParamsStr) |
| 44 | + : defaultSearchParams; |
28 | 45 | if (savedGalleryId != null && imageGalleries.includes(savedGalleryId)) { |
29 | 46 | galleryID = savedGalleryId; |
| 47 | + |
| 48 | + if (savedGalleryParams != null) { |
| 49 | + galleryParams = savedGalleryParams; |
| 50 | + } |
30 | 51 | } else { |
31 | 52 | localStorage.setItem(localStorageGalleryKey, galleryID); |
| 53 | + localStorage.setItem(localStorageGalleryParams, null); |
32 | 54 | } |
33 | 55 |
|
34 | 56 | // Get gallery image list. |
35 | | - var galleryImages = await findGalleryImages(galleryID); |
| 57 | + var galleryImages = await findGalleryImages(galleryID, galleryParams); |
36 | 58 | var totalImageCount = galleryImages.length; |
37 | 59 | var currentImageIndex = galleryImages.indexOf(imageID); |
38 | 60 | var nextImageID = |
|
113 | 135 |
|
114 | 136 | function redirectToImage(imageID) { |
115 | 137 | const baseImagesPath = "/images/"; |
116 | | - // window.location.href = `${baseImagesPath}${imageID}`; |
117 | 138 | window.location.replace(`${baseImagesPath}${imageID}`); |
118 | 139 | } |
119 | 140 |
|
|
134 | 155 | return ((index % arrayLength) + arrayLength) % arrayLength; |
135 | 156 | } |
136 | 157 |
|
| 158 | + function getFindFilter(searchParams) { |
| 159 | + var findFilter = { |
| 160 | + per_page: -1, |
| 161 | + sort: searchParams.has("sortby") ? searchParams.get("sortby") : "title", |
| 162 | + direction: searchParams.has("sortdir") |
| 163 | + ? searchParams.get("sortdir").toUpperCase() |
| 164 | + : "ASC", |
| 165 | + }; |
| 166 | + |
| 167 | + return findFilter; |
| 168 | + } |
| 169 | + |
| 170 | + function getImageFilter(galleryID, searchParams) { |
| 171 | + var imageFilter = { |
| 172 | + galleries: { value: galleryID, modifier: "INCLUDES_ALL" }, |
| 173 | + }; |
| 174 | + |
| 175 | + if (searchParams.has("c")) { |
| 176 | + searchParams.getAll("c").forEach((cStr) => { |
| 177 | + // Parse filter condition string. |
| 178 | + cStr = cStr.replaceAll("(", "{").replaceAll(")", "}"); |
| 179 | + cObj = JSON.parse(cStr); |
| 180 | + |
| 181 | + // Init filter type field. |
| 182 | + imageFilter[cObj.type] = {}; |
| 183 | + |
| 184 | + // Get all keys (except for "type"). |
| 185 | + var keys = Object.keys(cObj); |
| 186 | + keys.splice(keys.indexOf("type"), 1); |
| 187 | + |
| 188 | + // Add all filter data. |
| 189 | + keys.forEach((keyName) => { |
| 190 | + if (typeof cObj[keyName] === "object") { |
| 191 | + // Special parsing for object type "value" fields (used where there's possibly a value and value2) |
| 192 | + var keys2 = Object.keys(cObj[keyName]); |
| 193 | + keys2.forEach((keyName2) => { |
| 194 | + imageFilter[cObj.type][keyName2] = cObj[keyName][keyName2]; |
| 195 | + }); |
| 196 | + } else { |
| 197 | + imageFilter[cObj.type][keyName] = cObj[keyName]; |
| 198 | + } |
| 199 | + }); |
| 200 | + }); |
| 201 | + } |
| 202 | + |
| 203 | + console.log(imageFilter); |
| 204 | + return imageFilter; |
| 205 | + } |
| 206 | + |
137 | 207 | // *** GQL Calls *** |
138 | 208 |
|
139 | 209 | // Find Image by ID |
|
148 | 218 |
|
149 | 219 | // Find Images by Gallery ID |
150 | 220 | // Return Images list (id) |
151 | | - async function findGalleryImages(galleryID) { |
152 | | - const imageFilter = { |
153 | | - galleries: { value: galleryID, modifier: "INCLUDES_ALL" }, |
154 | | - }; |
155 | | - const findFilter = { per_page: -1, sort: "title" }; |
| 221 | + async function findGalleryImages(galleryID, galleryParams) { |
| 222 | + const imageFilter = getImageFilter(galleryID, galleryParams); |
| 223 | + const findFilter = getFindFilter(galleryParams); |
156 | 224 | const variables = { image_filter: imageFilter, filter: findFilter }; |
157 | 225 | const query = `query ($image_filter: ImageFilterType!, $filter: FindFilterType!) { findImages(image_filter: $image_filter, filter: $filter) { images { id } } }`; |
158 | 226 | return await csLib |
|
0 commit comments