This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
article.js
119 lines (105 loc) · 3.37 KB
/
article.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
// Reload page on hash change
const renderHash = () => {
window.history.go();
};
window.addEventListener('hashchange', renderHash, false);
// Define which article is being retrieved
const articleSlug = location.hash.slice(1);
// Create article container
const articleContainer = addToElementbyId('div', 'article', app);
// Call for article info
deliveryClient
.items()
.type('article')
.equalsFilter('elements.url_pattern', articleSlug)
.queryConfig({
urlSlugResolver: (link, context) => {
return resolveUrl(link);
},
richTextResolver: (item, context) => {
return resolveLinkedItems(item);
}
})
.toPromise()
.then(response => {
// Check if article found before adding
const article =
response.data.items && response.data.items.length ? response.data.items[0] : undefined;
// 404 message if not found
if (!article) {
app.innerHTML = '<p>That article could not be found.</p>';
return;
}
// Update title
document.title = `Article | ${article.system.name}`;
// Transform image
const transformedImageUrl = Kk.transformImageUrl(article.elements.teaser_image.value[0].url)
.withDpr(2)
.withCompression('lossless')
.withHeight(300)
.withWidth(300)
.getUrl();
// Create nodes
const headerImage = createElement(
'img',
'article-header',
'src',
transformedImageUrl
);
const title = createElement(
'h2',
'article-title',
'innerText',
article.elements.title.value
);
const richTextElement = article.elements.body_copy;
const rteResolver = Kk.createRichTextHtmlResolver().resolveRichText({
element: richTextElement,
linkedItems: Kk.linkedItemsHelper.convertLinkedItemsToArray(response.data.linkedItems),
urlResolver: (linkId, linkText, link) => {
// Set link based on type
const urlLocation =
link.type === 'article'
? `article.html#${link.urlSlug}`
: link.type === 'coffee'
? `coffee.html#${link.urlSlug}`
: 'unsupported-link';
return { linkUrl: urlLocation };
},
contentItemResolver: (itemId, item) => {
if (item.system.type === 'hosted_video') {
const videoID = item.elements.video_id.value;
// Check if a video host exists
const videoHost =
item.elements.video_host.value && item.elements.video_host.value.length
? item.elements.video_host.value[0].codename
: undefined;
if (videoHost) {
// Return based on hosting provider
const htmlCode = videoHost === 'youtube'
? `<iframe src='https://www.youtube.com/embed/${videoID}' width='560' height='315' frameborder='0'></iframe>`
: `<iframe src='https://player.vimeo.com/video/${videoID}' width='560' height='315' allowfullscreen frameborder='0'></iframe>`;
return {
contentItemHtml: htmlCode
};
}
}
return {
contentItemHtml: ''
};
}
});
const body = createElement(
'div',
'article-description',
'innerHTML',
rteResolver.html
);
// Add nodes to DOM
articleContainer.classList.add('card');
articleContainer.append(headerImage, title, body);
return;
})
.catch(err => {
reportErrors(err);
});