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>
|
|
|
|
*
|
|
|
|
* Usage:
|
2012-03-15 01:19:26 -04:00
|
|
|
* $config['additional_javascript'][] = 'js/jquery.min.js';
|
|
|
|
* $config['additional_javascript'][] = 'js/auto-reload.js';
|
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
|
|
|
|
|
|
|
var end_of_page = false;
|
2013-12-23 11:31:47 -05:00
|
|
|
|
|
|
|
var orig_title = document.title;
|
|
|
|
var new_posts = 0;
|
|
|
|
var first_new_post = null;
|
|
|
|
var update_title = function() {
|
|
|
|
document.title = (new_posts ? "("+new_posts+") " : "") + orig_title;
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2012-03-15 05:40:52 -04:00
|
|
|
|
2013-12-23 10:34:44 -05:00
|
|
|
clearTimeout(poll_interval);
|
|
|
|
poll_interval = setTimeout(poll, end_of_page ? 3000 : 10000);
|
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);
|
|
|
|
poll_interval = setTimeout(poll, 100);
|
|
|
|
end_of_page = true;
|
2012-03-15 05:40:52 -04:00
|
|
|
}).trigger('scroll');
|
2013-12-23 10:34:44 -05:00
|
|
|
|
|
|
|
poll_interval = setTimeout(poll, 3000);
|
2012-03-14 23:44:15 -04:00
|
|
|
});
|
|
|
|
|