-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
193 lines (181 loc) · 6.97 KB
/
content.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const COLOR_SCHEME = 'blue';
function generate_project_card(info) {
let elem = document.createElement('div');
elem.setAttribute('class', 'col s12 m6 l4');
elem.innerHTML =
'<a href="?content=project&project=' + info.id + '">' +
' <div class="card sticky-action hoverable">\n' +
' <div class="card-image waves-effect waves-block waves-light">\n' +
' <img class="activator" src="content/images/' + info.image + '" alt="">\n' +
' </div>\n' +
' <div class="card-content">\n' +
' <span class="card-title activator grey-text text-darken-4">' +
' ' + info.name +
' </span>\n' +
' </div>\n' +
' </div>' +
'</a>';
return elem;
}
function generate_text_page_block(content) {
let div = document.createElement('div');
switch (content.type) {
case 'text':
if (content.title) {
let title = document.createElement(content.text ? 'h3' : 'h1');
title.innerHTML = content.title;
title.setAttribute('class', content.text ?
COLOR_SCHEME + '-text' :
COLOR_SCHEME + '-text center');
div.appendChild(title);
}
if (content.text) {
let text = document.createElement('p');
text.innerHTML = content.text;
text.style.textAlign = 'justify';
div.appendChild(text);
}
break;
case 'image':
div.style.marginTop = '5px';
let row = document.createElement('div');
row.setAttribute('class', 'row');
row.style.margin = '0px';
let srcs = content.src.split(';');
let width = 100 / srcs.length + '%';
for (let src of srcs) {
src = src.trim();
let img = document.createElement('img');
let container = document.createElement('div');
let preloader = document.createElement('div');
preloader.setAttribute('class', 'preloader-wrapper big active');
preloader.innerHTML =
'<div class="spinner-layer spinner-blue-only">\n' +
' <div class="circle-clipper left">\n' +
' <div class="circle"></div>\n' +
' </div><div class="gap-patch">\n' +
' <div class="circle"></div>\n' +
' </div><div class="circle-clipper right">\n' +
' <div class="circle"></div>\n' +
' </div>\n' +
'</div>';
img.setAttribute('src', 'content/images/' + src);
img.style.display = 'block';
img.onload = function (ev) {
container.removeChild(preloader);
img.style.display = 'block';
};
container.setAttribute('class', 'col');
container.style.width = width;
if (!content.no_zoom) img.setAttribute('class', 'materialboxed');
img.style.width = '100%';
if (content.width !== undefined) {
img.style.width = content.width;
img.style.marginLeft = 'auto';
img.style.marginRight = 'auto';
}
container.appendChild(img);
container.appendChild(preloader);
row.appendChild(container);
}
div.appendChild(row);
if (content.caption) {
let caption = document.createElement('h5');
caption.setAttribute('class', COLOR_SCHEME + '-text text-lighten-4 center');
caption.style.marginBottom = '20px';
caption.style.marginTop = '3px';
caption.innerHTML = content.caption;
div.appendChild(caption);
}
break;
}
return div;
}
function generate_text_page(contents) {
let container = document.createElement('div');
container.setAttribute('class', 'container');
let div = document.createElement('div');
div.setAttribute('class', 'col s12 xl10 offset-xl1');
for (let content of contents) {
div.appendChild(generate_text_page_block(content));
}
container.appendChild(div);
return container;
}
function load_content() {
let content_node = document.getElementById('content');
let appendContent = function (elem) {
content_node.appendChild(elem);
materialize_init();
};
let params = new URL(document.location.href).searchParams;
let content_type = params.get('content');
if (!content_type) {
content_type = 'text_page';
params.set('content', content_type);
params.set('page', 'home');
}
console.log('loading ' + content_type);
switch (content_type) {
case 'projects':
load_object('content/projects/list', function (list) {
let urls = [];
for (let l of list) {
urls.push('content/projects/' + l);
}
load_objects(urls, function (projects) {
for (let project of projects) {
appendContent(generate_project_card(project));
}
});
});
break;
case 'project':
load_object('content/projects/' + params.get('project'), function (project) {
appendContent(generate_text_page(project.page));
});
break;
case 'text_page':
load_object('content/' + params.get('page'), function (contents) {
appendContent(generate_text_page(contents));
});
break;
}
}
function load_object(url, onload) {
let req = new XMLHttpRequest();
req.open('GET', `${url}.json5`);
req.onload = function () {
let json = req.responseText.replace(/@COLOR_SCHEME@/g, COLOR_SCHEME);
//TODO more general RegExp (replace @VAR@ with eval(VAR))
onload(JSON5.parse(json));
};
req.send();
}
function load_objects(urls, onload) {
let ct = 0;
let objects = [];
for (let i in urls) {
load_object(urls[i], function (obj) {
objects[i] = obj;
if (++ct === urls.length) {
onload(objects);
}
});
}
}
function init_color_scheme() {
let elem = document.getElementById('nav');
elem.setAttribute('class', elem.getAttribute('class') + ' ' + COLOR_SCHEME);
}
function materialize_init() {
let sel = document.querySelectorAll('.materialboxed');
if (sel) M.Materialbox.init(sel, {});
sel = document.querySelectorAll('p a');
for (let elem of sel) {
elem.setAttribute('class', elem.getAttribute('class') + ' ' + COLOR_SCHEME + '-text');
}
}
init_color_scheme();
load_content();
materialize_init();