Browse Source

Merge pull request #1324 from Alkarex/global-view-scroll

Global view scroll
Alexandre Alapetite 9 years ago
parent
commit
1f9f9ce972
2 changed files with 21 additions and 14 deletions
  1. 1 0
      p/scripts/global_view.js
  2. 20 14
      p/scripts/main.js

+ 1 - 0
p/scripts/global_view.js

@@ -26,6 +26,7 @@ function load_panel(link) {
 		// Sans ça, si l'on scroll en lisant une catégorie par exemple,
 		// en en ouvrant une autre ensuite, on se retrouve au même point de scroll
 		$("#panel").scrollTop(0);
+		$(window).scrollTop(0);
 
 		$('#panel').on('click', '#nav_menu_read_all button, #bigMarkAsRead', function () {
 			console.log($(this).attr("formaction"));

+ 20 - 14
p/scripts/main.js

@@ -449,26 +449,32 @@ function auto_share(key) {
 	}
 }
 
-function inMarkViewport(flux, box_to_follow) {
-	var bottom = flux.offset().top + flux.height(),
-		windowTop = box_to_follow.scrollTop();
-	return bottom < windowTop + 40;
+function scrollAsRead(box_to_follow) {
+	var minTop = 40 + (context.current_view === 'global' ? box_to_follow.offset().top : box_to_follow.scrollTop());
+	$('.not_read:visible').each(function () {
+			var $this = $(this);
+			if ($this.offset().top + $this.height() < minTop) {
+				mark_read($this, true);
+			}
+		});
 }
 
 function init_posts() {
-	var box_to_follow = $(window);
-	if (context.current_view === 'global') {
-		box_to_follow = $("#panel");
-	}
+	var box_to_follow = context.current_view === 'global' ? $("#panel") : $(window);
 
 	if (context.auto_mark_scroll) {
+		var lastScroll = 0,	//Throttle
+			timerId = 0;
 		box_to_follow.scroll(function () {
-			$('.not_read:visible').each(function () {
-				var $this = $(this);
-				if (inMarkViewport($this, box_to_follow)) {
-					mark_read($this, true);
-				}
-			});
+			window.clearTimeout(timerId);
+			if (lastScroll + 500 < Date.now()) {
+				lastScroll = Date.now();
+				scrollAsRead(box_to_follow);
+			} else {
+				timerId = window.setTimeout(function() {
+						scrollAsRead(box_to_follow);
+					}, 500);
+			}
 		});
 	}