Browse Source

Revert to old auto-reload.js

with barrucadu's modifications
pull/40/head
Simon McFarlane 9 years ago
parent
commit
7ab9cc43d1
  1. 392
      js/auto-reload.js

392
js/auto-reload.js

@ -17,221 +17,227 @@
* *
*/ */
au = false;
auto_reload_enabled = true; // for watch.js to interop auto_reload_enabled = true; // for watch.js to interop
function makeIcon(){
if(au) return;
au = true;
$("link[rel='icon']").attr("href", "../static/favicon_au.png");
}
$(document).ready(function(){ $(document).ready(function(){
if($('div.banner').length == 0) if($('div.banner').length == 0)
return; // not index return; // not index
if($(".post.op").size() != 1) if($(".post.op").size() != 1)
return; //not thread page return; //not thread page
var countdown_interval; var countdown_interval;
// Add an update link // Add an update link
$('.boardlist.bottom').prev().after("<span id='updater'><a href='#' id='update_thread' style='padding-left:10px'>["+_("Update")+"]</a> (<input type='checkbox' id='auto_update_status' checked> "+_("Auto")+") <span id='update_secs'></span></span>"); $(".threadlinks span:last-child").after("<span id='updater'><a href='#' id='update_thread' style='padding-left:10px'>"+_("Update")+"</a> ] (<input type='checkbox' id='auto_update_status' checked> "+_("Auto")+") <span id='update_secs'></span></span>");
// Grab the settings // Grab the settings
var settings = new script_settings('auto-reload'); var settings = new script_settings('auto-reload');
var poll_interval_mindelay = settings.get('min_delay_bottom', 5000); var poll_interval_mindelay = settings.get('min_delay_bottom', 5000);
var poll_interval_maxdelay = settings.get('max_delay', 600000); var poll_interval_maxdelay = settings.get('max_delay', 600000);
var poll_interval_errordelay = settings.get('error_delay', 30000); var poll_interval_errordelay = settings.get('error_delay', 30000);
// number of ms to wait before reloading // number of ms to wait before reloading
var poll_interval_delay = poll_interval_mindelay; var poll_interval_delay = poll_interval_mindelay;
var poll_current_time = poll_interval_delay; var poll_current_time = poll_interval_delay;
var end_of_page = false; var end_of_page = false;
var new_posts = 0; var new_posts = 0;
var first_new_post = null; var first_new_post = null;
var title = document.title; var title = document.title;
if (typeof update_title == "undefined") { if (typeof update_title == "undefined") {
var update_title = function() { var update_title = function() {
if (new_posts) { if (new_posts) {
document.title = "("+new_posts+") "+title; document.title = "("+new_posts+") "+title;
} else { } else {
document.title = title; document.title = title;
} }
}; };
} }
if (typeof add_title_collector != "undefined") if (typeof add_title_collector != "undefined")
add_title_collector(function(){ add_title_collector(function(){
return new_posts; return new_posts;
}); });
var window_active = true; var window_active = true;
$(window).focus(function() { $(window).focus(function() {
window_active = true; window_active = true;
recheck_activated(); recheck_activated();
// Reset the delay if needed // Reset the delay if needed
if(settings.get('reset_focus', true)) { if(settings.get('reset_focus', true)) {
poll_interval_delay = poll_interval_mindelay; poll_interval_delay = poll_interval_mindelay;
} }
}); });
$(window).blur(function() { $(window).blur(function() {
window_active = false; window_active = false;
}); });
$('#auto_update_status').click(function() {
if($("#auto_update_status").is(':checked')) {
auto_update(poll_interval_mindelay);
} else {
stop_auto_update();
$('#update_secs').text("");
}
}); $('#auto_update_status').click(function() {
if($("#auto_update_status").is(':checked')) {
auto_update(poll_interval_mindelay);
var decrement_timer = function() { } else {
poll_current_time = poll_current_time - 1000; stop_auto_update();
$('#update_secs').text(poll_current_time/1000); $('#update_secs').text("");
if (poll_current_time <= 0) {
poll(manualUpdate = false);
}
} }
var recheck_activated = function() { });
if (new_posts && window_active &&
$(window).scrollTop() + $(window).height() >=
$('div.boardlist.bottom').position().top) {
new_posts = 0;
} var decrement_timer = function() {
update_title(); poll_current_time = poll_current_time - 1000;
first_new_post = null; $('#update_secs').text(poll_current_time/1000);
};
if (poll_current_time <= 0) {
// automatically updates the thread after a specified delay poll(manualUpdate = false);
var auto_update = function(delay) {
clearInterval(countdown_interval);
poll_current_time = delay;
countdown_interval = setInterval(decrement_timer, 1000);
$('#update_secs').text(poll_current_time/1000);
} }
}
var stop_auto_update = function() {
clearInterval(countdown_interval); var recheck_activated = function() {
if (new_posts && window_active &&
$(window).scrollTop() + $(window).height() >=
$('footer').position().top) {
new_posts = 0;
} }
update_title();
var epoch = (new Date).getTime(); first_new_post = null;
var epochold = epoch; };
var timeDiff = function (delay) { // automatically updates the thread after a specified delay
if((epoch-epochold) > delay) { var auto_update = function(delay) {
epochold = epoch = (new Date).getTime(); clearInterval(countdown_interval);
return true;
}else{ poll_current_time = delay;
epoch = (new Date).getTime(); countdown_interval = setInterval(decrement_timer, 1000);
return; $('#update_secs').text(poll_current_time/1000);
} }
var stop_auto_update = function() {
clearInterval(countdown_interval);
}
var epoch = (new Date).getTime();
var epochold = epoch;
var timeDiff = function (delay) {
if((epoch-epochold) > delay) {
epochold = epoch = (new Date).getTime();
return true;
}else{
epoch = (new Date).getTime();
return;
} }
}
var poll = function(manualUpdate) {
stop_auto_update(); var poll = function(manualUpdate) {
$('#update_secs').text(_("Updating...")); stop_auto_update();
$('#update_secs').text(_("Updating..."));
$.ajax({
url: document.location, $.ajax({
success: function(data) { url: document.location,
var loaded_posts = 0; // the number of new posts loaded in this update success: function(data) {
$(data).find('div.post.reply').each(function() { var loaded_posts = 0; // the number of new posts loaded in this update
var id = $(this).attr('id'); $(data).find('div.postcontainer').each(function() {
if($('#' + id).length == 0) { var id = $(this).attr('id').substring(2);
if (!new_posts) { if($('#' + id).length == 0) {
first_new_post = this; if (!new_posts) {
} first_new_post = this;
$(this).insertAfter($('div.post:last').next()).after('<br class="clear">'); makeIcon();
new_posts++;
loaded_posts++;
$(document).trigger('new_post', this);
recheck_activated();
}
});
time_loaded = Date.now(); // interop with watch.js
if ($('#auto_update_status').is(':checked')) {
// If there are no new posts, double the delay. Otherwise set it to the min.
if(loaded_posts == 0) {
// if the update was manual, don't increase the delay
if (manualUpdate == false) {
poll_interval_delay *= 2;
// Don't increase the delay beyond the maximum
if(poll_interval_delay > poll_interval_maxdelay) {
poll_interval_delay = poll_interval_maxdelay;
}
}
} else {
poll_interval_delay = poll_interval_mindelay;
}
auto_update(poll_interval_delay);
} else {
// Decide the message to show if auto update is disabled
if (loaded_posts > 0)
$('#update_secs').text(fmt(_("Thread updated with {0} new post(s)"), [loaded_posts]));
else
$('#update_secs').text(_("No new posts found"));
}
},
error: function(xhr, status_text, error_text) {
if (status_text == "error") {
if (error_text == "Not Found") {
$('#update_secs').text(_("Thread deleted or pruned"));
$('#auto_update_status').prop('checked', false);
$('#auto_update_status').prop('disabled', true); // disable updates if thread is deleted
return;
} else {
$('#update_secs').text("Error: "+error_text);
}
} else if (status_text) {
$('#update_secs').text(_("Error: ")+status_text);
} else {
$('#update_secs').text(_("Unknown error"));
}
// Keep trying to update
if ($('#auto_update_status').is(':checked')) {
poll_interval_delay = poll_interval_errordelay;
auto_update(poll_interval_delay);
}
} }
$(this).insertAfter($('div.postcontainer:last').next()).after('<br class="clear">');
new_posts++;
loaded_posts++;
$(document).trigger('new_post', this);
recheck_activated();
}
}); });
time_loaded = Date.now(); // interop with watch.js
return false;
};
if ($('#auto_update_status').is(':checked')) {
$(window).scroll(function() { // If there are no new posts, double the delay. Otherwise set it to the min.
recheck_activated(); if(loaded_posts == 0) {
// if the update was manual, don't increase the delay
// if the newest post is not visible if (manualUpdate == false) {
if($(this).scrollTop() + $(this).height() < poll_interval_delay *= 2;
$('div.post:last').position().top + $('div.post:last').height()) {
end_of_page = false; // Don't increase the delay beyond the maximum
if(poll_interval_delay > poll_interval_maxdelay) {
poll_interval_delay = poll_interval_maxdelay;
}
}
} else {
poll_interval_delay = poll_interval_mindelay;
}
auto_update(poll_interval_delay);
} else {
// Decide the message to show if auto update is disabled
if (loaded_posts > 0)
$('#update_secs').text(fmt(_("Thread updated with {0} new post(s)"), [loaded_posts]));
else
$('#update_secs').text(_("No new posts found"));
}
},
error: function(xhr, status_text, error_text) {
if (status_text == "error") {
if (error_text == "Not Found") {
$('#update_secs').text(_("Thread deleted or pruned"));
$('#auto_update_status').prop('checked', false);
$('#auto_update_status').prop('disabled', true); // disable updates if thread is deleted
return; return;
} else {
$('#update_secs').text("Error: "+error_text);
}
} else if (status_text) {
$('#update_secs').text(_("Error: ")+status_text);
} else { } else {
if($("#auto_update_status").is(':checked') && timeDiff(poll_interval_mindelay)) { $('#update_secs').text(_("Unknown error"));
poll(manualUpdate = true);
}
end_of_page = true;
} }
});
$('#update_thread').on('click', function() { poll(manualUpdate = true); return false; }); // Keep trying to update
if ($('#auto_update_status').is(':checked')) {
poll_interval_delay = poll_interval_errordelay;
auto_update(poll_interval_delay);
}
}
});
if($("#auto_update_status").is(':checked')) { return false;
auto_update(poll_interval_delay); };
$(window).scroll(function() {
recheck_activated();
// if the newest post is not visible
if($(this).scrollTop() + $(this).height() <
$('div.post:last').position().top + $('div.post:last').height()) {
end_of_page = false;
return;
} else {
if($("#auto_update_status").is(':checked') && timeDiff(poll_interval_mindelay)) {
poll(manualUpdate = true);
}
end_of_page = true;
} }
}); });
$('#update_thread').on('click', function() { poll(manualUpdate = true); return false; });
if($("#auto_update_status").is(':checked')) {
auto_update(poll_interval_delay);
}
});

Loading…
Cancel
Save