-
Notifications
You must be signed in to change notification settings - Fork 4k
/
initScrolling.js.erb
293 lines (265 loc) · 10.6 KB
/
initScrolling.js.erb
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
'use strict';
/* global algoliasearch, insertAfter, insertArticles, buildArticleHTML, nextPage:writable, fetching:writable, done:writable */
var client;
function fetchNext(el, endpoint, insertCallback){
var indexParams = JSON.parse(el.dataset.params);
var urlParams = Object.keys(indexParams).map(function handleMap(k) {
return encodeURIComponent(k) + "=" + encodeURIComponent(indexParams[k])
}).join('&');
if (urlParams.indexOf("q=") > -1 ) {
return;
}
var fetchUrl = (endpoint+"?page="+nextPage+"&"+urlParams+"&signature="+parseInt(Date.now()/400000, 10)).replace("&&","&");
window.fetch(fetchUrl)
.then(function handleResponse(response) {
response.json().then(function insertEntries(entries) {
nextPage += 1;
insertCallback(entries);
if (entries.length === 0) {
document.getElementById("loading-articles").style.display = "none";
done = true;
}
});
}).catch(function logError(err) {
// eslint-disable-next-line no-console
console.log(err);
});
}
function insertNext(params, buildCallback) {
return function insertEntries(entries) {
var list = document.getElementById(params.listId || "sublist");
var newFollowersHTML = "";
entries.forEach(function insertAnEntry(entry){
let existingEl = document.getElementById((params.elId || "element") + "-" + entry.id);
if(!existingEl) {
var newHTML = buildCallback(entry)
newFollowersHTML += newHTML
}
});
var distanceFromBottom = document.documentElement.scrollHeight - document.body.scrollTop;
var newNode = document.createElement("div");
newNode.innerHTML = newFollowersHTML;
var singleArticles = document.getElementsByClassName("single-article");
var lastElement = singleArticles[singleArticles.length - 1];
insertAfter(newNode, lastElement);
if (nextPage > 0) {
fetching = false;
}
}
}
function buildFollowsHTML(follows) {
return '<div id="follows-' + follows.id + '" class="single-article">\n' +
' <a href="' + follows.path +'" class="block-link">\n' +
' <h2>\n' +
' <img alt="' + follows.username + ' profile image" src="' + follows.profile_image + '" />\n' +
' ' + follows.name + '\n' +
' <span class="dashboard-username">@' + follows.username + '</span>\n' +
' </h2>\n' +
' </a>\n' +
'</div>';
}
var negativeFollow = -1;
function buildTagsHTML(tag) {
var tagHTML = '';
if (tag.points < 0 && negativeFollow === -1){
tagHTML += '<h2>Negative Tags (anti-follows)</h2>';
negativeFollow = 0;
}
return tagHTML +
'<div id="follows-' + tag.id + '" class="single-article" style="border-color:' + tag.color + ';box-shadow: 3px 3px 0px ' + tag.color + '">\n' +
' <h2>\n' +
' <a href="/t/' + tag.name + '">\n' +
' ' + tag.name + '\n' +
' </a>\n' +
' <form class="edit_follow" id="edit_follow_' + tag.id + '" action="/follows/' + tag.id + '" accept-charset="UTF-8" method="post">\n' +
' <input name="utf8" type="hidden" value="✓">\n' +
' <input type="hidden" name="_method" value="patch">\n' +
' <input type="hidden" name="authenticity_token" value="' + tag.token + '">\n' +
' <label for="follow_points">Follow Weight:</label>\n' +
' <input step="any" required="required" type="number" value="' + tag.points + '" name="follow[points]" id="follow_points">\n' +
' <input type="submit" name="commit" value="Submit">\n' +
' </form>\n' +
' </h2>\n' +
'</div>';
}
function fetchNextFollowingPage(el){
var indexParams = JSON.parse(el.dataset.params);
var action = indexParams.action;
if (action.includes("users")){
fetchNext(el, "/api/followings/users", insertNext({ elId: "follows" }, buildFollowsHTML));
} else if (action.includes("podcasts")){
fetchNext(el, "/api/followings/podcasts", insertNext({ elId: "follows" }, buildFollowsHTML));
} else if (action.includes("organizations")){
fetchNext(el, "/api/followings/organizations", insertNext({ elId: "follows" }, buildFollowsHTML));
} else {
fetchNext(el, "/api/followings/tags", insertNext({ elId: "follows" }, buildTagsHTML));
}
}
function fetchNextFollowersPage(el){
var indexParams = JSON.parse(el.dataset.params);
var whichEndpoint = indexParams.which;
if (whichEndpoint.includes("organization")){
fetchNext(el, "/api/followers/organizations", insertNext({ elId: "follows" }, buildFollowsHTML));
} else {
fetchNext(el, "/api/followers/users", insertNext({ elId: "follows" }, buildFollowsHTML));
}
}
function buildVideoArticleHTML(videoArticle) {
return '<a class="single-video-article single-article" href="' + videoArticle.path + '" id="video-article-' + videoArticle.id + '">\n' +
' <div class="video-image" style="background-image: url(' + videoArticle.cloudinary_video_url + ')">\n' +
' <span class="video-timestamp">' + videoArticle.video_duration_in_minutes + '</span>\n' +
' </div>\n' +
' <p><strong>' + videoArticle.title + '</strong></p>\n' +
' <p>' + videoArticle.user.name + '</p>\n' +
'</a>';
}
function insertVideos(videoArticles) {
var list = document.getElementById("subvideos");
var newVideosHTML = "";
videoArticles.forEach(function insertAVideo(videoArticle){
var existingEl = document.getElementById("video-article-"+videoArticle.id);
if(!existingEl) {
var newHTML = buildVideoArticleHTML(videoArticle)
newVideosHTML += newHTML
}
});
var distanceFromBottom = document.documentElement.scrollHeight - document.body.scrollTop;
var newNode = document.createElement("div");
newNode.innerHTML = newVideosHTML;
newNode.className += "video-collection"
var singleArticles = document.getElementsByClassName("single-article");
var lastElement = singleArticles[singleArticles.length - 1];
insertAfter(newNode, lastElement);
if (nextPage > 0) {
fetching = false;
}
}
function fetchNextVideoPage(el){
fetchNext(el, "/api/videos", insertVideos)
}
function insertArticles(articles) {
var list = document.getElementById("substories");
var newArticlesHTML = "";
var el = document.getElementById("home-articles-object")
if (el) {el.outerHTML = "";}
articles.forEach(function insertAnArticle(article){
var existingEl = document.getElementById("article-link-"+article.id);
if ( existingEl &&
existingEl.parentElement &&
existingEl.parentElement.classList.contains('single-article-small-pic') &&
!document.getElementById("video-player-"+article.id)) {
existingEl.parentElement.outerHTML = buildArticleHTML(article);
} else if(!existingEl) {
var newHTML = buildArticleHTML(article)
newArticlesHTML += newHTML
initializeReadingListIcons();
}
});
var distanceFromBottom = document.documentElement.scrollHeight - document.body.scrollTop;
var newNode = document.createElement("div");
newNode.innerHTML = newArticlesHTML;
var singleArticles = document.getElementsByClassName("single-article");
var lastElement = singleArticles[singleArticles.length - 1];
insertAfter(newNode, lastElement);
if (nextPage > 0) {
fetching = false;
}
}
function fetchNextPodcastPage(el){
fetchNext(el, "/api/podcast_episodes", insertArticles)
}
function algoliaPaginate(tag){
var filters = [];
if (tag && tag.length > 0) {
filters.push(tag);
}
var searchObj = {
hitsPerPage: 15,
page: nextPage,
attributesToHighlight: [],
tagFilters: filters,
}
var homeEl = document.getElementById("index-container");
var paginationArticlesIndex;
if (homeEl.dataset.feed === "base-feed") {
paginationArticlesIndex = client.initIndex('ordered_articles_<%= Rails.env %>');
} else if (homeEl.dataset.feed === "latest") {
paginationArticlesIndex = client.initIndex('ordered_articles_by_published_at_<%= Rails.env %>');
} else {
searchObj.filters = ('published_at_int > ' + homeEl.dataset.articlesSince);
paginationArticlesIndex = client.initIndex('ordered_articles_by_positive_reactions_count_<%= Rails.env %>');
}
paginationArticlesIndex.search("*", searchObj)
.then(function searchDone(content) {
nextPage += 1;
insertArticles(content.hits);
const checkBlockedContentEvent = new CustomEvent('checkBlockedContent')
window.dispatchEvent(checkBlockedContentEvent)
if (content.hits.length === 0) {
document.getElementById("loading-articles").style.display = "none"
done = true;
}
});
}
function fetchNextPageIfNearBottom() {
var indexContainer = document.getElementById("index-container");
var elCheck = indexContainer && !document.getElementById("query-wrapper");
if (!elCheck) {
return;
}
var indexWhich = indexContainer.dataset.which;
var fetchCallback;
var scrollableElemId;
if (indexWhich === "podcast-episodes") {
scrollableElemId = "articles-list";
fetchCallback = function fetch() {
fetchNextPodcastPage(indexContainer);
};
} else if (indexWhich === "videos") {
scrollableElemId = "video-collection";
fetchCallback = function fetch() {
fetchNextVideoPage(indexContainer);
};
} else if (indexWhich === "followers") {
scrollableElemId = "user-dashboard";
fetchCallback = function fetch() {
fetchNextFollowersPage(indexContainer);
}
} else if (indexWhich === "following") {
scrollableElemId = "user-dashboard";
fetchCallback = function fetch() {
fetchNextFollowingPage(indexContainer);
}
} else {
scrollableElemId = "articles-list";
fetchCallback = function fetch() {
algoliaPaginate(indexContainer.dataset.algoliaTag);
};
}
var scrollableElem = document.getElementById(scrollableElemId);
if ( !done && !fetching && (window.scrollY) > (scrollableElem.scrollHeight - 3700) ) {
fetching = true;
fetchCallback();
}
}
function checkIfNearBottomOfPage() {
var publicSearchKey = '<%= ALGOLIASEARCH_PUBLIC_SEARCH_ONLY_KEY %>';
client = algoliasearch('<%= ApplicationConfig["ALGOLIASEARCH_APPLICATION_ID"] %>', publicSearchKey);
if (document.getElementsByClassName("single-article").length < 2 || window.location.search.indexOf("q=") > -1 ) {
document.getElementById("loading-articles").style.display = "none";
done = true;
} else {
document.getElementById("loading-articles").style.display = "block";
}
fetchNextPageIfNearBottom();
setInterval(function handleInterval(){
fetchNextPageIfNearBottom();
}, 210)
}
function initScrolling() {
var elCheck = document.getElementById("index-container");
if (elCheck) {
initScrolling.called = true;
checkIfNearBottomOfPage();
}
}