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") {
|
|
|
|
return this.handleScrollEvent(event);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Handle a scroll event hitting the page document
|
|
|
|
*/
|
|
|
|
PageScroller.prototype.handleScrollEvent = 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;
|
|
|
|
}
|
2012-11-26 16:08:52 +00:00
|
|
|
// Now scroll the body
|
2012-11-03 16:14:00 +00:00
|
|
|
var scrollPosition = $tw.utils.getScrollPosition();
|
2012-11-26 16:08:52 +00:00
|
|
|
this.cancelScroll();
|
2012-07-07 16:14:50 +00:00
|
|
|
this.startTime = new Date();
|
|
|
|
this.startX = scrollPosition.x;
|
|
|
|
this.startY = scrollPosition.y;
|
2012-11-03 16:14:00 +00:00
|
|
|
this.endX = bounds.left;
|
|
|
|
this.endY = bounds.top;
|
2012-07-07 16:14:50 +00:00
|
|
|
if((this.endX < this.startX) || (this.endX > (this.startX + window.innerWidth)) || (this.endY < this.startY) || (this.endY > (this.startY + window.innerHeight))) {
|
|
|
|
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);
|
2012-07-07 16:14:50 +00:00
|
|
|
window.scrollTo(self.startX + (self.endX - self.startX) * t,self.startY + (self.endY - self.startY) * t);
|
|
|
|
}, 10);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-26 16:08:52 +00:00
|
|
|
exports.PageScroller = PageScroller;
|
2012-07-07 16:14:50 +00:00
|
|
|
|
|
|
|
})();
|