thread_update.js
266 lines
| 7.2 KiB
| application/javascript
|
JavascriptLexer
|
r365 | /* | ||
@licstart The following is the entire license notice for the | ||||
JavaScript code in this page. | ||||
Copyright (C) 2013 neko259 | ||||
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. | ||||
*/ | ||||
|
r361 | var THREAD_UPDATE_DELAY = 10000; | ||
var loading = false; | ||||
|
r363 | var lastUpdateTime = null; | ||
|
r468 | var unreadPosts = 0 | ||
|
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 | |||
function updateThread() { | ||||
if (loading) { | ||||
return; | ||||
} | ||||
loading = true; | ||||
var threadPosts = $('div.thread').children('.post'); | ||||
var lastPost = threadPosts.last(); | ||||
var threadId = threadPosts.first().attr('id'); | ||||
|
r363 | var diffUrl = '/api/diff_thread/' + threadId + '/' + lastUpdateTime + '/'; | ||
|
r361 | $.getJSON(diffUrl) | ||
.success(function(data) { | ||||
|
r373 | var bottom = isPageBottom(); | ||
|
r536 | var lastUpdate = ''; | ||
|
r361 | var addedPosts = data.added; | ||
for (var i = 0; i < addedPosts.length; i++) { | ||||
var postText = addedPosts[i]; | ||||
|
r363 | var post = $(postText); | ||
|
r536 | |||
if (lastUpdate === '') { | ||||
lastUpdate = post.find('.pub_time').text(); | ||||
} | ||||
|
r363 | post.appendTo(lastPost.parent()); | ||
|
r361 | addRefLinkPreview(post[0]); | ||
|
r703 | highlightCode(); | ||
|
r361 | |||
lastPost = post; | ||||
|
r363 | blink(post); | ||
|
r361 | } | ||
|
r364 | var updatedPosts = data.updated; | ||
for (var i = 0; i < updatedPosts.length; i++) { | ||||
var postText = updatedPosts[i]; | ||||
var post = $(postText); | ||||
|
r536 | |||
if (lastUpdate === '') { | ||||
lastUpdate = post.find('.pub_time').text(); | ||||
} | ||||
|
r364 | var postId = post.attr('id'); | ||
var oldPost = $('div.thread').children('.post[id=' + postId + ']'); | ||||
oldPost.replaceWith(post); | ||||
addRefLinkPreview(post[0]); | ||||
|
r703 | highlightCode(); | ||
|
r364 | |||
blink(post); | ||||
} | ||||
|
r536 | |||
|
r373 | // TODO Process deleted posts | ||
|
r363 | |||
lastUpdateTime = data.last_update; | ||||
loading = false; | ||||
|
r373 | |||
if (bottom) { | ||||
|
r686 | scrollToBottom(); | ||
|
r373 | } | ||
|
r391 | |||
|
r536 | var hasPostChanges = (updatedPosts.length > 0) | ||
|| (addedPosts.length > 0); | ||||
if (hasPostChanges) { | ||||
updateMetadataPanel(lastUpdate); | ||||
} | ||||
|
r420 | |||
|
r427 | updateBumplimitProgress(data.added.length); | ||
|
r451 | |||
if (data.added.length + data.updated.length > 0) { | ||||
|
r468 | showNewPostsTitle(data.added.length); | ||
|
r451 | } | ||
|
r363 | }) | ||
.error(function(data) { | ||||
// TODO Show error message that server is unavailable? | ||||
|
r361 | loading = false; | ||
}); | ||||
} | ||||
|
r373 | function isPageBottom() { | ||
var scroll = $(window).scrollTop() / ($(document).height() | ||||
- $(window).height()) | ||||
return scroll == 1 | ||||
} | ||||
|
r363 | function initAutoupdate() { | ||
loading = false; | ||||
lastUpdateTime = $('.metapanel').attr('data-last-update'); | ||||
setInterval(updateThread, THREAD_UPDATE_DELAY); | ||||
} | ||||
|
r391 | |||
function getReplyCount() { | ||||
|
r404 | return $('.thread').children('.post').length | ||
|
r391 | } | ||
function getImageCount() { | ||||
return $('.thread').find('img').length | ||||
} | ||||
|
r420 | |||
|
r536 | function updateMetadataPanel(lastUpdate) { | ||
var replyCountField = $('#reply-count'); | ||||
var imageCountField = $('#image-count'); | ||||
replyCountField.text(getReplyCount()); | ||||
imageCountField.text(getImageCount()); | ||||
if (lastUpdate !== '') { | ||||
var lastUpdateField = $('#last-update'); | ||||
lastUpdateField.text(lastUpdate); | ||||
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(); | ||||
|
r585 | $('.thread').children('.post').addClass('dead_post'); | ||
|
r420 | } else { | ||
postsToLimitElement.text(newPostsToLimit); | ||||
progressBar.width((100 - postCount / bumplimit * 100.0) + '%'); | ||||
} | ||||
} | ||||
|
r427 | } | ||
|
r429 | |||
|
r451 | var documentOriginalTitle = ''; | ||
/** | ||||
* 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; | ||||
form.children('.form-errors').remove(); | ||||
if (status === 'ok') { | ||||
resetForm(form); | ||||
updateThread(); | ||||
} else { | ||||
var errors = json.errors; | ||||
for (var i = 0; i < errors.length; i++) { | ||||
var fieldErrors = errors[i]; | ||||
var error = fieldErrors.errors; | ||||
var errorList = $('<div class="form-errors">' + error | ||||
+ '<div>'); | ||||
errorList.appendTo(form); | ||||
} | ||||
} | ||||
} | ||||
|
r533 | |||
|
r458 | $(document).ready(function(){ | ||
initAutoupdate(); | ||||
|
r534 | |||
// Post form data over AJAX | ||||
var threadId = $('div.thread').children('.post').first().attr('id');; | ||||
var form = $('#form'); | ||||
|
r682 | |||
|
r534 | var options = { | ||
success: updateOnPost, | ||||
|
r682 | url: '/api/add_post/' + threadId + '/' | ||
|
r534 | }; | ||
form.ajaxForm(options); | ||||
|
r682 | resetForm(form); | ||
|
r458 | }); | ||