fineui是帆软报表和BI产品线所使用的前端框架。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.6 KiB

8 years ago
!(function () {
var PIXEL_STEP = 10;
var LINE_HEIGHT = 40;
var PAGE_HEIGHT = 800;
7 years ago
var requestAnimationFrame = _global.requestAnimationFrame || _global.webkitRequestAnimationFrame || _global.mozRequestAnimationFrame || _global.oRequestAnimationFrame || _global.msRequestAnimationFrame || _global.setTimeout;
8 years ago
7 years ago
function normalizeWheel (/* object*/event) /* object*/ {
8 years ago
var sX = 0,
sY = 0,
// spinX, spinY
pX = 0,
pY = 0; // pixelX, pixelY
// Legacy
7 years ago
if ("detail" in event) {
8 years ago
sY = event.detail;
}
7 years ago
if ("wheelDelta" in event) {
8 years ago
sY = -event.wheelDelta / 120;
}
7 years ago
if ("wheelDeltaY" in event) {
8 years ago
sY = -event.wheelDeltaY / 120;
}
7 years ago
if ("wheelDeltaX" in event) {
8 years ago
sX = -event.wheelDeltaX / 120;
}
// side scrolling on FF with DOMMouseScroll
7 years ago
if ("axis" in event && event.axis === event.HORIZONTAL_AXIS) {
8 years ago
sX = sY;
sY = 0;
}
pX = sX * PIXEL_STEP;
pY = sY * PIXEL_STEP;
7 years ago
if ("deltaY" in event) {
8 years ago
pY = event.deltaY;
}
7 years ago
if ("deltaX" in event) {
8 years ago
pX = event.deltaX;
}
if ((pX || pY) && event.deltaMode) {
if (event.deltaMode === 1) {
// delta in LINE units
pX *= LINE_HEIGHT;
pY *= LINE_HEIGHT;
} else {
// delta in PAGE units
pX *= PAGE_HEIGHT;
pY *= PAGE_HEIGHT;
}
}
// Fall-back if spin cannot be determined
if (pX && !sX) {
sX = pX < 1 ? -1 : 1;
}
if (pY && !sY) {
sY = pY < 1 ? -1 : 1;
}
return {
spinX: sX,
spinY: sY,
pixelX: pX,
pixelY: pY
};
}
BI.WheelHandler = function (onWheel, handleScrollX, handleScrollY, stopPropagation) {
this._animationFrameID = null;
this._deltaX = 0;
this._deltaY = 0;
this._didWheel = BI.bind(this._didWheel, this);
7 years ago
if (typeof handleScrollX !== "function") {
8 years ago
handleScrollX = handleScrollX ?
function () {
7 years ago
return true;
8 years ago
} :
function () {
7 years ago
return false;
8 years ago
};
}
7 years ago
if (typeof handleScrollY !== "function") {
8 years ago
handleScrollY = handleScrollY ?
function () {
7 years ago
return true;
8 years ago
} :
function () {
7 years ago
return false;
8 years ago
};
}
7 years ago
if (typeof stopPropagation !== "function") {
8 years ago
stopPropagation = stopPropagation ?
function () {
7 years ago
return true;
8 years ago
} :
function () {
7 years ago
return false;
8 years ago
};
}
this._handleScrollX = handleScrollX;
this._handleScrollY = handleScrollY;
this._stopPropagation = stopPropagation;
this._onWheelCallback = onWheel;
this.onWheel = BI.bind(this.onWheel, this);
};
BI.WheelHandler.prototype = {
constructor: BI.WheelHandler,
7 years ago
onWheel: function (/* object*/ event) {
8 years ago
var normalizedEvent = normalizeWheel(event);
var deltaX = this._deltaX + normalizedEvent.pixelX;
var deltaY = this._deltaY + normalizedEvent.pixelY;
var handleScrollX = this._handleScrollX(deltaX, deltaY);
var handleScrollY = this._handleScrollY(deltaY, deltaX);
if (!handleScrollX && !handleScrollY) {
return;
}
this._deltaX += handleScrollX ? normalizedEvent.pixelX : 0;
this._deltaY += handleScrollY ? normalizedEvent.pixelY : 0;
event.preventDefault ? event.preventDefault() : (event.returnValue = false);
var changed;
if (this._deltaX !== 0 || this._deltaY !== 0) {
if (this._stopPropagation()) {
event.stopPropagation ? event.stopPropagation() : (event.cancelBubble = true);
}
changed = true;
}
if (changed === true && this._animationFrameID === null) {
this._animationFrameID = requestAnimationFrame(this._didWheel);
}
},
_didWheel: function () {
this._animationFrameID = null;
this._onWheelCallback(this._deltaX, this._deltaY);
this._deltaX = 0;
this._deltaY = 0;
}
};
})();