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