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";
|
|
|
|
|
|
|
|
/*
|
2014-08-28 20:43:44 +00:00
|
|
|
Event handler for when the `tm-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() {
|
2013-06-10 13:11:58 +00:00
|
|
|
this.idRequestFrame = null;
|
|
|
|
this.requestAnimationFrame = window.requestAnimationFrame ||
|
|
|
|
window.webkitRequestAnimationFrame ||
|
|
|
|
window.mozRequestAnimationFrame ||
|
|
|
|
function(callback) {
|
|
|
|
return window.setTimeout(callback, 1000/60);
|
|
|
|
};
|
|
|
|
this.cancelAnimationFrame = window.cancelAnimationFrame ||
|
|
|
|
window.webkitCancelAnimationFrame ||
|
|
|
|
window.webkitCancelRequestAnimationFrame ||
|
|
|
|
window.mozCancelAnimationFrame ||
|
|
|
|
window.mozCancelRequestAnimationFrame ||
|
|
|
|
function(id) {
|
|
|
|
window.clearTimeout(id);
|
|
|
|
};
|
2012-07-07 16:14:50 +00:00
|
|
|
};
|
|
|
|
|
2012-11-26 16:08:52 +00:00
|
|
|
PageScroller.prototype.cancelScroll = function() {
|
2013-06-10 13:11:58 +00:00
|
|
|
if(this.idRequestFrame) {
|
|
|
|
this.cancelAnimationFrame.call(window,this.idRequestFrame);
|
|
|
|
this.idRequestFrame = null;
|
2012-07-07 16:14:50 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
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) {
|
2014-08-28 20:43:44 +00:00
|
|
|
if(event.type === "tm-scroll") {
|
2013-07-08 14:15:53 +00:00
|
|
|
return this.scrollIntoView(event.target);
|
2012-11-26 16:08:52 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Handle a scroll event hitting the page document
|
|
|
|
*/
|
2013-07-08 14:15:53 +00:00
|
|
|
PageScroller.prototype.scrollIntoView = function(element) {
|
2013-08-29 11:43:24 +00:00
|
|
|
var duration = $tw.utils.getAnimationDuration();
|
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();
|
2014-04-14 08:02:52 +00:00
|
|
|
this.startTime = Date.now();
|
2014-03-24 22:31:03 +00:00
|
|
|
var scrollPosition = $tw.utils.getScrollPosition();
|
|
|
|
// Get the client bounds of the element and adjust by the scroll position
|
|
|
|
var clientBounds = element.getBoundingClientRect(),
|
|
|
|
bounds = {
|
|
|
|
left: clientBounds.left + scrollPosition.x,
|
|
|
|
top: clientBounds.top + scrollPosition.y,
|
|
|
|
width: clientBounds.width,
|
|
|
|
height: clientBounds.height
|
|
|
|
};
|
|
|
|
// We'll consider the horizontal and vertical scroll directions separately via this function
|
2015-02-25 19:01:40 +00:00
|
|
|
// targetPos/targetSize - position and size of the target element
|
|
|
|
// currentPos/currentSize - position and size of the current scroll viewport
|
|
|
|
// returns: new position of the scroll viewport
|
2014-03-24 22:31:03 +00:00
|
|
|
var getEndPos = function(targetPos,targetSize,currentPos,currentSize) {
|
2015-02-25 20:24:07 +00:00
|
|
|
var newPos = currentPos;
|
2015-02-25 19:01:40 +00:00
|
|
|
// If the target is entirely above/left of the current view, then scroll to its top/left
|
2015-02-25 19:09:53 +00:00
|
|
|
if((targetPos + targetSize) <= (currentPos + 50)) {
|
2015-02-25 20:24:07 +00:00
|
|
|
newPos = targetPos;
|
2013-03-03 20:06:23 +00:00
|
|
|
// 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)) {
|
2015-02-25 20:24:07 +00:00
|
|
|
newPos = targetPos + targetSize - currentSize;
|
2015-02-25 19:01:40 +00:00
|
|
|
// If the target is out of view below/right, then just scroll to the top/left
|
2015-02-25 19:09:53 +00:00
|
|
|
} else if(targetPos > (currentPos + currentSize - 50)) {
|
2015-02-25 20:24:07 +00:00
|
|
|
newPos = targetPos;
|
|
|
|
}
|
|
|
|
// If we are scrolling within 50 pixels of the top/left then snap to zero
|
|
|
|
if(newPos < 50) {
|
|
|
|
newPos = 0;
|
2013-03-03 20:06:23 +00:00
|
|
|
}
|
2015-02-25 20:24:07 +00:00
|
|
|
return newPos;
|
2013-03-03 20:06:23 +00:00
|
|
|
},
|
|
|
|
endX = getEndPos(bounds.left,bounds.width,scrollPosition.x,window.innerWidth),
|
|
|
|
endY = getEndPos(bounds.top,bounds.height,scrollPosition.y,window.innerHeight);
|
2015-02-25 20:24:07 +00:00
|
|
|
// Only scroll if the position has changed
|
|
|
|
if(endX !== scrollPosition.x || endY !== scrollPosition.y) {
|
2013-06-10 13:11:58 +00:00
|
|
|
var self = this,
|
|
|
|
drawFrame;
|
|
|
|
drawFrame = function () {
|
2013-08-29 11:43:24 +00:00
|
|
|
var t;
|
|
|
|
if(duration <= 0) {
|
|
|
|
t = 1;
|
|
|
|
} else {
|
2014-04-14 08:02:52 +00:00
|
|
|
t = ((Date.now()) - self.startTime) / duration;
|
2013-08-29 11:43:24 +00:00
|
|
|
}
|
2012-07-07 16:14:50 +00:00
|
|
|
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);
|
2013-06-10 20:07:23 +00:00
|
|
|
if(t < 1) {
|
|
|
|
self.idRequestFrame = self.requestAnimationFrame.call(window,drawFrame);
|
|
|
|
}
|
2013-06-10 13:11:58 +00:00
|
|
|
};
|
|
|
|
drawFrame();
|
2012-07-07 16:14:50 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-26 16:08:52 +00:00
|
|
|
exports.PageScroller = PageScroller;
|
2012-07-07 16:14:50 +00:00
|
|
|
|
|
|
|
})();
|