calibre-web/cps/static/js/edit_books.js

204 lines
5.2 KiB
JavaScript
Raw Normal View History

/**
* Created by SpeedProg on 05.04.2015.
*/
/*
Takes a prefix, query typeahead callback, Bloodhound typeahead adapter
and returns the completions it gets from the bloodhound engine prefixed.
*/
2017-03-29 21:43:55 +02:00
function prefixed_source(prefix, query, cb, bhAdapter) {
bhAdapter(query, function(retArray){
var matches = [];
for (var i = 0; i < retArray.length; i++) {
var obj = {name : prefix + retArray[i].name};
matches.push(obj);
}
cb(matches);
});
}
function get_path(){
2017-03-30 21:17:18 +02:00
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;
}
var authors = new Bloodhound({
2017-03-29 21:43:55 +02:00
name: "authors",
datumTokenizer: function(datum) {
return [datum.name];
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
2017-03-30 21:17:18 +02:00
url: get_path()+"/get_authors_json?q=%QUERY"
}
});
function authors_source(query, cb) {
2017-03-29 21:43:55 +02:00
var bhAdapter = authors.ttAdapter();
var tokens = query.split("&");
var current_author = tokens[tokens.length-1].trim();
tokens.splice(tokens.length-1, 1); // remove last element
var prefix = "";
for (var i = 0; i < tokens.length; i++) {
var author = tokens[i].trim();
prefix += author + " & ";
}
2017-03-29 21:43:55 +02:00
prefixed_source(prefix, current_author, cb, bhAdapter);
}
var promise = authors.initialize();
promise.done(function(){
$("#bookAuthor").typeahead(
{
highlight: true, minLength: 1,
hint: true
}, {
2017-03-30 21:17:18 +02:00
name: "authors",
displayKey: "name",
source: authors_source
}
)
});
var series = new Bloodhound({
2017-03-29 21:43:55 +02:00
name: "series",
datumTokenizer: function(datum) {
return [datum.name];
},
queryTokenizer: function(query) {
return [query];
},
remote: {
2017-03-30 21:17:18 +02:00
url: get_path()+"/get_series_json?q=",
replace: function(url, query) {
2017-03-30 21:17:18 +02:00
return url+encodeURIComponent(query);
}
}
});
var promise = series.initialize();
promise.done(function(){
$("#series").typeahead(
{
highlight: true, minLength: 0,
hint: true
}, {
2017-03-30 21:17:18 +02:00
name: "series",
displayKey: "name",
source: series.ttAdapter()
}
)
});
var tags = new Bloodhound({
2017-03-29 21:43:55 +02:00
name: "tags",
datumTokenizer: function(datum) {
return [datum.name];
},
queryTokenizer: function(query) {
2017-03-30 21:17:18 +02:00
var tokens = query.split(",");
tokens = [tokens[tokens.length-1].trim()];
2017-03-30 21:17:18 +02:00
return tokens;
},
remote: {
2017-03-29 21:43:55 +02:00
url: get_path()+"/get_tags_json?q=%QUERY"
}
});
function tag_source(query, cb) {
2017-03-29 21:43:55 +02:00
var bhAdapter = tags.ttAdapter();
var tokens = query.split(",");
var current_tag = tokens[tokens.length-1].trim();
tokens.splice(tokens.length-1, 1); // remove last element
var prefix = "";
for (var i = 0; i < tokens.length; i++) {
var tag = tokens[i].trim();
prefix += tag + ", ";
}
2017-03-29 21:43:55 +02:00
prefixed_source(prefix, current_tag, cb, bhAdapter);
}
var promise = tags.initialize();
promise.done(function(){
$("#tags").typeahead(
{
highlight: true, minLength: 0,
hint: true
}, {
2017-03-30 21:17:18 +02:00
name: "tags",
displayKey: "name",
source: tag_source
}
)
});
var languages = new Bloodhound({
2017-03-29 21:43:55 +02:00
name: "languages",
datumTokenizer: function(datum) {
return [datum.name];
},
queryTokenizer: function(query) {
return [query];
},
remote: {
2017-03-30 21:17:18 +02:00
url: get_path()+"/get_languages_json?q=",
replace: function(url, query) {
url_query = url+encodeURIComponent(query);
return url_query;
}
}
});
function language_source(query, cb) {
2017-03-29 21:43:55 +02:00
var bhAdapter = languages.ttAdapter();
var tokens = query.split(",");
2017-03-29 21:43:55 +02:00
var currentLanguage = tokens[tokens.length-1].trim();
tokens.splice(tokens.length-1, 1); // remove last element
var prefix = "";
for (var i = 0; i < tokens.length; i++) {
var tag = tokens[i].trim();
prefix += tag + ", ";
}
2017-03-29 21:43:55 +02:00
prefixed_source(prefix, currentLanguage, cb, bhAdapter);
}
var promise = languages.initialize();
promise.done(function(){
$("#languages").typeahead(
{
highlight: true, minLength: 0,
hint: true
}, {
2017-03-30 21:17:18 +02:00
name: "languages",
displayKey: "name",
source: language_source
}
)
});
2017-03-30 21:17:18 +02:00
$("form").on("change input typeahead:selected", function(data){
var form = $("form").serialize();
$.getJSON( get_path()+"/get_matching_tags", form, function( data ) {
2017-03-30 21:17:18 +02:00
$(".tags_click").each(function() {
if ($.inArray(parseInt($(this).children("input").first().val(), 10), data.tags) === -1 ) {
if (!($(this).hasClass("active"))) {
$(this).addClass("disabled");
}
}
else {
2017-03-30 21:17:18 +02:00
$(this).removeClass("disabled");
}
});
});
});