You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
calibre-web/cps/static/js/edit_books.js

202 lines
5.4 KiB
JavaScript

/**
* Created by SpeedProg on 05.04.2015.
*/
/* global Bloodhound, language */
7 years ago
tinymce.init({
selector: '#description',
branding: false,
menubar: 'edit view format',
language: language
});
if (!Modernizr.inputtypes.date) {
$('#pubdate').datepicker({
format: 'yyyy-mm-dd',
language: language
}).on('change', function () {
// Show localized date over top of the standard YYYY-MM-DD date
let pubDate, results;
if ((results = /(\d{4})[-\/\\](\d{1,2})[-\/\\](\d{1,2})/.exec(this.value))) { // YYYY-MM-DD
pubDate = new Date(results[1], parseInt(results[2], 10)-1, results[3]) || new Date(this.value);
}
$('#fake_pubdate')
.val(pubDate.toLocaleDateString(language))
.removeClass('hidden');
}).trigger('change');
}
/*
Takes a prefix, query typeahead callback, Bloodhound typeahead adapter
and returns the completions it gets from the bloodhound engine prefixed.
*/
7 years ago
function prefixedSource(prefix, query, cb, bhAdapter) {
7 years ago
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);
});
}
7 years ago
function getPath(){
7 years ago
var jsFileLocation = $("script[src*=edit_books]").attr("src"); // the js file path
7 years ago
jsFileLocation = jsFileLocation.replace("/static/js/edit_books.js", ""); // the js folder path
return jsFileLocation;
}
var authors = new Bloodhound({
7 years ago
name: "authors",
7 years ago
datumTokenizer(datum) {
return [datum.name];
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
7 years ago
url: getPath()+"/get_authors_json?q=%QUERY"
}
});
var series = new Bloodhound({
7 years ago
name: "series",
7 years ago
datumTokenizer(datum) {
return [datum.name];
},
7 years ago
queryTokenizer(query) {
return [query];
},
remote: {
7 years ago
url: getPath()+"/get_series_json?q=",
7 years ago
replace(url, query) {
7 years ago
return url+encodeURIComponent(query);
}
}
});
7 years ago
var tags = new Bloodhound({
7 years ago
name: "tags",
7 years ago
datumTokenizer(datum) {
return [datum.name];
},
7 years ago
queryTokenizer(query) {
7 years ago
var tokens = query.split(",");
tokens = [tokens[tokens.length-1].trim()];
7 years ago
return tokens;
},
remote: {
7 years ago
url: getPath()+"/get_tags_json?q=%QUERY"
}
});
var languages = new Bloodhound({
7 years ago
name: "languages",
7 years ago
datumTokenizer(datum) {
return [datum.name];
},
7 years ago
queryTokenizer(query) {
return [query];
},
remote: {
7 years ago
url: getPath()+"/get_languages_json?q=",
7 years ago
replace(url, query) {
7 years ago
return url+encodeURIComponent(query);
}
}
});
7 years ago
function sourceSplit(query, cb, split, source) {
var bhAdapter = source.ttAdapter();
7 years ago
var tokens = query.split(split);
var currentSource = tokens[tokens.length-1].trim();
tokens.splice(tokens.length-1, 1); // remove last element
var prefix = "";
7 years ago
var newSplit;
if (split === "&"){
newSplit = " " + split + " ";
}else{
newSplit = split + " ";
}
for (var i = 0; i < tokens.length; i++) {
7 years ago
prefix += tokens[i].trim() + newSplit;
}
7 years ago
prefixedSource(prefix, currentSource, cb, bhAdapter);
}
7 years ago
var promiseAuthors = authors.initialize();
promiseAuthors.done(function(){
$("#bookAuthor").typeahead(
{
highlight: true, minLength: 1,
hint: true
}, {
name: "authors",
displayKey: "name",
7 years ago
source(query, cb){
7 years ago
return sourceSplit(query, cb, "&", authors); //sourceSplit //("&")
}
});
});
var promiseSeries = series.initialize();
promiseSeries.done(function(){
$("#series").typeahead(
{
highlight: true, minLength: 0,
hint: true
}, {
7 years ago
name: "series",
7 years ago
displayKey: "name",
7 years ago
source: series.ttAdapter()
}
7 years ago
);
});
7 years ago
var promiseTags = tags.initialize();
promiseTags.done(function(){
$("#tags").typeahead(
{
highlight: true, minLength: 0,
hint: true
}, {
name: "tags",
displayKey: "name",
7 years ago
source(query, cb){
7 years ago
return sourceSplit(query, cb, ",", tags);
}
});
});
var promiseLanguages = languages.initialize();
promiseLanguages.done(function(){
$("#languages").typeahead(
{
highlight: true, minLength: 0,
hint: true
}, {
name: "languages",
displayKey: "name",
7 years ago
source(query, cb){
7 years ago
return sourceSplit(query, cb, ",", languages); //(",")
}
});
});
$("#search").on("change input.typeahead:selected", function(){
7 years ago
var form = $("form").serialize();
7 years ago
$.getJSON( getPath()+"/get_matching_tags", form, function( data ) {
7 years ago
$(".tags_click").each(function() {
if ($.inArray(parseInt($(this).children("input").first().val(), 10), data.tags) === -1 ) {
if (!($(this).hasClass("active"))) {
$(this).addClass("disabled");
}
}
else {
7 years ago
$(this).removeClass("disabled");
}
});
});
});