Skip to content

Commit

Permalink
Update to ES5 standards
Browse files Browse the repository at this point in the history
Also fix ESLint issues
  • Loading branch information
jkrehm committed Aug 13, 2017
1 parent 29e2658 commit 8171943
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 190 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"env": {
"browser": true,
"es6": true,
"jquery": true
},
"globals": {
Expand Down Expand Up @@ -83,6 +82,8 @@
2,
"always"
],
"space-before-blocks": 2,
"space-infix-ops": 2,
"space-unary-ops": 2,
"use-isnan": 2,
"valid-typeof": 2,
Expand Down
79 changes: 49 additions & 30 deletions cps/static/js/details.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,58 @@
$( document ).ready(function() {
/* global _ */

$(function() {
$("#have_read_form").ajaxForm();
});

$("#have_read_cb").on("change", function() {
$(this).closest("form").submit();
});

$("#shelf-actions").on("click", "[data-shelf-action]", function (e) {
e.preventDefault();
(function() {
var templates = {
add: _.template(
$("#template-shelf-add").html()
),
remove: _.template(
$("#template-shelf-remove").html()
)
};

$("#shelf-actions").on("click", "[data-shelf-action]", function (e) {
e.preventDefault();

$.get(this.href)
.done(() => {
const $this = $(this);
switch ($this.data("shelf-action")) {
case "add":
$("#remove-from-shelves").append(`<a href="${$this.data("remove-href")}"
data-add-href="${this.href}"
class="btn btn-sm btn-default" data-shelf-action="remove"
><span class="glyphicon glyphicon-remove"></span> ${this.textContent}</a>`);
break;
case "remove":
$("#add-to-shelves").append(`<li><a href="${$this.data("add-href")}"
data-remove-href="${this.href}"
data-shelf-action="add"
>${this.textContent}</a></li>`);
break;
}
this.parentNode.removeChild(this);
})
.fail((xhr) => {
const $msg = $("<span/>", { "class": "text-danger"}).text(xhr.responseText);
$("#shelf-action-status").html($msg);
$.get(this.href)
.done(function() {
var $this = $(this);
switch ($this.data("shelf-action")) {
case "add":
$("#remove-from-shelves").append(
templates.remove({
add: this.href,
remove: $this.data("remove-href"),
content: this.textContent
})
);
break;
case "remove":
$("#add-to-shelves").append(
templates.add({
add: $this.data("add-href"),
remove: this.href,
content: this.textContent
})
);
break;
}
this.parentNode.removeChild(this);
}.bind(this))
.fail(function(xhr) {
var $msg = $("<span/>", { "class": "text-danger"}).text(xhr.responseText);
$("#shelf-action-status").html($msg);

setTimeout(() => {
$msg.remove();
}, 10000);
});
});
setTimeout(function() {
$msg.remove();
}, 10000);
});
});
})();
136 changes: 69 additions & 67 deletions cps/static/js/edit_books.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Takes a prefix, query typeahead callback, Bloodhound typeahead adapter
and returns the completions it gets from the bloodhound engine prefixed.
*/
function prefixedSource(prefix, query, cb, bhAdapter) {
bhAdapter(query, function(retArray){
bhAdapter(query, function(retArray) {
var matches = [];
for (var i = 0; i < retArray.length; i++) {
var obj = {name : prefix + retArray[i].name};
Expand All @@ -41,15 +41,15 @@ function prefixedSource(prefix, query, cb, bhAdapter) {
cb(matches);
});
}
function getPath(){
function getPath() {
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(datum) {
datumTokenizer: function datumTokenizer(datum) {
return [datum.name];
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
Expand All @@ -60,15 +60,15 @@ var authors = new Bloodhound({

var series = new Bloodhound({
name: "series",
datumTokenizer(datum) {
datumTokenizer: function datumTokenizer(datum) {
return [datum.name];
},
queryTokenizer(query) {
queryTokenizer: function queryTokenizer(query) {
return [query];
},
remote: {
url: getPath()+"/get_series_json?q=",
replace(url, query) {
replace: function replace(url, query) {
return url+encodeURIComponent(query);
}
}
Expand All @@ -77,10 +77,10 @@ var series = new Bloodhound({

var tags = new Bloodhound({
name: "tags",
datumTokenizer(datum) {
datumTokenizer: function datumTokenizer(datum) {
return [datum.name];
},
queryTokenizer(query) {
queryTokenizer: function queryTokenizer(query) {
var tokens = query.split(",");
tokens = [tokens[tokens.length-1].trim()];
return tokens;
Expand All @@ -92,15 +92,15 @@ var tags = new Bloodhound({

var languages = new Bloodhound({
name: "languages",
datumTokenizer(datum) {
datumTokenizer: function datumTokenizer(datum) {
return [datum.name];
},
queryTokenizer(query) {
queryTokenizer: function queryTokenizer(query) {
return [query];
},
remote: {
url: getPath()+"/get_languages_json?q=",
replace(url, query) {
replace: function replace(url, query) {
return url+encodeURIComponent(query);
}
}
Expand All @@ -115,9 +115,9 @@ function sourceSplit(query, cb, split, source) {
tokens.splice(tokens.length-1, 1); // remove last element
var prefix = "";
var newSplit;
if (split === "&"){
if (split === "&") {
newSplit = " " + split + " ";
}else{
} else {
newSplit = split + " ";
}
for (var i = 0; i < tokens.length; i++) {
Expand All @@ -127,76 +127,78 @@ function sourceSplit(query, cb, split, source) {
}

var promiseAuthors = authors.initialize();
promiseAuthors.done(function(){
promiseAuthors.done(function() {
$("#bookAuthor").typeahead(
{
highlight: true, minLength: 1,
hint: true
}, {
name: "authors",
displayKey: "name",
source(query, cb){
return sourceSplit(query, cb, "&", authors); //sourceSplit //("&")
{
highlight: true, minLength: 1,
hint: true
}, {
name: "authors",
displayKey: "name",
source: function source(query, cb) {
return sourceSplit(query, cb, "&", authors); //sourceSplit //("&")
}
});
}
);
});

var promiseSeries = series.initialize();
promiseSeries.done(function(){
promiseSeries.done(function() {
$("#series").typeahead(
{
highlight: true, minLength: 0,
hint: true
}, {
name: "series",
displayKey: "name",
source: series.ttAdapter()
}
{
highlight: true, minLength: 0,
hint: true
}, {
name: "series",
displayKey: "name",
source: series.ttAdapter()
}
);
});

var promiseTags = tags.initialize();
promiseTags.done(function(){
promiseTags.done(function() {
$("#tags").typeahead(
{
highlight: true, minLength: 0,
hint: true
}, {
name: "tags",
displayKey: "name",
source(query, cb){
return sourceSplit(query, cb, ",", tags);
}
});
});
{
highlight: true, minLength: 0,
hint: true
}, {
name: "tags",
displayKey: "name",
source: function source(query, cb) {
return sourceSplit(query, cb, ",", tags);
}
}
);
});

var promiseLanguages = languages.initialize();
promiseLanguages.done(function(){
promiseLanguages.done(function() {
$("#languages").typeahead(
{
highlight: true, minLength: 0,
hint: true
}, {
name: "languages",
displayKey: "name",
source(query, cb){
return sourceSplit(query, cb, ",", languages); //(",")
}
});
});
{
highlight: true, minLength: 0,
hint: true
}, {
name: "languages",
displayKey: "name",
source: function source(query, cb) {
return sourceSplit(query, cb, ",", languages); //(",")
}
}
);
});

$("#search").on("change input.typeahead:selected", function(){
$("#search").on("change input.typeahead:selected", function() {
var form = $("form").serialize();
$.getJSON( getPath()+"/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");
}
});
$(".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");
}
});
});
});
Loading

0 comments on commit 8171943

Please sign in to comment.