-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonToLayout.js
More file actions
73 lines (62 loc) · 3.2 KB
/
jsonToLayout.js
File metadata and controls
73 lines (62 loc) · 3.2 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(function(){
window.addEventListener('DOMContentLoaded', startWatchingFiles, false);
})();
var file;
function startWatchingFiles() {
document.getElementById('files').addEventListener('change', handleFileSelect, false);
}
function handleFileSelect(evt) {
file = evt.target.files[0]; // FileList object
console.log(evt);
processFiles();
}
function processFiles() {
// Loop through the FileList
if(file) {
var canvases = document.getElementsByClassName('canvas');
while(canvases[0]){
canvases[0].parentNode.removeChild(canvases[0]);
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
var jsonData = reader.result;
var layouts = JSON.parse(jsonData).layouts;
for (var layoutIndex = 0; layoutIndex < layouts.length; layoutIndex++) {
var span = document.createElement('div');
span.className = "canvas";
var layout = layouts[layoutIndex];
var canvasWidth = layout.canvasWidth;
var canvasHeight = layout.canvasHeight;
var elements = layout.elements;
var htmlText = ['<div style = "width:',canvasWidth,'px;height:',canvasHeight,'px;top:0;left:0;position:relative;border: 1px solid #000000">'].join('');
for (var index = 0; index < elements.length; index++) {
element = elements[index];
var htmlPlacement = [' style = "width:',element.width,'px; height:',element.height,'px; top:',element.y,'px; left:',element.x,'px; position: absolute; '].join('');
var color = element.color;
if (element.type == "button") {
htmlText = [htmlText,'<button',htmlPlacement,' background-color:',color,'">Button</button>\n'].join('');
}
else if (element.type == "title") {
htmlText = [htmlText,'<div class = "le" ',htmlPlacement, 'background-color:',color?color:'#29b571',';font-size:30px">Title</div>'].join('');
}
else if (element.type == "text") {
htmlText = [htmlText,'<div class = "le" ',htmlPlacement, 'background-color:',color?color:'#91115e;','">Some Text</div>'].join('');
}
else if (element.type == "image") {
htmlText = [htmlText,'<img src="https://placeimg.com/',element.width,'/',element.height,'/any"',htmlPlacement,'"></img>'].join('');
}
else {
htmlText = [htmlText,'<div',htmlPlacement, 'background-color:',color?color:'#ccd1cc','">Unassigned</div>'].join('');
}
}
span.innerHTML = [htmlText,'</div><br/><br/>'].join('');
document.getElementById('list').insertBefore(span, null);
}
};
})(file);
// Read in the text file.
reader.readAsText(file);
}
}