-
Notifications
You must be signed in to change notification settings - Fork 0
/
mag-story.html
122 lines (89 loc) · 2.46 KB
/
mag-story.html
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<script>
const RUN_ON_HOSTED_DEMO = false;
var URL = '';
var URL_DAM_BASE = '';
if (RUN_ON_HOSTED_DEMO){
URL = "https://demopublic.magnolia-cms.com/.rest/delivery/stories/v1/stories-demo/flavors-of-asia"
URL_DAM_BASE = "https://demopublic.magnolia-cms.com"
}else{
//Run on local demo - you must install and start a local Magnolia server.
URL = "http://localhost:8080/magnoliaPublic/.rest/delivery/storiesDemo/v1/stories-demo/flavors-of-asia"
URL_DAM_BASE = "http://localhost:8080"
}
function loadAndRender(){
fetch(URL)
.then(function(response) {
return response.json();
})
.then(function(myJson) {
//console.log(myJson);
var html = "";
if (RUN_ON_HOSTED_DEMO){
html += "<b><i>Running on Hosted Demo. Images probably won't be displayed because hosted endpoint does not have 'renditions' configured.</i></b>"
}
html += renderIntro(myJson);
var nodes = myJson['@nodes'];
for (let node_ref of nodes){
// Derefernce the block based on the @nodes array.
var block = myJson[node_ref];
// Only render nodes of type block.
if (block['@nodeType'] === "mgnl:block"){
var type = block['mgnl:type'];
html += "<h2>" + type + "</h2>";
if (type == 'text'){
html += renderText(block);
} else if (type === 'image'){
html += renderImage(block);
}else if (type === 'date'){
html += renderDate(block);
}
html += "<hr/>";
}
}
var element = document.getElementById("dynamic-content");
element.innerHTML = html;
});
}
function renderIntro(content){
return `<h1>${content.title}</h1>
<div>
<p>${content.lead}</p>
</div>
<div>
<p>by ${content.author}</p>
</div>
`
}
function renderText(block){
return `<div>
<p>${block.text}</p>
</div>`
}
function renderImage(block){
const image_url = block.image.renditions ? URL_DAM_BASE + block.image.renditions['240'].link : '';
return `<div>
<img src="${image_url}"></img>
<p>${block.imageAltText}</p>
</div>`
}
function renderDate(block){
var d = new Date(block.date)
return `<div>
<p>${d}</p>
</div>`
}
</script>
</head>
<body>
<h1>A Magnolia Story</h1>
<div id="dynamic-content">
</div>
<script>
loadAndRender();
</script>
</body>
</html>