-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
138 lines (128 loc) · 4.3 KB
/
index.js
File metadata and controls
138 lines (128 loc) · 4.3 KB
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
let posts = [];
let postsLim = 10;
const url = 'https://api.myjson.com/bins/152f9j';
fetch(url).then(
response => {
response.json().then(data => {
const rawData = data.data;
createList(rawData);
});
}
).catch((error) => {
console.log(JSON.stringify(error));
});
function createList(data) {
posts = data.map(post => {
let li = document.createElement("li");
let title = document.createElement("h2");
let description = document.createElement("p");
let img = document.createElement("img");
let createdAt = document.createElement("p");
let tags = [];
title.innerHTML = post.title;
description.innerHTML = post.description;
img.src = post.image;
createdAt.innerHTML = new Date(post.createdAt);
for (let i = 0; i < post.tags.length; i++) {
tags[i] = document.createElement("span");
tags[i].innerHTML = "#" + post.tags[i].toLowerCase();
}
let remove = document.createElement("button");
remove.innerHTML = "delete";
removePost = function() {
document.getElementById("list").removeChild(this.parentNode);
};
remove.addEventListener('click', removePost);
li.appendChild(title);
li.appendChild(description);
li.appendChild(img);
li.appendChild(createdAt);
for (let i = 0; i < post.tags.length; i++) {
li.appendChild(tags[i]);
}
li.appendChild(remove);
return li;
});
//console.log(posts.length);
sortByDateAsc(posts);
var k;
if(posts.length < 10)
postsLim = posts.length;
else postsLim = 10;
for (let i = 0; i < postsLim; i++) {
//console.log(posts[i].children[3].innerHTML);
list = document.getElementById('list');
list.appendChild(posts[i]);
}
}
window.onscroll = function(){
if(posts.length === 0)
return;
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight){
//console.log('bottom');
for (let i = postsLim; i < postsLim + 10 && i < posts.length; i++) {
list = document.getElementById('list');
list.appendChild(posts[i]);
}
postsLim += 10;
}
}
function sortByDateAsc(posts){
posts.sort((a, b) => {
a = new Date(a.children[3].innerHTML);
b = new Date(b.children[3].innerHTML);
return (a - b);
});
//console.log("sort");
}
function sortByDateDesc(posts){
posts.sort((a, b) => {
a = new Date(a.children[3].innerHTML);
b = new Date(b.children[3].innerHTML);
return (b - a);
});
}
let ascButton = document.getElementById('sort-asc');
let sortAsc = function(){
sortByDateAsc(posts);
let tmpList = document.getElementById("list").cloneNode(false);
for (let i = 0; i < postsLim; i++) {
tmpList.appendChild(posts[i]);
}
document.getElementById("list").parentNode.replaceChild(tmpList, document.getElementById('list'));
};
ascButton.addEventListener('click', sortAsc);
let descButton = document.getElementById('sort-desc');
let sortDesc = function(){
sortByDateDesc(posts);
let tmpList = document.getElementById("list").cloneNode(false);
for (let i = 0; i < postsLim; i++) {
tmpList.appendChild(posts[i]);
}
document.getElementById("list").parentNode.replaceChild(tmpList, document.getElementById('list'));
};
descButton.addEventListener('click', sortDesc);
let defaultBtn = document.getElementById('default');
let setToDefault = function(){
window.scrollTo(0,0);
if(posts.length < 10)
postsLim = posts.length;
else postsLim = 10;
let tmpList = document.getElementById("list").cloneNode(false);
for (let i = 0; i < postsLim; i++) {
tmpList.appendChild(posts[i]);
}
document.getElementById("list").parentNode.replaceChild(tmpList, document.getElementById('list'));
/*for (let i = 0; i < postsLim; i++) {
list = document.getElementById('list');
list.removeChild(posts[i]);
}
console.log(list);
console.log("empty");
postsLim = 10;
for (let i = 0; i < postsLim; i++) {
list = document.getElementById('list');
list.appendChild(posts[i]);
}*/
};
defaultBtn.addEventListener('click', setToDefault);