forked from disqus/disqus-install-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infinite_scroll_template.html
150 lines (129 loc) · 5.48 KB
/
infinite_scroll_template.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
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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" type="text/css" href="//media.disquscdn.com/disqus-install-examples/assets/css/kube.min.css" />
<style>
#container {
margin: auto;
max-width: 1128px;
padding: 12px;
}
.article {
margin: 32px 0;
}
#disqus_thread {
margin-top: 10px;
}
</style>
</head>
<body>
<div id="container">
<h1>Infinite Scroll Template</h1>
<p>Demonstrates how to load Disqus on a page that continuously adds content to the bottom of the page, each with their own discussion.</p>
<div id="articles-list"></div>
<button id="load-more-articles" class="button w100">
Load More Articles
</button>
</div>
<script>
var doc = window.document;
var articlesList = doc.getElementById('articles-list');
var articleText = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt' +
' ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris' +
' nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse' +
' cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa' +
' qui officia deserunt mollit anim id est laborum.';
var disqus_config;
var disqusLoaded = false;
var initDisqus = function () {
if (disqusLoaded)
return;
disqusLoaded = true;
var scr = doc.createElement('script');
scr.src = '//kaffepausen.disqus.com/embed.js';
scr.setAttribute('data-timestamp', +new Date());
(doc.head || doc.body).appendChild(scr);
};
var resetDisqus = function (newIdentifier, newUrl, newTitle) {
if (!disqusLoaded) {
disqus_config = function () {
this.page.url = newUrl;
this.page.identifier = newIdentifier;
this.page.title = newTitle;
};
initDisqus();
} else {
window.DISQUS.reset({
reload: true,
config: function () {
this.page.identifier = newIdentifier;
this.page.url = newUrl;
this.page.title = newTitle;
}
});
}
};
var handleDisqusButtonClick = function (event) {
// Get new thread values
var newIdentifier = event.target.getAttribute('data-disqus-identifier');
var newUrl = event.target.getAttribute('data-disqus-url');
var newTitle = event.target.getAttribute('data-disqus-title');
// Remove existing Disqus thread if in DOM
var disqusThread = doc.getElementById('disqus_thread');
if (disqusThread)
disqusThread.parentElement.removeChild(disqusThread);
// Create new disqus thread div below button
disqusThread = doc.createElement('div');
disqusThread.id = 'disqus_thread';
event.target.parentElement.appendChild(disqusThread);
// Reset Disqus script
resetDisqus(newIdentifier, newUrl, newTitle);
};
var createArticle = function (index) {
// Outer div
var wrapper = doc.createElement('div');
wrapper.id = 'article-' + index;
wrapper.className = 'article';
// Article title
var title = doc.createElement('h2');
title.innerHTML = 'Article ' + index;
// Paragraph contents
var paragraphs = [1, 2, 3].map(function (number) {
var par = doc.createElement('p');
par.innerText = articleText;
return par;
});
// Load comments button
var commentsButton = doc.createElement('button');
commentsButton.className = 'button small outline secondary';
commentsButton.setAttribute('data-disqus-identifier', 'new-ident-' + index);
commentsButton.setAttribute('data-disqus-url', 'https://example.com/article-' + index);
commentsButton.setAttribute('data-disqus-title', 'Article ' + index);
commentsButton.innerText = 'Load Comments';
commentsButton.onclick = handleDisqusButtonClick;
// Comments div
var commentsWrapper = doc.createElement('div');
commentsWrapper.className = 'disqus-comments';
commentsWrapper.appendChild(commentsButton);
// Assemble the final template
wrapper.appendChild(title);
paragraphs.forEach(function (par) {
wrapper.appendChild(par);
});
wrapper.appendChild(commentsWrapper);
return wrapper;
};
var globalArticleIndex = 0;
var addMoreContent = function (numArticles) {
for (idx = 0; idx <= numArticles; idx++) {
articlesList.appendChild(createArticle(globalArticleIndex));
globalArticleIndex++
}
};
var loadMoreButton = doc.getElementById('load-more-articles');
loadMoreButton.onclick = addMoreContent.bind(this, 3);
addMoreContent(3);
</script>
</body>
</html>