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) {
|
2018-01-30 11:29:07 +00:00
|
|
|
var self = this,
|
|
|
|
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
|
|
|
// Get the client bounds of the element and adjust by the scroll position
|
2018-01-30 11:29:07 +00:00
|
|
|
var getBounds = function() {
|
|
|
|
var clientBounds = element.getBoundingClientRect(),
|
|
|
|
scrollPosition = $tw.utils.getScrollPosition();
|
|
|
|
return {
|
|
|
|
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
|
|
|
|
// 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
|
|
|
|
getEndPos = function(targetPos,targetSize,currentPos,currentSize) {
|
|
|
|
var newPos = targetPos;
|
2015-02-25 20:24:07 +00:00
|
|
|
// 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
|
|
|
},
|
2018-01-30 11:29:07 +00:00
|
|
|
drawFrame = function drawFrame() {
|
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);
|
2018-01-30 11:29:07 +00:00
|
|
|
var scrollPosition = $tw.utils.getScrollPosition(),
|
|
|
|
bounds = getBounds(),
|
|
|
|
endX = getEndPos(bounds.left,bounds.width,scrollPosition.x,window.innerWidth),
|
|
|
|
endY = getEndPos(bounds.top,bounds.height,scrollPosition.y,window.innerHeight);
|
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
|
|
|
};
|
2018-01-30 11:29:07 +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
|
|
|
|
|
|
|
})();
|