2012-03-31 04:18:53 -04:00
|
|
|
/*
|
|
|
|
* inline-expanding.js
|
|
|
|
* https://github.com/savetheinternet/Tinyboard/blob/master/js/inline-expanding.js
|
|
|
|
*
|
|
|
|
* Released under the MIT license
|
2013-07-27 00:14:43 -04:00
|
|
|
* Copyright (c) 2012-2013 Michael Save <savetheinternet@tinyboard.org>
|
2014-01-19 08:27:24 -05:00
|
|
|
* Copyright (c) 2013-2014 Marcin Łabanowski <marcin@6irc.net>
|
2012-03-31 04:18:53 -04:00
|
|
|
*
|
|
|
|
* Usage:
|
2014-10-28 11:35:58 -04:00
|
|
|
* $config['additional_javascript'][] = 'js/jquery.min.js';
|
2012-03-31 04:18:53 -04:00
|
|
|
* $config['additional_javascript'][] = 'js/inline-expanding.js';
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-01-22 11:03:37 -05:00
|
|
|
$(document).ready(function(){
|
2015-01-11 22:41:54 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var DEFAULT_MAX = 5; // default maximum image loads
|
2013-06-15 01:39:39 -04:00
|
|
|
var inline_expand_post = function() {
|
|
|
|
var link = this.getElementsByTagName('a');
|
2013-06-15 01:39:39 -04:00
|
|
|
|
2015-01-11 22:41:54 -05:00
|
|
|
var loadingQueue = (function () {
|
|
|
|
var MAX_IMAGES = localStorage.inline_expand_max || DEFAULT_MAX; // maximum number of images to load concurrently, 0 to disable
|
|
|
|
var loading = 0; // number of images that is currently loading
|
|
|
|
var waiting = []; // waiting queue
|
|
|
|
|
|
|
|
var enqueue = function (ele) {
|
|
|
|
waiting.push(ele);
|
|
|
|
};
|
|
|
|
var dequeue = function () {
|
|
|
|
return waiting.shift();
|
|
|
|
};
|
|
|
|
var update = function() {
|
|
|
|
var ele;
|
|
|
|
while (loading < MAX_IMAGES || MAX_IMAGES === 0) {
|
|
|
|
ele = dequeue();
|
|
|
|
if (ele) {
|
|
|
|
++loading;
|
|
|
|
ele.deferred.resolve();
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return {
|
|
|
|
remove: function (ele) {
|
|
|
|
var i = waiting.indexOf(ele);
|
|
|
|
if (i > -1) {
|
|
|
|
waiting.splice(i, 1);
|
|
|
|
}
|
2015-02-20 04:51:06 -05:00
|
|
|
if ($(ele).data('imageLoading') === 'true') {
|
|
|
|
$(ele).data('imageLoading', 'false');
|
2015-01-11 22:41:54 -05:00
|
|
|
clearTimeout(ele.timeout);
|
|
|
|
--loading;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
add: function (ele) {
|
|
|
|
ele.deferred = $.Deferred();
|
|
|
|
ele.deferred.done(function () {
|
|
|
|
var $loadstart = $.Deferred();
|
|
|
|
var thumb = ele.childNodes[0];
|
|
|
|
var img = ele.childNodes[1];
|
|
|
|
|
|
|
|
var onLoadStart = function (img) {
|
|
|
|
if (img.naturalWidth) {
|
|
|
|
$loadstart.resolve(img, thumb);
|
|
|
|
} else {
|
|
|
|
return (ele.timeout = setTimeout(onLoadStart, 30, img));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$(img).one('load', function () {
|
|
|
|
$.when($loadstart).done(function () {
|
|
|
|
// once fully loaded, update the waiting queue
|
|
|
|
--loading;
|
2015-02-20 04:51:06 -05:00
|
|
|
$(ele).data('imageLoading', 'false');
|
2015-01-11 22:41:54 -05:00
|
|
|
update();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
$loadstart.done(function (img, thumb) {
|
|
|
|
thumb.style.display = 'none';
|
|
|
|
img.style.display = '';
|
|
|
|
});
|
|
|
|
|
|
|
|
img.setAttribute('src', ele.href);
|
2015-02-20 04:51:06 -05:00
|
|
|
$(ele).data('imageLoading', 'true');
|
2015-01-11 22:41:54 -05:00
|
|
|
ele.timeout = onLoadStart(img);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (loading < MAX_IMAGES || MAX_IMAGES === 0) {
|
|
|
|
++loading;
|
|
|
|
ele.deferred.resolve();
|
|
|
|
} else {
|
|
|
|
enqueue(ele);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
2013-07-27 01:04:36 -04:00
|
|
|
for (var i = 0; i < link.length; i++) {
|
2015-01-11 22:41:54 -05: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/)) {
|
2013-06-15 01:39:39 -04:00
|
|
|
link[i].onclick = function(e) {
|
2015-01-11 22:41:54 -05:00
|
|
|
var img, post_body, still_open, canvas, scroll;
|
2014-10-27 04:57:03 -04:00
|
|
|
var thumb = this.childNodes[0];
|
2014-10-28 11:35:58 -04:00
|
|
|
var padding = 5;
|
|
|
|
var boardlist = $('.boardlist')[0];
|
|
|
|
|
2014-10-27 04:49:17 -04:00
|
|
|
|
2014-10-27 04:57:03 -04:00
|
|
|
if (thumb.className == 'hidden')
|
2013-07-27 01:04:36 -04:00
|
|
|
return false;
|
2015-01-11 22:41:54 -05:00
|
|
|
if (e.which == 2 || e.ctrlKey) // open in new tab
|
2013-06-15 01:39:39 -04:00
|
|
|
return true;
|
2015-02-20 04:51:06 -05:00
|
|
|
if (!$(this).data('expanded')) {
|
2015-03-06 07:59:47 -05:00
|
|
|
|
|
|
|
if (~this.parentNode.className.indexOf('multifile'))
|
2015-03-08 00:06:52 -05:00
|
|
|
$(this).data('width', this.parentNode.style.width);
|
2015-03-06 07:59:47 -05:00
|
|
|
|
2014-04-30 10:21:02 -04:00
|
|
|
this.parentNode.removeAttribute('style');
|
2015-02-20 04:51:06 -05:00
|
|
|
$(this).data('expanded', 'true');
|
2014-06-10 14:00:49 -04:00
|
|
|
|
2014-10-27 04:57:03 -04:00
|
|
|
if (thumb.tagName === 'CANVAS') {
|
2014-11-28 22:01:30 -05:00
|
|
|
canvas = thumb;
|
|
|
|
thumb = thumb.nextSibling;
|
|
|
|
this.removeChild(canvas);
|
|
|
|
canvas.style.display = 'block';
|
2014-06-10 14:00:49 -04:00
|
|
|
}
|
|
|
|
|
2014-10-27 04:57:03 -04:00
|
|
|
thumb.style.opacity = '0.4';
|
|
|
|
thumb.style.filter = 'alpha(opacity=40)';
|
2014-10-27 04:49:17 -04:00
|
|
|
|
|
|
|
img = document.createElement('img');
|
|
|
|
img.className = 'full-image';
|
|
|
|
img.style.display = 'none';
|
2015-01-11 22:41:54 -05:00
|
|
|
img.setAttribute('alt', 'Fullsized image');
|
2014-10-27 04:49:17 -04:00
|
|
|
this.appendChild(img);
|
|
|
|
|
2015-01-11 22:41:54 -05:00
|
|
|
loadingQueue.add(this);
|
2013-06-15 01:39:39 -04:00
|
|
|
} else {
|
2015-01-11 22:41:54 -05:00
|
|
|
loadingQueue.remove(this);
|
2014-10-28 11:35:58 -04:00
|
|
|
|
2015-01-11 22:41:54 -05:00
|
|
|
scroll = false;
|
2014-10-28 11:35:58 -04:00
|
|
|
|
2015-01-11 22:41:54 -05:00
|
|
|
// scroll to thumb if not triggered by 'shrink all image'
|
|
|
|
if (e.target.className == 'full-image') {
|
|
|
|
scroll = true;
|
2014-10-28 11:35:58 -04:00
|
|
|
}
|
|
|
|
|
2014-06-12 13:48:31 -04:00
|
|
|
if (~this.parentNode.className.indexOf('multifile'))
|
2015-03-06 07:59:47 -05:00
|
|
|
this.parentNode.style.width = $(this).data('width');
|
2014-10-27 04:49:17 -04:00
|
|
|
|
2014-10-27 04:57:03 -04:00
|
|
|
thumb.style.opacity = '';
|
|
|
|
thumb.style.display = '';
|
2015-01-11 22:41:54 -05:00
|
|
|
if (thumb.nextSibling) this.removeChild(thumb.nextSibling); //full image loaded or loading
|
2015-02-20 04:51:06 -05:00
|
|
|
$(this).removeData('expanded');
|
2014-10-27 04:57:03 -04:00
|
|
|
delete thumb.style.filter;
|
2014-06-10 14:00:49 -04:00
|
|
|
|
2015-01-11 22:41:54 -05:00
|
|
|
// do the scrolling after page reflow
|
|
|
|
if (scroll) {
|
|
|
|
post_body = $(thumb).parentsUntil('form > div').last();
|
|
|
|
|
|
|
|
// on multifile posts, determin how many other images are still expanded
|
|
|
|
still_open = post_body.find('.post-image').filter(function(){
|
2015-02-24 07:34:13 -05:00
|
|
|
return $(this).parent().data('expanded') == 'true';
|
2015-01-11 22:41:54 -05:00
|
|
|
}).length;
|
|
|
|
|
|
|
|
// deal with differnt boards' menu styles
|
|
|
|
if ($(boardlist).css('position') == 'fixed')
|
|
|
|
padding += boardlist.getBoundingClientRect().height;
|
|
|
|
|
|
|
|
if (still_open > 0) {
|
|
|
|
if (thumb.getBoundingClientRect().top - padding < 0)
|
|
|
|
$(document).scrollTop($(thumb).parent().parent().offset().top - padding);
|
|
|
|
} else {
|
|
|
|
if (post_body[0].getBoundingClientRect().top - padding < 0)
|
|
|
|
$(document).scrollTop(post_body.offset().top - padding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-10 14:00:49 -04:00
|
|
|
if (localStorage.no_animated_gif === 'true' && typeof unanimate_gif === 'function') {
|
2014-10-27 04:57:03 -04:00
|
|
|
unanimate_gif(thumb);
|
2014-06-10 14:00:49 -04:00
|
|
|
}
|
2012-03-31 04:18:53 -04:00
|
|
|
}
|
2013-07-27 02:19:58 -04:00
|
|
|
return false;
|
2014-10-27 04:49:17 -04:00
|
|
|
};
|
2012-03-31 04:18:53 -04:00
|
|
|
}
|
|
|
|
}
|
2014-10-27 04:49:17 -04:00
|
|
|
};
|
2013-06-15 01:39:39 -04:00
|
|
|
|
2015-01-11 22:41:54 -05:00
|
|
|
// setting up user option
|
|
|
|
if (window.Options && Options.get_tab('general')) {
|
|
|
|
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>');
|
|
|
|
$('#inline-expand-max input')
|
|
|
|
.css('width', '50px')
|
|
|
|
.val(localStorage.inline_expand_max || DEFAULT_MAX)
|
|
|
|
.on('change', function (e) {
|
|
|
|
// validation in case some fucktard tries to enter a negative floating point number
|
|
|
|
var n = parseInt(e.target.value);
|
|
|
|
var val = (n<0) ? 0 : n;
|
|
|
|
|
|
|
|
localStorage.inline_expand_max = val;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-07-27 02:19:58 -04:00
|
|
|
if (window.jQuery) {
|
|
|
|
$('div[id^="thread_"]').each(inline_expand_post);
|
2013-06-15 01:39:39 -04:00
|
|
|
|
2013-07-27 02:19:58 -04:00
|
|
|
// allow to work with auto-reload.js, etc.
|
2014-01-21 13:25:11 -05:00
|
|
|
$(document).on('new_post', function(e, post) {
|
2013-07-27 02:19:58 -04:00
|
|
|
inline_expand_post.call(post);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
inline_expand_post.call(document);
|
|
|
|
}
|
2012-03-31 04:18:53 -04:00
|
|
|
});
|