-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
245 lines (204 loc) · 7.23 KB
/
script.js
File metadata and controls
245 lines (204 loc) · 7.23 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const BASE_URL = 'https://mira-adaptor.vercel.app/midjourney';
let xgallery_images; // Declare xgallery_images as a global variable
// Function to use promise to get JSON data from BASE_URL
function getGalleryData() {
return new Promise((resolve, reject) => {
fetch(BASE_URL)
.then(response => response.json())
.then(data => resolve(data))
.catch(error => reject(error));
});
}
xgallery_images = getGalleryData();
async function loadGallery() {
xgallery_images = await getGalleryData();
$(document).ready(function() {
// SHUFFLE THE GALLERY
shuffle(xgallery_images);
// LOAD IMAGES INTO GALLERY
xgallery_images.forEach(function(xg_image, i) {
$(".xg-container").append(createXGImage(xg_image, i));
});
// HOVER GALLERY IMAGE
$(".xg-img-wrap").mouseenter(function() {
var el = $(this);
var to = setTimeout(function() {
el.find(".xg-img-info").addClass("xg-img-info-open");
}, 100);
el.mouseleave(function() {
clearInterval(to);
el.find(".xg-img-info").removeClass("xg-img-info-open");
});
});
// FILTER BUTTON CLICK
// $(".xg-btn-tag").on("click", function() {
// if ($(this).hasClass("xg-btn-active")) {
// return;
// }
// var tagFilter = $(this).data("tag");
// $(".xg-btn-tag").removeClass("xg-btn-active");
// $(this).addClass("xg-btn-active");
// $(".xg-loader").fadeIn(100);
// if (tagFilter == "all") {
// $(".xg-img-wrap").fadeIn(100);
// } else {
// $(".xg-img-wrap").fadeIn(100);
// $(".xg-img-wrap").each(function(i, el) {
// if (!$(el).data("tags").includes(tagFilter))
// $(el).fadeOut(100);
// });
// }
// $(".xg-loader").delay(500).fadeOut(100);
// });
// $(".xg-btn-tag").on("click", function() {
// if ($(this).hasClass("xg-btn-active")) {
// return;
// }
// var tagFilter = $(this).data("tag");
// $(".xg-btn-tag").removeClass("xg-btn-active");
// $(this).addClass("xg-btn-active");
// $(".xg-loader").fadeIn(100);
// if (tagFilter == "all") {
// $(".xg-img-wrap").fadeIn(100);
// } else {
// $(".xg-img-wrap").fadeIn(100);
// $(".xg-img-wrap").each(function(i, el) {
// var elTags = $(el).data("tags");
// console.log('tag called');
// // Check if elTags is an array or a string
// if (Array.isArray(elTags)) {
// console.log('array found')
// if (!elTags.includes(tagFilter))
// $(el).fadeOut(100);
// } else {
// if (elTags != tagFilter)
// $(el).fadeOut(100);
// }
// });
// }
// $(".xg-loader").delay(500).fadeOut(100);
// });
// $(".xg-btn-tag").on("click", function() {
// if ($(this).hasClass("xg-btn-active")) {
// return;
// }
// var tagFilter = $(this).data("tag");
// $(".xg-btn-tag").removeClass("xg-btn-active");
// $(this).addClass("xg-btn-active");
// $(".xg-loader").fadeIn(100);
// if (tagFilter == "all") {
// $(".xg-img-wrap").fadeIn(100);
// } else {
// $(".xg-img-wrap").fadeIn(100);
// $(".xg-img-wrap").each(function(i, el) {
// try {
// var elTags = JSON.parse($(el).attr("data-tags"));
// if (Array.isArray(elTags)) {
// if (!elTags.includes(tagFilter))
// $(el).fadeOut(100);
// } else {
// if (elTags != tagFilter)
// $(el).fadeOut(100);
// }
// } catch (e) {
// console.log('Error parsing tags:', e);
// }
// });
// }
// $(".xg-loader").delay(500).fadeOut(100);
// });
$(".xg-btn-tag").on("click", function() {
if ($(this).hasClass("xg-btn-active")) {
return;
}
var tagFilter = $(this).data("tag");
$(".xg-btn-tag").removeClass("xg-btn-active");
$(this).addClass("xg-btn-active");
$(".xg-loader").fadeIn(100);
if (tagFilter == "all") {
$(".xg-img-wrap").fadeIn(100);
} else {
$(".xg-img-wrap").fadeOut(100).promise().done(function() {
$(".xg-img-wrap").each(function(i, el) {
var elTags = $(el).data("tags").split(' ');
if (elTags.includes(tagFilter)) {
$(el).fadeIn(100);
}
});
});
}
$(".xg-loader").delay(500).fadeOut(100);
});
// OPEN IMAGE PREVIEW
$(".xg-img-wrap").on("click", function() {
var id = $(this).data("index");
$("body").addClass("scroll-fixed");
$(".xg-img-preview").fadeIn(100);
$(".xg-img-preview").html(createXGImagePreview(xgallery_images[id]));
});
// ZOOM IN IMAGE PREVIEW
$(document).on("click", ".xgp-img", function() {
$(".xgp-close").toggleClass("x-cloak");
$(this).closest(".xgp-img-inner").siblings(".xgp-details").fadeToggle(100);
$(this).closest(".xgp-img-inner").toggleClass("xgp-zoomed-inner");
$(this).toggleClass("xgp-zoomed");
$(".xgp-wrap").toggleClass("xg-overflow");
});
// CLOSE PREVIEW - ESCAPE
$(document).keyup(function(e) {
if (e.key === "Escape") {
$("body").removeClass("scroll-fixed");
$(".xg-img-preview").fadeOut(100);
}
});
// CLOSE PREVIEW - CLICK
$(document).on("click", ".xgp-close", function(e) {
$("body").removeClass("scroll-fixed");
$(".xg-img-preview").fadeOut(100);
});
});
} // This is the closing bracket for async function loadGallery()
loadGallery(); // Call the function after it is defined
function createXGImagePreview(xgi) {
var xg_img =
'<div class="xgp-wrap">\
<div class="xgp-close"></div>\
<div class="xgp-img-inner">\
<img draggable="false" class="xgp-img" src="' + xgi.url + '">\
</div>\
<div class="xgp-details">\
<div class="xgp-prompt">' + xgi.prompt + '</div>\
<div class="xgp-date">' + xgi.date + '</div>\
<div class="xgp-tags">\
<div class="xgp-tag">' + xgi.tags + '</div>\
</div>\
</div>';
return xg_img;
}
function createXGImage(xgi, i) {
// Convert tags to a string if it's an array
var tags = Array.isArray(xgi.tags) ? xgi.tags.join(' ') : xgi.tags;
var xg_img =
'<div class="xg-img-wrap" data-tags="' + tags+ '" data-index="' + i + '">\
<div class="xg-img-info">\
<div class="xg-img-info-inner">\
<div class="xg-img-prompt">' + xgi.prompt + '</div>\
<div class="xg-img-date">' + xgi.date + '</div>\
</div>\
</div>\
<img draggable="false" class="xg-img" src="' + xgi.url + '">\
</div>';
return xg_img;
}
function shuffle(arr) {
let index = arr.length,
randomIndex;
while (index != 0) {
randomIndex = Math.floor(Math.random() * index);
index--;
[arr[index], arr[randomIndex]] = [
arr[randomIndex], arr[index]
];
}
return arr;
}