@checksub_team/peaks_timeline 2.3.0-alpha.0 → 2.3.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/view.js CHANGED
@@ -345,46 +345,72 @@ define([
345
345
  * @param {Function} [updateWhileNotScrolling] Called when not scrolling.
346
346
  */
347
347
  View.prototype.updateWithAutoScroll = function(updateWhileScrolling,
348
- updateWhileNotScrolling) {
348
+ updateWhileNotScrolling,
349
+ enableVerticalAutoScroll) {
349
350
  var self = this;
350
351
 
351
352
  var pointer = this.getPointerPosition();
352
353
  var pointerX = pointer ? pointer.x : null;
354
+ var pointerY = pointer ? pointer.y : null;
353
355
  var viewWidth = this.getWidth();
356
+ var viewHeight = this.getHeight();
354
357
  var thresholdPx = Math.round(this._peaks.options.autoScrollThreshold * viewWidth);
358
+ var thresholdPy = Math.round(this._peaks.options.autoScrollThreshold * viewHeight);
355
359
 
356
360
  var MAX_AUTO_SCROLL_PX_PER_FRAME = 30;
357
361
  var NOMINAL_FRAME_MS = 16.67; // ~60fps
358
362
 
359
- function getAutoScrollVelocity(pointerXValue) {
360
- if (typeof pointerXValue !== 'number' || thresholdPx <= 0) {
363
+ function getAutoScrollVelocity(pointerValue, threshold, size) {
364
+ if (typeof pointerValue !== 'number' || threshold <= 0 || size <= 0) {
361
365
  return 0;
362
366
  }
363
367
 
364
- if (pointerXValue < thresholdPx) {
368
+ if (pointerValue < threshold) {
365
369
  return Math.round(
366
- -MAX_AUTO_SCROLL_PX_PER_FRAME * Math.min(1, (thresholdPx - pointerXValue) / thresholdPx)
370
+ -MAX_AUTO_SCROLL_PX_PER_FRAME * Math.min(1, (threshold - pointerValue) / threshold)
367
371
  );
368
372
  }
369
- if (pointerXValue > viewWidth - thresholdPx) {
373
+ if (pointerValue > size - threshold) {
370
374
  return Math.round(
371
375
  MAX_AUTO_SCROLL_PX_PER_FRAME
372
- * Math.min(1, (pointerXValue - (viewWidth - thresholdPx)) / thresholdPx)
376
+ * Math.min(1, (pointerValue - (size - threshold)) / threshold)
373
377
  );
374
378
  }
375
379
  return 0;
376
380
  }
377
381
 
378
- var velocityPxPerFrame = getAutoScrollVelocity(pointerX);
382
+ var velocityXPerFrame = getAutoScrollVelocity(pointerX, thresholdPx, viewWidth);
383
+
384
+ var verticalScrollingEnabled = Boolean(enableVerticalAutoScroll
385
+ && this._peaks.options.enableVerticalScrolling);
386
+
387
+ var maxFrameOffsetY = 0;
388
+
389
+ if (verticalScrollingEnabled) {
390
+ maxFrameOffsetY = this.getFullHeight() - viewHeight;
391
+ if (!Number.isFinite(maxFrameOffsetY) || maxFrameOffsetY < 0) {
392
+ maxFrameOffsetY = 0;
393
+ }
394
+ }
395
+
396
+ var velocityYPerFrame = verticalScrollingEnabled && maxFrameOffsetY > 0 ?
397
+ getAutoScrollVelocity(pointerY, thresholdPy, viewHeight) :
398
+ 0;
379
399
 
380
400
  // For left scroll (negative), only scroll if we can actually move left.
381
401
  // For right scroll (positive), allow scrolling (timeline can extend).
382
- var canScroll = velocityPxPerFrame > 0 || (velocityPxPerFrame < 0 && self.getFrameOffset() > 0);
402
+ var canScrollX = velocityXPerFrame > 0 || (velocityXPerFrame < 0 && self.getFrameOffset() > 0);
403
+ var canScrollY = verticalScrollingEnabled && maxFrameOffsetY > 0 && (
404
+ (velocityYPerFrame > 0 && self.getFrameOffsetY() < maxFrameOffsetY)
405
+ || (velocityYPerFrame < 0 && self.getFrameOffsetY() > 0)
406
+ );
383
407
 
384
- // Keep the current velocity on the instance for debugging/inspection.
385
- this._limited = velocityPxPerFrame;
408
+ // Keep the current velocities on the instance for debugging/inspection.
409
+ this._autoScrollVelocityX = velocityXPerFrame;
410
+ this._autoScrollVelocityY = velocityYPerFrame;
386
411
 
387
- if (velocityPxPerFrame !== 0 && canScroll) {
412
+ if ((velocityXPerFrame !== 0 && canScrollX)
413
+ || (velocityYPerFrame !== 0 && canScrollY)) {
388
414
  if (!this._scrollingRafId) {
389
415
  var lastTime = performance.now();
390
416
 
@@ -393,23 +419,64 @@ define([
393
419
  return;
394
420
  }
395
421
 
422
+ var xVel = self._autoScrollVelocityX || 0;
423
+ var yVel = self._autoScrollVelocityY || 0;
424
+
425
+ var canContinueX = xVel > 0 || (xVel < 0 && self.getFrameOffset() > 0);
426
+ var maxY = 0;
427
+ var canContinueY = false;
428
+
429
+ if (verticalScrollingEnabled) {
430
+ maxY = self.getFullHeight() - self.getHeight();
431
+ if (!Number.isFinite(maxY) || maxY < 0) {
432
+ maxY = 0;
433
+ }
434
+
435
+ canContinueY = maxY > 0 && (
436
+ (yVel > 0 && self.getFrameOffsetY() < maxY)
437
+ || (yVel < 0 && self.getFrameOffsetY() > 0)
438
+ );
439
+ }
440
+
441
+ if ((xVel === 0 || !canContinueX)
442
+ && (yVel === 0 || !canContinueY)) {
443
+ self.stopAutoScroll();
444
+ updateWhileScrolling();
445
+ return;
446
+ }
447
+
396
448
  // Calculate time delta for consistent scroll speed regardless of frame rate
397
449
  var deltaTime = currentTime - lastTime;
398
- var scrollAmount = Math.round(self._limited * deltaTime / NOMINAL_FRAME_MS);
450
+ var scrollAmountX = Math.round(xVel * deltaTime / NOMINAL_FRAME_MS);
451
+ var scrollAmountY = Math.round(yVel * deltaTime / NOMINAL_FRAME_MS);
399
452
 
400
453
  lastTime = currentTime;
401
454
 
402
- var newOffset = self.getFrameOffset() + scrollAmount;
455
+ var newOffsetX = self.getFrameOffset() + scrollAmountX;
456
+ var newOffsetY = self.getFrameOffsetY() + scrollAmountY;
403
457
 
404
- if (newOffset < 0) {
405
- self.updateTimeline(0);
406
- self._scrollingRafId = null;
458
+ if (newOffsetX < 0) {
459
+ newOffsetX = 0;
460
+ }
461
+
462
+ if (verticalScrollingEnabled) {
463
+ if (newOffsetY < 0) {
464
+ newOffsetY = 0;
465
+ }
466
+ else if (newOffsetY > maxY) {
467
+ newOffsetY = maxY;
468
+ }
469
+ }
470
+
471
+ if (!verticalScrollingEnabled) {
472
+ self.updateTimeline(newOffsetX);
407
473
  }
408
474
  else {
409
- self.updateTimeline(newOffset);
410
- updateWhileScrolling();
411
- self._scrollingRafId = requestAnimationFrame(scrollFrame);
475
+ self.updateTimeline(newOffsetX, newOffsetY);
412
476
  }
477
+
478
+ updateWhileScrolling();
479
+ self._scrollingRafId = requestAnimationFrame(scrollFrame);
413
480
  }
414
481
 
415
482
  self._scrollingRafId = requestAnimationFrame(scrollFrame);