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

181 lines
4.8 KiB
JavaScript

/**
* Created by SpeedProg on 05.04.2015.
*/
7 years ago
/* global Bloodhound */
/*
Takes a prefix, query typeahead callback, Bloodhound typeahead adapter
and returns the completions it gets from the bloodhound engine prefixed.
*/
7 years ago
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(){
7 years ago
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({
7 years ago
name: "authors",
datumTokenizer: function(datum) {
return [datum.name];
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
7 years ago
url: get_path()+"/get_authors_json?q=%QUERY"
}
});
var series = new Bloodhound({
7 years ago
name: "series",
datumTokenizer: function(datum) {
return [datum.name];
},
queryTokenizer: function(query) {
return [query];
},
remote: {
7 years ago
url: get_path()+"/get_series_json?q=",
replace: function(url, query) {
7 years ago
return url+encodeURIComponent(query);
}
}
});
7 years ago
var tags = new Bloodhound({
7 years ago
name: "tags",
datumTokenizer: function(datum) {
return [datum.name];
},
queryTokenizer: function(query) {
7 years ago
var tokens = query.split(",");
tokens = [tokens[tokens.length-1].trim()];
7 years ago
return tokens;
},
remote: {
7 years ago
url: get_path()+"/get_tags_json?q=%QUERY"
}
});
var languages = new Bloodhound({
7 years ago
name: "languages",
datumTokenizer: function(datum) {
return [datum.name];
},
queryTokenizer: function(query) {
return [query];
},
remote: {
7 years ago
url: get_path()+"/get_languages_json?q=",
replace: function(url, query) {
url_query = url+encodeURIComponent(query);
return url_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
prefixed_source(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",
source: function(query, cb){
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
var promiseTags = tags.initialize();
promiseTags.done(function(){
$("#tags").typeahead(
{
highlight: true, minLength: 0,
hint: true
}, {
name: "tags",
displayKey: "name",
source: function(query, cb){
return sourceSplit(query, cb, ",", tags);
}
});
});
var promiseLanguages = languages.initialize();
promiseLanguages.done(function(){
$("#languages").typeahead(
{
highlight: true, minLength: 0,
hint: true
}, {
name: "languages",
displayKey: "name",
source: function(query, cb){
return sourceSplit(query, cb, ",", languages); //(",")
}
});
});
7 years ago
$("form").on("change input typeahead:selected", function(data){
var form = $("form").serialize();
$.getJSON( get_path()+"/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");
}
});
});
});