thread_update.js
336 lines
| 9.1 KiB
| application/javascript
|
JavascriptLexer
|
r365 | /* | ||
@licstart The following is the entire license notice for the | ||||
JavaScript code in this page. | ||||
|
r895 | Copyright (C) 2013-2014 neko259 | ||
|
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. | ||||
*/ | ||||
|
r853 | var wsUser = ''; | ||
|
r361 | |||
|
r725 | var unreadPosts = 0; | ||
|
r892 | var documentOriginalTitle = ''; | ||
|
r363 | |||
|
r853 | // Thread ID does not change, can be stored one time | ||
var threadId = $('div.thread').children('.post').first().attr('id'); | ||||
|
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 | ||||
*/ | ||||
|
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, | ||||
"timestamp": metapanel.getAttribute('data-last-update'), | ||||
"token": metapanel.getAttribute('data-ws-token'), | ||||
|
r854 | "debug": false | ||
|
r853 | }); | ||
centrifuge.on('error', function(error_message) { | ||||
|
r863 | console.log("Error connecting to websocket server."); | ||
return false; | ||||
|
r853 | }); | ||
centrifuge.on('connect', function() { | ||||
var channelName = 'thread:' + threadId; | ||||
centrifuge.subscribe(channelName, function(message) { | ||||
|
r895 | getThreadDiff(); | ||
|
r853 | }); | ||
|
r892 | // For the case we closed the browser and missed some updates | ||
getThreadDiff(); | ||||
|
r1011 | $('#autoupdate').hide(); | ||
|
r853 | }); | ||
centrifuge.connect(); | ||||
|
r863 | |||
return true; | ||||
|
r853 | } | ||
|
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() { | ||||
|
r894 | var lastUpdateTime = $('.metapanel').attr('data-last-update'); | ||
|
r892 | |||
|
r894 | var diffUrl = '/api/diff_thread/' + threadId + '/' + lastUpdateTime + '/'; | ||
|
r892 | |||
$.getJSON(diffUrl) | ||||
.success(function(data) { | ||||
var addedPosts = data.added; | ||||
|
r1074 | var hasMetaUpdates = false; | ||
|
r892 | |||
for (var i = 0; i < addedPosts.length; i++) { | ||||
var postText = addedPosts[i]; | ||||
var post = $(postText); | ||||
|
r1074 | updatePost(post); | ||
hasMetaUpdates = true; | ||||
|
r892 | } | ||
var updatedPosts = data.updated; | ||||
for (var i = 0; i < updatedPosts.length; i++) { | ||||
var postText = updatedPosts[i]; | ||||
var post = $(postText); | ||||
|
r1074 | updatePost(post); | ||
hasMetaUpdates = true; | ||||
|
r892 | } | ||
|
r1074 | if (hasMetaUpdates) { | ||
updateMetadataPanel(); | ||||
} | ||||
|
r1073 | |||
|
r892 | // TODO Process removed posts if any | ||
|
r895 | $('.metapanel').attr('data-last-update', data.last_update); | ||
|
r892 | }) | ||
} | ||||
/** | ||||
|
r895 | * Add or update the post on html page. | ||
|
r892 | */ | ||
|
r895 | function updatePost(postHtml) { | ||
|
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); | ||||
|
r895 | var threadBlock = $('div.thread'); | ||
|
r853 | |||
|
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 { | ||||
var threadPosts = threadBlock.children('.post'); | ||||
|
r853 | var lastPost = threadPosts.last(); | ||
post.appendTo(lastPost.parent()); | ||||
updateBumplimitProgress(1); | ||||
showNewPostsTitle(1); | ||||
if (bottom) { | ||||
scrollToBottom(); | ||||
} | ||||
} | ||||
processNewPost(post); | ||||
} | ||||
|
r892 | /** | ||
* Initiate a blinking animation on a node to show it was updated. | ||||
*/ | ||||
|
r363 | function blink(node) { | ||
var blinkCount = 2; | ||||
var nodeToAnimate = node; | ||||
for (var i = 0; i < blinkCount; i++) { | ||||
|
r481 | nodeToAnimate = nodeToAnimate.fadeTo('fast', 0.5).fadeTo('fast', 1.0); | ||
|
r363 | } | ||
} | ||||
|
r361 | |||
|
r373 | function isPageBottom() { | ||
var scroll = $(window).scrollTop() / ($(document).height() | ||||
|
r855 | - $(window).height()); | ||
|
r373 | |||
return scroll == 1 | ||||
} | ||||
|
r363 | function initAutoupdate() { | ||
|
r864 | return connectWebsocket(); | ||
|
r363 | } | ||
|
r391 | |||
function getReplyCount() { | ||||
|
r404 | return $('.thread').children('.post').length | ||
|
r391 | } | ||
function getImageCount() { | ||||
return $('.thread').find('img').length | ||||
} | ||||
|
r420 | |||
|
r892 | /** | ||
* Update post count, images count and last update time in the metadata | ||||
* panel. | ||||
*/ | ||||
|
r1073 | function updateMetadataPanel() { | ||
|
r536 | var replyCountField = $('#reply-count'); | ||
var imageCountField = $('#image-count'); | ||||
replyCountField.text(getReplyCount()); | ||||
imageCountField.text(getImageCount()); | ||||
|
r1073 | var lastUpdate = $('.post:last').children('.post-info').first() | ||
.children('.pub_time').first().html(); | ||||
|
r536 | if (lastUpdate !== '') { | ||
var lastUpdateField = $('#last-update'); | ||||
|
r1017 | lastUpdateField.html(lastUpdate); | ||
|
r536 | blink(lastUpdateField); | ||
} | ||||
blink(replyCountField); | ||||
blink(imageCountField); | ||||
} | ||||
|
r429 | /** | ||
* Update bumplimit progress bar | ||||
*/ | ||||
|
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; | ||||
|
r429 | if (newPostsToLimit <= 0) { | ||
$('.bar-bg').remove(); | ||||
|
r420 | } else { | ||
postsToLimitElement.text(newPostsToLimit); | ||||
progressBar.width((100 - postCount / bumplimit * 100.0) + '%'); | ||||
} | ||||
} | ||||
|
r427 | } | ||
|
r429 | |||
|
r451 | /** | ||
* Show 'new posts' text in the title if the document is not visible to a user | ||||
*/ | ||||
|
r468 | function showNewPostsTitle(newPostCount) { | ||
|
r451 | if (document.hidden) { | ||
|
r468 | if (documentOriginalTitle === '') { | ||
documentOriginalTitle = document.title; | ||||
} | ||||
unreadPosts = unreadPosts + newPostCount; | ||||
document.title = '[' + unreadPosts + '] ' + documentOriginalTitle; | ||||
|
r451 | |||
document.addEventListener('visibilitychange', function() { | ||||
if (documentOriginalTitle !== '') { | ||||
document.title = documentOriginalTitle; | ||||
documentOriginalTitle = ''; | ||||
|
r468 | unreadPosts = 0; | ||
|
r451 | } | ||
document.removeEventListener('visibilitychange', null); | ||||
}); | ||||
} | ||||
|
r458 | } | ||
|
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'); | ||||
|
r682 | $('.file_wrap').find('.file-thumb').remove(); | ||
|
r533 | } | ||
|
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; | ||||
|
r725 | showAsErrors(form, ''); | ||
|
r682 | |||
if (status === 'ok') { | ||||
|
r1059 | resetFormPosition(); | ||
|
r682 | resetForm(form); | ||
|
r923 | getThreadDiff(); | ||
|
r1057 | scrollToBottom(); | ||
|
r682 | } else { | ||
var errors = json.errors; | ||||
for (var i = 0; i < errors.length; i++) { | ||||
var fieldErrors = errors[i]; | ||||
var error = fieldErrors.errors; | ||||
|
r725 | showAsErrors(form, error); | ||
|
r682 | } | ||
} | ||||
} | ||||
|
r533 | |||
|
r709 | /** | ||
|
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) { | ||||
|
r1005 | var errorList = $('<div class="form-errors">' + text + '<div>'); | ||
|
r725 | errorList.appendTo(form); | ||
} | ||||
} | ||||
/** | ||||
|
r709 | * Run js methods that are usually run on the document, on the new post | ||
*/ | ||||
function processNewPost(post) { | ||||
|
r710 | addRefLinkPreview(post[0]); | ||
|
r709 | highlightCode(post); | ||
|
r853 | blink(post); | ||
|
r709 | } | ||
|
r458 | $(document).ready(function(){ | ||
|
r922 | if (initAutoupdate()) { | ||
// Post form data over AJAX | ||||
var threadId = $('div.thread').children('.post').first().attr('id'); | ||||
|
r534 | |||
|
r922 | var form = $('#form'); | ||
|
r682 | |||
|
r1024 | if (form.length > 0) { | ||
var options = { | ||||
beforeSubmit: function(arr, $form, options) { | ||||
showAsErrors($('form'), gettext('Sending message...')); | ||||
}, | ||||
success: updateOnPost, | ||||
url: '/api/add_post/' + threadId + '/' | ||||
}; | ||||
|
r534 | |||
|
r1024 | form.ajaxForm(options); | ||
|
r534 | |||
|
r1024 | resetForm(form); | ||
} | ||||
|
r856 | } | ||
|
r1011 | |||
$('#autoupdate').click(getThreadDiff); | ||||
|
r458 | }); | ||