-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCFDirect.js
More file actions
62 lines (57 loc) · 2.53 KB
/
CFDirect.js
File metadata and controls
62 lines (57 loc) · 2.53 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
function date2str(x, y) {
var z = {
M: x.getMonth() + 1,
d: x.getDate(),
h: x.getHours(),
m: x.getMinutes(),
s: x.getSeconds()
};
y = y.replace(/(M+|d+|h+|m+|s+)/g, function (v) {
return ((v.length > 1 ? "0" : "") + eval('z.' + v.slice(-1))).slice(-2)
});
return y.replace(/(y+)/g, function (v) {
return x.getFullYear().toString().slice(-v.length)
});
}
var contentfulClient = null;
var currentEntry = null;
function CFDirect(accessToken, space, entry) {
contentfulClient = contentful.createClient({
accessToken: accessToken,
space: space
});
contentfulClient.getEntry(entry)
.then(function (entry) {
console.log(entry);
currentEntry = entry;
console.log(document.querySelectorAll("[cf-data-key]"));
document.querySelectorAll("[cf-data-list]").forEach(function (itm) {
//itm is a datalist
var key = itm.getAttribute('cf-data-list');
var html = itm.innerHTML;
itm.innerHTML = '';
currentEntry.fields[key].forEach(function (i2) {
var inner = document.createElement('div');
inner.innerHTML = html;
inner.querySelectorAll('[cf-data-listitem]').forEach(function (listitm) {
listitm.innerHTML = i2;
});
itm.insertAdjacentHTML('beforeend', inner.innerHTML);
});
});
document.querySelectorAll("[cf-data-key]").forEach(function (itm) {
var key = itm.getAttribute('cf-data-key');
if (itm.tagName.toLowerCase() === 'img') {
itm.setAttribute('src', currentEntry.fields[key].fields.file.url);
} else if (itm.getAttribute('cf-data-format') === 'rich') {
itm.innerHTML = documentToHtmlString(currentEntry.fields[key]);
} else if (itm.getAttribute('cf-data-format') === 'markdown') {
itm.innerHTML = marked(currentEntry.fields[key]);
} else if (itm.getAttribute('cf-data-format') != null) { //(currentEntry.fields[key] instanceof Date) &&
itm.innerHTML = date2str(new Date(currentEntry.fields[key]), itm.getAttribute('cf-data-format'));
} else {
itm.innerHTML = currentEntry.fields[key];
}
});
});
}