controller.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* eslint no-bitwise: ["error", { "allow": [">>"] }] */
  2. import { nextTick } from '../../shared/utils.js';
  3. export default function Controller(_ref) {
  4. let {
  5. swiper,
  6. extendParams,
  7. on
  8. } = _ref;
  9. extendParams({
  10. controller: {
  11. control: undefined,
  12. inverse: false,
  13. by: 'slide' // or 'container'
  14. }
  15. });
  16. swiper.controller = {
  17. control: undefined
  18. };
  19. function LinearSpline(x, y) {
  20. const binarySearch = function search() {
  21. let maxIndex;
  22. let minIndex;
  23. let guess;
  24. return (array, val) => {
  25. minIndex = -1;
  26. maxIndex = array.length;
  27. while (maxIndex - minIndex > 1) {
  28. guess = maxIndex + minIndex >> 1;
  29. if (array[guess] <= val) {
  30. minIndex = guess;
  31. } else {
  32. maxIndex = guess;
  33. }
  34. }
  35. return maxIndex;
  36. };
  37. }();
  38. this.x = x;
  39. this.y = y;
  40. this.lastIndex = x.length - 1; // Given an x value (x2), return the expected y2 value:
  41. // (x1,y1) is the known point before given value,
  42. // (x3,y3) is the known point after given value.
  43. let i1;
  44. let i3;
  45. this.interpolate = function interpolate(x2) {
  46. if (!x2) return 0; // Get the indexes of x1 and x3 (the array indexes before and after given x2):
  47. i3 = binarySearch(this.x, x2);
  48. i1 = i3 - 1; // We have our indexes i1 & i3, so we can calculate already:
  49. // y2 := ((x2−x1) × (y3−y1)) ÷ (x3−x1) + y1
  50. return (x2 - this.x[i1]) * (this.y[i3] - this.y[i1]) / (this.x[i3] - this.x[i1]) + this.y[i1];
  51. };
  52. return this;
  53. } // xxx: for now i will just save one spline function to to
  54. function getInterpolateFunction(c) {
  55. if (!swiper.controller.spline) {
  56. swiper.controller.spline = swiper.params.loop ? new LinearSpline(swiper.slidesGrid, c.slidesGrid) : new LinearSpline(swiper.snapGrid, c.snapGrid);
  57. }
  58. }
  59. function setTranslate(_t, byController) {
  60. const controlled = swiper.controller.control;
  61. let multiplier;
  62. let controlledTranslate;
  63. const Swiper = swiper.constructor;
  64. function setControlledTranslate(c) {
  65. // this will create an Interpolate function based on the snapGrids
  66. // x is the Grid of the scrolled scroller and y will be the controlled scroller
  67. // it makes sense to create this only once and recall it for the interpolation
  68. // the function does a lot of value caching for performance
  69. const translate = swiper.rtlTranslate ? -swiper.translate : swiper.translate;
  70. if (swiper.params.controller.by === 'slide') {
  71. getInterpolateFunction(c); // i am not sure why the values have to be multiplicated this way, tried to invert the snapGrid
  72. // but it did not work out
  73. controlledTranslate = -swiper.controller.spline.interpolate(-translate);
  74. }
  75. if (!controlledTranslate || swiper.params.controller.by === 'container') {
  76. multiplier = (c.maxTranslate() - c.minTranslate()) / (swiper.maxTranslate() - swiper.minTranslate());
  77. controlledTranslate = (translate - swiper.minTranslate()) * multiplier + c.minTranslate();
  78. }
  79. if (swiper.params.controller.inverse) {
  80. controlledTranslate = c.maxTranslate() - controlledTranslate;
  81. }
  82. c.updateProgress(controlledTranslate);
  83. c.setTranslate(controlledTranslate, swiper);
  84. c.updateActiveIndex();
  85. c.updateSlidesClasses();
  86. }
  87. if (Array.isArray(controlled)) {
  88. for (let i = 0; i < controlled.length; i += 1) {
  89. if (controlled[i] !== byController && controlled[i] instanceof Swiper) {
  90. setControlledTranslate(controlled[i]);
  91. }
  92. }
  93. } else if (controlled instanceof Swiper && byController !== controlled) {
  94. setControlledTranslate(controlled);
  95. }
  96. }
  97. function setTransition(duration, byController) {
  98. const Swiper = swiper.constructor;
  99. const controlled = swiper.controller.control;
  100. let i;
  101. function setControlledTransition(c) {
  102. c.setTransition(duration, swiper);
  103. if (duration !== 0) {
  104. c.transitionStart();
  105. if (c.params.autoHeight) {
  106. nextTick(() => {
  107. c.updateAutoHeight();
  108. });
  109. }
  110. c.$wrapperEl.transitionEnd(() => {
  111. if (!controlled) return;
  112. if (c.params.loop && swiper.params.controller.by === 'slide') {
  113. c.loopFix();
  114. }
  115. c.transitionEnd();
  116. });
  117. }
  118. }
  119. if (Array.isArray(controlled)) {
  120. for (i = 0; i < controlled.length; i += 1) {
  121. if (controlled[i] !== byController && controlled[i] instanceof Swiper) {
  122. setControlledTransition(controlled[i]);
  123. }
  124. }
  125. } else if (controlled instanceof Swiper && byController !== controlled) {
  126. setControlledTransition(controlled);
  127. }
  128. }
  129. function removeSpline() {
  130. if (!swiper.controller.control) return;
  131. if (swiper.controller.spline) {
  132. swiper.controller.spline = undefined;
  133. delete swiper.controller.spline;
  134. }
  135. }
  136. on('beforeInit', () => {
  137. swiper.controller.control = swiper.params.controller.control;
  138. });
  139. on('update', () => {
  140. removeSpline();
  141. });
  142. on('resize', () => {
  143. removeSpline();
  144. });
  145. on('observerUpdate', () => {
  146. removeSpline();
  147. });
  148. on('setTranslate', (_s, translate, byController) => {
  149. if (!swiper.controller.control) return;
  150. swiper.controller.setTranslate(translate, byController);
  151. });
  152. on('setTransition', (_s, duration, byController) => {
  153. if (!swiper.controller.control) return;
  154. swiper.controller.setTransition(duration, byController);
  155. });
  156. Object.assign(swiper.controller, {
  157. setTranslate,
  158. setTransition
  159. });
  160. }