-
Notifications
You must be signed in to change notification settings - Fork 1
/
gallery.js
368 lines (328 loc) · 18.6 KB
/
gallery.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
var toggleRedditImageGallery = function() {
// Create private scope
(function() {
// Toggle our state if that function loaded
if (typeof toggleRedditImageGalleryState === "function")
toggleRedditImageGalleryState();
// Check is Never Ending Reddit is enabled
var neverEndingReddit = ($("#progressIndicator.neverEndingReddit").length > 0);
// Find the elements we need to use
var $siteContainer = $("#siteTable");
var $pageControls = $siteContainer.parent().children('p.nextprev.RIG');
// Determine if the gallery has been initialized, initialize it if it's not
var isGallery = ($siteContainer.data('IsGallery') === "true");
if (!isGallery) {
// No gallery exists yet, set the HTML up aswell
var $gallery = $("<div>").attr("id", "GalleryContainer");
$gallery.html(
'<table id="ImgColumnTable">' +
'<tr>' +
'<td><div class="imgColumn"></div></td>' +
'<td><div class="imgColumn"></div></td>' +
'<td><div class="imgColumn"></div></td>' +
'</tr>' +
'</table>' +
'<div id="ImageLoader"></div>'
)
.data('images', JSON.stringify([]))
.data('inverval', JSON.stringify(null))
.insertBefore($siteContainer);
// If neverending reddit isn't enabled, we need the "next page / previous page" buttons.
if (!neverEndingReddit) {
$pageControls = $('<p>').addClass('nextprev RIG').html($siteContainer.find("p.nextprev").html()).insertAfter($gallery);
}
}
var $gallery = $("#GalleryContainer");
var $imageColumns = $gallery.find(".imgColumn");
// An invisible element in which elements can load before being added to the page
var $imageLoader = $("#ImageLoader");
// Check if there is already an active setInterval active, if it is- we need to stop it before adding a new one
var updateInterval = JSON.parse($gallery.data('inverval'));
if (!isGallery) {
$siteContainer.data('IsGallery', 'true');
$gallery.show();
$pageControls.show();
$siteContainer.hide();
function preloadImage(image, callback) {
$("<img>").attr('src', image).load(callback);
}
function targetColumn() {
var shortest = null;
$imageColumns.each(function() {
if (shortest === null) {
shortest = this;
}
else if (this.scrollHeight === 0) {
shortest = this;
return false;
}
else if (shortest.scrollHeight > this.scrollHeight) {
shortest = this;
}
});
return shortest;
}
function addImage($this, postNum, link, image, title) {
var $titleDiv = $("<div>").addClass('ImageTitleBar');
$this.find('.arrow').each(function() {
var $arrow = $(this);
var $clone = $arrow.clone();
$clone.removeAttr('onclick');
$clone.click(function() {
$arrow.trigger('click');
$clone.attr('class', $arrow.attr('class'));
});
$clone.appendTo($titleDiv);
});
var $source = $("<span>").addClass('rig_source').appendTo($titleDiv);
var $sub = $this.find('a.subreddit');
if ($sub.length > 0)
$source.text('Subreddit: ').append($sub).append(' | ');
$("<span>").text("Source: ").append($this.find('span.domain a')).appendTo($source);
var $commentsEle = $this.find('.comments');
var $imageDiv = $("<div>").addClass('GalleryImageContainer').data('post_num', postNum);
var $imageInfo = $("<div>").addClass('ImageInfo').append($("<h4>").append($("<a>").attr({href: link, target: '_blank', title: title}).text(title))).append($commentsEle);
$imageDiv.html('<a href="' + link + '" target="_blank"><img src="' + image + '" /></a>').append($imageInfo).prepend($titleDiv);
preloadImage(image, function() {
var placed = false;
var target = targetColumn();
$(target).find('.GalleryImageContainer').each(function() {
if ($this.data('post_num') > postNum) {
placed = true;
$imageDiv.insertBefore($(this));
return false;
}
});
if (!placed) {
$(target).append($imageDiv);
}
});
}
function UpdateGallery() {
// The links that have already been loaded
var curImages = JSON.parse($gallery.data('images'));
// Find all reddit posts no9t already added
var $posts = $siteContainer.find('div.thing.link:not(.added)');
var postNumCounter = curImages.length;
// Go through all these posts
$posts.each(function() {
$(this).addClass('added');
var $titleEle = $(this).find('a.title').first();
var title = $titleEle.text();
var link = $titleEle.attr('href');
// Dont add this again if it's already been added
if ($.inArray(link, curImages) >= 0)
return;
curImages.push(link);
var postNum = ++postNumCounter;
// Convert mobile imgur links to regular
if (link.substring(0, 19) === "http://m.imgur.com/") {
link = "http://i.imgur.com/" + link.substring(19);
}
// [IMGUR] Fix image link missing .jpg/.png/.gif
if (link.substring(0, 17) === "http://imgur.com/" && link.substring(17, 19) !== "a/") {
var split = link.substring(17).split(".");
if (split.length > 1) {
link = 'http://i.imgur.com/' + split[0] + '.' + split[1];
}
else {
// Imgur doesn't care what extension we add
link = 'http://i.imgur.com/' + split[0] + '.png';
}
}
var isAlbum = false;
if (link.substring(0, 19) === "http://i.imgur.com/")
addImage($(this), postNum, link, link, title);
else if (link.substring(0, 19) === "http://imgur.com/a/")
isAlbum = true;
else {
var ext = link.split(".").pop().toLowerCase();
if (ext === "jpg" || ext === "jpeg" || ext === "gif" || ext === "png")
addImage($(this), postNum, link, link, title);
else if (link.indexOf('tumblr.com/post/') >= 0) {
// Tumblr Post, not direct image link
var cleanLink = link.split("#")[0];
// Try get image url
var splitUrl = cleanLink.split("/");
var base_hostname = splitUrl[2];
var postId = splitUrl[4];
var apiUrl = 'http://api.tumblr.com/v2/blog/' + base_hostname + '/posts?api_key=eGBoYN6P3un5qGNALl1cyDBY34eCr6hDy1iZU9ZfSmPuLYcvN3&id=' + postId;
//$("<script>").attr({"type": "text/javascript", "src": apiUrl}).appendTo($("head"));
// eGBoYN6P3un5qGNALl1cyDBY34eCr6hDy1iZU9ZfSmPuLYcvN3
var $this = $(this);
$.getJSON(apiUrl, function(json) {
if (json.meta.status != 200 || json.meta.msg != "OK")
return;
var post = json.response.posts[0];
if (post.type === "text") {
var html = post.body;
var index1 = html.indexOf('<a href="http://') + 9;
var index2 = html.indexOf('"><img src="', index1);
var imageUrl = html.substring(index1, index2);
addImage($this, postNum, link, imageUrl, title);
}
else if (post.type === "photo") {
var imageUrl = post.photos[0].alt_sizes[0].url;
addImage($this, postNum, link, imageUrl, title);
}
});
}
else
{
console.log('[Reddit Image Gallery] Could not parse:', link, title);
}
}
// Imgur Album
if (isAlbum) {
var $this = $(this);
var $titleDiv = $("<div>").addClass('ImageTitleBar');
$this.find('.arrow').each(function() {
var $arrow = $(this);
var $clone = $arrow.clone();
$clone.removeAttr('onclick');
$clone.click(function() {
$arrow.trigger('click');
$clone.attr('class', $arrow.attr('class'));
});
$clone.appendTo($titleDiv);
});
var $source = $("<span>").addClass('rig_source').appendTo($titleDiv);
var $sub = $this.find('a.subreddit');
if ($sub.length > 0)
$source.text('Subreddit: ').append($sub).append(' | ');
$("<span>").text("Source: ").append($this.find('span.domain a')).appendTo($source);
var $commentsEle = $this.find('.comments');
var spliturl = link.substring(19).split("#");
var album = spliturl[0];
var imgNum = 0;
if (spliturl.length > 1)
imgNum = parseInt(spliturl[1]);
(function() {
var thisLink = link;
$.ajax(
{
type: "GET",
url: "https://api.imgur.com/3/album/" + album + "/images",
beforeSend: function(jqXHR) {
jqXHR.setRequestHeader("Authorization", "Client-ID 0f5063d9d3c80cb");
}
}
).success(function(data) {
// Add all image URL's to a list
var images = data.data;
if (typeof images.images === "object" && images.images instanceof Array)
images = images.images;
var imageList = [];
for (var j in images) {
if (images[j] === null)
continue;
imageList.push(images[j].link);
}
// Determine how many thumbnails we can show
var albumWidth = $(".imgColumn")[0].scrollWidth - 20;
var numThumbs = Math.floor((albumWidth - 5) / 50);
var $imgPic = $("<img>").attr('src', imageList[imgNum]);
var $imgLink = $("<a>").attr('href', imageList[imgNum]).addClass('albumImage').append($imgPic);
var $thumbnails = $("<div>").addClass('AlbumThumbs');
var $imageInfo = $("<div>").addClass('ImageInfo').append($("<h4>").append($("<a>").attr({href: thisLink, target: '_blank', title: title}).text(title))).append($thumbnails).append($commentsEle);
var $imageDiv = $("<div>").addClass('GalleryImageContainer GalleryAlbumContainer').append($imgLink).append($imageInfo).prepend($titleDiv);
// Album controls
(function() {
var curImg = parseInt(imgNum);
var maxImg = imageList.length - 1;
var $imgNumSpan = $('<span>').addClass('albumTag');
$imageInfo.append($imgNumSpan);
function openImg(num) {
curImg = parseInt(num);
updateAlbum();
}
function updateThumbnails() {
var from = Math.max(0, Math.round(curImg - (numThumbs / 2)));
var to = from + numThumbs;
if (to > imageList.length) {
to = imageList.length;
from = Math.max(0, to - numThumbs);
}
$thumbnails.html("");
var albumHtml = "";
for (var j in imageList) {
// We only need to display a few images
if (j < from || j >= to)
continue;
(function() {
var index = j;
var img = imageList[index];
var extPos = img.lastIndexOf('.');
var thumbImg = img.substring(0, extPos) + 's' + img.substring(extPos);
var $thumb = $("<div>").addClass('albumThumb').css('background-image', "url('" + thumbImg + "')").click(function() {
openImg(index)
});
if (parseInt(index) === curImg)
$thumb.addClass('active');
$thumbnails.append($thumb);
preloadImage(img, null);
}());
}
}
function updateAlbum() {
updateThumbnails();
$imgPic.attr('src', imageList[curImg]);
$imgLink.attr('href', imageList[curImg])
$imgNumSpan.text('Album (' + (curImg + 1) + '/' + (maxImg + 1) + ')');
}
function prevImg() {
if (--curImg < 0)
curImg = maxImg;
updateAlbum();
}
function nextImg() {
if (++curImg > maxImg)
curImg = 0;
updateAlbum();
}
updateAlbum();
var $prev = $('<span>').addClass('albumControl albumPrevious').html('<div>◄</div>').click(prevImg);
var $next = $('<span>').addClass('albumControl albumNext').html('<div>►</div>').click(nextImg);
$imageDiv.append($prev).append($next);
}());
preloadImage(imageList[imgNum], function() {
var placed = false;
var target = targetColumn();
$(target).find('.GalleryImageContainer').each(function() {
if ($(this).data('post_num') > postNum) {
placed = true;
$imageDiv.insertBefore($(this));
return false;
}
});
if (!placed) {
$(target).append($imageDiv);
}
});
});
}());
}
});
$gallery.data('images', JSON.stringify(curImages));
}
UpdateGallery();
var curHtml = $siteContainer.html();
updateInterval = setInterval(function() {
var newHtml = $siteContainer.html();
if (newHtml !== curHtml) {
curHtml = newHtml;
UpdateGallery();
}
}, 250);
$gallery.data('inverval', JSON.stringify(updateInterval));
}
else {
clearInterval(updateInterval);
$gallery.data('inverval', JSON.stringify(updateInterval));
$siteContainer.data('IsGallery', 'false');
$siteContainer.show();
$gallery.hide();
$pageControls.hide();
}
}());
};