This commit is contained in:
Ozzie Isaacs 2017-04-21 20:44:17 +02:00
parent 18744975b3
commit ede8ae6742
3 changed files with 49 additions and 45 deletions

View File

@ -5,6 +5,8 @@
* Douban Books api document: https://developers.douban.com/wiki/?title=book_v2 (Chinese Only)
*/
/* global i18nMsg */
var dbResults = [];
var ggResults = [];
$(document).ready(function () {
var msg = i18nMsg;
@ -19,8 +21,6 @@ $(document).ready(function () {
// var gg_get_info = "/books/v1/volumes/";
var ggDone = false;
var dbResults = [];
var ggResults = [];
var showFlag = 0;
String.prototype.replaceAll = function (s1, s2) {
return this.replace(new RegExp(s1, "gm"), s2);
@ -32,11 +32,11 @@ $(document).ready(function () {
var bookHtml;
showFlag++;
if (showFlag === 1) {
$("#meta-info").html("<ul id=\"book-list\" class=\"media-list\"></ul>");
$("#metaModal #meta-info").html("<ul id=\"book-list\" class=\"media-list\"></ul>");
}
if (ggDone && dbDone) {
if (!ggResults && !dbResults) {
$("#meta-info").html("<p class=\"text-danger\">"+ msg.no_result +"</p>");
$("#metaModal #meta-info").html("<p class=\"text-danger\">"+ msg.no_result +"</p>");
return;
}
}
@ -51,8 +51,8 @@ $(document).ready(function () {
}
bookHtml = "<li class=\"media\">" +
"<img class=\"pull-left img-responsive\" data-toggle=\"modal\" data-target=\"#metaModal\" src=\"" +
bookCover + "\" alt=\"Cover\" style=\"width:100px;height:150px\" onclick='javascript:getMeta(\"google\"," +
i + ")\\>\"" +
bookCover + "\" alt=\"Cover\" style=\"width:100px;height:150px\" onclick='getMeta(\"google\"," +
i + ")'>" +
"<div class=\"media-body\">" +
"<h4 class=\"media-heading\"><a href=\"https://books.google.com/books?id=" +
book.id + "\" target=\"_blank\">" + book.volumeInfo.title + "</a></h4>" +
@ -62,7 +62,7 @@ $(document).ready(function () {
"<p>"+ msg.source + ":<a href=\"https://books.google.com\" target=\"_blank\">Google Books</a></p>" +
"</div>" +
"</li>";
$("#book-list").append(bookHtml);
$("#metaModal #book-list").append(bookHtml);
}
ggDone = false;
}
@ -71,8 +71,8 @@ $(document).ready(function () {
book = dbResults[i];
bookHtml = "<li class=\"media\">" +
"<img class=\"pull-left img-responsive\" data-toggle=\"modal\" data-target=\"#metaModal\" src=\"" +
book.image + "\" alt=\"Cover\" style=\"width:100px;height: 150px\" onclick='javascript:getMeta(\"douban\"," +
i + ")\\'>" +
book.image + "\" alt=\"Cover\" style=\"width:100px;height: 150px\" onclick='getMeta(\"douban\"," +
i + ")'>" +
"<div class=\"media-body\">" +
"<h4 class=\"media-heading\"><a href=\"https://book.douban.com/subject/" +
book.id + "\" target=\"_blank\">" + book.title + "</a></h4>" +
@ -82,7 +82,7 @@ $(document).ready(function () {
"<p>" + msg.source + ":<a href=\"https://book.douban.com\" target=\"_blank\">Douban Books</a></p>" +
"</div>" +
"</li>";
$("#book-list").append(bookHtml);
$("#metaModal #book-list").append(bookHtml);
}
dbDone = false;
}
@ -106,38 +106,6 @@ $(document).ready(function () {
});
}
function getMeta (source, id) {
var meta;
var tags;
if (source === "google") {
meta = ggResults[id];
$("#description").val(meta.volumeInfo.description);
$("#bookAuthor").val(meta.volumeInfo.authors.join(" & "));
$("#book_title").val(meta.volumeInfo.title);
if (meta.volumeInfo.categories) {
tags = meta.volumeInfo.categories.join(",");
$("#tags").val(tags);
}
if (meta.volumeInfo.averageRating) {
$("#rating").val(Math.round(meta.volumeInfo.averageRating));
}
return;
}
if (source === "douban") {
meta = dbResults[id];
$("#description").val(meta.summary);
$("#bookAuthor").val(meta.author.join(" & "));
$("#book_title").val(meta.title);
tags = "";
for (var i = 0; i < meta.tags.length; i++) {
tags = tags + meta.tags[i].title + ",";
}
$("#tags").val(tags);
$("#rating").val(Math.round(meta.rating.average / 2));
return;
}
}
function dbSearchBook (title) {
var url = douban + dbSearch + "?q=" + title + "&fields=all&count=10";
$.ajax({
@ -149,7 +117,7 @@ $(document).ready(function () {
dbResults = data.books;
},
error () {
$("#meta-info").html("<p class=\"text-danger\">"+ msg.search_error+"!</p>");
$("#metaModal #meta-info").html("<p class=\"text-danger\">"+ msg.search_error+"!</p>");
},
complete () {
dbDone = true;
@ -160,7 +128,7 @@ $(document).ready(function () {
function doSearch (keyword) {
showFlag = 0;
$("#meta-info").text(msg.loading);
$("#metaModal #meta-info").text(msg.loading);
// var keyword = $("#keyword").val();
if (keyword) {
dbSearchBook(keyword);
@ -183,4 +151,36 @@ $(document).ready(function () {
}
});
});
});
function getMeta (source, id) {
var meta;
var tags;
if (source === "google") {
meta = ggResults[id];
$("#description").val(meta.volumeInfo.description);
$("#bookAuthor").val(meta.volumeInfo.authors.join(" & "));
$("#book_title").val(meta.volumeInfo.title);
if (meta.volumeInfo.categories) {
tags = meta.volumeInfo.categories.join(",");
$("#tags").val(tags);
}
if (meta.volumeInfo.averageRating) {
$("#rating").val(Math.round(meta.volumeInfo.averageRating));
}
return;
}
if (source === "douban") {
meta = dbResults[id];
$("#description").val(meta.summary);
$("#bookAuthor").val(meta.author.join(" & "));
$("#book_title").val(meta.title);
tags = "";
for (var i = 0; i < meta.tags.length; i++) {
tags = tags + meta.tags[i].title + ",";
}
$("#tags").val(tags);
$("#rating").val(Math.round(meta.rating.average / 2));
return;
}
}

View File

@ -117,6 +117,9 @@
</form>
{% endif %}
{% endblock %}
{% block modal %}
{% if g.user.role_delete_books() %}
<div class="modal fade" id="deleteModal" role="dialog" aria-labelledby="metaDeleteLabel">
<div class="modal-dialog">

View File

@ -188,6 +188,7 @@
</div>
</div>
</div>
{% block modal %}{% endblock %}
{% block js %}{% endblock %}
</body>
</html>