-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.js
More file actions
40 lines (34 loc) · 1.3 KB
/
layout.js
File metadata and controls
40 lines (34 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
init();
function init() {
fetchLayout()
.then(res => {
genHtmlLayout(res);
})
.then(() => {
console.log($('#grid-1').html());
});
}
function fetchLayout() {
return fetch('./layout.json')
.then(res => {
return res.json();
});
}
function genHtmlLayout(data) {
if (!data) return false;
let layoutHtml = '';
let screenWidth = data.screen.width,
screenHeight = data.screen.height,
screenCols = data.screen.cols,
screenRows = data.screen.rows;
layoutHtml += `<div style="width: ${screenWidth}px; height: ${screenHeight}px; position: relative;">`;
let colGap = data.screen.gap;
let colWidth = ((screenWidth - colGap) / screenCols) - colGap;
let colHeight = ((screenHeight - colGap) / screenRows) - colGap;
let layout = data.layout;
for (let i = 0; i < layout.length; i++) {
layoutHtml += `<div id="grid-${i}" class="grid" style="top: ${(colGap + colHeight) * layout[i].y + colGap}px; left: ${(colGap + colWidth) * layout[i].x + colGap}px; width: ${(colGap + colWidth) * layout[i].cols - colGap}px; height: ${(colGap + colHeight) * layout[i].rows - colGap}px">${i}</div>`;
}
layoutHtml += `</div>`;
document.getElementById('wrapper').innerHTML = layoutHtml;
}