2012-03-14 23:44:15 -04:00
|
|
|
/*
|
|
|
|
* auto-reload.js
|
2012-03-30 20:13:11 -04:00
|
|
|
* https://github.com/savetheinternet/Tinyboard/blob/master/js/auto-reload.js
|
2012-03-14 23:44:15 -04:00
|
|
|
*
|
|
|
|
* Brings AJAX to Tinyboard.
|
|
|
|
*
|
|
|
|
* Released under the MIT license
|
|
|
|
* Copyright (c) 2012 Michael Save <savetheinternet@tinyboard.org>
|
2014-01-19 07:40:29 -05:00
|
|
|
* Copyright (c) 2013-2014 Marcin Łabanowski <marcin@6irc.net>
|
2014-01-19 08:27:24 -05:00
|
|
|
* Copyright (c) 2013 undido <firekid109@hotmail.com>
|
2012-03-14 23:44:15 -04:00
|
|
|
*
|
|
|
|
* Usage:
|
2012-03-15 01:19:26 -04:00
|
|
|
* $config['additional_javascript'][] = 'js/jquery.min.js';
|
2013-12-28 19:10:35 -05:00
|
|
|
* //$config['additional_javascript'][] = 'js/titlebar-notifications.js';
|
2012-03-15 01:19:26 -04:00
|
|
|
* $config['additional_javascript'][] = 'js/auto-reload.js';
|
2012-03-14 23:44:15 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-12-28 19:10:35 -05:00
|
|
|
auto_reload_enabled = true; // for watch.js to interop
|
|
|
|
|
2012-03-14 23:44:15 -04:00
|
|
|
$(document).ready(function(){
|
|
|
|
if($('div.banner').length == 0)
|
|
|
|
return; // not index
|
2013-03-19 23:56:59 -04:00
|
|
|
|
|
|
|
if($(".post.op").size() != 1)
|
|
|
|
return; //not thread page
|
2012-03-14 23:44:15 -04:00
|
|
|
|
2012-03-15 05:40:52 -04:00
|
|
|
var poll_interval;
|
2013-12-23 10:34:44 -05:00
|
|
|
|
2014-05-05 14:28:07 -04:00
|
|
|
// number of ms to wait before reloading
|
|
|
|
var poll_interval_delay;
|
|
|
|
|
|
|
|
// If at the bottom of the page, reload more quickly.
|
|
|
|
var poll_interval_mindelay_bottom = 3000;
|
|
|
|
var poll_interval_mindelay_top = 10000;
|
|
|
|
|
|
|
|
poll_interval_delay = poll_interval_mindelay_bottom;
|
|
|
|
|
2014-05-05 14:30:59 -04:00
|
|
|
// Don't take longer than this to reload.
|
|
|
|
var poll_interval_maxdelay = 600000;
|
|
|
|
|
2014-05-05 14:28:07 -04:00
|
|
|
// Upon scrolling to the bottom, reload very quickly.
|
|
|
|
var poll_interval_shortdelay = 100;
|
|
|
|
|
2013-12-23 10:34:44 -05:00
|
|
|
var end_of_page = false;
|
2013-12-23 11:31:47 -05:00
|
|
|
|
|
|
|
var new_posts = 0;
|
|
|
|
var first_new_post = null;
|
2013-12-28 19:10:35 -05:00
|
|
|
|
|
|
|
if (typeof update_title == "undefined") {
|
|
|
|
var update_title = function() { };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof add_title_collector != "undefined")
|
|
|
|
add_title_collector(function(){
|
|
|
|
return new_posts;
|
|
|
|
});
|
2013-12-23 11:31:47 -05:00
|
|
|
|
|
|
|
var window_active = true;
|
|
|
|
$(window).focus(function() {
|
|
|
|
window_active = true;
|
|
|
|
recheck_activated();
|
|
|
|
});
|
|
|
|
$(window).blur(function() {
|
|
|
|
window_active = false;
|
|
|
|
});
|
2012-03-15 05:40:52 -04:00
|
|
|
|
2013-12-23 11:31:47 -05:00
|
|
|
var recheck_activated = function() {
|
|
|
|
if (new_posts && window_active &&
|
|
|
|
$(window).scrollTop() + $(window).height() >=
|
|
|
|
$(first_new_post).position().top) {
|
|
|
|
|
|
|
|
new_posts = 0;
|
|
|
|
}
|
|
|
|
update_title();
|
|
|
|
};
|
|
|
|
|
2012-03-15 05:40:52 -04:00
|
|
|
var poll = function() {
|
2012-03-14 23:44:15 -04:00
|
|
|
$.ajax({
|
|
|
|
url: document.location,
|
|
|
|
success: function(data) {
|
|
|
|
$(data).find('div.post.reply').each(function() {
|
|
|
|
var id = $(this).attr('id');
|
|
|
|
if($('#' + id).length == 0) {
|
2013-12-23 11:31:47 -05:00
|
|
|
if (!new_posts) {
|
|
|
|
first_new_post = this;
|
|
|
|
}
|
2012-03-15 18:08:03 -04:00
|
|
|
$(this).insertAfter($('div.post:last').next()).after('<br class="clear">');
|
2013-12-23 11:31:47 -05:00
|
|
|
new_posts++;
|
2012-03-18 08:55:01 -04:00
|
|
|
$(document).trigger('new_post', this);
|
2013-12-23 11:31:47 -05:00
|
|
|
recheck_activated();
|
2012-03-14 23:44:15 -04:00
|
|
|
}
|
|
|
|
});
|
2013-12-28 19:10:35 -05:00
|
|
|
time_loaded = Date.now(); // interop with watch.js
|
2012-03-14 23:44:15 -04:00
|
|
|
}
|
|
|
|
});
|
2012-03-15 05:40:52 -04:00
|
|
|
|
2013-12-23 10:34:44 -05:00
|
|
|
clearTimeout(poll_interval);
|
2014-05-05 14:28:07 -04:00
|
|
|
|
2014-05-05 14:29:19 -04:00
|
|
|
// If there are no new posts, double the delay. Otherwise set it to the min.
|
|
|
|
if(new_posts == 0) {
|
|
|
|
poll_interval_delay *= 2;
|
2014-05-05 14:30:59 -04:00
|
|
|
|
|
|
|
// Don't increase the delay beyond the maximum
|
|
|
|
if(poll_interval_delay > poll_interval_maxdelay) {
|
|
|
|
poll_interval_delay = poll_interval_maxdelay;
|
|
|
|
}
|
2014-05-05 14:29:19 -04:00
|
|
|
} else {
|
|
|
|
poll_interval_delay = end_of_page
|
|
|
|
? poll_interval_mindelay_bottom
|
|
|
|
: poll_interval_mindelay_top;
|
|
|
|
}
|
|
|
|
|
2014-05-05 14:28:07 -04:00
|
|
|
poll_interval = setTimeout(poll, poll_interval_delay);
|
2012-03-15 05:40:52 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
$(window).scroll(function() {
|
2013-12-23 11:31:47 -05:00
|
|
|
recheck_activated();
|
|
|
|
|
|
|
|
if($(this).scrollTop() + $(this).height() <
|
|
|
|
$('div.post:last').position().top + $('div.post:last').height()) {
|
2013-12-23 10:34:44 -05:00
|
|
|
end_of_page = false;
|
2012-03-15 05:40:52 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-23 10:34:44 -05:00
|
|
|
clearTimeout(poll_interval);
|
2014-05-05 14:28:07 -04:00
|
|
|
poll_interval = setTimeout(poll, poll_interval_shortdelay);
|
2013-12-23 10:34:44 -05:00
|
|
|
end_of_page = true;
|
2012-03-15 05:40:52 -04:00
|
|
|
}).trigger('scroll');
|
2013-12-23 10:34:44 -05:00
|
|
|
|
2014-05-05 14:28:07 -04:00
|
|
|
poll_interval = setTimeout(poll, poll_interval_delay);
|
2012-03-14 23:44:15 -04:00
|
|
|
});
|
|
|
|
|