generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 5
/
citations.js
155 lines (133 loc) · 4.58 KB
/
citations.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
import { decorateLinks, fetchFragment } from '../../scripts/scripts.js';
import { div } from '../../scripts/dom-helpers.js';
import { createOptimizedPicture, fetchPlaceholders } from '../../scripts/lib-franklin.js';
let placeholders = {};
function viewLongDescription(citation) {
const shortDescriptionBlock = citation.querySelector('.citation-short-description');
const longDescriptionBlock = citation.querySelector('.citation-long-description');
shortDescriptionBlock.style.display = 'none';
longDescriptionBlock.classList.add('show');
}
function viewShortDescription(citation) {
const shortDescriptionBlock = citation.querySelector('.citation-short-description');
const longDescriptionBlock = citation.querySelector('.citation-long-description');
longDescriptionBlock.classList.remove('show');
shortDescriptionBlock.style.display = 'block';
}
async function parseCitationFragments(fragmentPaths) {
const fragments = {};
await Promise.all(fragmentPaths.map(async (path) => {
const fragmentHtml = await fetchFragment(path);
if (fragmentHtml) {
const fragmentElement = document.createElement('div');
fragmentElement.innerHTML = fragmentHtml;
const titleBlock = fragmentElement.querySelector('h2');
// remove the 'Citations : ' prefix from the title
if (titleBlock) {
const link = titleBlock.querySelector('a');
link.innerText = link.innerText.replace('Citations : ', '');
}
const pTags = fragmentElement.querySelectorAll('p');
const dateBlock = pTags[0];
const publicationNameBlock = pTags[1];
const descriptionBlock = pTags[2];
const contributorsBlock = pTags[3];
const gotToBlock = pTags[4];
fragments[path] = {
dateBlock,
publicationNameBlock,
titleBlock,
descriptionBlock,
contributorsBlock,
gotToBlock,
};
}
}));
return fragments;
}
function buildCitation(fragment) {
const {
dateBlock,
publicationNameBlock,
titleBlock,
descriptionBlock,
contributorsBlock,
gotToBlock,
} = fragment;
// create short description which limits the description to 50 words
let shortDescriptionBlock = null;
if (descriptionBlock) {
const shortDescription = descriptionBlock.innerText.split(' ').slice(0, 40).join(' ');
shortDescriptionBlock = document.createElement('p');
// append ... to the end of the short description
shortDescriptionBlock.innerText = `${shortDescription}...`;
}
const viewMoreBlock = div({ class: 'view-change view-more' });
viewMoreBlock.innerHTML = `
${placeholders.viewMore || 'View more'} <i class="fa fa-angle-down" aria-hidden="true"></i>
`;
const viewLessBlock = div({ class: 'view-change view-less' });
viewLessBlock.innerHTML = `
${placeholders.viewLess || 'View less'} <i class="fa fa-angle-up" aria-hidden="true"></i>
`;
const citation = div(
{ class: 'citation' },
div(
{ class: 'citation-icon' },
createOptimizedPicture('/images/resource-icons/citation.png', 'citation icon'),
),
div(
{ class: 'citation-info' },
div(
{ class: 'citation-header' },
dateBlock,
publicationNameBlock,
titleBlock,
),
div(
{ class: 'citation-short-description' },
shortDescriptionBlock,
viewMoreBlock,
),
div(
{ class: 'citation-long-description' },
descriptionBlock || '',
contributorsBlock || '',
gotToBlock || '',
viewLessBlock,
),
),
);
decorateLinks(citation);
return citation;
}
export default async function decorate(block) {
const fragmentPaths = [...block.querySelectorAll('a')].map((a) => a.href);
block.innerHTML = '';
if (fragmentPaths.length === 0) {
return '';
}
placeholders = await fetchPlaceholders();
const fragments = await parseCitationFragments(fragmentPaths);
fragmentPaths.forEach((path) => {
const fragment = fragments[path];
if (fragment) {
block.append(buildCitation(fragment));
}
});
// add listener for when the user clicks on view more
block.querySelectorAll('.view-more').forEach((viewMoreBlock) => {
viewMoreBlock.addEventListener('click', () => {
const citation = viewMoreBlock.closest('.citation');
viewLongDescription(citation);
});
});
// add listener for when the user clicks on view less
block.querySelectorAll('.view-less').forEach((viewLessBlock) => {
viewLessBlock.addEventListener('click', () => {
const citation = viewLessBlock.closest('.citation');
viewShortDescription(citation);
});
});
return block;
}