##// END OF EJS Templates
Further updates to post partial replacement
neko259 -
r1315:d6b6426d default
parent child Browse files
Show More
@@ -30,6 +30,13 b' var POST_UPDATED = 1;'
30 30
31 31 var JS_AUTOUPDATE_PERIOD = 20000;
32 32
33 var ALLOWED_FOR_PARTIAL_UPDATE = [
34 'refmap',
35 'post-info'
36 ];
37
38 var ATTR_CLASS = 'class';
39
33 40 var wsUser = '';
34 41
35 42 var unreadPosts = 0;
@@ -165,7 +172,8 b' function updatePost(postHtml) {'
165 172 var type;
166 173
167 174 if (existingPosts.size() > 0) {
168 existingPosts.first().replaceWith(post);
175 replacePartial(existingPosts.first(), post, false);
176 post = existingPosts.first();
169 177
170 178 type = POST_UPDATED;
171 179 } else {
@@ -362,13 +370,16 b' function processNewPost(post) {'
362 370 blink(post);
363 371 }
364 372
365 function replacePartial(oldNode, newNode) {
366 var oldContent = oldNode[0].outerHTML;
367 var newContent = newNode[0].outerHTML;
373 function replacePartial(oldNode, newNode, recursive) {
374 if (!equalNodes(oldNode, newNode)) {
375 // Update parent node class attribute
376 var oldClass = oldNode.attr(ATTR_CLASS);
377 var newClass = newNode.attr(ATTR_CLASS);
378 if (oldClass != newClass) {
379 oldNode.attr(ATTR_CLASS, newClass);
380 };
368 381
369 // TODO Handle different children sizes
370
371 if (oldContent != newContent) {
382 // Replace children
372 383 var children = oldNode.children();
373 384 if (children.length == 0) {
374 385 console.log(oldContent);
@@ -377,13 +388,38 b' function replacePartial(oldNode, newNode'
377 388 oldNode.replaceWith(newNode);
378 389 } else {
379 390 var newChildren = newNode.children();
380 children.each(function(i) {
381 replacePartial(children.eq(i), newChildren.eq(i));
391 newChildren.each(function(i) {
392 var newChild = newChildren.eq(i);
393 var newChildClass = newChild.attr(ATTR_CLASS);
394
395 // Update only certain allowed blocks (e.g. not images)
396 if (ALLOWED_FOR_PARTIAL_UPDATE.indexOf(newChildClass) > -1) {
397 var oldChild = oldNode.children('.' + newChildClass);
398
399 if (oldChild.length == 0) {
400 oldNode.append(newChild);
401 } else {
402 if (!equalNodes(oldChild, newChild)) {
403 if (recursive) {
404 replacePartial(oldChild, newChild, false);
405 } else {
406 oldChild.replaceWith(newChild);
407 }
408 }
409 }
410 }
382 411 });
383 412 }
384 413 }
385 414 }
386 415
416 /**
417 * Compare nodes by content
418 */
419 function equalNodes(node1, node2) {
420 return node1[0].outerHTML == node2[0].outerHTML;
421 }
422
387 423 $(document).ready(function(){
388 424 if (initAutoupdate()) {
389 425 // Post form data over AJAX
General Comments 0
You need to be logged in to leave comments. Login now