From 0098dce5976bffb23dbb31d151d2d871c7cab8f9 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Fri, 25 Aug 2017 00:00:51 -0700 Subject: radically reduce code duplication in updateConvItems(); it's a bit easier to understand now although the different modes aren't as cleanly separated as they were. --- view/js/main.js | 164 +++++++++++++++++++------------------------------------- 1 file changed, 55 insertions(+), 109 deletions(-) (limited to 'view/js') diff --git a/view/js/main.js b/view/js/main.js index fd3c8b08f..ece4c2015 100644 --- a/view/js/main.js +++ b/view/js/main.js @@ -533,133 +533,76 @@ function updatePageItems(mode, data) { function updateConvItems(mode,data) { - if(mode === 'update') { + if(mode === 'update' || mode === 'replace') { prev = 'threads-begin'; - - $('.thread-wrapper.toplevel_item',data).each(function() { - - var ident = $(this).attr('id'); - // This should probably use the context argument instead - var commentWrap = $('#'+ident+' .collapsed-comments').attr('id'); - var itmId = 0; - var isVisible = false; - - if(typeof commentWrap !== 'undefined') - itmId = commentWrap.replace('collapsed-comments-',''); - - if($('#' + ident).length == 0 && profile_page == 1) { - $('img',this).each(function() { - $(this).attr('src',$(this).attr('dst')); - }); - if($('#collapsed-comments-'+itmId).is(':visible')) - isVisible = true; - $('#' + prev).after($(this)); - if(isVisible) - showHideComments(itmId); - $("> .wall-item-outside-wrapper .autotime, > .thread-wrapper .autotime",this).timeago(); - $("> .shared_header .autotime",this).timeago(); - } - else { - $('img',this).each(function() { - $(this).attr('src',$(this).attr('dst')); - }); - if($('#collapsed-comments-'+itmId).is(':visible')) - isVisible = true; - $('#' + ident).replaceWith($(this)); - if(isVisible) - showHideComments(itmId); - $("> .wall-item-outside-wrapper .autotime, > .thread-wrapper .autotime",this).timeago(); - $("> .shared_header .autotime",this).timeago(); - } - prev = ident; - }); } if(mode === 'append') { - next = 'threads-end'; + } + + if(mode === 'replace') { + $('.thread-wrapper').remove(); // clear existing content + } - $('.thread-wrapper.toplevel_item',data).each(function() { - var ident = $(this).attr('id'); - var commentWrap = $('#'+ident+' .collapsed-comments').attr('id'); - var itmId = 0; - var isVisible = false; + $('.thread-wrapper.toplevel_item',data).each(function() { - if(typeof commentWrap !== 'undefined') - itmId = commentWrap.replace('collapsed-comments-', ''); + var ident = $(this).attr('id'); - if($('#' + ident).length == 0) { - $('img',this).each(function() { - $(this).attr('src',$(this).attr('dst')); - }); - if($('#collapsed-comments-'+itmId).is(':visible')) - isVisible = true; - $('#threads-end').before($(this)); - if(isVisible) - showHideComments(itmId); - $("> .wall-item-outside-wrapper .autotime, > .thread-wrapper .autotime",this).timeago(); - $("> .shared_header .autotime",this).timeago(); - } - else { - $('img',this).each(function() { - $(this).attr('src', $(this).attr('dst')); - }); - if($('#collapsed-comments-'+itmId).is(':visible')) - isVisible = true; - $('#' + ident).replaceWith($(this)); - if(isVisible) - showHideComments(itmId); - $("> .wall-item-outside-wrapper .autotime, > .thread-wrapper .autotime",this).timeago(); - $("> .shared_header .autotime",this).timeago(); - } - }); + var commentWrap = $('#'+ident+' .collapsed-comments').attr('id'); + var itmId = 0; + var isVisible = false; - if(loadingPage) { - loadingPage = false; - } - } - if(mode === 'replace') { - // clear existing content - $('.thread-wrapper').remove(); + // figure out the comment state + if(typeof commentWrap !== 'undefined') + itmId = commentWrap.replace('collapsed-comments-',''); + + if($('#collapsed-comments-'+itmId).is(':visible')) + isVisible = true; - prev = 'threads-begin'; + // insert the content according to the mode and first_page + // and whether or not the content exists already (overwrite it) - $('.thread-wrapper.toplevel_item',data).each(function() { + if($('#' + ident).length == 0) { + if((mode === 'update' || mode === 'replace') && profile_page == 1) { + $('#' + prev).after($(this)); + prev = ident; + } + if(mode === 'append') { + $('#' + next).before($(this)); + } + } + else { + $('#' + ident).replaceWith($(this)); + } - var ident = $(this).attr('id'); - var commentWrap = $('#'+ident+' .collapsed-comments').attr('id'); - var itmId = 0; - var isVisible = false; + // set the comment state to the state we discovered earlier - if(typeof commentWrap !== 'undefined') - itmId = commentWrap.replace('collapsed-comments-',''); + if(isVisible) + showHideComments(itmId); - if($('#' + ident).length == 0 && profile_page == 1) { - $('img',this).each(function() { - $(this).attr('src',$(this).attr('dst')); - }); - if($('#collapsed-comments-'+itmId).is(':visible')) - isVisible = true; - $('#' + prev).after($(this)); - if(isVisible) - showHideComments(itmId); - $("> .wall-item-outside-wrapper .autotime, > .thread-wrapper .autotime",this).timeago(); - $("> .shared_header .autotime",this).timeago(); - } - prev = ident; - }); + // trigger the autotime function on all newly created content - if(loadingPage) { + $("> .wall-item-outside-wrapper .autotime, > .thread-wrapper .autotime",this).timeago(); + $("> .shared_header .autotime",this).timeago(); + + if((mode === 'append' || mode === 'replace') && (loadingPage)) { loadingPage = false; } - if (window.location.search.indexOf("mid=") != -1 || window.location.pathname.indexOf("display") != -1) { - var title = $(".wall-item-title").text(); - title.replace(/^\s+/, ''); - title.replace(/\s+$/, ''); - if (title) - document.title = title + " - " + document.title; + // if single thread view and the item has a title, display it in the title bar + + if(mode === 'replace') { + if (window.location.search.indexOf("mid=") != -1 || window.location.pathname.indexOf("display") != -1) { + var title = $(".wall-item-title").text(); + title.replace(/^\s+/, ''); + title.replace(/\s+$/, ''); + if (title) + document.title = title + " - " + document.title; + } } - } + }); + + // reset rotators and cursors we may have set before reaching this place $('.like-rotator').spin(false); @@ -668,6 +611,9 @@ function updateConvItems(mode,data) { $('body').css('cursor', 'auto'); } + // Setup to determine if the media player is playing. This affects + // some content loading decisions. + $('video').off('playing'); $('video').off('pause'); $('audio').off('playing'); -- cgit v1.2.3