Skip to content

Commit 79debbb

Browse files
authored
refactor redundant calls to get slide element index in gridview (#103)
1 parent 7953c4e commit 79debbb

2 files changed

Lines changed: 21 additions & 20 deletions

File tree

photomap/frontend/static/javascript/grid-view.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ class GridViewManager {
7676
}
7777

7878
initializeGridSwiper() {
79-
8079
this.gridInitialized = false;
8180
showSpinner();
8281
eventRegistry.removeAll("grid");
@@ -131,6 +130,12 @@ class GridViewManager {
131130
window.swiper = this.swiper; // for debugging
132131
}
133132

133+
getIndexForSlideElement(slideEl) {
134+
const globalIndex = parseInt(slideEl.dataset.globalIndex, 10);
135+
if (isNaN(globalIndex)) return null;
136+
return globalIndex;
137+
}
138+
134139
addGridEventListeners() {
135140
eventRegistry.install(
136141
{ type: "grid", event: "swiperModeChanged" },
@@ -199,17 +204,15 @@ class GridViewManager {
199204

200205
if (this.swiper) {
201206
this.swiper.on("slideNextTransitionStart", async () => {
202-
showSpinner();
203-
await this.waitForBatchLoadingToFinish();
207+
if (this.suppressSlideChange) return;
204208
const slidesLeft =
205209
Math.floor(this.swiper.slides.length / this.currentRows) -
206210
this.swiper.activeIndex;
207211
if (slidesLeft <= this.currentColumns) {
212+
showSpinner();
208213
const lastSlideIndex =
209-
parseInt(
210-
this.swiper.slides[this.swiper.slides.length - 1].dataset
211-
.globalIndex,
212-
10
214+
this.getIndexForSlideElement(
215+
this.swiper.slides[this.swiper.slides.length - 1]
213216
) || 0;
214217
const index = slideState.isSearchMode
215218
? slideState.globalToSearch(lastSlideIndex) + 1
@@ -229,16 +232,13 @@ class GridViewManager {
229232

230233
this.swiper.on("slidePrevTransitionStart", async () => {
231234
if (this.suppressSlideChange) return;
232-
await this.waitForBatchLoadingToFinish();
233-
this.setBatchLoading(true);
234-
const firstSlide = parseInt(
235-
this.swiper.slides[0].dataset.globalIndex,
236-
10
237-
);
235+
const firstSlide = this.getIndexForSlideElement(this.swiper.slides[0]);
238236
const index = slideState.isSearchMode
239237
? slideState.globalToSearch(firstSlide)
240238
: firstSlide;
241239
if (firstSlide > 0 && this.swiper.activeIndex === 0) {
240+
await this.waitForBatchLoadingToFinish();
241+
this.setBatchLoading(true);
242242
await this.loadBatch(index - this.slidesPerBatch, false);
243243
}
244244
this.setBatchLoading(false);
@@ -266,10 +266,8 @@ class GridViewManager {
266266
) {
267267
const topLeftSlideEl = this.swiper.slides[activeIndex];
268268
if (topLeftSlideEl) {
269-
const topLeftGlobal = parseInt(
270-
topLeftSlideEl.dataset.globalIndex,
271-
10
272-
);
269+
const topLeftGlobal =
270+
this.getIndexForSlideElement(topLeftSlideEl);
273271
slideState.updateFromExternal(
274272
topLeftGlobal,
275273
slideState.globalToSearch(topLeftGlobal)
@@ -369,8 +367,11 @@ class GridViewManager {
369367
let topLeftIndex =
370368
Math.floor(startIndex / this.slidesPerBatch) * this.slidesPerBatch;
371369

372-
console.log(`Loading batch at index ${topLeftIndex} (${append ? "append" : "prepend"
373-
})`);
370+
console.log(
371+
`Loading batch at index ${topLeftIndex} (${
372+
append ? "append" : "prepend"
373+
})`
374+
);
374375

375376
const slides = [];
376377
let actuallyLoaded = 0;

photomap/frontend/static/javascript/slide-state.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class SlideStateManager {
137137

138138
this.notifySlideChanged();
139139
}
140-
140+
141141
/**
142142
* Exit search mode and return to album browsing
143143
*/

0 commit comments

Comments
 (0)