Skip to content

Commit 94acfdb

Browse files
Optimization. Adjust number of clones according to the track width or height.
1 parent 2e0e055 commit 94acfdb

File tree

11 files changed

+206
-117
lines changed

11 files changed

+206
-117
lines changed

dist/js/splide.esm.js

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -286,16 +286,17 @@ function _extends() { _extends = Object.assign || function (target) { for (var i
286286
* @author Naotoshi Fujita
287287
* @copyright Naotoshi Fujita. All rights reserved.
288288
*/
289-
289+
var keys = Object.keys;
290290
/**
291291
* Iterate an object like Array.forEach.
292292
* IE doesn't support forEach of HTMLCollection.
293293
*
294294
* @param {Object} obj - An object.
295295
* @param {function} callback - A function handling each value. Arguments are value, property and index.
296296
*/
297+
297298
function each(obj, callback) {
298-
Object.keys(obj).some(function (key, index) {
299+
keys(obj).some(function (key, index) {
299300
return callback(obj[key], key, index);
300301
});
301302
}
@@ -309,7 +310,7 @@ function each(obj, callback) {
309310
*/
310311

311312
function values(obj) {
312-
return Object.keys(obj).map(function (key) {
313+
return keys(obj).map(function (key) {
313314
return obj[key];
314315
});
315316
}
@@ -360,7 +361,7 @@ function merge(_ref, from) {
360361

361362
function object_assign(to, from) {
362363
to._s = from;
363-
Object.keys(from).forEach(function (key) {
364+
keys(from).forEach(function (key) {
364365
if (!to[key]) {
365366
Object.defineProperty(to, key, Object.getOwnPropertyDescriptor(from, key));
366367
}
@@ -501,7 +502,7 @@ function find(elm, selector) {
501502
function child(parent, tagOrClassName) {
502503
if (parent) {
503504
return values(parent.children).filter(function (child) {
504-
return hasClass(child, tagOrClassName.split(' ')[0]) || child.tagName.toLowerCase() === tagOrClassName;
505+
return hasClass(child, tagOrClassName.split(' ')[0]) || child.tagName === tagOrClassName;
505506
})[0] || null;
506507
}
507508

@@ -544,8 +545,9 @@ function domify(html) {
544545

545546
function dom_remove(elms) {
546547
toArray(elms).forEach(function (elm) {
547-
if (elm && elm.parentElement) {
548-
elm.parentElement.removeChild(elm);
548+
if (elm) {
549+
var parent = elm.parentElement;
550+
parent && parent.removeChild(elm);
549551
}
550552
});
551553
}
@@ -569,8 +571,9 @@ function append(parent, child) {
569571
*/
570572

571573
function before(elm, ref) {
572-
if (elm && ref && ref.parentElement) {
573-
ref.parentElement.insertBefore(elm, ref);
574+
if (elm && ref) {
575+
var parent = ref.parentElement;
576+
parent && parent.insertBefore(elm, ref);
574577
}
575578
}
576579
/**
@@ -1987,7 +1990,7 @@ var STYLE_RESTORE_EVENTS = 'update.slide';
19871990
*
19881991
* @type {Element|null}
19891992
*/
1990-
container: find(slide, "." + Splide.classes.container),
1993+
container: child(slide, Splide.classes.container),
19911994

19921995
/**
19931996
* Whether this is a cloned slide or not.
@@ -2017,8 +2020,8 @@ var STYLE_RESTORE_EVENTS = 'update.slide';
20172020
*/
20182021

20192022
if (updateOnMove) {
2020-
Splide.on('move.slide', function () {
2021-
if (Splide.index === realIndex) {
2023+
Splide.on('move.slide', function (newIndex) {
2024+
if (newIndex === realIndex) {
20222025
_update(true, false);
20232026
}
20242027
});
@@ -2214,8 +2217,7 @@ var UID_NAME = 'uid';
22142217
_this.destroy();
22152218

22162219
_this.init();
2217-
});
2218-
Splide.on('updated', function () {
2220+
}).on('updated', function () {
22192221
removeClass(root, getClasses());
22202222
addClass(root, getClasses());
22212223
});
@@ -2311,7 +2313,7 @@ var UID_NAME = 'uid';
23112313
}
23122314

23132315
if (slide instanceof Element) {
2314-
var ref = this.slides[index]; // This will be removed in mount() of Slide component.
2316+
var ref = this.slides[index]; // This will be removed in mount() of a Slide component.
23152317

23162318
applyStyle(slide, {
23172319
display: 'none'
@@ -3176,6 +3178,13 @@ var controller_floor = Math.floor;
31763178
* @type {Array}
31773179
*/
31783180
var clones = [];
3181+
/**
3182+
* Store the current clone count on one side.
3183+
*
3184+
* @type {number}
3185+
*/
3186+
3187+
var cloneCount = 0;
31793188
/**
31803189
* Keep Elements component.
31813190
*
@@ -3194,14 +3203,12 @@ var controller_floor = Math.floor;
31943203
* Called when the component is mounted.
31953204
*/
31963205
mount: function mount() {
3197-
var _this = this;
3198-
31993206
if (Splide.is(LOOP)) {
3200-
generateClones();
3201-
Splide.on('refresh', function () {
3202-
_this.destroy();
3203-
3204-
generateClones();
3207+
init();
3208+
Splide.on('refresh', init).on('resize', function () {
3209+
if (cloneCount !== getCloneCount()) {
3210+
Splide.refresh();
3211+
}
32053212
});
32063213
}
32073214
},
@@ -3233,18 +3240,29 @@ var controller_floor = Math.floor;
32333240
}
32343241

32353242
};
3243+
/**
3244+
* Initialization.
3245+
*/
3246+
3247+
function init() {
3248+
Clones.destroy();
3249+
cloneCount = getCloneCount();
3250+
generateClones(cloneCount);
3251+
}
32363252
/**
32373253
* Generate and append/prepend clones.
3254+
*
3255+
* @param {number} count - The half number of clones.
32383256
*/
32393257

3240-
function generateClones() {
3258+
3259+
function generateClones(count) {
32413260
var length = Elements.length;
32423261

32433262
if (!length) {
32443263
return;
32453264
}
32463265

3247-
var count = getCloneCount();
32483266
var slides = Elements.slides;
32493267

32503268
while (slides.length < count) {
@@ -3283,15 +3301,15 @@ var controller_floor = Math.floor;
32833301

32843302
if (options.clones) {
32853303
return options.clones;
3286-
} // Use the slide length in autoWidth mode because the number candnot be calculated.
3304+
} // Use the slide length in autoWidth mode because the number cannot be calculated.
32873305

32883306

32893307
var baseCount = options.autoWidth ? Elements.length : options.perPage;
32903308
var dimension = options.direction === TTB ? 'Height' : 'Width';
32913309
var fixedSize = options["fixed" + dimension];
32923310

32933311
if (fixedSize) {
3294-
// Roughly determine the count. This needs not to be strict.
3312+
// Roughly calculate the count. This needs not to be strict.
32953313
baseCount = Math.ceil(Elements.track["client" + dimension] / fixedSize);
32963314
}
32973315

@@ -4442,7 +4460,7 @@ var PAUSE_FLAGS = {
44424460

44434461
function apply(uncover) {
44444462
Components.Elements.each(function (Slide) {
4445-
var img = child(Slide.slide, 'img') || child(Slide.container, 'img');
4463+
var img = child(Slide.slide, 'IMG') || child(Slide.container, 'IMG');
44464464

44474465
if (img && img.src) {
44484466
cover(img, uncover);
@@ -5220,6 +5238,13 @@ var TAB_INDEX = 'tabindex';
52205238
*/
52215239

52225240
var Elements = Components.Elements;
5241+
/**
5242+
* All attributes related with A11y.
5243+
*
5244+
* @type {string[]}
5245+
*/
5246+
5247+
var allAttributes = [ARIA_HIDDEN, TAB_INDEX, ARIA_CONTROLS, ARIA_LABEL, ARIA_CURRENRT, 'role'];
52235248
/**
52245249
* A11y component object.
52255250
*
@@ -5242,7 +5267,9 @@ var TAB_INDEX = 'tabindex';
52425267
updateSlide(Slide.slide, true);
52435268
}).on('hidden', function (Slide) {
52445269
updateSlide(Slide.slide, false);
5245-
}).on('arrows:mounted', initArrows).on('arrows:updated', updateArrows).on('pagination:mounted', initPagination).on('pagination:updated', updatePagination);
5270+
}).on('arrows:mounted', initArrows).on('arrows:updated', updateArrows).on('pagination:mounted', initPagination).on('pagination:updated', updatePagination).on('refresh', function () {
5271+
removeAttribute(Components.Clones.clones, allAttributes);
5272+
});
52465273

52475274
if (Splide.options.isNavigation) {
52485275
Splide.on('navigation:mounted', initNavigation).on('active', function (Slide) {
@@ -5259,8 +5286,9 @@ var TAB_INDEX = 'tabindex';
52595286
* Destroy.
52605287
*/
52615288
destroy: function destroy() {
5262-
var arrows = Components.Arrows ? Components.Arrows.arrows : {};
5263-
removeAttribute(Elements.slides.concat([arrows.prev, arrows.next, Elements.play, Elements.pause]), [ARIA_HIDDEN, TAB_INDEX, ARIA_CONTROLS, ARIA_LABEL, ARIA_CURRENRT, 'role']);
5289+
var Arrows = Components.Arrows;
5290+
var arrows = Arrows ? Arrows.arrows : {};
5291+
removeAttribute(Elements.slides.concat([arrows.prev, arrows.next, Elements.play, Elements.pause]), allAttributes);
52645292
}
52655293
};
52665294
/**
@@ -5377,16 +5405,15 @@ var TAB_INDEX = 'tabindex';
53775405

53785406

53795407
function initNavigation(main) {
5380-
Elements.each(function (_ref) {
5381-
var slide = _ref.slide,
5382-
realIndex = _ref.realIndex,
5383-
index = _ref.index;
5408+
Elements.each(function (Slide) {
5409+
var slide = Slide.slide;
5410+
var realIndex = Slide.realIndex;
53845411

53855412
if (!isButton(slide)) {
53865413
setAttribute(slide, 'role', 'button');
53875414
}
53885415

5389-
var slideIndex = realIndex > -1 ? realIndex : index;
5416+
var slideIndex = realIndex > -1 ? realIndex : Slide.index;
53905417
var label = sprintf(i18n.slideX, slideIndex + 1);
53915418
var mainSlide = main.Components.Elements.getSlide(slideIndex);
53925419
setAttribute(slide, ARIA_LABEL, label);
@@ -5404,8 +5431,8 @@ var TAB_INDEX = 'tabindex';
54045431
*/
54055432

54065433

5407-
function updateNavigation(_ref2, active) {
5408-
var slide = _ref2.slide;
5434+
function updateNavigation(_ref, active) {
5435+
var slide = _ref.slide;
54095436

54105437
if (active) {
54115438
setAttribute(slide, ARIA_CURRENRT, true);
@@ -5423,7 +5450,7 @@ var TAB_INDEX = 'tabindex';
54235450

54245451

54255452
function isButton(elm) {
5426-
return elm.tagName.toLowerCase() === 'button';
5453+
return elm.tagName === 'BUTTON';
54275454
}
54285455

54295456
return A11y;

0 commit comments

Comments
 (0)