thread_update.js
102 lines
| 2.9 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; | ||
function blink(node) { | ||||
var blinkCount = 2; | ||||
var blinkDelay = 250; | ||||
var nodeToAnimate = node; | ||||
for (var i = 0; i < blinkCount; i++) { | ||||
nodeToAnimate = nodeToAnimate.fadeOut(blinkDelay).fadeIn(blinkDelay); | ||||
} | ||||
} | ||||
|
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) { | ||||
var addedPosts = data.added; | ||||
for (var i = 0; i < addedPosts.length; i++) { | ||||
var postText = addedPosts[i]; | ||||
|
r363 | var post = $(postText); | ||
post.appendTo(lastPost.parent()); | ||||
|
r361 | addRefLinkPreview(post[0]); | ||
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); | ||||
var postId = post.attr('id'); | ||||
var oldPost = $('div.thread').children('.post[id=' + postId + ']'); | ||||
oldPost.replaceWith(post); | ||||
addRefLinkPreview(post[0]); | ||||
blink(post); | ||||
} | ||||
|
r363 | // TODO Process updated and deleted posts | ||
lastUpdateTime = data.last_update; | ||||
loading = false; | ||||
}) | ||||
.error(function(data) { | ||||
// TODO Show error message that server is unavailable? | ||||
|
r361 | loading = false; | ||
}); | ||||
} | ||||
|
r363 | function initAutoupdate() { | ||
loading = false; | ||||
lastUpdateTime = $('.metapanel').attr('data-last-update'); | ||||
setInterval(updateThread, THREAD_UPDATE_DELAY); | ||||
} | ||||