UNPKG

8.9 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6var _dom = _interopRequireDefault(require("../../utils/dom"));
7
8var _utils = require("../../utils/utils");
9
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12function _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); }
13
14var Virtual = {
15 update: function update(force) {
16 var swiper = this;
17 var _swiper$params = swiper.params,
18 slidesPerView = _swiper$params.slidesPerView,
19 slidesPerGroup = _swiper$params.slidesPerGroup,
20 centeredSlides = _swiper$params.centeredSlides;
21 var _swiper$params$virtua = swiper.params.virtual,
22 addSlidesBefore = _swiper$params$virtua.addSlidesBefore,
23 addSlidesAfter = _swiper$params$virtua.addSlidesAfter;
24 var _swiper$virtual = swiper.virtual,
25 previousFrom = _swiper$virtual.from,
26 previousTo = _swiper$virtual.to,
27 slides = _swiper$virtual.slides,
28 previousSlidesGrid = _swiper$virtual.slidesGrid,
29 renderSlide = _swiper$virtual.renderSlide,
30 previousOffset = _swiper$virtual.offset;
31 swiper.updateActiveIndex();
32 var activeIndex = swiper.activeIndex || 0;
33 var offsetProp;
34 if (swiper.rtlTranslate) offsetProp = 'right';else offsetProp = swiper.isHorizontal() ? 'left' : 'top';
35 var slidesAfter;
36 var slidesBefore;
37
38 if (centeredSlides) {
39 slidesAfter = Math.floor(slidesPerView / 2) + slidesPerGroup + addSlidesAfter;
40 slidesBefore = Math.floor(slidesPerView / 2) + slidesPerGroup + addSlidesBefore;
41 } else {
42 slidesAfter = slidesPerView + (slidesPerGroup - 1) + addSlidesAfter;
43 slidesBefore = slidesPerGroup + addSlidesBefore;
44 }
45
46 var from = Math.max((activeIndex || 0) - slidesBefore, 0);
47 var to = Math.min((activeIndex || 0) + slidesAfter, slides.length - 1);
48 var offset = (swiper.slidesGrid[from] || 0) - (swiper.slidesGrid[0] || 0);
49 (0, _utils.extend)(swiper.virtual, {
50 from: from,
51 to: to,
52 offset: offset,
53 slidesGrid: swiper.slidesGrid
54 });
55
56 function onRendered() {
57 swiper.updateSlides();
58 swiper.updateProgress();
59 swiper.updateSlidesClasses();
60
61 if (swiper.lazy && swiper.params.lazy.enabled) {
62 swiper.lazy.load();
63 }
64 }
65
66 if (previousFrom === from && previousTo === to && !force) {
67 if (swiper.slidesGrid !== previousSlidesGrid && offset !== previousOffset) {
68 swiper.slides.css(offsetProp, offset + "px");
69 }
70
71 swiper.updateProgress();
72 return;
73 }
74
75 if (swiper.params.virtual.renderExternal) {
76 swiper.params.virtual.renderExternal.call(swiper, {
77 offset: offset,
78 from: from,
79 to: to,
80 slides: function getSlides() {
81 var slidesToRender = [];
82
83 for (var i = from; i <= to; i += 1) {
84 slidesToRender.push(slides[i]);
85 }
86
87 return slidesToRender;
88 }()
89 });
90
91 if (swiper.params.virtual.renderExternalUpdate) {
92 onRendered();
93 }
94
95 return;
96 }
97
98 var prependIndexes = [];
99 var appendIndexes = [];
100
101 if (force) {
102 swiper.$wrapperEl.find("." + swiper.params.slideClass).remove();
103 } else {
104 for (var i = previousFrom; i <= previousTo; i += 1) {
105 if (i < from || i > to) {
106 swiper.$wrapperEl.find("." + swiper.params.slideClass + "[data-swiper-slide-index=\"" + i + "\"]").remove();
107 }
108 }
109 }
110
111 for (var _i = 0; _i < slides.length; _i += 1) {
112 if (_i >= from && _i <= to) {
113 if (typeof previousTo === 'undefined' || force) {
114 appendIndexes.push(_i);
115 } else {
116 if (_i > previousTo) appendIndexes.push(_i);
117 if (_i < previousFrom) prependIndexes.push(_i);
118 }
119 }
120 }
121
122 appendIndexes.forEach(function (index) {
123 swiper.$wrapperEl.append(renderSlide(slides[index], index));
124 });
125 prependIndexes.sort(function (a, b) {
126 return b - a;
127 }).forEach(function (index) {
128 swiper.$wrapperEl.prepend(renderSlide(slides[index], index));
129 });
130 swiper.$wrapperEl.children('.swiper-slide').css(offsetProp, offset + "px");
131 onRendered();
132 },
133 renderSlide: function renderSlide(slide, index) {
134 var swiper = this;
135 var params = swiper.params.virtual;
136
137 if (params.cache && swiper.virtual.cache[index]) {
138 return swiper.virtual.cache[index];
139 }
140
141 var $slideEl = params.renderSlide ? (0, _dom.default)(params.renderSlide.call(swiper, slide, index)) : (0, _dom.default)("<div class=\"" + swiper.params.slideClass + "\" data-swiper-slide-index=\"" + index + "\">" + slide + "</div>");
142 if (!$slideEl.attr('data-swiper-slide-index')) $slideEl.attr('data-swiper-slide-index', index);
143 if (params.cache) swiper.virtual.cache[index] = $slideEl;
144 return $slideEl;
145 },
146 appendSlide: function appendSlide(slides) {
147 var swiper = this;
148
149 if (typeof slides === 'object' && 'length' in slides) {
150 for (var i = 0; i < slides.length; i += 1) {
151 if (slides[i]) swiper.virtual.slides.push(slides[i]);
152 }
153 } else {
154 swiper.virtual.slides.push(slides);
155 }
156
157 swiper.virtual.update(true);
158 },
159 prependSlide: function prependSlide(slides) {
160 var swiper = this;
161 var activeIndex = swiper.activeIndex;
162 var newActiveIndex = activeIndex + 1;
163 var numberOfNewSlides = 1;
164
165 if (Array.isArray(slides)) {
166 for (var i = 0; i < slides.length; i += 1) {
167 if (slides[i]) swiper.virtual.slides.unshift(slides[i]);
168 }
169
170 newActiveIndex = activeIndex + slides.length;
171 numberOfNewSlides = slides.length;
172 } else {
173 swiper.virtual.slides.unshift(slides);
174 }
175
176 if (swiper.params.virtual.cache) {
177 var cache = swiper.virtual.cache;
178 var newCache = {};
179 Object.keys(cache).forEach(function (cachedIndex) {
180 var $cachedEl = cache[cachedIndex];
181 var cachedElIndex = $cachedEl.attr('data-swiper-slide-index');
182
183 if (cachedElIndex) {
184 $cachedEl.attr('data-swiper-slide-index', parseInt(cachedElIndex, 10) + 1);
185 }
186
187 newCache[parseInt(cachedIndex, 10) + numberOfNewSlides] = $cachedEl;
188 });
189 swiper.virtual.cache = newCache;
190 }
191
192 swiper.virtual.update(true);
193 swiper.slideTo(newActiveIndex, 0);
194 },
195 removeSlide: function removeSlide(slidesIndexes) {
196 var swiper = this;
197 if (typeof slidesIndexes === 'undefined' || slidesIndexes === null) return;
198 var activeIndex = swiper.activeIndex;
199
200 if (Array.isArray(slidesIndexes)) {
201 for (var i = slidesIndexes.length - 1; i >= 0; i -= 1) {
202 swiper.virtual.slides.splice(slidesIndexes[i], 1);
203
204 if (swiper.params.virtual.cache) {
205 delete swiper.virtual.cache[slidesIndexes[i]];
206 }
207
208 if (slidesIndexes[i] < activeIndex) activeIndex -= 1;
209 activeIndex = Math.max(activeIndex, 0);
210 }
211 } else {
212 swiper.virtual.slides.splice(slidesIndexes, 1);
213
214 if (swiper.params.virtual.cache) {
215 delete swiper.virtual.cache[slidesIndexes];
216 }
217
218 if (slidesIndexes < activeIndex) activeIndex -= 1;
219 activeIndex = Math.max(activeIndex, 0);
220 }
221
222 swiper.virtual.update(true);
223 swiper.slideTo(activeIndex, 0);
224 },
225 removeAllSlides: function removeAllSlides() {
226 var swiper = this;
227 swiper.virtual.slides = [];
228
229 if (swiper.params.virtual.cache) {
230 swiper.virtual.cache = {};
231 }
232
233 swiper.virtual.update(true);
234 swiper.slideTo(0, 0);
235 }
236};
237var _default = {
238 name: 'virtual',
239 params: {
240 virtual: {
241 enabled: false,
242 slides: [],
243 cache: true,
244 renderSlide: null,
245 renderExternal: null,
246 renderExternalUpdate: true,
247 addSlidesBefore: 0,
248 addSlidesAfter: 0
249 }
250 },
251 create: function create() {
252 var swiper = this;
253 (0, _utils.bindModuleMethods)(swiper, {
254 virtual: _extends({}, Virtual, {
255 slides: swiper.params.virtual.slides,
256 cache: {}
257 })
258 });
259 },
260 on: {
261 beforeInit: function beforeInit(swiper) {
262 if (!swiper.params.virtual.enabled) return;
263 swiper.classNames.push(swiper.params.containerModifierClass + "virtual");
264 var overwriteParams = {
265 watchSlidesProgress: true
266 };
267 (0, _utils.extend)(swiper.params, overwriteParams);
268 (0, _utils.extend)(swiper.originalParams, overwriteParams);
269
270 if (!swiper.params.initialSlide) {
271 swiper.virtual.update();
272 }
273 },
274 setTranslate: function setTranslate(swiper) {
275 if (!swiper.params.virtual.enabled) return;
276 swiper.virtual.update();
277 }
278 }
279};
280exports.default = _default;
\No newline at end of file