swiper-controller.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. (function (factory) {
  2. if (typeof module === "object" && typeof module.exports === "object") {
  3. var v = factory(require, exports);
  4. if (v !== undefined) module.exports = v;
  5. }
  6. else if (typeof define === "function" && define.amd) {
  7. define(["require", "exports", "./swiper", "./swiper-index", "./swiper-utils", "./swiper-progress"], factory);
  8. }
  9. })(function (require, exports) {
  10. "use strict";
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. var swiper_1 = require("./swiper");
  13. var swiper_index_1 = require("./swiper-index");
  14. var swiper_utils_1 = require("./swiper-utils");
  15. var swiper_progress_1 = require("./swiper-progress");
  16. /*=========================
  17. Controller
  18. ===========================*/
  19. exports.SWIPER_CONTROLLER = {
  20. LinearSpline: function (_s, _platform, x, y) {
  21. this.x = x;
  22. this.y = y;
  23. this.lastIndex = x.length - 1;
  24. // Given an x value (x2), return the expected y2 value:
  25. // (x1,y1) is the known point before given value,
  26. // (x3,y3) is the known point after given value.
  27. var i1, i3;
  28. this.interpolate = function (x2) {
  29. if (!x2)
  30. return 0;
  31. // Get the indexes of x1 and x3 (the array indexes before and after given x2):
  32. i3 = binarySearch(this.x, x2);
  33. i1 = i3 - 1;
  34. // We have our indexes i1 & i3, so we can calculate already:
  35. // y2 := ((x2−x1) × (y3−y1)) ÷ (x3−x1) + y1
  36. return ((x2 - this.x[i1]) * (this.y[i3] - this.y[i1])) / (this.x[i3] - this.x[i1]) + this.y[i1];
  37. };
  38. var binarySearch = (function () {
  39. var maxIndex, minIndex, guess;
  40. return function (array, val) {
  41. minIndex = -1;
  42. maxIndex = array.length;
  43. while (maxIndex - minIndex > 1)
  44. if (array[guess = maxIndex + minIndex >> 1] <= val) {
  45. minIndex = guess;
  46. }
  47. else {
  48. maxIndex = guess;
  49. }
  50. return maxIndex;
  51. };
  52. })();
  53. },
  54. // xxx: for now i will just save one spline function to to
  55. getInterpolateFunction: function (s, plt, c) {
  56. if (!s._spline)
  57. s._spline = s.loop ?
  58. new exports.SWIPER_CONTROLLER.LinearSpline(s, plt, s._slidesGrid, c._slidesGrid) :
  59. new exports.SWIPER_CONTROLLER.LinearSpline(s, plt, s._snapGrid, c._snapGrid);
  60. },
  61. setTranslate: function (s, plt, translate, byController, setWrapperTranslate) {
  62. var controlled = s.control;
  63. var multiplier, controlledTranslate;
  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. translate = c._rtl && swiper_utils_1.isHorizontal(c) ? -s._translate : s._translate;
  70. if (s.controlBy === 'slide') {
  71. exports.SWIPER_CONTROLLER.getInterpolateFunction(s, plt, c);
  72. // i am not sure why the values have to be multiplicated this way, tried to invert the snapGrid
  73. // but it did not work out
  74. controlledTranslate = -s._spline.interpolate(-translate);
  75. }
  76. if (!controlledTranslate || s.controlBy === 'container') {
  77. multiplier = (swiper_utils_1.maxTranslate(c) - swiper_utils_1.minTranslate(c)) / (swiper_utils_1.maxTranslate(s) - swiper_utils_1.minTranslate(s));
  78. controlledTranslate = (translate - swiper_utils_1.minTranslate(s)) * multiplier + swiper_utils_1.minTranslate(c);
  79. }
  80. if (s.controlInverse) {
  81. controlledTranslate = swiper_utils_1.maxTranslate(c) - controlledTranslate;
  82. }
  83. swiper_progress_1.updateProgress(c, controlledTranslate);
  84. setWrapperTranslate(c, plt, controlledTranslate, false, s);
  85. swiper_index_1.updateActiveIndex(c);
  86. }
  87. if (Array.isArray(controlled)) {
  88. for (var i = 0; i < controlled.length; i++) {
  89. if (controlled[i] !== byController) {
  90. setControlledTranslate(controlled[i]);
  91. }
  92. }
  93. }
  94. else if (byController !== controlled) {
  95. setControlledTranslate(controlled);
  96. }
  97. },
  98. setTransition: function (s, plt, duration, byController, setWrapperTransition) {
  99. var controlled = s.control;
  100. var i;
  101. function setControlledTransition(c) {
  102. setWrapperTransition(c, plt, duration, s);
  103. if (duration !== 0) {
  104. swiper_1.onTransitionStart(c);
  105. plt.transitionEnd(c._wrapper, function () {
  106. if (!controlled)
  107. return;
  108. if (c.loop && s.controlBy === 'slide') {
  109. swiper_1.fixLoop(c, plt);
  110. }
  111. swiper_1.onTransitionEnd(c, plt);
  112. });
  113. }
  114. }
  115. if (Array.isArray(controlled)) {
  116. for (i = 0; i < controlled.length; i++) {
  117. if (controlled[i] !== byController) {
  118. setControlledTransition(controlled[i]);
  119. }
  120. }
  121. }
  122. else if (byController !== controlled) {
  123. setControlledTransition(controlled);
  124. }
  125. }
  126. };
  127. });
  128. //# sourceMappingURL=swiper-controller.js.map