UNPKG

6.58 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6var _utils = require("../../utils/utils");
7
8function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
9
10var Controller = {
11 LinearSpline: function LinearSpline(x, y) {
12 var binarySearch = function search() {
13 var maxIndex;
14 var minIndex;
15 var guess;
16 return function (array, val) {
17 minIndex = -1;
18 maxIndex = array.length;
19
20 while (maxIndex - minIndex > 1) {
21 guess = maxIndex + minIndex >> 1;
22
23 if (array[guess] <= val) {
24 minIndex = guess;
25 } else {
26 maxIndex = guess;
27 }
28 }
29
30 return maxIndex;
31 };
32 }();
33
34 this.x = x;
35 this.y = y;
36 this.lastIndex = x.length - 1; // Given an x value (x2), return the expected y2 value:
37 // (x1,y1) is the known point before given value,
38 // (x3,y3) is the known point after given value.
39
40 var i1;
41 var i3;
42
43 this.interpolate = function interpolate(x2) {
44 if (!x2) return 0; // Get the indexes of x1 and x3 (the array indexes before and after given x2):
45
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
50 return (x2 - this.x[i1]) * (this.y[i3] - this.y[i1]) / (this.x[i3] - this.x[i1]) + this.y[i1];
51 };
52
53 return this;
54 },
55 // xxx: for now i will just save one spline function to to
56 getInterpolateFunction: function getInterpolateFunction(c) {
57 var swiper = this;
58
59 if (!swiper.controller.spline) {
60 swiper.controller.spline = swiper.params.loop ? new Controller.LinearSpline(swiper.slidesGrid, c.slidesGrid) : new Controller.LinearSpline(swiper.snapGrid, c.snapGrid);
61 }
62 },
63 setTranslate: function setTranslate(_setTranslate, byController) {
64 var swiper = this;
65 var controlled = swiper.controller.control;
66 var multiplier;
67 var controlledTranslate;
68 var Swiper = swiper.constructor;
69
70 function setControlledTranslate(c) {
71 // this will create an Interpolate function based on the snapGrids
72 // x is the Grid of the scrolled scroller and y will be the controlled scroller
73 // it makes sense to create this only once and recall it for the interpolation
74 // the function does a lot of value caching for performance
75 var translate = swiper.rtlTranslate ? -swiper.translate : swiper.translate;
76
77 if (swiper.params.controller.by === 'slide') {
78 swiper.controller.getInterpolateFunction(c); // i am not sure why the values have to be multiplicated this way, tried to invert the snapGrid
79 // but it did not work out
80
81 controlledTranslate = -swiper.controller.spline.interpolate(-translate);
82 }
83
84 if (!controlledTranslate || swiper.params.controller.by === 'container') {
85 multiplier = (c.maxTranslate() - c.minTranslate()) / (swiper.maxTranslate() - swiper.minTranslate());
86 controlledTranslate = (translate - swiper.minTranslate()) * multiplier + c.minTranslate();
87 }
88
89 if (swiper.params.controller.inverse) {
90 controlledTranslate = c.maxTranslate() - controlledTranslate;
91 }
92
93 c.updateProgress(controlledTranslate);
94 c.setTranslate(controlledTranslate, swiper);
95 c.updateActiveIndex();
96 c.updateSlidesClasses();
97 }
98
99 if (Array.isArray(controlled)) {
100 for (var i = 0; i < controlled.length; i += 1) {
101 if (controlled[i] !== byController && controlled[i] instanceof Swiper) {
102 setControlledTranslate(controlled[i]);
103 }
104 }
105 } else if (controlled instanceof Swiper && byController !== controlled) {
106 setControlledTranslate(controlled);
107 }
108 },
109 setTransition: function setTransition(duration, byController) {
110 var swiper = this;
111 var Swiper = swiper.constructor;
112 var controlled = swiper.controller.control;
113 var i;
114
115 function setControlledTransition(c) {
116 c.setTransition(duration, swiper);
117
118 if (duration !== 0) {
119 c.transitionStart();
120
121 if (c.params.autoHeight) {
122 (0, _utils.nextTick)(function () {
123 c.updateAutoHeight();
124 });
125 }
126
127 c.$wrapperEl.transitionEnd(function () {
128 if (!controlled) return;
129
130 if (c.params.loop && swiper.params.controller.by === 'slide') {
131 c.loopFix();
132 }
133
134 c.transitionEnd();
135 });
136 }
137 }
138
139 if (Array.isArray(controlled)) {
140 for (i = 0; i < controlled.length; i += 1) {
141 if (controlled[i] !== byController && controlled[i] instanceof Swiper) {
142 setControlledTransition(controlled[i]);
143 }
144 }
145 } else if (controlled instanceof Swiper && byController !== controlled) {
146 setControlledTransition(controlled);
147 }
148 }
149};
150var _default = {
151 name: 'controller',
152 params: {
153 controller: {
154 control: undefined,
155 inverse: false,
156 by: 'slide' // or 'container'
157
158 }
159 },
160 create: function create() {
161 var swiper = this;
162 (0, _utils.bindModuleMethods)(swiper, {
163 controller: _extends({
164 control: swiper.params.controller.control
165 }, Controller)
166 });
167 },
168 on: {
169 update: function update(swiper) {
170 if (!swiper.controller.control) return;
171
172 if (swiper.controller.spline) {
173 swiper.controller.spline = undefined;
174 delete swiper.controller.spline;
175 }
176 },
177 resize: function resize(swiper) {
178 if (!swiper.controller.control) return;
179
180 if (swiper.controller.spline) {
181 swiper.controller.spline = undefined;
182 delete swiper.controller.spline;
183 }
184 },
185 observerUpdate: function observerUpdate(swiper) {
186 if (!swiper.controller.control) return;
187
188 if (swiper.controller.spline) {
189 swiper.controller.spline = undefined;
190 delete swiper.controller.spline;
191 }
192 },
193 setTranslate: function setTranslate(swiper, translate, byController) {
194 if (!swiper.controller.control) return;
195 swiper.controller.setTranslate(translate, byController);
196 },
197 setTransition: function setTransition(swiper, duration, byController) {
198 if (!swiper.controller.control) return;
199 swiper.controller.setTransition(duration, byController);
200 }
201 }
202};
203exports.default = _default;
\No newline at end of file