Code cosmetics

This commit is contained in:
OzzieIsaacs
2017-04-02 10:05:07 +02:00
parent 66181daf6d
commit 7c6d527a55
17 changed files with 254 additions and 255 deletions
+10 -11
View File
@@ -8,7 +8,7 @@
Takes a prefix, query typeahead callback, Bloodhound typeahead adapter
and returns the completions it gets from the bloodhound engine prefixed.
*/
function prefixed_source(prefix, query, cb, bhAdapter) {
function prefixedSource(prefix, query, cb, bhAdapter) {
bhAdapter(query, function(retArray){
var matches = [];
for (var i = 0; i < retArray.length; i++) {
@@ -18,7 +18,7 @@ function prefixed_source(prefix, query, cb, bhAdapter) {
cb(matches);
});
}
function get_path(){
function getPath(){
var jsFileLocation = $("script[src*=edit_books]").attr("src"); // the js file path
jsFileLocation = jsFileLocation.replace("/static/js/edit_books.js", ''); // the js folder path
return jsFileLocation;
@@ -31,7 +31,7 @@ var authors = new Bloodhound({
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: get_path()+"/get_authors_json?q=%QUERY"
url: getPath()+"/get_authors_json?q=%QUERY"
}
});
@@ -44,7 +44,7 @@ var series = new Bloodhound({
return [query];
},
remote: {
url: get_path()+"/get_series_json?q=",
url: getPath()+"/get_series_json?q=",
replace: function(url, query) {
return url+encodeURIComponent(query);
}
@@ -63,7 +63,7 @@ var tags = new Bloodhound({
return tokens;
},
remote: {
url: get_path()+"/get_tags_json?q=%QUERY"
url: getPath()+"/get_tags_json?q=%QUERY"
}
});
@@ -76,10 +76,9 @@ var languages = new Bloodhound({
return [query];
},
remote: {
url: get_path()+"/get_languages_json?q=",
url: getPath()+"/get_languages_json?q=",
replace: function(url, query) {
url_query = url+encodeURIComponent(query);
return url_query;
return url+encodeURIComponent(query);
}
}
});
@@ -101,7 +100,7 @@ function sourceSplit(query, cb, split, source) {
for (var i = 0; i < tokens.length; i++) {
prefix += tokens[i].trim() + newSplit;
}
prefixed_source(prefix, currentSource, cb, bhAdapter);
prefixedSource(prefix, currentSource, cb, bhAdapter);
}
var promiseAuthors = authors.initialize();
@@ -130,7 +129,7 @@ var promiseSeries = series.initialize();
displayKey: "name",
source: series.ttAdapter()
}
)
);
});
var promiseTags = tags.initialize();
@@ -165,7 +164,7 @@ var promiseLanguages = languages.initialize();
$("form").on("change input typeahead:selected", function(data){
var form = $("form").serialize();
$.getJSON( get_path()+"/get_matching_tags", form, function( data ) {
$.getJSON( getPath()+"/get_matching_tags", form, function( data ) {
$(".tags_click").each(function() {
if ($.inArray(parseInt($(this).children("input").first().val(), 10), data.tags) === -1 ) {
if (!($(this).hasClass("active"))) {
+93 -93
View File
@@ -9,36 +9,95 @@ $(document).ready(function () {
var msg = i18nMsg;
var douban = "https://api.douban.com";
var dbSearch = "/v2/book/search";
var db_get_info = "/v2/book/";
var db_get_info_by_isbn = "/v2/book/isbn/ ";
// var dbGetInfo = "/v2/book/";
// var db_get_info_by_isbn = "/v2/book/isbn/ ";
var dbDone = false;
var google = "https://www.googleapis.com/";
var gg_search = "/books/v1/volumes";
var gg_get_info = "/books/v1/volumes/";
var gg_done = false;
var ggSearch = "/books/v1/volumes";
// var gg_get_info = "/books/v1/volumes/";
var ggDone = false;
var db_results = [];
var gg_results = [];
var show_flag = 0;
String.prototype.replaceAll = function (s1, s2) {  
var dbResults = [];
var ggResults = [];
var showFlag = 0;
String.prototype.replaceAll = function (s1, s2) {
return this.replace(new RegExp(s1, "gm"), s2);
};
function gg_search_book (title) {
function showResult () {
showFlag++;
if (showFlag === 1) {
$("#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>");
return;
}
}
if (ggDone && ggResults.length > 0) {
for (var i = 0; i < ggResults.length; i++) {
var book = ggResults[i];
var bookCover;
if (book.volumeInfo.imageLinks) {
bookCover = book.volumeInfo.imageLinks.thumbnail;
} else {
bookCover = "/static/generic_cover.jpg";
}
var 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:get_meta("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>' +
"<p>"+ msg.author +":" + book.volumeInfo.authors + "</p>" +
"<p>"+ msg.publisher + ":" + book.volumeInfo.publisher + "</p>" +
"<p>"+ msg.description + ":" + book.volumeInfo.description + "</p>" +
"<p>"+ msg.source + ':<a href="https://books.google.com" target="_blank">Google Books</a></p>' +
"</div>" +
"</li>";
$("#book-list").append(bookHtml);
}
ggDone = false;
}
if (dbDone && dbResults.length > 0) {
for (var i = 0; i < dbResults.length; i++) {
var book = dbResults[i];
var 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:get_meta("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>" +
"<p>" + msg.author + ":" + book.author + "</p>" +
"<p>" + msg.publisher + ":" + book.publisher + "</p>" +
"<p>" + msg.description + ":" + book.summary + "</p>" +
"<p>" + msg.source + ':<a href="https://book.douban.com" target="_blank">Douban Books</a></p>' +
"</div>" +
"</li>";
$("#book-list").append(bookHtml);
}
dbDone = false;
}
}
function ggSearchBook (title) {
title = title.replaceAll(/\s+/, "+");
var url = google + gg_search + "?q=" + title;
var url = google + ggSearch + "?q=" + title;
$.ajax({
url,
type: "GET",
dataType: "jsonp",
jsonp: "callback",
success: function (data) {
gg_results = data.items;
ggResults = data.items;
},
complete: function () {
gg_done = true;
show_result();
ggDone = true;
showResult();
}
});
}
@@ -47,7 +106,7 @@ $(document).ready(function () {
var meta;
var tags;
if (source === "google") {
meta = gg_results[id];
meta = ggResults[id];
$("#description").val(meta.volumeInfo.description);
$("#bookAuthor").val(meta.volumeInfo.authors.join(" & "));
$("#book_title").val(meta.volumeInfo.title);
@@ -61,13 +120,13 @@ $(document).ready(function () {
return;
}
if (source === "douban") {
meta = db_results[id];
meta = dbResults[id];
$("#description").val(meta.summary);
$("#bookAuthor").val(meta.author.join(" & "));
$("#book_title").val(meta.title);
tags = '';
tags = "";
for (var i = 0; i < meta.tags.length; i++) {
tags = tags + meta.tags[i].title + ',';
tags = tags + meta.tags[i].title + ",";
}
$("#tags").val(tags);
$("#rating").val(Math.round(meta.rating.average / 2));
@@ -75,91 +134,32 @@ $(document).ready(function () {
}
}
function do_search (keyword) {
show_flag = 0;
function doSearch (keyword) {
showFlag = 0;
$("#meta-info").text(msg.loading);
var keyword = $("#keyword").val();
// var keyword = $("#keyword").val();
if (keyword) {
db_search_book(keyword);
gg_search_book(keyword);
dbSearchBook(keyword);
ggSearchBook(keyword);
}
}
function show_result () {
show_flag++;
if (show_flag == 1) {
$('#meta-info').html('<ul id="book-list" class="media-list"></ul>');
}
if (gg_done && dbDone) {
if (!gg_results && !db_results) {
$('#meta-info').html('<p class="text-danger">'+ msg.no_result +'</p>');
return;
}
}
if (gg_done && gg_results.length > 0) {
for (var i = 0; i < gg_results.length; i++) {
var book = gg_results[i];
var book_cover;
if (book.volumeInfo.imageLinks) {
book_cover = book.volumeInfo.imageLinks.thumbnail;
} else {
book_cover = '/static/generic_cover.jpg';
}
var book_html = '<li class="media">' +
'<img class="pull-left img-responsive" data-toggle="modal" data-target="#metaModal" src="' +
book_cover + '" alt="Cover" style="width:100px;height:150px" onclick=\'javascript:get_meta("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>' +
'<p>'+ msg.author +':' + book.volumeInfo.authors + '</p>' +
'<p>'+ msg.publisher + ':' + book.volumeInfo.publisher + '</p>' +
'<p>'+ msg.description + ':' + book.volumeInfo.description + '</p>' +
'<p>'+ msg.source + ':<a href="https://books.google.com" target="_blank">Google Books</a></p>' +
'</div>' +
'</li>';
$("#book-list").append(book_html);
}
gg_done = false;
}
if (dbDone && db_results.length > 0) {
for (var i = 0; i < db_results.length; i++) {
var book = db_results[i];
var book_html = '<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:get_meta("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>' +
'<p>' + msg.author + ':' + book.author + '</p>' +
'<p>' + msg.publisher + ':' + book.publisher + '</p>' +
'<p>' + msg.description + ':' + book.summary + '</p>' +
'<p>' + msg.source + ':<a href="https://book.douban.com" target="_blank">Douban Books</a></p>' +
'</div>' +
"</li>";
$("#book-list").append(book_html);
}
dbDone = false;
}
}
db_search_book = function (title) {
var url = douban + dbSearch + '?q=' + title + '&fields=all&count=10';
function dbSearchBook (title) {
var url = douban + dbSearch + "?q=" + title + "&fields=all&count=10";
$.ajax({
url: url,
type: "GET",
dataType: "jsonp",
jsonp: 'callback',
jsonp: "callback",
success: function (data) {
db_results = data.books;
dbResults = data.books;
},
error: function () {
$('#meta-info').html('<p class="text-danger">'+ msg.search_error+'!</p>');
$("#meta-info").html('<p class="text-danger">'+ msg.search_error+"!</p>");
},
complete: function () {
dbDone = true;
show_result();
showResult();
}
});
}
@@ -167,15 +167,15 @@ $(document).ready(function () {
$("#do-search").click(function () {
var keyword = $("#keyword").val();
if (keyword) {
do_search(keyword);
doSearch(keyword);
}
});
$("#get_meta").click(function () {
var book_title = $("#book_title").val();
if (book_title) {
$("#keyword").val(book_title);
do_search(book_title);
var bookTitle = $("#book_title").val();
if (bookTitle) {
$("#keyword").val(bookTitle);
doSearch(bookTitle);
}
});
+6 -6
View File
@@ -49,13 +49,13 @@ $(function() {
});
});
$("#check_for_update").click(function() {
var button_text = $("#check_for_update").html();
var buttonText = $("#check_for_update").html();
$("#check_for_update").html("...");
$.ajax({
dataType: "json",
url: window.location.pathname+"/../../get_update_status",
success: function(data) {
$("#check_for_update").html(button_text);
$("#check_for_update").html(buttonText);
if (data.status === true) {
$("#check_for_update").addClass("hidden");
$("#perform_update").removeClass("hidden");
@@ -67,7 +67,7 @@ $(function() {
});
$("#restart_database").click(function() {
$.ajax({
dataType: 'json',
dataType: "json",
url: window.location.pathname+"/../../shutdown",
data: {"parameter":2}
});
@@ -76,7 +76,7 @@ $(function() {
$("#spinner2").show();
$.ajax({
type: "POST",
dataType: 'json',
dataType: "json",
data: { start: "True"},
url: window.location.pathname+"/../../get_updater_status",
success: function(data) {
@@ -94,10 +94,10 @@ $(function() {
function updateTimer() {
$.ajax({
dataType: 'json',
dataType: "json",
url: window.location.pathname+"/../../get_updater_status",
success: function(data) {
console.log(data.status);
// console.log(data.status);
$("#UpdateprogressDialog #Updatecontent").html(updateText[data.status]);
if (data.status >6){
clearInterval(updateTimerID);