2019-01-20 18:37:45 +00:00
|
|
|
/* This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
* Copyright (C) 2018 idalin<dalin.lin@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-07-07 19:24:29 +00:00
|
|
|
/* global _, i18nMsg, tinymce, getPath */
|
2017-02-28 06:52:55 +00:00
|
|
|
|
2017-08-13 05:24:56 +00:00
|
|
|
$(function () {
|
2021-07-07 19:24:29 +00:00
|
|
|
var msg = i18nMsg;
|
2021-08-18 19:38:20 +00:00
|
|
|
var keyword = ""
|
2021-07-07 19:24:29 +00:00
|
|
|
|
|
|
|
var templates = {
|
|
|
|
bookResult: _.template(
|
|
|
|
$("#template-book-result").html()
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2021-12-11 00:06:04 +00:00
|
|
|
function getUniqueValues(attribute_name, book){
|
|
|
|
var presentArray = $.map($("#"+attribute_name).val().split(","), $.trim);
|
|
|
|
if ( presentArray.length === 1 && presentArray[0] === "") {
|
|
|
|
presentArray = [];
|
2021-11-03 14:16:15 +00:00
|
|
|
}
|
2021-12-11 00:06:04 +00:00
|
|
|
$.each(book[attribute_name], function(i, el) {
|
|
|
|
if ($.inArray(el, presentArray) === -1) presentArray.push(el);
|
2017-02-28 06:52:55 +00:00
|
|
|
});
|
2021-12-11 00:06:04 +00:00
|
|
|
return presentArray
|
|
|
|
}
|
|
|
|
|
|
|
|
function populateForm (book) {
|
2024-08-18 16:52:13 +00:00
|
|
|
tinymce.get("comments").setContent(book.description);
|
2021-12-11 00:06:04 +00:00
|
|
|
var uniqueTags = getUniqueValues('tags', book)
|
|
|
|
var uniqueLanguages = getUniqueValues('languages', book)
|
2021-07-07 19:24:29 +00:00
|
|
|
var ampSeparatedAuthors = (book.authors || []).join(" & ");
|
2024-08-18 16:52:13 +00:00
|
|
|
$("#authors").val(ampSeparatedAuthors);
|
2024-08-18 14:27:02 +00:00
|
|
|
$("#title").val(book.title);
|
2021-11-03 14:16:15 +00:00
|
|
|
$("#tags").val(uniqueTags.join(", "));
|
2021-12-11 00:06:04 +00:00
|
|
|
$("#languages").val(uniqueLanguages.join(", "));
|
2021-07-07 19:24:29 +00:00
|
|
|
$("#rating").data("rating").setValue(Math.round(book.rating));
|
2022-01-07 11:40:03 +00:00
|
|
|
if(book.cover && $("#cover_url").length){
|
2021-07-07 19:24:29 +00:00
|
|
|
$(".cover img").attr("src", book.cover);
|
|
|
|
$("#cover_url").val(book.cover);
|
2017-04-03 17:57:45 +00:00
|
|
|
}
|
2021-07-07 19:24:29 +00:00
|
|
|
$("#pubdate").val(book.publishedDate);
|
|
|
|
$("#publisher").val(book.publisher);
|
|
|
|
if (typeof book.series !== "undefined") {
|
|
|
|
$("#series").val(book.series);
|
2021-12-11 00:06:04 +00:00
|
|
|
$("#series_index").val(book.series_index);
|
2021-07-07 19:24:29 +00:00
|
|
|
}
|
2021-12-11 00:06:04 +00:00
|
|
|
if (typeof book.identifiers !== "undefined") {
|
|
|
|
populateIdentifiers(book.identifiers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function populateIdentifiers(identifiers){
|
|
|
|
for (const property in identifiers) {
|
|
|
|
console.log(`${property}: ${identifiers[property]}`);
|
|
|
|
if ($('input[name="identifier-type-'+property+'"]').length) {
|
|
|
|
$('input[name="identifier-val-'+property+'"]').val(identifiers[property])
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
addIdentifier(property, identifiers[property])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addIdentifier(name, value){
|
|
|
|
var line = '<tr>';
|
|
|
|
line += '<td><input type="text" class="form-control" name="identifier-type-'+ name +'" required="required" placeholder="' + _("Identifier Type") +'" value="'+ name +'"></td>';
|
|
|
|
line += '<td><input type="text" class="form-control" name="identifier-val-'+ name +'" required="required" placeholder="' + _("Identifier Value") +'" value="'+ value +'"></td>';
|
|
|
|
line += '<td><a class="btn btn-default" onclick="removeIdentifierLine(this)">'+_("Remove")+'</a></td>';
|
|
|
|
line += '</tr>';
|
|
|
|
$("#identifier-table").append(line);
|
2021-07-07 19:24:29 +00:00
|
|
|
}
|
2021-07-07 19:10:38 +00:00
|
|
|
|
|
|
|
function doSearch (keyword) {
|
|
|
|
if (keyword) {
|
|
|
|
$("#meta-info").text(msg.loading);
|
|
|
|
$.ajax({
|
|
|
|
url: getPath() + "/metadata/search",
|
|
|
|
type: "POST",
|
|
|
|
data: {"query": keyword},
|
|
|
|
dataType: "json",
|
|
|
|
success: function success(data) {
|
2022-02-25 04:18:07 +00:00
|
|
|
if (data.length) {
|
|
|
|
$("#meta-info").html("<ul id=\"book-list\" class=\"media-list\"></ul>");
|
|
|
|
data.forEach(function(book) {
|
|
|
|
var $book = $(templates.bookResult(book));
|
|
|
|
$book.find("img").on("click", function () {
|
|
|
|
populateForm(book);
|
|
|
|
});
|
|
|
|
$("#book-list").append($book);
|
2021-07-07 19:10:38 +00:00
|
|
|
});
|
2022-02-25 04:18:07 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$("#meta-info").html("<p class=\"text-danger\">" + msg.no_result + "!</p>" + $("#meta-info")[0].innerHTML)
|
|
|
|
}
|
2021-07-07 19:10:38 +00:00
|
|
|
},
|
|
|
|
error: function error() {
|
|
|
|
$("#meta-info").html("<p class=\"text-danger\">" + msg.search_error + "!</p>" + $("#meta-info")[0].innerHTML);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-05-16 11:24:42 +00:00
|
|
|
|
2021-07-08 17:14:38 +00:00
|
|
|
function populate_provider() {
|
|
|
|
$("#metadata_provider").empty();
|
|
|
|
$.ajax({
|
|
|
|
url: getPath() + "/metadata/provider",
|
|
|
|
type: "get",
|
|
|
|
dataType: "json",
|
|
|
|
success: function success(data) {
|
|
|
|
data.forEach(function(provider) {
|
|
|
|
var checked = "";
|
|
|
|
if (provider.active) {
|
|
|
|
checked = "checked";
|
|
|
|
}
|
2021-08-18 19:38:20 +00:00
|
|
|
var $provider_button = '<input type="checkbox" id="show-' + provider.name + '" class="pill" data-initial="' + provider.initial + '" data-control="' + provider.id + '" ' + checked + '><label for="show-' + provider.name + '">' + provider.name + ' <span class="glyphicon glyphicon-ok"></span></label>'
|
2021-07-08 17:14:38 +00:00
|
|
|
$("#metadata_provider").append($provider_button);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-01 11:50:17 +00:00
|
|
|
$(document).on("change", ".pill", function () {
|
2021-08-18 19:38:20 +00:00
|
|
|
var element = $(this);
|
|
|
|
var id = element.data("control");
|
|
|
|
var initial = element.data("initial");
|
|
|
|
var val = element.prop('checked');
|
|
|
|
var params = {id : id, value: val};
|
|
|
|
if (!initial) {
|
|
|
|
params['initial'] = initial;
|
|
|
|
params['query'] = keyword;
|
|
|
|
}
|
2021-08-01 11:50:17 +00:00
|
|
|
$.ajax({
|
|
|
|
method:"post",
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
dataType: "json",
|
2021-08-18 19:38:20 +00:00
|
|
|
url: getPath() + "/metadata/provider/" + id,
|
|
|
|
data: JSON.stringify(params),
|
|
|
|
success: function success(data) {
|
|
|
|
element.data("initial", "true");
|
|
|
|
data.forEach(function(book) {
|
|
|
|
var $book = $(templates.bookResult(book));
|
|
|
|
$book.find("img").on("click", function () {
|
|
|
|
populateForm(book);
|
|
|
|
});
|
|
|
|
$("#book-list").append($book);
|
|
|
|
});
|
|
|
|
}
|
2021-08-01 11:50:17 +00:00
|
|
|
});
|
|
|
|
});
|
2021-07-08 17:14:38 +00:00
|
|
|
|
2021-07-07 19:10:38 +00:00
|
|
|
$("#meta-search").on("submit", function (e) {
|
|
|
|
e.preventDefault();
|
2021-08-18 19:38:20 +00:00
|
|
|
keyword = $("#keyword").val();
|
|
|
|
$('.pill').each(function(){
|
|
|
|
$(this).data("initial", $(this).prop('checked'));
|
|
|
|
});
|
2021-07-07 19:10:38 +00:00
|
|
|
doSearch(keyword);
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#get_meta").click(function () {
|
2021-07-08 17:14:38 +00:00
|
|
|
populate_provider();
|
2024-08-18 14:27:02 +00:00
|
|
|
var bookTitle = $("#title").val();
|
2021-07-08 17:14:38 +00:00
|
|
|
$("#keyword").val(bookTitle);
|
2021-08-18 19:38:20 +00:00
|
|
|
keyword = bookTitle;
|
2021-07-08 17:14:38 +00:00
|
|
|
doSearch(bookTitle);
|
2021-07-07 19:10:38 +00:00
|
|
|
});
|
2021-07-31 07:00:34 +00:00
|
|
|
$("#metaModal").on("show.bs.modal", function(e) {
|
|
|
|
$(e.relatedTarget).one('focus', function (e) {
|
|
|
|
$(this).blur();
|
|
|
|
});
|
|
|
|
});
|
2017-04-21 18:44:17 +00:00
|
|
|
});
|