1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2025-02-02 12:57:35 +01:00
vichan/js/inline-expanding.js

227 lines
6.5 KiB
JavaScript
Raw Normal View History

2012-03-31 19:18:53 +11:00
/*
* inline-expanding.js
* https://github.com/savetheinternet/Tinyboard/blob/master/js/inline-expanding.js
*
* Released under the MIT license
* Copyright (c) 2012-2013 Michael Save <savetheinternet@tinyboard.org>
* Copyright (c) 2013-2014 Marcin Łabanowski <marcin@6irc.net>
2012-03-31 19:18:53 +11:00
*
* Usage:
* $config['additional_javascript'][] = 'js/jquery.min.js';
2012-03-31 19:18:53 +11:00
* $config['additional_javascript'][] = 'js/inline-expanding.js';
*/
2024-09-05 15:14:11 +02:00
$(document).ready(function() {
2015-01-12 11:41:54 +08:00
'use strict';
2024-09-05 15:14:11 +02:00
// Default maximum image loads.
const DEFAULT_MAX = 5;
let inline_expand_post = function() {
let link = this.getElementsByTagName('a');
2024-09-05 15:14:11 +02:00
let loadingQueue = (function() {
let MAX_IMAGES = localStorage.inline_expand_max || DEFAULT_MAX; // Maximum number of images to load concurrently, 0 to disable.
let loading = 0; // Number of images that is currently loading.
let waiting = []; // Waiting queue.
2015-01-12 11:41:54 +08:00
2024-09-05 15:14:11 +02:00
let enqueue = function(ele) {
2015-01-12 11:41:54 +08:00
waiting.push(ele);
};
2024-09-05 15:14:11 +02:00
let dequeue = function() {
2015-01-12 11:41:54 +08:00
return waiting.shift();
};
2024-09-05 15:14:11 +02:00
let update = function() {
2015-01-12 11:41:54 +08:00
while (loading < MAX_IMAGES || MAX_IMAGES === 0) {
2024-09-05 15:14:11 +02:00
let ele = dequeue();
2015-01-12 11:41:54 +08:00
if (ele) {
++loading;
ele.deferred.resolve();
} else {
return;
}
}
};
return {
2024-09-05 15:14:11 +02:00
remove: function(ele) {
let i = waiting.indexOf(ele);
2015-01-12 11:41:54 +08:00
if (i > -1) {
waiting.splice(i, 1);
}
if ($(ele).data('imageLoading') === 'true') {
$(ele).data('imageLoading', 'false');
2015-01-12 11:41:54 +08:00
clearTimeout(ele.timeout);
--loading;
}
},
2024-09-05 15:14:11 +02:00
add: function(ele) {
2015-01-12 11:41:54 +08:00
ele.deferred = $.Deferred();
ele.deferred.done(function () {
2024-09-05 15:14:11 +02:00
let $loadstart = $.Deferred();
let thumb = ele.childNodes[0];
let img = ele.childNodes[1];
2015-01-12 11:41:54 +08:00
2024-09-05 15:14:11 +02:00
let onLoadStart = function (img) {
2015-01-12 11:41:54 +08:00
if (img.naturalWidth) {
$loadstart.resolve(img, thumb);
} else {
return (ele.timeout = setTimeout(onLoadStart, 30, img));
}
};
$(img).one('load', function () {
$.when($loadstart).done(function () {
2024-09-05 15:14:11 +02:00
// Once fully loaded, update the waiting queue.
2015-01-12 11:41:54 +08:00
--loading;
$(ele).data('imageLoading', 'false');
2015-01-12 11:41:54 +08:00
update();
});
});
$loadstart.done(function (img, thumb) {
thumb.style.display = 'none';
img.style.display = '';
});
img.setAttribute('src', ele.href);
$(ele).data('imageLoading', 'true');
2015-01-12 11:41:54 +08:00
ele.timeout = onLoadStart(img);
});
if (loading < MAX_IMAGES || MAX_IMAGES === 0) {
++loading;
ele.deferred.resolve();
} else {
enqueue(ele);
}
}
};
})();
2024-09-05 15:14:11 +02:00
for (let i = 0; i < link.length; i++) {
2015-01-12 11:41:54 +08:00
if (typeof link[i] == "object" && link[i].childNodes && typeof link[i].childNodes[0] !== 'undefined' &&
link[i].childNodes[0].src && link[i].childNodes[0].className.match(/post-image/) && !link[i].className.match(/file/)) {
link[i].onclick = function(e) {
2024-09-05 15:14:11 +02:00
let thumb = this.childNodes[0];
let padding = 5;
let boardlist = $('.boardlist')[0];
2024-09-05 15:14:11 +02:00
if (thumb.className == 'hidden') {
return false;
2024-09-05 15:14:11 +02:00
}
if (e.which == 2 || e.ctrlKey) {
// Open in new tab.
return true;
2024-09-05 15:14:11 +02:00
}
if (!$(this).data('expanded')) {
2024-09-05 15:14:11 +02:00
if (~this.parentNode.className.indexOf('multifile')) {
2015-03-08 13:06:52 +08:00
$(this).data('width', this.parentNode.style.width);
2024-09-05 15:14:11 +02:00
}
this.parentNode.removeAttribute('style');
$(this).data('expanded', 'true');
2014-10-27 16:57:03 +08:00
if (thumb.tagName === 'CANVAS') {
2024-09-05 15:14:11 +02:00
let canvas = thumb;
thumb = thumb.nextSibling;
this.removeChild(canvas);
canvas.style.display = 'block';
}
2014-10-27 16:57:03 +08:00
thumb.style.opacity = '0.4';
thumb.style.filter = 'alpha(opacity=40)';
2024-09-05 15:14:11 +02:00
let img = document.createElement('img');
img.className = 'full-image';
img.style.display = 'none';
2015-01-12 11:41:54 +08:00
img.setAttribute('alt', 'Fullsized image');
this.appendChild(img);
2015-01-12 11:41:54 +08:00
loadingQueue.add(this);
} else {
2015-01-12 11:41:54 +08:00
loadingQueue.remove(this);
2024-09-05 15:14:11 +02:00
let scroll = false;
2024-09-05 15:14:11 +02:00
// Scroll to thumb if not triggered by 'shrink all image'.
2015-01-12 11:41:54 +08:00
if (e.target.className == 'full-image') {
scroll = true;
}
2024-09-05 15:14:11 +02:00
if (~this.parentNode.className.indexOf('multifile')) {
this.parentNode.style.width = $(this).data('width');
2024-09-05 15:14:11 +02:00
}
2014-10-27 16:57:03 +08:00
thumb.style.opacity = '';
thumb.style.display = '';
2024-09-05 15:14:11 +02:00
if (thumb.nextSibling) {
// Full image loaded or loading.
this.removeChild(thumb.nextSibling);
}
$(this).removeData('expanded');
2014-10-27 16:57:03 +08:00
delete thumb.style.filter;
2024-09-05 15:14:11 +02:00
// Do the scrolling after page reflow.
2015-01-12 11:41:54 +08:00
if (scroll) {
2024-09-05 15:14:11 +02:00
let post_body = $(thumb).parentsUntil('form > div').last();
2015-01-12 11:41:54 +08:00
2024-09-05 15:14:11 +02:00
// On multifile posts, determine how many other images are still expanded.
let still_open = post_body.find('.post-image').filter(function() {
return $(this).parent().data('expanded') == 'true';
2015-01-12 11:41:54 +08:00
}).length;
2024-09-05 15:14:11 +02:00
// Deal with different boards menu styles.
if ($(boardlist).css('position') == 'fixed') {
2015-01-12 11:41:54 +08:00
padding += boardlist.getBoundingClientRect().height;
2024-09-05 15:14:11 +02:00
}
2015-01-12 11:41:54 +08:00
if (still_open > 0) {
2024-09-05 15:14:11 +02:00
if (thumb.getBoundingClientRect().top - padding < 0) {
2015-01-12 11:41:54 +08:00
$(document).scrollTop($(thumb).parent().parent().offset().top - padding);
2024-09-05 15:14:11 +02:00
}
2015-01-12 11:41:54 +08:00
} else {
2024-09-05 15:14:11 +02:00
if (post_body[0].getBoundingClientRect().top - padding < 0) {
2015-01-12 11:41:54 +08:00
$(document).scrollTop(post_body.offset().top - padding);
2024-09-05 15:14:11 +02:00
}
2015-01-12 11:41:54 +08:00
}
}
if (localStorage.no_animated_gif === 'true' && typeof unanimate_gif === 'function') {
2014-10-27 16:57:03 +08:00
unanimate_gif(thumb);
}
2012-03-31 19:18:53 +11:00
}
return false;
};
2012-03-31 19:18:53 +11:00
}
}
};
2024-09-05 15:14:11 +02:00
// Setting up user option.
2015-01-12 11:41:54 +08:00
if (window.Options && Options.get_tab('general')) {
2024-09-05 15:14:11 +02:00
Options.extend_tab('general', '<span id="inline-expand-max">' +
_('Number of simultaneous image downloads (0 to disable): ') +
'<input type="number" step="1" min="0" size="4"></span>');
2015-01-12 11:41:54 +08:00
$('#inline-expand-max input')
.css('width', '50px')
.val(localStorage.inline_expand_max || DEFAULT_MAX)
.on('change', function (e) {
2024-09-05 15:14:11 +02:00
// Validation in case some fucktard tries to enter a negative floating point number.
let n = parseInt(e.target.value);
let val = (n < 0) ? 0 : n;
2015-01-12 11:41:54 +08:00
localStorage.inline_expand_max = val;
});
}
if (window.jQuery) {
$('div[id^="thread_"]').each(inline_expand_post);
2024-09-05 15:14:11 +02:00
// Allow to work with auto-reload.js, etc.
$(document).on('new_post', function(e, post) {
inline_expand_post.call(post);
});
} else {
inline_expand_post.call(document);
}
2012-03-31 19:18:53 +11:00
});