Merge pull request #97 from towards-a-new-leftypol/fixes
WIP post ajax behaviour fixes
This commit is contained in:
commit
d227644986
246
js/ajax.js
246
js/ajax.js
|
@ -13,128 +13,134 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$(window).ready(function() {
|
$(window).ready(function() {
|
||||||
var settings = new script_settings('ajax');
|
var settings = new script_settings('ajax');
|
||||||
var do_not_ajax = false;
|
var do_not_ajax = false;
|
||||||
|
|
||||||
// Enable submit button if disabled (cache problem)
|
// Enable submit button if disabled (cache problem)
|
||||||
$('input[type="submit"]').removeAttr('disabled');
|
$('input[type="submit"]').removeAttr('disabled');
|
||||||
|
|
||||||
var setup_form = function($form) {
|
var setup_form = function($form) {
|
||||||
$form.submit(function() {
|
$form.submit(function() {
|
||||||
if (do_not_ajax)
|
if (do_not_ajax)
|
||||||
return true;
|
return true;
|
||||||
var form = this;
|
var form = this;
|
||||||
var submit_txt = $(this).find('input[type="submit"]').val();
|
var submit_txt = $(this).find('input[type="submit"]').val();
|
||||||
if (window.FormData === undefined)
|
if (window.FormData === undefined)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
var formData = new FormData(this);
|
var formData = new FormData(this);
|
||||||
formData.append('json_response', '1');
|
formData.append('json_response', '1');
|
||||||
formData.append('post', submit_txt);
|
formData.append('post', submit_txt);
|
||||||
|
|
||||||
$(document).trigger("ajax_before_post", formData);
|
$(document).trigger("ajax_before_post", formData);
|
||||||
|
|
||||||
var updateProgress = function(e) {
|
var updateProgress = function(e) {
|
||||||
var percentage;
|
var percentage;
|
||||||
if (e.position === undefined) { // Firefox
|
if (e.position === undefined) { // Firefox
|
||||||
percentage = Math.round(e.loaded * 100 / e.total);
|
percentage = Math.round(e.loaded * 100 / e.total);
|
||||||
}
|
}
|
||||||
else { // Chrome?
|
else { // Chrome?
|
||||||
percentage = Math.round(e.position * 100 / e.total);
|
percentage = Math.round(e.position * 100 / e.total);
|
||||||
}
|
}
|
||||||
$(form).find('input[type="submit"]').val(_('Posting... (#%)').replace('#', percentage));
|
$(form).find('input[type="submit"]').val(_('Posting... (#%)').replace('#', percentage));
|
||||||
};
|
};
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: this.action,
|
url: this.action,
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
xhr: function() {
|
xhr: function() {
|
||||||
var xhr = $.ajaxSettings.xhr();
|
var xhr = $.ajaxSettings.xhr();
|
||||||
if(xhr.upload) {
|
if(xhr.upload) {
|
||||||
xhr.upload.addEventListener('progress', updateProgress, false);
|
xhr.upload.addEventListener('progress', updateProgress, false);
|
||||||
}
|
}
|
||||||
return xhr;
|
return xhr;
|
||||||
},
|
},
|
||||||
success: function(post_response) {
|
success: function(post_response) {
|
||||||
if (post_response.error) {
|
if (post_response.error) {
|
||||||
if (post_response.banned) {
|
if (post_response.banned) {
|
||||||
// You are banned. Must post the form normally so the user can see the ban message.
|
// You are banned. Must post the form normally so the user can see the ban message.
|
||||||
do_not_ajax = true;
|
do_not_ajax = true;
|
||||||
$(form).find('input[type="submit"]').each(function() {
|
$(form).find('input[type="submit"]').each(function() {
|
||||||
var $replacement = $('<input type="hidden">');
|
var $replacement = $('<input type="hidden">');
|
||||||
$replacement.attr('name', $(this).attr('name'));
|
$replacement.attr('name', $(this).attr('name'));
|
||||||
$replacement.val(submit_txt);
|
$replacement.val(submit_txt);
|
||||||
$(this)
|
$(this)
|
||||||
.after($replacement)
|
.after($replacement)
|
||||||
.replaceWith($('<input type="button">').val(submit_txt));
|
.replaceWith($('<input type="button">').val(submit_txt));
|
||||||
});
|
});
|
||||||
$(form).submit();
|
$(form).submit();
|
||||||
} else {
|
} else {
|
||||||
alert(post_response.error);
|
alert(post_response.error);
|
||||||
$(form).find('input[type="submit"]').val(submit_txt);
|
$(form).find('input[type="submit"]').val(submit_txt);
|
||||||
$(form).find('input[type="submit"]').removeAttr('disabled');
|
$(form).find('input[type="submit"]').removeAttr('disabled');
|
||||||
}
|
}
|
||||||
} else if (post_response.redirect && post_response.id) {
|
} else if (post_response.redirect && post_response.id) {
|
||||||
if (!$(form).find('input[name="thread"]').length
|
if (!$(form).find('input[name="thread"]').length
|
||||||
|| (!settings.get('always_noko_replies', true) && !post_response.noko)) {
|
|| (!settings.get('always_noko_replies', true) && !post_response.noko)) {
|
||||||
document.location = post_response.redirect;
|
document.location = post_response.redirect;
|
||||||
} else {
|
} else {
|
||||||
$.ajax({
|
/*
|
||||||
url: document.location,
|
$.ajax({
|
||||||
success: function(data) {
|
url: document.location,
|
||||||
$(data).find('div.post.reply').each(function() {
|
success: function(data) {
|
||||||
var id = $(this).attr('id');
|
$(data).find('div.post.reply').each(function() {
|
||||||
if($('#' + id).length == 0) {
|
var id = $(this).attr('id');
|
||||||
$(this).insertAfter($('div.post:last').next()).after('<br class="clear">');
|
if($('#' + id).length == 0) {
|
||||||
$(document).trigger('new_post', this);
|
$(this).insertAfter($('div.post:last').next()).after('<br class="clear">');
|
||||||
// watch.js & auto-reload.js retrigger
|
$(document).trigger('new_post', this);
|
||||||
setTimeout(function() { $(window).trigger("scroll"); }, 100);
|
// watch.js & auto-reload.js retrigger
|
||||||
}
|
setTimeout(function() { $(window).trigger("scroll"); }, 100);
|
||||||
});
|
}
|
||||||
|
});
|
||||||
highlightReply(post_response.id);
|
|
||||||
window.location.hash = post_response.id;
|
highlightReply(post_response.id);
|
||||||
$(window).scrollTop($(document).height());
|
window.location.hash = post_response.id;
|
||||||
|
$(window).scrollTop($(document).height());
|
||||||
$(form).find('input[type="submit"]').val(submit_txt);
|
|
||||||
$(form).find('input[type="submit"]').removeAttr('disabled');
|
$(form).find('input[type="submit"]').val(submit_txt);
|
||||||
$(form).find('input[name="subject"],input[name="file_url"],\
|
$(form).find('input[type="submit"]').removeAttr('disabled');
|
||||||
textarea[name="body"],input[type="file"]').val('').change();
|
$(form).find('input[name="subject"],input[name="file_url"],\
|
||||||
},
|
textarea[name="body"],input[type="file"]').val('').change();
|
||||||
cache: false,
|
},
|
||||||
contentType: false,
|
cache: false,
|
||||||
processData: false
|
contentType: false,
|
||||||
}, 'html');
|
processData: false
|
||||||
}
|
}, 'html');
|
||||||
$(form).find('input[type="submit"]').val(_('Posted...'));
|
*/
|
||||||
$(document).trigger("ajax_after_post", post_response);
|
}
|
||||||
} else {
|
$(form).find('input[type="submit"]').val(_('Posted...'));
|
||||||
alert(_('An unknown error occured when posting!'));
|
$(document).trigger("ajax_after_post", post_response);
|
||||||
$(form).find('input[type="submit"]').val(submit_txt);
|
$(form).find('input[type="submit"]').val(submit_txt);
|
||||||
$(form).find('input[type="submit"]').removeAttr('disabled');
|
$(form).find('input[type="submit"]').removeAttr('disabled');
|
||||||
}
|
$(form).find('input[name="subject"],input[name="file_url"],\
|
||||||
},
|
textarea[name="body"],input[type="file"]').val('').change();
|
||||||
error: function(xhr, status, er) {
|
} else {
|
||||||
console.log(xhr);
|
alert(_('An unknown error occured when posting!'));
|
||||||
alert(_('The server took too long to submit your post. Your post was probably still submitted. If it wasn\'t, we might be experiencing issues right now -- please try your post again later. Error information: ') + "<div><textarea>" + JSON.stringify(xhr) + "</textarea></div>");
|
$(form).find('input[type="submit"]').val(submit_txt);
|
||||||
$(form).find('input[type="submit"]').val(submit_txt);
|
$(form).find('input[type="submit"]').removeAttr('disabled');
|
||||||
$(form).find('input[type="submit"]').removeAttr('disabled');
|
}
|
||||||
},
|
},
|
||||||
data: formData,
|
error: function(xhr, status, er) {
|
||||||
cache: false,
|
console.log(xhr);
|
||||||
contentType: false,
|
alert(_('The server took too long to submit your post. Your post was probably still submitted. If it wasn\'t, we might be experiencing issues right now -- please try your post again later. Error information: ') + "<div><textarea>" + JSON.stringify(xhr) + "</textarea></div>");
|
||||||
processData: false
|
$(form).find('input[type="submit"]').val(submit_txt);
|
||||||
}, 'json');
|
$(form).find('input[type="submit"]').removeAttr('disabled');
|
||||||
|
},
|
||||||
$(form).find('input[type="submit"]').val(_('Posting...'));
|
data: formData,
|
||||||
$(form).find('input[type="submit"]').attr('disabled', true);
|
cache: false,
|
||||||
|
contentType: false,
|
||||||
return false;
|
processData: false
|
||||||
});
|
}, 'json');
|
||||||
};
|
|
||||||
setup_form($('form[name="post"]'));
|
$(form).find('input[type="submit"]').val(_('Posting...'));
|
||||||
$(window).on('quick-reply', function() {
|
$(form).find('input[type="submit"]').attr('disabled', true);
|
||||||
$('form#quick-reply').off('submit');
|
|
||||||
setup_form($('form#quick-reply'));
|
return false;
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
setup_form($('form[name="post"]'));
|
||||||
|
$(window).on('quick-reply', function() {
|
||||||
|
$('form#quick-reply').off('submit');
|
||||||
|
setup_form($('form#quick-reply'));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -18,24 +18,24 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// From http://stackoverflow.com/a/14035162
|
// From http://stackoverflow.com/a/14035162
|
||||||
$.fn.scrollStopped = function(callback) {
|
$.fn.scrollStopped = function(callback) {
|
||||||
$(this).scroll(function(){
|
$(this).scroll(function(){
|
||||||
var self = this, $this = $(self);
|
var self = this, $this = $(self);
|
||||||
if ($this.data('scrollTimeout')) {
|
if ($this.data('scrollTimeout')) {
|
||||||
clearTimeout($this.data('scrollTimeout'));
|
clearTimeout($this.data('scrollTimeout'));
|
||||||
}
|
}
|
||||||
$this.data('scrollTimeout', setTimeout(callback,250,self));
|
$this.data('scrollTimeout', setTimeout(callback,250,self));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function makeIcon(mode){
|
function makeIcon(mode){
|
||||||
var favicon = $("link[rel='shortcut icon']");
|
var favicon = $("link[rel='shortcut icon']");
|
||||||
|
|
||||||
if (!favicon.length) {
|
if (!favicon.length) {
|
||||||
var favicon = $('<link rel="shortcut icon"></link>').appendTo('head');
|
var favicon = $('<link rel="shortcut icon"></link>').appendTo('head');
|
||||||
}
|
}
|
||||||
|
|
||||||
$("link[rel='shortcut icon']").attr("href", configRoot+"static/favicon"+(mode?"-"+mode:"")+".ico");
|
$("link[rel='shortcut icon']").attr("href", configRoot+"static/favicon"+(mode?"-"+mode:"")+".ico");
|
||||||
}
|
}
|
||||||
|
|
||||||
+function(){
|
+function(){
|
||||||
|
@ -43,297 +43,305 @@ var notify = false;
|
||||||
auto_reload_enabled = true; // for watch.js to interop
|
auto_reload_enabled = true; // for watch.js to interop
|
||||||
|
|
||||||
$(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").length != 1)
|
if($(".post.op").length != 1)
|
||||||
{
|
{
|
||||||
return; //not thread page
|
return; //not thread page
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds Options panel item
|
// Adds Options panel item
|
||||||
if (typeof localStorage.auto_thread_update === 'undefined') {
|
if (typeof localStorage.auto_thread_update === 'undefined') {
|
||||||
localStorage.auto_thread_update = 'true'; //default value
|
localStorage.auto_thread_update = 'true'; //default value
|
||||||
}
|
}
|
||||||
if (window.Options && Options.get_tab('general')) {
|
if (window.Options && Options.get_tab('general')) {
|
||||||
Options.extend_tab("general", "<fieldset id='auto-update-fs'><legend>"+_("Auto update")+"</legend>"
|
Options.extend_tab("general", "<fieldset id='auto-update-fs'><legend>"+_("Auto update")+"</legend>"
|
||||||
+ ('<label id="auto-thread-update"><input type="checkbox">' + _('Auto update thread') + '</label>')
|
+ ('<label id="auto-thread-update"><input type="checkbox">' + _('Auto update thread') + '</label>')
|
||||||
+ ('<label id="auto_thread_desktop_notifications"><input type="checkbox">' + _('Show desktop notifications when users quote me') + '</label>')
|
+ ('<label id="auto_thread_desktop_notifications"><input type="checkbox">' + _('Show desktop notifications when users quote me') + '</label>')
|
||||||
+ ('<label id="auto_thread_desktop_notifications_all"><input type="checkbox">' + _('Show desktop notifications on all replies') + '</label>')
|
+ ('<label id="auto_thread_desktop_notifications_all"><input type="checkbox">' + _('Show desktop notifications on all replies') + '</label>')
|
||||||
+ '</fieldset>');
|
+ '</fieldset>');
|
||||||
|
|
||||||
$('#auto-thread-update>input').on('click', function() {
|
$('#auto-thread-update>input').on('click', function() {
|
||||||
if ($('#auto-thread-update>input').is(':checked')) {
|
if ($('#auto-thread-update>input').is(':checked')) {
|
||||||
localStorage.auto_thread_update = 'true';
|
localStorage.auto_thread_update = 'true';
|
||||||
} else {
|
} else {
|
||||||
localStorage.auto_thread_update = 'false';
|
localStorage.auto_thread_update = 'false';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#auto_thread_desktop_notifications>input,#auto_thread_desktop_notifications_all>input').on('click', function() {
|
$('#auto_thread_desktop_notifications>input,#auto_thread_desktop_notifications_all>input').on('click', function() {
|
||||||
if (!("Notification" in window)) return;
|
if (!("Notification" in window)) return;
|
||||||
|
|
||||||
var setting = $(this).parent().attr('id');
|
var setting = $(this).parent().attr('id');
|
||||||
|
|
||||||
if ($(this).is(':checked')) {
|
if ($(this).is(':checked')) {
|
||||||
Notification.requestPermission(function(permission){
|
Notification.requestPermission(function(permission){
|
||||||
if (permission === "granted") {
|
if (permission === "granted") {
|
||||||
localStorage[setting] = 'true';
|
localStorage[setting] = 'true';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (Notification.permission === "granted") {
|
if (Notification.permission === "granted") {
|
||||||
localStorage[setting] = 'true';
|
localStorage[setting] = 'true';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
localStorage[setting] = 'false';
|
localStorage[setting] = 'false';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (localStorage.auto_thread_update === 'true') {
|
if (localStorage.auto_thread_update === 'true') {
|
||||||
$('#auto-thread-update>input').prop('checked', true);
|
$('#auto-thread-update>input').prop('checked', true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (localStorage.auto_thread_desktop_notifications === 'true') {
|
if (localStorage.auto_thread_desktop_notifications === 'true') {
|
||||||
$('#auto_thread_desktop_notifications>input').prop('checked', true);
|
$('#auto_thread_desktop_notifications>input').prop('checked', true);
|
||||||
notify = "mention";
|
notify = "mention";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (localStorage.auto_thread_desktop_notifications_all === 'true') {
|
if (localStorage.auto_thread_desktop_notifications_all === 'true') {
|
||||||
$('#auto_thread_desktop_notifications_all>input').prop('checked', true);
|
$('#auto_thread_desktop_notifications_all>input').prop('checked', true);
|
||||||
notify = "all";
|
notify = "all";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// not thread
|
// not thread
|
||||||
if (active_page != 'thread')
|
if (active_page != 'thread')
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var countdown_interval;
|
var countdown_interval;
|
||||||
|
|
||||||
// Add an update link
|
// Add an update link
|
||||||
$(".threadlinks span:last-child").append("<span id='updater'><a href='#' id='update_thread'>["+_("Update")+"]</a> (<input type='checkbox' id='auto_update_status'> "+_("Auto")+") <span id='update_secs'></span></span>");
|
$(".threadlinks span:last-child").append("<span id='updater'><a href='#' id='update_thread'>["+_("Update")+"]</a> (<input type='checkbox' id='auto_update_status'> "+_("Auto")+") <span id='update_secs'></span></span>");
|
||||||
|
|
||||||
// Set the updater checkbox according to user setting
|
// Set the updater checkbox according to user setting
|
||||||
if (localStorage.auto_thread_update === 'true') {
|
if (localStorage.auto_thread_update === 'true') {
|
||||||
$('#auto_update_status').prop('checked', true);
|
$('#auto_update_status').prop('checked', true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
||||||
makeIcon(false);
|
makeIcon(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
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() {
|
$('#auto_update_status').click(function() {
|
||||||
if($("#auto_update_status").is(':checked')) {
|
if($("#auto_update_status").is(':checked')) {
|
||||||
auto_update(poll_interval_mindelay);
|
auto_update(poll_interval_mindelay);
|
||||||
} else {
|
} else {
|
||||||
stop_auto_update();
|
stop_auto_update();
|
||||||
$('#update_secs').text("");
|
$('#update_secs').text("");
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
var decrement_timer = function() {
|
var decrement_timer = function() {
|
||||||
poll_current_time = poll_current_time - 1000;
|
poll_current_time = poll_current_time - 1000;
|
||||||
$('#update_secs').text(poll_current_time/1000);
|
$('#update_secs').text(poll_current_time/1000);
|
||||||
|
|
||||||
if (poll_current_time <= 0) {
|
if (poll_current_time <= 0) {
|
||||||
poll(manualUpdate = false);
|
poll(manualUpdate = false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var recheck_activated = function(end_of_page) {
|
var recheck_activated = function(end_of_page) {
|
||||||
if (typeof end_of_page == "undefined") var end_of_page = false;
|
if (typeof end_of_page == "undefined") var end_of_page = false;
|
||||||
if (end_of_page || (new_posts && window_active &&
|
if (end_of_page || (new_posts && window_active &&
|
||||||
$(window).scrollTop() + $(window).height() >=
|
$(window).scrollTop() + $(window).height() >=
|
||||||
$('div.boardlist.bottom').position().top)) {
|
$('div.boardlist.bottom').position().top)) {
|
||||||
|
|
||||||
new_posts = 0;
|
new_posts = 0;
|
||||||
}
|
}
|
||||||
update_title();
|
update_title();
|
||||||
first_new_post = null;
|
first_new_post = null;
|
||||||
};
|
};
|
||||||
|
|
||||||
// automatically updates the thread after a specified delay
|
// automatically updates the thread after a specified delay
|
||||||
var auto_update = function(delay) {
|
var auto_update = function(delay) {
|
||||||
clearInterval(countdown_interval);
|
clearInterval(countdown_interval);
|
||||||
|
|
||||||
poll_current_time = delay;
|
poll_current_time = delay;
|
||||||
countdown_interval = setInterval(decrement_timer, 1000);
|
countdown_interval = setInterval(decrement_timer, 1000);
|
||||||
$('#update_secs').text(poll_current_time/1000);
|
$('#update_secs').text(poll_current_time/1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
var stop_auto_update = function() {
|
var stop_auto_update = function() {
|
||||||
clearInterval(countdown_interval);
|
clearInterval(countdown_interval);
|
||||||
}
|
}
|
||||||
|
|
||||||
var epoch = (new Date).getTime();
|
var epoch = (new Date).getTime();
|
||||||
var epochold = epoch;
|
var epochold = epoch;
|
||||||
|
|
||||||
var timeDiff = function (delay) {
|
var timeDiff = function (delay) {
|
||||||
if((epoch-epochold) > delay) {
|
if((epoch-epochold) > delay) {
|
||||||
epochold = epoch = (new Date).getTime();
|
epochold = epoch = (new Date).getTime();
|
||||||
return true;
|
return true;
|
||||||
}else{
|
}else{
|
||||||
epoch = (new Date).getTime();
|
epoch = (new Date).getTime();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var poll = function(manualUpdate) {
|
var poll = function(manualUpdate) {
|
||||||
stop_auto_update();
|
stop_auto_update();
|
||||||
$('#update_secs').text(_("Updating..."));
|
$('#update_secs').text(_("Updating..."));
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: document.location,
|
url: document.location,
|
||||||
success: function(data) {
|
success: function(data) {
|
||||||
var loaded_posts = 0; // the number of new posts loaded in this update
|
var loaded_posts = 0; // the number of new posts loaded in this update
|
||||||
$(data).find('div.post.reply').each(function() {
|
|
||||||
var id = $(this).attr('id');
|
|
||||||
if($('#' + id).length == 0) {
|
|
||||||
if (!new_posts) {
|
|
||||||
first_new_post = this;
|
|
||||||
makeIcon('reply');
|
|
||||||
if (notify === "all" || (notify === "mention" && $(this).find('.own_post').length)) {
|
|
||||||
var body = $(this).children('.body').html().replace(/<br\s*[\/]?>/gi, "\n");
|
|
||||||
var n = new Notification("New reply to "+$('title').text(), {body: $('<div/>').html(body).text()});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($("div.post").length > 1){
|
|
||||||
$(this).parent().insertAfter($('div.post:not(.post-hover):last').parent().next()).after('<br class="clear">');
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$(this).insertAfter($('div.post:not(.post-hover):last')).after('<br class="clear">');
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
$(post).on('submit', function(e){
|
$(data).find('div.post.reply').each(function() {
|
||||||
poll(manualUpdate = true);
|
var id = $(this).attr('id');
|
||||||
dothis(this);
|
|
||||||
});
|
|
||||||
|
|
||||||
$(window).scrollStopped(function() {
|
|
||||||
// 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;
|
|
||||||
} else {
|
|
||||||
if($("#auto_update_status").is(':checked') && timeDiff(poll_interval_mindelay)) {
|
|
||||||
poll(manualUpdate = true);
|
|
||||||
}
|
|
||||||
end_of_page = true;
|
|
||||||
}
|
|
||||||
recheck_activated(end_of_page);
|
|
||||||
});
|
|
||||||
|
|
||||||
$('#update_thread').on('click', function() { poll(manualUpdate = true); return false; });
|
// check that this post doesn't already exist
|
||||||
|
if($('#' + id).length == 0) {
|
||||||
|
if (!new_posts) {
|
||||||
|
first_new_post = this;
|
||||||
|
makeIcon('reply');
|
||||||
|
if (notify === "all" || (notify === "mention" && $(this).find('.own_post').length)) {
|
||||||
|
var body = $(this).children('.body').html().replace(/<br\s*[\/]?>/gi, "\n");
|
||||||
|
var n = new Notification("New reply to "+$('title').text(), {body: $('<div/>').html(body).text()});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($("div.post").length > 1){
|
||||||
|
$(this).parent().insertAfter($('div.post:not(.post-hover):last').parent().next()).after('<br class="clear">');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$(this).insertAfter($('div.post:not(.post-hover):last')).after('<br class="clear">');
|
||||||
|
}
|
||||||
|
new_posts++;
|
||||||
|
loaded_posts++;
|
||||||
|
$(document).trigger('new_post', this);
|
||||||
|
recheck_activated();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if($("#auto_update_status").is(':checked')) {
|
time_loaded = Date.now(); // interop with watch.js
|
||||||
auto_update(poll_interval_delay);
|
|
||||||
}
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
$(post).on('submit', function(e){
|
||||||
|
setTimeout(
|
||||||
|
function() {
|
||||||
|
poll(manualUpdate = true)
|
||||||
|
},
|
||||||
|
500
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
$(window).scrollStopped(function() {
|
||||||
|
// 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;
|
||||||
|
} else {
|
||||||
|
if($("#auto_update_status").is(':checked') && timeDiff(poll_interval_mindelay)) {
|
||||||
|
poll(manualUpdate = true);
|
||||||
|
}
|
||||||
|
end_of_page = true;
|
||||||
|
}
|
||||||
|
recheck_activated(end_of_page);
|
||||||
|
});
|
||||||
|
|
||||||
|
$('#update_thread').on('click', function() { poll(manualUpdate = true); return false; });
|
||||||
|
|
||||||
|
if($("#auto_update_status").is(':checked')) {
|
||||||
|
auto_update(poll_interval_delay);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}();
|
}();
|
||||||
|
|
Loading…
Reference in New Issue