##// END OF EJS Templates
Removed cache from thread gallery template
Removed cache from thread gallery template

File last commit:

r1089:e299d6db default
r1104:21ab7968 default
Show More
thread_update.js
339 lines | 9.3 KiB | application/javascript | JavascriptLexer
neko259
Decreased anti-flood posting delay. Added lisence text to the thread...
r365 /*
@licstart The following is the entire license notice for the
JavaScript code in this page.
neko259
Trigger post update on a new post instead of sending the post itself. This fixed issues with posts sent in a fixed locale instead of a different locale for each client
r895 Copyright (C) 2013-2014 neko259
neko259
Decreased anti-flood posting delay. Added lisence text to the thread...
r365
The JavaScript code in this page is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this page.
*/
neko259
Refactored thread update code. Use indexOf() instead of contains() for strings...
r1085 var CLASS_POST = '.post'
neko259
Added centrifuge (websocket) support for thread autoupdate. Only websocket version is supported for now
r853 var wsUser = '';
neko259
Added thread autoupdate. Currently has some bugs, not ready for merge with the main branch
r361
neko259
Added settings to limit posting speed. Added message when the form data is sent and response not yet received
r725 var unreadPosts = 0;
neko259
Run thread update when connecting to websocket to get missed posts if the...
r892 var documentOriginalTitle = '';
neko259
Update thread by time, not post id. This will help with getting updated posts
r363
neko259
Added centrifuge (websocket) support for thread autoupdate. Only websocket version is supported for now
r853 // Thread ID does not change, can be stored one time
neko259
Refactored thread update code. Use indexOf() instead of contains() for strings...
r1085 var threadId = $('div.thread').children(CLASS_POST).first().attr('id');
neko259
Added centrifuge (websocket) support for thread autoupdate. Only websocket version is supported for now
r853
neko259
Trigger post update on a new post instead of sending the post itself. This fixed issues with posts sent in a fixed locale instead of a different locale for each client
r895 /**
* Connect to websocket server and subscribe to thread updates. On any update we
* request a thread diff.
*
* @returns {boolean} true if connected, false otherwise
*/
neko259
Added centrifuge (websocket) support for thread autoupdate. Only websocket version is supported for now
r853 function connectWebsocket() {
var metapanel = $('.metapanel')[0];
var wsHost = metapanel.getAttribute('data-ws-host');
var wsPort = metapanel.getAttribute('data-ws-port');
if (wsHost.length > 0 && wsPort.length > 0)
var centrifuge = new Centrifuge({
"url": 'ws://' + wsHost + ':' + wsPort + "/connection/websocket",
"project": metapanel.getAttribute('data-ws-project'),
"user": wsUser,
neko259
Updated API to use a new timestamp format for getting updated posts and get...
r1086 "timestamp": metapanel.getAttribute('data-ws-token-time'),
neko259
Added centrifuge (websocket) support for thread autoupdate. Only websocket version is supported for now
r853 "token": metapanel.getAttribute('data-ws-token'),
neko259
Removed debug settings from the websocket JS script
r854 "debug": false
neko259
Added centrifuge (websocket) support for thread autoupdate. Only websocket version is supported for now
r853 });
centrifuge.on('error', function(error_message) {
neko259
Added 'bumpable' field. Disable thread bump once and for all, not counting it...
r863 console.log("Error connecting to websocket server.");
neko259
Updated API to use a new timestamp format for getting updated posts and get...
r1086 console.log(error_message);
neko259
Added 'bumpable' field. Disable thread bump once and for all, not counting it...
r863 return false;
neko259
Added centrifuge (websocket) support for thread autoupdate. Only websocket version is supported for now
r853 });
centrifuge.on('connect', function() {
var channelName = 'thread:' + threadId;
centrifuge.subscribe(channelName, function(message) {
neko259
Trigger post update on a new post instead of sending the post itself. This fixed issues with posts sent in a fixed locale instead of a different locale for each client
r895 getThreadDiff();
neko259
Added centrifuge (websocket) support for thread autoupdate. Only websocket version is supported for now
r853 });
neko259
Run thread update when connecting to websocket to get missed posts if the...
r892 // For the case we closed the browser and missed some updates
getThreadDiff();
neko259
Added button to update thread manually if websocket connection failed
r1011 $('#autoupdate').hide();
neko259
Added centrifuge (websocket) support for thread autoupdate. Only websocket version is supported for now
r853 });
centrifuge.connect();
neko259
Added 'bumpable' field. Disable thread bump once and for all, not counting it...
r863
return true;
neko259
Added centrifuge (websocket) support for thread autoupdate. Only websocket version is supported for now
r853 }
neko259
Run thread update when connecting to websocket to get missed posts if the...
r892 /**
* Get diff of the posts from the current thread timestamp.
* This is required if the browser was closed and some post updates were
* missed.
*/
function getThreadDiff() {
neko259
Another fix to thread update
r894 var lastUpdateTime = $('.metapanel').attr('data-last-update');
neko259
Updated API to use a new timestamp format for getting updated posts and get...
r1086 var lastPostId = $('.post').last().attr('id');
neko259
Run thread update when connecting to websocket to get missed posts if the...
r892
neko259
Updated API to use a new timestamp format for getting updated posts and get...
r1086 var diffUrl = '/api/diff_thread?thread=' + threadId + '&last_update=' + encodeURIComponent(lastUpdateTime)
+ '&last_post=' + lastPostId;
neko259
Run thread update when connecting to websocket to get missed posts if the...
r892
$.getJSON(diffUrl)
.success(function(data) {
var addedPosts = data.added;
for (var i = 0; i < addedPosts.length; i++) {
var postText = addedPosts[i];
var post = $(postText);
neko259
Don't update metadata panel on thread opening
r1074 updatePost(post);
neko259
Run thread update when connecting to websocket to get missed posts if the...
r892 }
neko259
Refactored thread update code. Use indexOf() instead of contains() for strings...
r1085 var addedPostsCount = addedPosts.length;
neko259
Add new posts title to the title only if there are any
r1089 if (addedPostsCount > 0) {
updateBumplimitProgress(addedPostsCount);
showNewPostsTitle(addedPostsCount);
}
neko259
Refactored thread update code. Use indexOf() instead of contains() for strings...
r1085
neko259
Run thread update when connecting to websocket to get missed posts if the...
r892 var updatedPosts = data.updated;
for (var i = 0; i < updatedPosts.length; i++) {
var postText = updatedPosts[i];
var post = $(postText);
neko259
Don't update metadata panel on thread opening
r1074 updatePost(post);
neko259
Run thread update when connecting to websocket to get missed posts if the...
r892 }
neko259
Add new posts title to the title only if there are any
r1089 var hasMetaUpdates = addedPostsCount > 0 || updatedPosts.length > 0;
neko259
Don't update metadata panel on thread opening
r1074 if (hasMetaUpdates) {
updateMetadataPanel();
}
neko259
Update metadata panel only once when a multiple of posts in updated during...
r1073
neko259
Run thread update when connecting to websocket to get missed posts if the...
r892 // TODO Process removed posts if any
neko259
Trigger post update on a new post instead of sending the post itself. This fixed issues with posts sent in a fixed locale instead of a different locale for each client
r895 $('.metapanel').attr('data-last-update', data.last_update);
neko259
Run thread update when connecting to websocket to get missed posts if the...
r892 })
}
/**
neko259
Trigger post update on a new post instead of sending the post itself. This fixed issues with posts sent in a fixed locale instead of a different locale for each client
r895 * Add or update the post on html page.
neko259
Run thread update when connecting to websocket to get missed posts if the...
r892 */
neko259
Trigger post update on a new post instead of sending the post itself. This fixed issues with posts sent in a fixed locale instead of a different locale for each client
r895 function updatePost(postHtml) {
neko259
Added centrifuge (websocket) support for thread autoupdate. Only websocket version is supported for now
r853 // This needs to be set on start because the page is scrolled after posts
// are added or updated
var bottom = isPageBottom();
var post = $(postHtml);
neko259
Trigger post update on a new post instead of sending the post itself. This fixed issues with posts sent in a fixed locale instead of a different locale for each client
r895 var threadBlock = $('div.thread');
neko259
Added centrifuge (websocket) support for thread autoupdate. Only websocket version is supported for now
r853
neko259
Trigger post update on a new post instead of sending the post itself. This fixed issues with posts sent in a fixed locale instead of a different locale for each client
r895 var postId = post.attr('id');
// If the post already exists, replace it. Otherwise add as a new one.
var existingPosts = threadBlock.children('.post[id=' + postId + ']');
if (existingPosts.size() > 0) {
existingPosts.replaceWith(post);
} else {
neko259
Refactored thread update code. Use indexOf() instead of contains() for strings...
r1085 post.appendTo(threadBlock);
neko259
Added centrifuge (websocket) support for thread autoupdate. Only websocket version is supported for now
r853
if (bottom) {
scrollToBottom();
}
}
processNewPost(post);
}
neko259
Run thread update when connecting to websocket to get missed posts if the...
r892 /**
* Initiate a blinking animation on a node to show it was updated.
*/
neko259
Update thread by time, not post id. This will help with getting updated posts
r363 function blink(node) {
var blinkCount = 2;
var nodeToAnimate = node;
for (var i = 0; i < blinkCount; i++) {
neko259
Made blink effect on thread update more soft
r481 nodeToAnimate = nodeToAnimate.fadeTo('fast', 0.5).fadeTo('fast', 1.0);
neko259
Update thread by time, not post id. This will help with getting updated posts
r363 }
}
neko259
Added thread autoupdate. Currently has some bugs, not ready for merge with the main branch
r361
neko259
Fixed getting precise and synced last update time. Added autoscroll to bottom after updating if user is at the page bottom
r373 function isPageBottom() {
var scroll = $(window).scrollTop() / ($(document).height()
neko259
Added missing semicolon to the thread update script
r855 - $(window).height());
neko259
Fixed getting precise and synced last update time. Added autoscroll to bottom after updating if user is at the page bottom
r373
return scroll == 1
}
neko259
Update thread by time, not post id. This will help with getting updated posts
r363 function initAutoupdate() {
neko259
Fixed bumping and autoupdate initialization
r864 return connectWebsocket();
neko259
Update thread by time, not post id. This will help with getting updated posts
r363 }
neko259
Update reply and image count after thread update
r391
function getReplyCount() {
neko259
Refactored thread update code. Use indexOf() instead of contains() for strings...
r1085 return $('.thread').children(CLASS_POST).length
neko259
Update reply and image count after thread update
r391 }
function getImageCount() {
return $('.thread').find('img').length
}
neko259
Updating bumplimit progress on thread update
r420
neko259
Run thread update when connecting to websocket to get missed posts if the...
r892 /**
* Update post count, images count and last update time in the metadata
* panel.
*/
neko259
Update metadata panel only once when a multiple of posts in updated during...
r1073 function updateMetadataPanel() {
neko259
Updating last_update time if the thread in autoupdate
r536 var replyCountField = $('#reply-count');
var imageCountField = $('#image-count');
replyCountField.text(getReplyCount());
imageCountField.text(getImageCount());
neko259
Update metadata panel only once when a multiple of posts in updated during...
r1073 var lastUpdate = $('.post:last').children('.post-info').first()
.children('.pub_time').first().html();
neko259
Updating last_update time if the thread in autoupdate
r536 if (lastUpdate !== '') {
var lastUpdateField = $('#last-update');
neko259
Set timestamps to local ones by JS
r1017 lastUpdateField.html(lastUpdate);
neko259
Updating last_update time if the thread in autoupdate
r536 blink(lastUpdateField);
}
blink(replyCountField);
blink(imageCountField);
}
neko259
Add dead_post class to the posts when thread reached bumplimit on autoupdate
r429 /**
* Update bumplimit progress bar
*/
neko259
Updating bumplimit progress on thread update
r420 function updateBumplimitProgress(postDelta) {
var progressBar = $('#bumplimit_progress');
if (progressBar) {
var postsToLimitElement = $('#left_to_limit');
var oldPostsToLimit = parseInt(postsToLimitElement.text());
var postCount = getReplyCount();
var bumplimit = postCount - postDelta + oldPostsToLimit;
var newPostsToLimit = bumplimit - postCount;
neko259
Add dead_post class to the posts when thread reached bumplimit on autoupdate
r429 if (newPostsToLimit <= 0) {
$('.bar-bg').remove();
neko259
Updating bumplimit progress on thread update
r420 } else {
postsToLimitElement.text(newPostsToLimit);
progressBar.width((100 - postCount / bumplimit * 100.0) + '%');
}
}
neko259
Updated posts must not affect bumplimit bar
r427 }
neko259
Add dead_post class to the posts when thread reached bumplimit on autoupdate
r429
neko259
Added new posts notification in the not active browser page
r451 /**
* Show 'new posts' text in the title if the document is not visible to a user
*/
neko259
Showing unread posts count when new posts appear in the background tab
r468 function showNewPostsTitle(newPostCount) {
neko259
Added new posts notification in the not active browser page
r451 if (document.hidden) {
neko259
Showing unread posts count when new posts appear in the background tab
r468 if (documentOriginalTitle === '') {
documentOriginalTitle = document.title;
}
unreadPosts = unreadPosts + newPostCount;
document.title = '[' + unreadPosts + '] ' + documentOriginalTitle;
neko259
Added new posts notification in the not active browser page
r451
document.addEventListener('visibilitychange', function() {
if (documentOriginalTitle !== '') {
document.title = documentOriginalTitle;
documentOriginalTitle = '';
neko259
Showing unread posts count when new posts appear in the background tab
r468 unreadPosts = 0;
neko259
Added new posts notification in the not active browser page
r451 }
document.removeEventListener('visibilitychange', null);
});
}
neko259
Added a server-side gallery and mode switcher
r458 }
neko259
Added posting over ajax
r533 /**
* Clear all entered values in the form fields
*/
function resetForm(form) {
form.find('input:text, input:password, input:file, select, textarea').val('');
form.find('input:radio, input:checkbox')
.removeAttr('checked').removeAttr('selected');
neko259
Fixed clearing image after posting.
r682 $('.file_wrap').find('.file-thumb').remove();
neko259
Added posting over ajax
r533 }
neko259
Fixed clearing image after posting.
r682 /**
* When the form is posted, this method will be run as a callback
*/
function updateOnPost(response, statusText, xhr, form) {
var json = $.parseJSON(response);
var status = json.status;
neko259
Added settings to limit posting speed. Added message when the form data is sent and response not yet received
r725 showAsErrors(form, '');
neko259
Fixed clearing image after posting.
r682
if (status === 'ok') {
neko259
Changed closeForm to resetFormPosition in JS because the form isn't closed any...
r1059 resetFormPosition();
neko259
Fixed clearing image after posting.
r682 resetForm(form);
neko259
Fixed resetting form after thread update
r923 getThreadDiff();
neko259
Fixes to previous commit
r1057 scrollToBottom();
neko259
Fixed clearing image after posting.
r682 } else {
var errors = json.errors;
for (var i = 0; i < errors.length; i++) {
var fieldErrors = errors[i];
var error = fieldErrors.errors;
neko259
Added settings to limit posting speed. Added message when the form data is sent and response not yet received
r725 showAsErrors(form, error);
neko259
Fixed clearing image after posting.
r682 }
}
}
neko259
Added posting over ajax
r533
neko259
Run js highlight on the new posts only, not on all page each time. Add...
r709 /**
neko259
Added settings to limit posting speed. Added message when the form data is sent and response not yet received
r725 * Show text in the errors row of the form.
* @param form
* @param text
*/
function showAsErrors(form, text) {
form.children('.form-errors').remove();
if (text.length > 0) {
neko259
Post message by ctrl-enter on the text field
r1005 var errorList = $('<div class="form-errors">' + text + '<div>');
neko259
Added settings to limit posting speed. Added message when the form data is sent and response not yet received
r725 errorList.appendTo(form);
}
}
/**
neko259
Run js highlight on the new posts only, not on all page each time. Add...
r709 * Run js methods that are usually run on the document, on the new post
*/
function processNewPost(post) {
neko259
Fixed thread update
r710 addRefLinkPreview(post[0]);
neko259
Run js highlight on the new posts only, not on all page each time. Add...
r709 highlightCode(post);
neko259
Added centrifuge (websocket) support for thread autoupdate. Only websocket version is supported for now
r853 blink(post);
neko259
Run js highlight on the new posts only, not on all page each time. Add...
r709 }
neko259
Added a server-side gallery and mode switcher
r458 $(document).ready(function(){
neko259
Added required tags. At least one such tag is needed to create a thread. All...
r922 if (initAutoupdate()) {
// Post form data over AJAX
var threadId = $('div.thread').children('.post').first().attr('id');
neko259
Fixed form file upload
r534
neko259
Added required tags. At least one such tag is needed to create a thread. All...
r922 var form = $('#form');
neko259
Fixed clearing image after posting.
r682
neko259
Init autoupdate in archived threads
r1024 if (form.length > 0) {
var options = {
beforeSubmit: function(arr, $form, options) {
showAsErrors($('form'), gettext('Sending message...'));
},
success: updateOnPost,
url: '/api/add_post/' + threadId + '/'
};
neko259
Fixed form file upload
r534
neko259
Init autoupdate in archived threads
r1024 form.ajaxForm(options);
neko259
Fixed form file upload
r534
neko259
Init autoupdate in archived threads
r1024 resetForm(form);
}
neko259
Use thread autoupdate only if websockets available
r856 }
neko259
Added button to update thread manually if websocket connection failed
r1011
$('#autoupdate').click(getThreadDiff);
neko259
Added a server-side gallery and mode switcher
r458 });