Bug 36566: Correct eslint errors in OPAC enhanced content JS

This patch fixes various eslint errors in enhanced content JS files:

- Consistent indentation
- Remove variables which are declared but not used
- Add missing semicolons
- Add missing "var" declarations

To test, apply the patch and clear your browser cache if necessary.

- Go to Administration -> System preferences and enable these
  preferences:
  - OPACAmazonCoverImages
  - BakerTaylorEnabled
  - GoogleJackets
  - OPACLocalCoverImages
  - OpenLibraryCovers
- Go to the OPAC and confirm that covers from these services appear
  correctly in search results and on detail pages.

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Owen Leonard 2024-04-10 11:30:33 +00:00 committed by Martin Renvoize
parent 8fdddccffb
commit 3d82116830
No known key found for this signature in database
GPG Key ID: 422B469130441A0F
5 changed files with 176 additions and 173 deletions

View File

@ -1,14 +1,16 @@
/* global __ */
/* exported verify_images */
// http://www.oreillynet.com/pub/a/javascript/2003/10/21/amazonhacks.html
function verify_images() {
$("img").each(function(i){
if ((this.src.indexOf('images.amazon.com') >= 0) || (this.src.indexOf('g-images.amazon.com') >=0) || (this.src.indexOf('syndetics.com') >=0) ) {
w = this.width;
h = this.height;
$("img").each(function(){
if ((this.src.indexOf('images.amazon.com') >= 0) || (this.src.indexOf('g-images.amazon.com') >=0) || (this.src.indexOf('syndetics.com') >=0) ) {
var w = this.width;
var h = this.height;
if ((w == 1) || (h == 1)) {
$(this).parent().html("<span class=\"no-image\">"+ __("No cover image available") +"</span>");
} else if ((this.complete != null) && (!this.complete)) {
$(this).parent().html("<span class=\"no-image\">"+ __("No cover image available") +"</span>");
}
}
});
}
});
}

View File

@ -1,11 +1,13 @@
/* global __ */
/* exported bt_verify_images */
// http://www.oreillynet.com/pub/a/javascript/2003/10/21/amazonhacks.html
function bt_verify_images() {
$("img").each(function(i){
if (this.src.indexOf('btol.com') >= 0) {
h = this.height;
$("img").each(function(){
if (this.src.indexOf('btol.com') >= 0) {
var h = this.height;
if (h == 20) {
$(this).before("<span class=\"no-image\" style=\"margin-bottom:5px;width:80px;\">"+ __("No cover image available" ) +"</span>");
}
}
});
}
}
});
}

View File

@ -1,3 +1,4 @@
/* global __ */
if (typeof KOHA == "undefined" || !KOHA) {
var KOHA = {};
}
@ -7,7 +8,6 @@ if (typeof KOHA == "undefined" || !KOHA) {
*/
KOHA.Google = {
/**
* Search all:
* <div title="biblionumber" id="isbn" class="gbs-thumbnail"></div>
@ -19,7 +19,7 @@ KOHA.Google = {
*/
GetCoverFromIsbn: function(newWindow) {
var bibkeys = [];
$("[id^=gbs-thumbnail]").each(function(i) {
$("[id^=gbs-thumbnail]").each(function() {
bibkeys.push($(this).attr("class")); // id=isbn
});
bibkeys = bibkeys.join(',');
@ -39,45 +39,46 @@ KOHA.Google = {
* and link to preview if div id is gbs-thumbnail-preview
*/
gbsCallBack: function(booksInfo) {
var target = '';
if (this.openInNewWindow) {
var target = '';
if (this.openInNewWindow) {
target = 'target="_blank" rel="noreferrer" ';
}
for (id in booksInfo) {
var book = booksInfo[id];
$("[id^=gbs-thumbnail]."+book.bib_key).each(function() {
if (typeof(book.thumbnail_url) != "undefined") {
if ( $(this).data('use-data-link') ) {
var a = document.createElement("a");
a.href = book.thumbnail_url;
var img = document.createElement("img");
img.src = book.thumbnail_url;
img.setAttribute('data-link', book.info_url);
a.append(img)
$(this).empty().append(a);
} else {
var img = document.createElement("img");
img.src = book.thumbnail_url;
$(this).empty().append(img);
var re = /^gbs-thumbnail-preview/;
if ( re.exec($(this).attr("id")) ) {
$(this).append(
'<div class="google-books-preview">' +
'<a '+target+'href="' +
book.info_url +
'"><img src="' +
'https://books.google.com/intl/en/googlebooks/images/gbs_preview_sticker1.gif' +
'"></a></div>'
);
}
}
} else {
var message = document.createElement("span");
$(message).attr("class","no-image");
$(message).html(__("No cover image available"));
$(this).empty().append(message);
}
});
}
for (var id in booksInfo) {
var book = booksInfo[id];
$("[id^=gbs-thumbnail]."+book.bib_key).each(function() {
if (typeof(book.thumbnail_url) != "undefined") {
var img;
if ( $(this).data('use-data-link') ) {
var a = document.createElement("a");
a.href = book.thumbnail_url;
img = document.createElement("img");
img.src = book.thumbnail_url;
img.setAttribute('data-link', book.info_url);
a.append(img);
$(this).empty().append(a);
} else {
img = document.createElement("img");
img.src = book.thumbnail_url;
$(this).empty().append(img);
var re = /^gbs-thumbnail-preview/;
if ( re.exec($(this).attr("id")) ) {
$(this).append(
'<div class="google-books-preview">' +
'<a '+target+'href="' +
book.info_url +
'"><img src="' +
'https://books.google.com/intl/en/googlebooks/images/gbs_preview_sticker1.gif' +
'"></a></div>'
);
}
}
} else {
var message = document.createElement("span");
$(message).attr("class","no-image");
$(message).html(__("No cover image available"));
$(this).empty().append(message);
}
});
}
this.done = 1;
}

View File

@ -1,3 +1,4 @@
/* global __ */
if (typeof KOHA == "undefined" || !KOHA) {
var KOHA = {};
}
@ -17,8 +18,8 @@ KOHA.LocalCover = {
* The result is asynchronously returned by OpenLibrary and catched by
* olCallBack().
*/
GetCoverFromBibnumber: function(uselink) {
$("div[id^=local-thumbnail],span[id^=local-thumbnail]").each(function(i) {
GetCoverFromBibnumber: function() {
$("div[id^=local-thumbnail],span[id^=local-thumbnail]").each(function() {
var mydiv = this;
var message = document.createElement("span");
$(message).attr("class","no-image");
@ -36,16 +37,17 @@ KOHA.LocalCover = {
$(mydiv).children('.no-image').remove();
}
catch(err){
};
// Nothing
}
} else if (this.width > 1) { // don't show the silly 1px "no image" img
$(mydiv).empty().append(img);
$(mydiv).children('.no-image').remove();
}
})
});
});
},
GetCoverFromItemnumber: function(uselink) {
$("div[class^=local-thumbnail],span[class^=local-thumbnail]").each(function(i) {
$("div[class^=local-thumbnail],span[class^=local-thumbnail]").each(function() {
var mydiv = this;
var message = document.createElement("span");
var imagenumber = $(mydiv).data("imagenumber");
@ -65,7 +67,8 @@ KOHA.LocalCover = {
$(mydiv).children('.no-image').remove();
}
catch(err){
};
// Nothing
}
} else if (this.width > 1) { // don't show the silly 1px "no image" img
if (uselink) {
var a = $("<a />").attr('href', '/cgi-bin/koha/opac-imageviewer.pl?imagenumber=' + imagenumber + '&biblionumber=' + biblionumber);
@ -76,7 +79,7 @@ KOHA.LocalCover = {
}
$(mydiv).children('.no-image').remove();
}
})
});
});
},
};

View File

@ -1,3 +1,4 @@
/* global __ */
if (typeof KOHA == "undefined" || !KOHA) {
var KOHA = {};
}
@ -18,7 +19,7 @@ KOHA.OpenLibrary = new function() {
*/
this.GetCoverFromIsbn = function() {
var bibkeys = [];
$("[id^=openlibrary-thumbnail]").each(function(i) {
$("[id^=openlibrary-thumbnail]").each(function() {
bibkeys.push("ISBN:" + $(this).attr("class")); // id=isbn
});
bibkeys = bibkeys.join(',');
@ -29,30 +30,32 @@ KOHA.OpenLibrary = new function() {
"&callback=KOHA.OpenLibrary.olCallBack&jscmd=data");
scriptElement.setAttribute("type", "text/javascript");
document.documentElement.firstChild.appendChild(scriptElement);
}
};
/**
* Add cover pages <div
* and link to preview if div id is gbs-thumbnail-preview
*/
this.olCallBack = function(booksInfo) {
for (id in booksInfo) {
for (var id in booksInfo) {
var book = booksInfo[id];
var isbn = id.substring(5);
var a;
$("[id^=openlibrary-thumbnail]."+isbn).each(function() {
var a = document.createElement("a");
a = document.createElement("a");
a.href = booksInfo.url;
if (book.cover) {
var img;
if ( $(this).data('use-data-link') ) {
var a = document.createElement("a");
a.href = book.cover.large;
var img = document.createElement("img");
img.src = book.cover.medium;
img.setAttribute('data-link', book.cover.large);
a.append(img)
$(this).empty().append(a);
a = document.createElement("a");
a.href = book.cover.large;
img = document.createElement("img");
img.src = book.cover.medium;
img.setAttribute('data-link', book.cover.large);
a.append(img);
$(this).empty().append(a);
} else {
var img = document.createElement("img");
img = document.createElement("img");
img.src = book.cover.medium;
img.height = '110';
$(this).append(img);
@ -66,7 +69,7 @@ KOHA.OpenLibrary = new function() {
});
}
this.done = 1;
}
};
var search_url = 'https://openlibrary.org/search?';
this.searchUrl = function( q ) {
@ -85,7 +88,7 @@ KOHA.OpenLibrary = new function() {
url: search_url_json,
dataType: 'json',
data: params,
error: function( xhr, error ) {
error: function( xhr ) {
try {
callback( JSON.parse( xhr.responseText ));
} catch ( e ) {
@ -110,102 +113,94 @@ A demonstration use of this script is available here:
http://internetarchive.github.com/read_api_extras/readapi_demo.html
*/
var ol_readapi_automator =
(function () { // open anonymous scope for tidiness
// 'constants'
var readapi_bibids = ['isbn', 'lccn', 'oclc', 'olid', 'iaid', 'bibkeys'];
var magic_classname = 'ol_readapi_book';
var ol_readapi_books = $("." + magic_classname );
var result;
// 'constants'
var readapi_bibids = ['isbn', 'lccn', 'oclc', 'olid', 'iaid', 'bibkeys'];
var magic_classname = 'ol_readapi_book';
var ol_readapi_books = $("." + magic_classname );
var result;
// added to book divs to correlate with API results
var magic_bookid = 'ol_bookid';
var ol_button_classname = 'ol_readapi_button';
// added to book divs to correlate with API results
var magic_bookid = 'ol_bookid';
var ol_button_classname = 'ol_readapi_button';
// Find all book divs and concatenate ids from them to create a read
// API query url
function create_query() {
var q = 'https://openlibrary.org/api/volumes/brief/json/';
// Find all book divs and concatenate ids from them to create a read
// API query url
function create_query() {
var q = 'https://openlibrary.org/api/volumes/brief/json/';
function add_el(i, el) {
// tag with number found so it's easy to discover later
// (necessary? just go by index?)
// (choose better name?)
$(el).attr(magic_bookid, i);
function add_el(i, el) {
// tag with number found so it's easy to discover later
// (necessary? just go by index?)
// (choose better name?)
$(el).attr(magic_bookid, i);
if (i > 0) {
q += '|';
}
q += 'id:' + i;
if (i > 0) {
q += '|';
}
q += 'id:' + i;
for (bi in readapi_bibids) {
bibid = readapi_bibids[bi];
if ($(el).attr(bibid)) {
q += ';' + bibid + ':' + $(el).attr(bibid);
for (var bi in readapi_bibids) {
var bibid = readapi_bibids[bi];
if ($(el).attr(bibid)) {
q += ';' + bibid + ':' + $(el).attr(bibid);
}
}
}
$('.' + magic_classname).each(add_el);
return q;
}
$('.' + magic_classname).each(add_el);
return q;
}
function make_read_button(bookdata) {
buttons = {
'full access':
"https://openlibrary.org/images/button-read-open-library.png",
'lendable':
"https://openlibrary.org/images/button-borrow-open-library.png",
'checked out':
"https://openlibrary.org/images/button-checked-out-open-library.png"
};
if (bookdata.items.length == 0) {
return false;
function make_read_button(bookdata) {
var buttons = {
'full access':
"https://openlibrary.org/images/button-read-open-library.png",
'lendable':
"https://openlibrary.org/images/button-borrow-open-library.png",
'checked out':
"https://openlibrary.org/images/button-checked-out-open-library.png"
};
if (bookdata.items.length == 0) {
return false;
}
var first = bookdata.items[0];
if (!(first.status in buttons)) {
return false;
}
result = '<a target="_blank" href="' + first.itemURL + '">' +
'<img class="' + ol_button_classname +
'" src="' + buttons[first.status] + '"/></a>';
return result;
}
first = bookdata.items[0];
if (!(first.status in buttons)) {
return false;
}
result = '<a target="_blank" href="' + first.itemURL + '">' +
'<img class="' + ol_button_classname +
'" src="' + buttons[first.status] + '"/></a>';
console.log( result );
return result;
}
// Default function for decorating document elements with read API data
function default_decorate_el_fn(el, bookdata) {
// Note that 'bookdata' may be undefined, if the Read API call
// didn't return results for this book
var decoration;
if (bookdata) {
decoration = make_read_button(bookdata);
// Default function for decorating document elements with read API data
function default_decorate_el_fn(el, bookdata) {
// Note that 'bookdata' may be undefined, if the Read API call
// didn't return results for this book
var decoration;
if (bookdata) {
decoration = make_read_button(bookdata);
}
if (decoration) {
el.innerHTML += decoration;
el.style.display = 'block';
} else {
el.style.display = 'none';
}
}
if (decoration) {
el.innerHTML += decoration;
el.style.display = 'block'
} else {
el.style.display = 'none';
}
}
function do_query(q, decorate_el_fn) {
if (!decorate_el_fn) {
decorate_el_fn = default_decorate_el_fn;
}
var starttime = (new Date()).getTime();
// Call a function on each <div class="ol_readapi_book"> element
// with the target element and the data found for that element.
// Use decorate_el_fn if supplied, falling back to
// default_decorate_el_fn, above.
function query_callback(data, textStatus, jqXHR) {
var endtime = (new Date()).getTime();
var duration = (endtime - starttime) / 1000;
// console.log('took ' + duration + ' seconds');
$('.' + magic_classname).each(function(i, el) {
function do_query(q, decorate_el_fn) {
if (!decorate_el_fn) {
decorate_el_fn = default_decorate_el_fn;
}
// Call a function on each <div class="ol_readapi_book"> element
// with the target element and the data found for that element.
// Use decorate_el_fn if supplied, falling back to
// default_decorate_el_fn, above.
function query_callback(data) {
$('.' + magic_classname).each(function(i, el) {
var bookid = $(el).attr(magic_bookid);
if (bookid && bookid in data) {
decorate_el_fn(el, data[bookid]);
@ -213,29 +208,29 @@ function do_query(q, decorate_el_fn) {
decorate_el_fn(el);
}
});
}
// console.log('calling ' + q);
$.ajax({ url: q,
data: { 'show_all_items': 'true' },
dataType: 'jsonp',
success: query_callback
});
}
// console.log('calling ' + q);
$.ajax({ url: q,
data: { 'show_all_items': 'true' },
dataType: 'jsonp',
success: query_callback
});
}
if( ol_readapi_books.length > 0 ){
// Do stuff
var q = create_query();
do_query(q);
if( ol_readapi_books.length > 0 ){
// Do stuff
var q = create_query();
do_query(q);
result = {
do_query: do_query,
create_query: create_query,
make_read_button: make_read_button
};
}
result = {
do_query: do_query,
create_query: create_query,
make_read_button: make_read_button
};
}
return result;
return result;
})(); // close anonymous scope
/*