controller.js 5.5 KB

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