117 lines
3.7 KiB
Text
117 lines
3.7 KiB
Text
function insertArticle(article,parent,insertPlace) {
|
|
if (article){
|
|
var newItem = document.createElement("DIV");
|
|
if (article.cached_user) {
|
|
article.user = article.cached_user.table
|
|
}
|
|
if (article.cached_organization) {
|
|
article.organization = article.cached_organization.table
|
|
}
|
|
newItem.innerHTML = buildArticleHTML(article)
|
|
parent.insertBefore(newItem, insertPlace);
|
|
initializeReadingListIcons();
|
|
}
|
|
}
|
|
|
|
function insertInitialArticles(user) {
|
|
var el = document.getElementById("home-articles-object");
|
|
if (!el) {return}
|
|
el.innerHTML = "";
|
|
insertTopArticles(user);
|
|
el.outerHTML = "";
|
|
}
|
|
|
|
function insertTopArticles(user){
|
|
var el = document.getElementById("home-articles-object");
|
|
var articlesJSON = JSON.parse(el.dataset.articles)
|
|
var insertPlace = document.getElementById("article-index-hidden-div");
|
|
if (insertPlace) {
|
|
articlesJSON.forEach(function(article){
|
|
var articlePoints = 0
|
|
var containsUserID = findOne([article.user_id], user.followed_user_ids || [])
|
|
var containsOrganizationID = findOne([article.organization_id], user.followed_organization_ids || [])
|
|
var intersectedTags = intersect_arrays(user.followed_tag_names, article.cached_tag_list_array)
|
|
var followedPoints = 1
|
|
var experienceDifference = Math.abs(article['experience_level_rating'] - user.experience_level || 5)
|
|
var containsPreferredLanguage = findOne([article.language || 'en'], user.preferred_languages_array || ['en'])
|
|
JSON.parse(user.followed_tags).map(function(tag) {
|
|
if (intersectedTags.includes(tag.name)) {
|
|
followedPoints = followedPoints + tag.points
|
|
}
|
|
})
|
|
articlePoints = articlePoints + followedPoints
|
|
if (containsUserID) {
|
|
articlePoints = articlePoints + 1
|
|
}
|
|
if (containsOrganizationID) {
|
|
articlePoints = articlePoints + 1
|
|
}
|
|
var rand = Math.random();
|
|
if (rand < 0.3) {
|
|
articlePoints = articlePoints + 3
|
|
} else if (rand < 0.6) {
|
|
articlePoints = articlePoints + 6
|
|
}
|
|
if (containsPreferredLanguage) {
|
|
articlePoints = articlePoints + 1
|
|
} else {
|
|
articlePoints = articlePoints - 10
|
|
}
|
|
articlePoints = articlePoints - (experienceDifference/2);
|
|
article['points'] = articlePoints
|
|
});
|
|
var sortedArticles = articlesJSON.sort(function(a, b) {
|
|
return b.points - a.points;
|
|
});
|
|
sortedArticles.forEach(function(article){
|
|
var parent = insertPlace.parentNode;
|
|
if ( article.points > 0 && !document.getElementById("article-link-"+article.id) ) {
|
|
insertArticle(article,parent,insertPlace);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
function findOne(haystack, arr) {
|
|
return arr.some(function (v) {
|
|
return haystack.indexOf(v) >= 0;
|
|
});
|
|
};
|
|
|
|
|
|
function intersect_arrays(a, b) {
|
|
var sorted_a = a.concat().sort();
|
|
var sorted_b = b.concat().sort();
|
|
var common = [];
|
|
var a_i = 0;
|
|
var b_i = 0;
|
|
|
|
while (a_i < a.length
|
|
&& b_i < b.length)
|
|
{
|
|
if (sorted_a[a_i] === sorted_b[b_i]) {
|
|
common.push(sorted_a[a_i]);
|
|
a_i++;
|
|
b_i++;
|
|
}
|
|
else if(sorted_a[a_i] < sorted_b[b_i]) {
|
|
a_i++;
|
|
}
|
|
else {
|
|
b_i++;
|
|
}
|
|
}
|
|
return common;
|
|
}
|
|
|
|
function insertTimes() {
|
|
var elements = document.getElementsByClassName('time-ago-indicator-initial-placeholder');
|
|
for (var i = 0; i < elements.length; i++) {
|
|
|
|
// getElementsByClassName will get updated everytime we change DOM
|
|
// so elements[0] will produce the next unchanged element every iteration
|
|
var element = elements[0];
|
|
|
|
element.outerHTML = timeAgo({ oldTimeInSeconds: element.dataset.seconds });
|
|
}
|
|
};
|