From 250f9411da417b1496e8739fa57c669c756d7083 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Wed, 20 Jun 2012 22:20:48 +0100 Subject: [PATCH] Smooth scrolling for the sideways storyview --- core/modules/macros/story/views/classic.js | 36 +-------------------- core/modules/macros/story/views/sideways.js | 1 + core/modules/utils.dom.js | 32 ++++++++++++++++++ 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/core/modules/macros/story/views/classic.js b/core/modules/macros/story/views/classic.js index 06b232fd6..deb6da3d5 100644 --- a/core/modules/macros/story/views/classic.js +++ b/core/modules/macros/story/views/classic.js @@ -12,40 +12,6 @@ A storyview that shows a sequence of tiddlers and navigates by smoothly scrollin /*global $tw: false */ "use strict"; -function slowInSlowOut(t) { - return (1 - ((Math.cos(t * Math.PI) + 1) / 2)); -} - -function animateScroll(startX,startY,endX,endY,duration) { - var startTime = new Date(), - timerId = window.setInterval(function() { - var t = ((new Date()) - startTime) / duration; - if(t >= 1) { - window.clearInterval(timerId); - t = 1; - } - t = slowInSlowOut(t); - var x = startX + (endX - startX) * t, - y = startY + (endY - startY) * t; - window.scrollTo(x,y); - }, 10); -} - -/* -Smoothly scroll an element back into view if needed -*/ -function scrollIntoView(element) { - var x = element.offsetLeft, - y = element.offsetTop, - winWidth = window.innerWidth, - winHeight = window.innerHeight, - scrollLeft = window.scrollX || document.documentElement.scrollLeft, - scrollTop = window.scrollY || document.documentElement.scrollTop; - if((x < scrollLeft) || (x > (scrollLeft + winWidth)) || (y < scrollTop) || (y > (scrollTop + winHeight))) { - animateScroll(scrollLeft,scrollTop,x,y,$tw.config.preferences.animationDuration); - } -} - function ClassicScroller(story) { this.story = story; } @@ -57,7 +23,7 @@ Visualise navigation to the specified tiddler macro, optionally specifying a sou sourceNode: optional tree node that initiated the navigation */ ClassicScroller.prototype.navigate = function(targetTiddlerNode,isNew,sourceEvent) { - scrollIntoView(targetTiddlerNode.domNode); + $tw.utils.scrollIntoView(targetTiddlerNode.domNode); }; exports.classic = ClassicScroller; diff --git a/core/modules/macros/story/views/sideways.js b/core/modules/macros/story/views/sideways.js index 3c6328e3c..e3ecdd309 100644 --- a/core/modules/macros/story/views/sideways.js +++ b/core/modules/macros/story/views/sideways.js @@ -38,6 +38,7 @@ Visualise navigation to the specified tiddler macro, optionally specifying a sou */ SidewaysView.prototype.navigate = function(targetTiddlerNode,isNew,sourceEvent) { setStoryElementStyles(targetTiddlerNode.domNode); + $tw.utils.scrollIntoView(targetTiddlerNode.domNode); }; exports.sideways = SidewaysView; diff --git a/core/modules/utils.dom.js b/core/modules/utils.dom.js index dd83c5041..7486ab551 100644 --- a/core/modules/utils.dom.js +++ b/core/modules/utils.dom.js @@ -75,4 +75,36 @@ exports.applyStyleSheet = function(id,css) { } }; +/* +Smoothly scroll an element back into view if needed +*/ +exports.scrollIntoView = function(element) { + var slowInSlowOut = function(t) { + return (1 - ((Math.cos(t * Math.PI) + 1) / 2)); + }, + animateScroll = function(startX,startY,endX,endY,duration) { + var startTime = new Date(), + timerId = window.setInterval(function() { + var t = ((new Date()) - startTime) / duration; + if(t >= 1) { + window.clearInterval(timerId); + t = 1; + } + t = slowInSlowOut(t); + var x = startX + (endX - startX) * t, + y = startY + (endY - startY) * t; + window.scrollTo(x,y); + }, 10); + }, + x = element.offsetLeft, + y = element.offsetTop, + winWidth = window.innerWidth, + winHeight = window.innerHeight, + scrollLeft = window.scrollX || document.documentElement.scrollLeft, + scrollTop = window.scrollY || document.documentElement.scrollTop; + if((x < scrollLeft) || (x > (scrollLeft + winWidth)) || (y < scrollTop) || (y > (scrollTop + winHeight))) { + animateScroll(scrollLeft,scrollTop,x,y,$tw.config.preferences.animationDuration); + } +}; + })();