swiper-controller.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { fixLoop, onTransitionEnd, onTransitionStart } from './swiper';
  2. import { updateActiveIndex } from './swiper-index';
  3. import { isHorizontal, maxTranslate, minTranslate } from './swiper-utils';
  4. import { updateProgress } from './swiper-progress';
  5. /*=========================
  6. Controller
  7. ===========================*/
  8. export const SWIPER_CONTROLLER = {
  9. LinearSpline: function (_s, _platform, x, y) {
  10. this.x = x;
  11. this.y = y;
  12. this.lastIndex = x.length - 1;
  13. // Given an x value (x2), return the expected y2 value:
  14. // (x1,y1) is the known point before given value,
  15. // (x3,y3) is the known point after given value.
  16. var i1, i3;
  17. this.interpolate = function (x2) {
  18. if (!x2)
  19. return 0;
  20. // Get the indexes of x1 and x3 (the array indexes before and after given x2):
  21. i3 = binarySearch(this.x, x2);
  22. i1 = i3 - 1;
  23. // We have our indexes i1 & i3, so we can calculate already:
  24. // y2 := ((x2−x1) × (y3−y1)) ÷ (x3−x1) + y1
  25. return ((x2 - this.x[i1]) * (this.y[i3] - this.y[i1])) / (this.x[i3] - this.x[i1]) + this.y[i1];
  26. };
  27. var binarySearch = (function () {
  28. var maxIndex, minIndex, guess;
  29. return function (array, val) {
  30. minIndex = -1;
  31. maxIndex = array.length;
  32. while (maxIndex - minIndex > 1)
  33. if (array[guess = maxIndex + minIndex >> 1] <= val) {
  34. minIndex = guess;
  35. }
  36. else {
  37. maxIndex = guess;
  38. }
  39. return maxIndex;
  40. };
  41. })();
  42. },
  43. // xxx: for now i will just save one spline function to to
  44. getInterpolateFunction: function (s, plt, c) {
  45. if (!s._spline)
  46. s._spline = s.loop ?
  47. new SWIPER_CONTROLLER.LinearSpline(s, plt, s._slidesGrid, c._slidesGrid) :
  48. new SWIPER_CONTROLLER.LinearSpline(s, plt, s._snapGrid, c._snapGrid);
  49. },
  50. setTranslate: function (s, plt, translate, byController, setWrapperTranslate) {
  51. var controlled = s.control;
  52. var multiplier, controlledTranslate;
  53. function setControlledTranslate(c) {
  54. // this will create an Interpolate function based on the snapGrids
  55. // x is the Grid of the scrolled scroller and y will be the controlled scroller
  56. // it makes sense to create this only once and recall it for the interpolation
  57. // the function does a lot of value caching for performance
  58. translate = c._rtl && isHorizontal(c) ? -s._translate : s._translate;
  59. if (s.controlBy === 'slide') {
  60. SWIPER_CONTROLLER.getInterpolateFunction(s, plt, c);
  61. // i am not sure why the values have to be multiplicated this way, tried to invert the snapGrid
  62. // but it did not work out
  63. controlledTranslate = -s._spline.interpolate(-translate);
  64. }
  65. if (!controlledTranslate || s.controlBy === 'container') {
  66. multiplier = (maxTranslate(c) - minTranslate(c)) / (maxTranslate(s) - minTranslate(s));
  67. controlledTranslate = (translate - minTranslate(s)) * multiplier + minTranslate(c);
  68. }
  69. if (s.controlInverse) {
  70. controlledTranslate = maxTranslate(c) - controlledTranslate;
  71. }
  72. updateProgress(c, controlledTranslate);
  73. setWrapperTranslate(c, plt, controlledTranslate, false, s);
  74. updateActiveIndex(c);
  75. }
  76. if (Array.isArray(controlled)) {
  77. for (var i = 0; i < controlled.length; i++) {
  78. if (controlled[i] !== byController) {
  79. setControlledTranslate(controlled[i]);
  80. }
  81. }
  82. }
  83. else if (byController !== controlled) {
  84. setControlledTranslate(controlled);
  85. }
  86. },
  87. setTransition: function (s, plt, duration, byController, setWrapperTransition) {
  88. var controlled = s.control;
  89. var i;
  90. function setControlledTransition(c) {
  91. setWrapperTransition(c, plt, duration, s);
  92. if (duration !== 0) {
  93. onTransitionStart(c);
  94. plt.transitionEnd(c._wrapper, () => {
  95. if (!controlled)
  96. return;
  97. if (c.loop && s.controlBy === 'slide') {
  98. fixLoop(c, plt);
  99. }
  100. onTransitionEnd(c, plt);
  101. });
  102. }
  103. }
  104. if (Array.isArray(controlled)) {
  105. for (i = 0; i < controlled.length; i++) {
  106. if (controlled[i] !== byController) {
  107. setControlledTransition(controlled[i]);
  108. }
  109. }
  110. }
  111. else if (byController !== controlled) {
  112. setControlledTransition(controlled);
  113. }
  114. }
  115. };
  116. //# sourceMappingURL=swiper-controller.js.map