|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- import clamp from 'lodash-es/clamp';
- import { isOneOf, getPosition, eventScope, setStyle, } from '../utils/';
- var Direction;
- (function (Direction) {
- Direction[Direction["X"] = 0] = "X";
- Direction[Direction["Y"] = 1] = "Y";
- })(Direction || (Direction = {}));
- export function mouseHandler(scrollbar) {
- var addEvent = eventScope(scrollbar);
- var container = scrollbar.containerEl;
- var _a = scrollbar.track, xAxis = _a.xAxis, yAxis = _a.yAxis;
- function calcOffset(direction, clickPosition) {
- var size = scrollbar.size;
- if (direction === Direction.X) {
- var totalWidth = size.container.width + (xAxis.thumb.realSize - xAxis.thumb.displaySize);
- return clickPosition / totalWidth * size.content.width;
- }
- if (direction === Direction.Y) {
- var totalHeight = size.container.height + (yAxis.thumb.realSize - yAxis.thumb.displaySize);
- return clickPosition / totalHeight * size.content.height;
- }
- return 0;
- }
- function getTrackDirection(elem) {
- if (isOneOf(elem, [xAxis.element, xAxis.thumb.element])) {
- return Direction.X;
- }
- if (isOneOf(elem, [yAxis.element, yAxis.thumb.element])) {
- return Direction.Y;
- }
- return void 0;
- }
- var isMouseDown;
- var isMouseMoving;
- var startOffsetToThumb;
- var startTrackDirection;
- var containerRect;
- addEvent(container, 'click', function (evt) {
- if (isMouseMoving || !isOneOf(evt.target, [xAxis.element, yAxis.element])) {
- return;
- }
- var track = evt.target;
- var direction = getTrackDirection(track);
- var rect = track.getBoundingClientRect();
- var clickPos = getPosition(evt);
- var offset = scrollbar.offset, limit = scrollbar.limit;
- if (direction === Direction.X) {
- var offsetOnTrack = clickPos.x - rect.left - xAxis.thumb.displaySize / 2;
- scrollbar.setMomentum(clamp(calcOffset(direction, offsetOnTrack) - offset.x, -offset.x, limit.x - offset.x), 0);
- }
- if (direction === Direction.Y) {
- var offsetOnTrack = clickPos.y - rect.top - yAxis.thumb.displaySize / 2;
- scrollbar.setMomentum(0, clamp(calcOffset(direction, offsetOnTrack) - offset.y, -offset.y, limit.y - offset.y));
- }
- });
- addEvent(container, 'mousedown', function (evt) {
- if (!isOneOf(evt.target, [xAxis.thumb.element, yAxis.thumb.element])) {
- return;
- }
- isMouseDown = true;
- var thumb = evt.target;
- var cursorPos = getPosition(evt);
- var thumbRect = thumb.getBoundingClientRect();
- startTrackDirection = getTrackDirection(thumb);
- // pointer offset to thumb
- startOffsetToThumb = {
- x: cursorPos.x - thumbRect.left,
- y: cursorPos.y - thumbRect.top,
- };
- // container bounding rectangle
- containerRect = container.getBoundingClientRect();
- // prevent selection, see:
- // https://github.com/idiotWu/smooth-scrollbar/issues/48
- setStyle(scrollbar.containerEl, {
- '-user-select': 'none',
- });
- });
- addEvent(window, 'mousemove', function (evt) {
- if (!isMouseDown)
- return;
- isMouseMoving = true;
- var offset = scrollbar.offset;
- var cursorPos = getPosition(evt);
- if (startTrackDirection === Direction.X) {
- // get percentage of pointer position in track
- // then tranform to px
- // don't need easing
- var offsetOnTrack = cursorPos.x - startOffsetToThumb.x - containerRect.left;
- scrollbar.setPosition(calcOffset(startTrackDirection, offsetOnTrack), offset.y);
- }
- if (startTrackDirection === Direction.Y) {
- var offsetOnTrack = cursorPos.y - startOffsetToThumb.y - containerRect.top;
- scrollbar.setPosition(offset.x, calcOffset(startTrackDirection, offsetOnTrack));
- }
- });
- addEvent(window, 'mouseup blur', function () {
- isMouseDown = isMouseMoving = false;
- setStyle(scrollbar.containerEl, {
- '-user-select': '',
- });
- });
- }
- //# sourceMappingURL=mouse.js.map
|