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.
parallel-library/cps/static/js/edit_books.js

203 lines
5.4 KiB
JavaScript

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