@qooxdoo/framework
Version:
The JS Framework for Coders
140 lines (115 loc) • 3.9 kB
JavaScript
/* ************************************************************************
qooxdoo - the new era of web development
http://qooxdoo.org
Copyright:
2004-2014 1&1 Internet AG, Germany, http://www.1und1.de
License:
MIT: https://opensource.org/licenses/MIT
See the LICENSE file in the project's top-level directory for details.
Authors:
* Martin Wittemann (martinwittemann)
************************************************************************ */
/**
* Mixin holding the handler for roll event. Please
* keep in mind that the including widget has to have the scroll bars
* implemented as child controls named <code>scrollbar-x</code> and
* <code>scrollbar-y</code> to get the handler working. Also, you have to
* attach the listener yourself.
*/
qx.Mixin.define("qx.ui.core.scroll.MRoll", {
/**
* Allow scrolling by dragging the area. CAUTION: Enabling this property disallows the capability of drag&drop.
*/
properties: {
scrollByDrag: {
init: false,
check: "Boolean"
}
},
members: {
_cancelRoll: null,
/**
* Responsible for adding the event listener needed for scroll handling.
*/
_addRollHandling() {
this.addListener("roll", this._onRoll, this);
this.addListener("pointerdown", this._onPointerDownForRoll, this);
},
/**
* Responsible for removing the event listener needed for scroll handling.
*/
_removeRollHandling() {
this.removeListener("roll", this._onRoll, this);
this.removeListener("pointerdown", this._onPointerDownForRoll, this);
},
/**
* Handler for the pointerdown event which simply stops the momentum scrolling.
*
* @param e {qx.event.type.Pointer} pointerdown event
*/
_onPointerDownForRoll(e) {
this._cancelRoll = e.getPointerId();
},
/**
* Roll event handler
*
* @param e {qx.event.type.Roll} Roll event
*/
_onRoll(e) {
// only wheel and touch
if (e.getPointerType() == "mouse" && !this.getScrollByDrag()) {
return;
}
if (this._cancelRoll && e.getMomentum()) {
e.stopMomentum();
this._cancelRoll = null;
return;
}
this._cancelRoll = null;
var showX = this._isChildControlVisible("scrollbar-x");
var showY = this._isChildControlVisible("scrollbar-y");
var scrollbarY = showY ? this.getChildControl("scrollbar-y", true) : null;
var scrollbarX = showX ? this.getChildControl("scrollbar-x", true) : null;
var deltaY = e.getDelta().y;
var deltaX = e.getDelta().x;
var endY = !showY;
var endX = !showX;
// y case
if (scrollbarY) {
if (deltaY !== 0) {
scrollbarY.scrollBy(parseInt(deltaY, 10));
}
var position = scrollbarY.getPosition();
var max = scrollbarY.getMaximum();
// pass the event to the parent if the scrollbar is at an edge
if ((deltaY < 0 && position <= 0) || (deltaY > 0 && position >= max)) {
endY = true;
}
}
// x case
if (scrollbarX) {
if (deltaX !== 0) {
scrollbarX.scrollBy(parseInt(deltaX, 10));
}
var position = scrollbarX.getPosition();
var max = scrollbarX.getMaximum();
// pass the event to the parent if the scrollbar is at an edge
if ((deltaX < 0 && position <= 0) || (deltaX > 0 && position >= max)) {
endX = true;
}
}
if (endX && endY) {
e.stopMomentum();
}
// pass the event to the parent if both scrollbars are at the end
if (
(!endY && deltaX === 0) ||
(!endX && deltaY === 0) ||
((!endX || !endY) && deltaX !== 0 && deltaY !== 0)
) {
// Stop bubbling and native event only if a scrollbar is visible
e.stop();
}
}
}
});