Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Step 2 - Populating section with segments
  • Loading branch information
shaileshpandit committed Feb 10, 2021
1 parent 7ddef4c commit 200db9f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
7 changes: 7 additions & 0 deletions api.js
Expand Up @@ -13,4 +13,11 @@ function getSections() {
return { sectionId: section.sectionId, totalImages: section.totalImages };
});
});
}

// get all segments inside one section - e.g. one segment per day
function getSegments(sectionId) {
return sectionStore.then(delay(50 + Math.random() * 500)).then(store => {
return store.find(section => section.sectionId == sectionId).segments
});
}
1 change: 1 addition & 0 deletions index.html
Expand Up @@ -12,6 +12,7 @@
<link rel="stylesheet" href="/style.css">

<!-- import the webpage's javascript files -->
<script src="justified-layout.min.js"></script>
<script src="/api.js" defer></script>
<script src="/script.js" defer></script>
</head>
Expand Down
13 changes: 13 additions & 0 deletions justified-layout.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions script.js
@@ -1,3 +1,5 @@
var justifiedLayout = require('justified-layout');

// app state - list of all sections and their image counts
var allSections = [];

Expand All @@ -22,6 +24,9 @@ function loadUi() {
function populateGrid(gridNode) {
const sectionsHtml = allSections.map(getDetachedSectionHtml).join("\n");
gridNode.innerHTML = sectionsHtml;

// populate each section eagerly
gridNode.querySelectorAll(".section").forEach(populateSection);
}

// generates detached section html, detached section has estimated height and no segments loaded
Expand All @@ -39,4 +44,29 @@ function estimateSectionHeight(section) {
const height = rows * config.targetRowHeight;

return height;
}

// populates section with actual segments html
function populateSection(sectionDiv) {
getSegments(sectionDiv.id).then(segments => {
// adds all segments as childs of section
sectionDiv.innerHTML = segments.map(getSegmentHtml).join("\n");
sectionDiv.style.height = "100%"
});
}

// generates Segment html
function getSegmentHtml(segment) {
const sizes = segment.images.map(image => image.metadata);
var geometry = justifiedLayout(sizes, config);

// gets tiles for each box given by justified layout lib
var tiles = geometry.boxes.map(getTileHtml).join("\n");

return `<div id="${segment.segmentId}" class="segment" style="width: ${config.containerWidth}px; height: ${geometry.containerHeight}px;">${tiles}</div>`;
}

// generates Tile html
function getTileHtml(box) {
return `<div class="tile" style="width: ${box.width}px; height: ${box.height}px; left: ${box.left}px; top: ${box.top}px;"></div>`;
}
12 changes: 12 additions & 0 deletions style.css
Expand Up @@ -9,5 +9,17 @@ body {
.section {
position: relative;
background-color: blueviolet;
padding: 20px 0px;
margin: 30px 0px;
}

.segment {
position: relative;
background-color: blue;
margin: 20px 0px;
}

.tile {
position: absolute;
background-color: yellow;
}

0 comments on commit 200db9f

Please sign in to comment.