Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Step 1 - Dividing whole grid in sections and estimating their heights
- Loading branch information
1 parent
c57c91f
commit 7ddef4c
Showing
5 changed files
with
3,732 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| function delay(ms) { | ||
| return function (x) { | ||
| return new Promise(resolve => setTimeout(() => resolve(x), ms)); | ||
| }; | ||
| } | ||
|
|
||
| let sectionStore = fetch("/store.json").then(res => res.json()); | ||
|
|
||
| // get all sections in users photo library - e.g. one section per month | ||
| function getSections() { | ||
| return sectionStore.then(delay(50 + Math.random() * 500)).then(store => { | ||
| return store.map(section => { | ||
| return { sectionId: section.sectionId, totalImages: section.totalImages }; | ||
| }); | ||
| }); | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // app state - list of all sections and their image counts | ||
| var allSections = []; | ||
|
|
||
| // grid's config - should be updated on viewport resize | ||
| var config = { | ||
| containerWidth: window.innerWidth, | ||
| targetRowHeight: 150 | ||
| }; | ||
|
|
||
| loadUi(); | ||
|
|
||
|
|
||
| // gets all sections using api, populates grid div | ||
| function loadUi() { | ||
| getSections().then(sections => { | ||
| allSections = sections; | ||
| populateGrid(document.getElementById("grid")); | ||
| }); | ||
| } | ||
|
|
||
| // populates grid node with all detached sections | ||
| function populateGrid(gridNode) { | ||
| const sectionsHtml = allSections.map(getDetachedSectionHtml).join("\n"); | ||
| gridNode.innerHTML = sectionsHtml; | ||
| } | ||
|
|
||
| // generates detached section html, detached section has estimated height and no segments loaded | ||
| function getDetachedSectionHtml(section) { | ||
| return `<div id="${section.sectionId}" class="section" style="width: ${config.containerWidth}px; height: ${estimateSectionHeight(section)}px;"></div>`; | ||
| } | ||
|
|
||
| // estimates section height, taken from google photos blog | ||
| // Ideally we would use the average aspect ratio for the photoset, however assume | ||
| // a normal landscape aspect ratio of 3:2, then discount for the likelihood we | ||
| // will be scaling down and coalescing. | ||
| function estimateSectionHeight(section) { | ||
| const unwrappedWidth = (3 / 2) * section.totalImages * config.targetRowHeight * (7 / 10); | ||
| const rows = Math.ceil(unwrappedWidth / config.containerWidth); | ||
| const height = rows * config.targetRowHeight; | ||
|
|
||
| return height; | ||
| } |
Oops, something went wrong.