/
script.js
141 lines (133 loc) · 4.24 KB
/
script.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
const request = new Request('./videos.json', { method: 'GET' });
let min;
let sek;
let skil;
const results = document.querySelector('.body');
let div0;
let div1;
let div2;
let div3;
let div4;
let div5;
let link;
let mynd;
let h1;
let h2;
let timinn;
let p1;
let i;
let j;
let texti;
const videoSida = './video.html?id=';
function removeBuffering() {
const buff = document.querySelector('.buff');
const main = document.querySelector('main');
main.removeChild(buff);
}
function buffering() {
const main = document.querySelector('main');
const buff = document.createElement('h1');
const textNode = document.createTextNode('Hleð inn gögn....');
buff.appendChild(textNode);
buff.classList.add('buff');
main.appendChild(buff);
}
function timiSidan(msek) {
const d = new Date();
const timi = d - msek;
if (Math.floor(timi / 1000 / 60 / 60 / 24 / 365) > 0) {
texti = `Fyrir ${Math.floor(timi / 1000 / 60 / 60 / 24 / 365)} árum síðan`;
return texti;
} else if (Math.floor(timi / 1000 / 60 / 60 / 24 / 30) > 0) {
texti = `Fyrir ${Math.floor(timi / 1000 / 60 / 60 / 24 / 30)} mánuðum síðan`;
return texti;
} else if (Math.floor(timi / 1000 / 60 / 60 / 24 / 7) > 0) {
texti = `Fyrir ${Math.floor(timi / 1000 / 60 / 60 / 24 / 7)} vikum síðan`;
return texti;
} else if (Math.floor(timi / 1000 / 60 / 60 / 24) > 0) {
texti = `Fyrir ${Math.floor(timi / 1000 / 60 / 60 / 24)} dögum síðan`;
return texti;
}
texti = `Fyrir ${Math.floor(timi / 1000 / 60 / 60)} sek síðan`;
return texti;
}
function lengdMyndbands(seconds) {
min = Math.floor(seconds / 60);
sek = Math.floor(seconds % 60);
if (sek < 10) {
sek = `0${sek}`;
}
if (min < 10) {
min = `0${min}`;
}
skil = `${min}:${sek}`;
return skil;
}
function addText(obj) {
div0 = document.createElement('div');
for (j = 0; j < obj.categories.length; j += 1) {
div1 = document.createElement('div');
div2 = document.createElement('div');
div5 = document.createElement('div');
h1 = document.createElement('h1');
div2.appendChild(h1);
div1.appendChild(div2);
h1.appendChild(document.createTextNode(obj.categories[j].title));
div0.appendChild(div1);
div1.setAttribute('class', 'row kafli');
div2.setAttribute('class', 'col');
div5.setAttribute('class', 'row');
for (i = 0; i < obj.categories[j].videos.length; i += 1) {
div3 = document.createElement('div');
div4 = document.createElement('div');
link = document.createElement('a');
mynd = document.createElement('img');
h2 = document.createElement('h2');
p1 = document.createElement('p');
timinn = document.createElement('div');
div3.appendChild(div4);
div4.appendChild(link);
link.appendChild(mynd);
div4.appendChild(timinn);
div4.appendChild(h2);
div4.appendChild(p1);
div3.setAttribute('class', 'col col-12 col-m-6 col-l-4');
link.setAttribute('href', videoSida + obj.videos[obj.categories[j].videos[i] - 1].id);
div4.setAttribute('class', 'container');
mynd.src = obj.videos[obj.categories[j].videos[i] - 1].poster;
mynd.setAttribute('class', 'mynd');
timinn.setAttribute('class', 'timinn');
mynd.setAttribute('id', obj.videos[obj.categories[j].videos[i] - 1].id);
texti = timiSidan(obj.videos[obj.categories[j].videos[i] - 1].created);
p1.appendChild(document.createTextNode(texti));
h2.appendChild(document.createTextNode(obj.videos[obj.categories[j].videos[i] - 1].title));
texti = lengdMyndbands(obj.videos[obj.categories[j].videos[i] - 1].duration);
timinn.appendChild(document.createTextNode(texti));
div5.appendChild(div3);
}
div0.appendChild(div5);
}
results.appendChild(div0);
const grid = document.querySelector('.body');
removeBuffering();
grid.hidden = false;
}
function setUp() {
fetch(request)
.then((response) => {
if (response.status === 200) {
return response.json();
}
throw new Error('Something went wrong on api server!');
})
.then((response) => {
console.debug(response);
const json = response;
addText(json);
})
.catch((error) => {
console.error(error);
});
}
buffering();
document.addEventListener('DOMContentLoaded', setUp);