forked from IvanYurchenko/fantasyworldsrating
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
87 lines (76 loc) · 2.79 KB
/
code.js
File metadata and controls
87 lines (76 loc) · 2.79 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
/**
* Created by Ivan Yurchenko on 7/4/17.
*/
$(document).ready(function () {
function httpGet(url, callback) {
var request = new XMLHttpRequest();
request.onload = function () {
if (request.status == 200) {
callback(request.response);
} else {
request.onerror();
}
}
request.onerror = function () {
console.log('Request failed ' + request.statusText);
$('div.container').append('An error has occurred. Detailed info: '
+ request.statusText);
}
request.open('GET', url, true);
request.send(null);
}
function showSpinner() {
// Spinner configuration
var opts = { lines: 13, length: 26, width: 12, radius: 36, scale: 0.5 };
var target = document.getElementById('spinner');
var spinner = new Spinner(opts).spin(target);
return function() {
spinner.stop();
};
}
// Show spinner and store function that stops it
var stopSpinner = showSpinner();
httpGet('https://api.fantasy-worlds.org/books', function (response) {
var items = JSON.parse(response).items;
var books = items.map(function (item) {
var ratingStr = item[6];
var book = {
id: parseInt(item[0]),
authorName: item[1],
authorSurname: item[2],
titleRu: item[3],
titleEn: item[4],
year: item[7],
rating: parseFloat(ratingStr),
peopleRated: parseInt(ratingStr.substr(ratingStr.indexOf('/') + 1))
};
return book;
});
books.sort(function (a, b) {
const bRating = b.peopleRated > 0 ? Math.log2(b.peopleRated) * b.rating : 0;
const aRating = a.peopleRated > 0 ? Math.log2(a.peopleRated) * a.rating : 0;
const result = bRating - aRating;
return result;
});
// Append all books to the document
var container = $('div.container');
var div = $('<div></div>');
for (var i = 0; i < books.length; i++) {
var book = books[i];
var a = $('<a></a>');
var innerDiv = $('<div></div>');
a.attr('href', 'https://fantasy-worlds.org/lib/id' + book.id);
var text = '(' + book.rating + '/10 от ' + book.peopleRated + ') ' + book.titleRu + ' - '
+ book.authorName + ' ' + book.authorSurname;
if(!!book.year) {
text += ' [' + book.year + ']';
}
a.text(text);
innerDiv.append(a);
div.append(innerDiv);
}
container.append(div);
// Stop the spinner
stopSpinner();
});
});