Skip to content

Commit 86e243d

Browse files
feat(planet): implement progressive loading for project cards
1 parent 9c9732c commit 86e243d

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

planet/js/GlobalPlanet.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,25 +245,35 @@ class GlobalPlanet {
245245
} else if (l === this.page + 1) {
246246
this.downloadProjectsToCache(
247247
toDownload,
248+
function (id) {
249+
this.renderSingleProject(id);
250+
}.bind(this),
248251
function () {
249-
this.render(data);
252+
this.hideLoading();
250253
this.showLoadMore();
254+
this.index += this.page;
251255
}.bind(this)
252256
);
253257
} else {
254258
this.downloadProjectsToCache(
255259
toDownload,
260+
function (id) {
261+
this.renderSingleProject(id);
262+
}.bind(this),
256263
function () {
257-
this.render(data);
264+
this.hideLoading();
258265
this.hideLoadMore();
266+
this.index += this.page;
259267
}.bind(this)
260268
);
261269
}
262270
}
263271

264-
downloadProjectsToCache(data, callback) {
272+
downloadProjectsToCache(data, onEachComplete, onAllComplete) {
265273
const Planet = this.Planet;
266274
this.loadCount = data.length;
275+
this.loadedCount = 0;
276+
const totalCount = data.length;
267277

268278
for (let i = 0; i < data.length; i++) {
269279
(function () {
@@ -272,28 +282,43 @@ class GlobalPlanet {
272282
id,
273283
function (d) {
274284
const tempid = id;
275-
this.addProjectToCache(tempid, d, callback);
285+
this.addProjectToCache(
286+
tempid,
287+
d,
288+
onEachComplete,
289+
onAllComplete,
290+
totalCount
291+
);
276292
}.bind(this)
277293
);
278-
}.bind(this)());
294+
}).bind(this)();
279295
}
280296
}
281297

282-
addProjectToCache(id, data, callback) {
298+
addProjectToCache(id, data, onEachComplete, onAllComplete, totalCount) {
283299
if (data.success) {
284300
this.cache[id] = data.data;
285301
this.cache[id].ProjectData = null;
286302
this.loadCount -= 1;
303+
this.loadedCount += 1;
287304

288-
if (this.loadCount <= 0) callback();
305+
if (onEachComplete) {
306+
onEachComplete(id);
307+
}
308+
309+
this.updateLoadingProgress(this.loadedCount, totalCount);
310+
311+
if (this.loadCount <= 0 && onAllComplete) {
312+
onAllComplete();
313+
}
289314
} else this.throwOfflineError();
290315
}
291316

292317
forceAddToCache(id, callback) {
293318
this.Planet.ServerInterface.getProjectDetails(
294319
id,
295320
function (d) {
296-
this.addProjectToCache(id, d, callback);
321+
this.addProjectToCache(id, d, null, callback, 1);
297322
}.bind(this)
298323
);
299324
}
@@ -375,6 +400,16 @@ class GlobalPlanet {
375400
this.afterAddProjects();
376401
}
377402

403+
renderSingleProject(id) {
404+
if (this.cache.hasOwnProperty(id)) {
405+
const g = new GlobalCard(this.Planet);
406+
g.init(id);
407+
g.render();
408+
this.cards.push(g);
409+
jQuery(".tooltipped").tooltip({ delay: 50 });
410+
}
411+
}
412+
378413
afterAddProjects() {
379414
this.index += this.page;
380415
this.hideLoading();
@@ -408,6 +443,11 @@ class GlobalPlanet {
408443
document.getElementById("global-load").style.display = "block";
409444
}
410445

446+
updateLoadingProgress(loaded, total) {
447+
// Progress is shown by cards appearing incrementally
448+
// The loading spinner remains visible until all projects are loaded
449+
}
450+
411451
hideLoadMore() {
412452
document.getElementById("load-more-projects").style.display = "none";
413453
this.loadButtonShown = false;

0 commit comments

Comments
 (0)