This repository has been archived by the owner on Feb 28, 2019. It is now read-only.
forked from Jasonette/Jasonette-Web
-
Notifications
You must be signed in to change notification settings - Fork 5
/
item.js
75 lines (74 loc) · 3.08 KB
/
item.js
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
73
74
75
var Item = {
build: function(layout, parentLayout) {
if (layout.components) {
return Item.layout(layout, parentLayout);
} else {
return Item.components(layout, parentLayout);
}
},
layout: function(layout, parentLayout) {
var style = {};
if (layout && layout.style) {
if (layout.style.background && !/http/.test(layout.style.background)) { style.backgroundColor = layout.style.background }
if (layout.style.background && /http/.test(layout.style.background)) { style.backgroundImage = 'url(' + layout.style.background + ')' }
if (layout.style.background && /http/.test(layout.style.background)) { style.backgroundSize = 'cover' }
if (layout.style.padding) { style.padding = layout.style.padding + 'px' }
if (layout.style.width) { style.width = layout.style.width + 'px' }
if (layout.style.height) { style.height = layout.style.height + 'px' }
if (layout.style.align) { style.textAlign = layout.style.align }
if (layout.style.align) { style.alignItems = layout.style.align }
if ((parentLayout && parentLayout.type=='vertical' && layout.style.height) || (parentLayout && parentLayout.type=='horizontal' && layout.style.width)) {
style.flexGrow = "0";
} else {
style.flexGrow = "1";
}
if (parentLayout && parentLayout.type=='vertical' && parentLayout.style && parentLayout.style.spacing) {
style.marginBottom = parentLayout.style.spacing+'px';
}
if (parentLayout && parentLayout.type=='horizontal' && parentLayout.style && parentLayout.style.spacing) {
style.marginRight = parentLayout.style.spacing+'px';
}
}
var transformed = {
style: style,
class: layout.type + " layout",
$components: layout.components ? layout.components.map(function(component) { return Item.build(component, layout) }) : []
};
if (layout.href) {
transformed.onclick = function(e) {
if (layout.href.view === 'web') {
window.location.href = layout.href.url;
} else {
window.location.href = layout.href.url.replace(/\.json$/,'') + "/edit";
}
}
}
return transformed;
},
components: function(input, parentLayout) {
var c = Components[input.type];
var transformed;
if (c) {
if (input.class) input.className = input.class;
transformed = c(input);
} else if (input.class === 'spacing') {
transformed = { class: input.class };
} else {
transformed = { $text: input.type };
}
var style = {};
if (parentLayout && parentLayout.type === 'vertical' && parentLayout.style && parentLayout.style.spacing) {
style["marginBottom"] = parentLayout.style.spacing + "px";
} else if (parentLayout && parentLayout.type === 'horizontal' && parentLayout.style && parentLayout.style.spacing) {
style["marginRight"] = parentLayout.style.spacing + "px";
}
if (transformed.style) {
Object.keys(style).forEach(function(key) {
transformed.style[key] = style[key];
})
} else {
transformed.style = style;
}
return transformed;
}
}