2012-07-07 16:14:50 +00:00
|
|
|
/*\
|
2012-07-14 14:57:36 +00:00
|
|
|
title: $:/core/modules/utils/dom/scroller.js
|
2012-07-07 16:14:50 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: utils
|
|
|
|
|
2012-08-03 14:09:48 +00:00
|
|
|
Module that creates a $tw.utils.Scroller object prototype that manages scrolling in the browser
|
2012-07-07 16:14:50 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
2012-11-26 16:08:52 +00:00
|
|
|
Event handler for when the `tw-scroll` event hits the document body
|
2012-07-07 16:14:50 +00:00
|
|
|
*/
|
2012-11-26 16:08:52 +00:00
|
|
|
var PageScroller = function() {
|
|
|
|
this.timerId = null;
|
2012-07-07 16:14:50 +00:00
|
|
|
};
|
|
|
|
|
2012-11-26 16:08:52 +00:00
|
|
|
PageScroller.prototype.cancelScroll = function() {
|
2012-07-07 16:14:50 +00:00
|
|
|
if(this.timerId) {
|
|
|
|
window.clearInterval(this.timerId);
|
|
|
|
this.timerId = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2012-11-26 16:08:52 +00:00
|
|
|
Handle an event
|
2012-07-07 16:14:50 +00:00
|
|
|
*/
|
2012-11-26 16:08:52 +00:00
|
|
|
PageScroller.prototype.handleEvent = function(event) {
|
|
|
|
if(event.type === "tw-scroll") {
|
2013-02-08 09:22:21 +00:00
|
|
|
return this.scrollIntoView(event);
|
2012-11-26 16:08:52 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Handle a scroll event hitting the page document
|
|
|
|
*/
|
2013-02-08 09:22:21 +00:00
|
|
|
PageScroller.prototype.scrollIntoView = function(event) {
|
2012-11-03 16:14:00 +00:00
|
|
|
// Get the offset bounds of the element
|
2012-11-26 16:08:52 +00:00
|
|
|
var domNode = event.target,
|
|
|
|
bounds = {
|
2012-11-03 16:14:00 +00:00
|
|
|
left: domNode.offsetLeft,
|
|
|
|
top: domNode.offsetTop,
|
|
|
|
width: domNode.offsetWidth,
|
|
|
|
height: domNode.offsetHeight
|
|
|
|
};
|
|
|
|
// Walk up the tree adjusting the offset bounds by each offsetParent
|
|
|
|
while(domNode.offsetParent) {
|
|
|
|
domNode = domNode.offsetParent;
|
|
|
|
bounds.left += domNode.offsetLeft;
|
|
|
|
bounds.top += domNode.offsetTop;
|
|
|
|
}
|
2013-03-03 20:06:23 +00:00
|
|
|
// Now get ready to scroll the body
|
2012-11-26 16:08:52 +00:00
|
|
|
this.cancelScroll();
|
2012-07-07 16:14:50 +00:00
|
|
|
this.startTime = new Date();
|
2013-03-03 20:06:23 +00:00
|
|
|
var scrollPosition = $tw.utils.getScrollPosition(),
|
|
|
|
// We'll consider the horizontal and vertical scroll directions separately via this function
|
|
|
|
getEndPos = function(targetPos,targetSize,currentPos,currentSize) {
|
|
|
|
// If the target is above/left of the current view, then scroll to it's top/left
|
|
|
|
if(targetPos <= currentPos) {
|
|
|
|
return targetPos;
|
|
|
|
// If the target is smaller than the window and the scroll position is too far up, then scroll till the target is at the bottom of the window
|
|
|
|
} else if(targetSize < currentSize && currentPos < (targetPos + targetSize - currentSize)) {
|
|
|
|
return targetPos + targetSize - currentSize;
|
|
|
|
// If the target is big, then just scroll to the top
|
|
|
|
} else if(currentPos < targetPos) {
|
|
|
|
return targetPos;
|
|
|
|
// Otherwise, stay where we are
|
|
|
|
} else {
|
|
|
|
return currentPos;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
endX = getEndPos(bounds.left,bounds.width,scrollPosition.x,window.innerWidth),
|
|
|
|
endY = getEndPos(bounds.top,bounds.height,scrollPosition.y,window.innerHeight);
|
|
|
|
// Only scroll if necessary
|
|
|
|
if(endX !== scrollPosition.x || endY !== scrollPosition.y) {
|
2012-07-07 16:14:50 +00:00
|
|
|
var self = this;
|
|
|
|
this.timerId = window.setInterval(function() {
|
|
|
|
var t = ((new Date()) - self.startTime) / $tw.config.preferences.animationDuration;
|
|
|
|
if(t >= 1) {
|
2012-11-26 16:08:52 +00:00
|
|
|
self.cancelScroll();
|
2012-07-07 16:14:50 +00:00
|
|
|
t = 1;
|
|
|
|
}
|
2012-11-08 18:34:04 +00:00
|
|
|
t = $tw.utils.slowInSlowOut(t);
|
2013-03-03 20:06:23 +00:00
|
|
|
window.scrollTo(scrollPosition.x + (endX - scrollPosition.x) * t,scrollPosition.y + (endY - scrollPosition.y) * t);
|
2012-07-07 16:14:50 +00:00
|
|
|
}, 10);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-26 16:08:52 +00:00
|
|
|
exports.PageScroller = PageScroller;
|
2012-07-07 16:14:50 +00:00
|
|
|
|
|
|
|
})();
|