From 30cc49bbab07eb77ace9144488712b4eff4e55bd Mon Sep 17 00:00:00 2001 From: Owen Leonard Date: Wed, 10 Apr 2024 11:30:33 +0000 Subject: [PATCH] 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 Signed-off-by: Kyle M Hall --- .../opac-tmpl/bootstrap/js/amazonimages.js | 14 +- .../bootstrap/js/bakertaylorimages.js | 14 +- .../opac-tmpl/bootstrap/js/google-jackets.js | 81 +++--- .../opac-tmpl/bootstrap/js/localcovers.js | 17 +- .../opac-tmpl/bootstrap/js/openlibrary.js | 231 +++++++++--------- 5 files changed, 180 insertions(+), 177 deletions(-) diff --git a/koha-tmpl/opac-tmpl/bootstrap/js/amazonimages.js b/koha-tmpl/opac-tmpl/bootstrap/js/amazonimages.js index acb21518f26..8bfa357c54e 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/js/amazonimages.js +++ b/koha-tmpl/opac-tmpl/bootstrap/js/amazonimages.js @@ -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(""+ __("No cover image available") +""); } else if ((this.complete != null) && (!this.complete)) { $(this).parent().html(""+ __("No cover image available") +""); } } - }); - } + }); +} diff --git a/koha-tmpl/opac-tmpl/bootstrap/js/bakertaylorimages.js b/koha-tmpl/opac-tmpl/bootstrap/js/bakertaylorimages.js index cda542e277a..85d8f332449 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/js/bakertaylorimages.js +++ b/koha-tmpl/opac-tmpl/bootstrap/js/bakertaylorimages.js @@ -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(""+ __("No cover image available" ) +""); } - } - }); - } + } + }); +} diff --git a/koha-tmpl/opac-tmpl/bootstrap/js/google-jackets.js b/koha-tmpl/opac-tmpl/bootstrap/js/google-jackets.js index c030fff2d20..6e258702e13 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/js/google-jackets.js +++ b/koha-tmpl/opac-tmpl/bootstrap/js/google-jackets.js @@ -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: *
@@ -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( - '
' + - '
' - ); - } - } - } 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( + '
' + + '
' + ); + } + } + } else { + var message = document.createElement("span"); + $(message).attr("class","no-image"); + $(message).html(__("No cover image available")); + $(this).empty().append(message); + } + }); } this.done = 1; } diff --git a/koha-tmpl/opac-tmpl/bootstrap/js/localcovers.js b/koha-tmpl/opac-tmpl/bootstrap/js/localcovers.js index 21ffaf872fe..5b698a71317 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/js/localcovers.js +++ b/koha-tmpl/opac-tmpl/bootstrap/js/localcovers.js @@ -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 = $("").attr('href', '/cgi-bin/koha/opac-imageviewer.pl?imagenumber=' + imagenumber + '&biblionumber=' + biblionumber); @@ -76,7 +79,7 @@ KOHA.LocalCover = { } $(mydiv).children('.no-image').remove(); } - }) + }); }); }, }; diff --git a/koha-tmpl/opac-tmpl/bootstrap/js/openlibrary.js b/koha-tmpl/opac-tmpl/bootstrap/js/openlibrary.js index ee08b535c1f..e05ffd30f33 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/js/openlibrary.js +++ b/koha-tmpl/opac-tmpl/bootstrap/js/openlibrary.js @@ -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
0) { - q += '|'; - } - q += 'id:' + i; + // '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'; + + // 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); + + 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; -} -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; + $('.' + magic_classname).each(add_el); + return q; } - first = bookdata.items[0]; - if (!(first.status in buttons)) { - return false; - } - result = '' + - ''; - 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); - } - if (decoration) { - el.innerHTML += decoration; - el.style.display = 'block' - } else { - el.style.display = 'none'; + 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 = '' + + ''; + return result; } -} -function do_query(q, decorate_el_fn) { - if (!decorate_el_fn) { - decorate_el_fn = default_decorate_el_fn; + // 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'; + } } - var starttime = (new Date()).getTime(); - - // Call a function on each
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
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 /* -- 2.30.2