Koha/koha-tmpl/opac-tmpl/bootstrap/js/ill-availability.js
Andrew Isherwood f5edd39e61
Bug 23173: Provide core infrastructure
This patch adds the required infrastructure to enable ILL availability
plugins to intercept the request creation process and, using the
supplied metadata, search for and display possible relevant items from
whichever availability plugins are installed.

Currently three availability plugins exist:

z39.50 - Searches any number of the Koha instance's configured Z targets
https://github.com/PTFS-Europe/koha-plugin-ill-avail-z3950

EDS - Searches the EBSCO Discovery Service
https://github.com/PTFS-Europe/koha-plugin-ill-avail-eds

Unpaywall - Searches the Unpaywall API for possible open access versions
of the requested item
https://github.com/PTFS-Europe/koha-plugin-ill-avail-unpaywall

The Unpaywall plugin is intended to serve as a "reference" plugin as the
API it deals with is extremely simple

Signed-off-by: Niamh Walker-Headon <Niamh.Walker-Headon@tudublin.ie>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
2020-04-06 11:04:19 +01:00

178 lines
7 KiB
JavaScript

$(document).ready(function() {
window.doSearch = function() {
// In case the source doesn't supply data required for DT to calculate
// pagination, we need to do it ourselves
var ownPagination = false;
var directionSet = false;
var start = 0;
var forward = true; // true == forward, false == backwards
// Arbitrary starting value, it will be corrected by the first
// page of results
var pageSize = 20;
var tableTmpl = {
ajax: {
cache: true, // Prevent DT appending a "_" cache param
},
columns: [
// defaultContent prevents DT from choking if
// the API response doesn't return a column
{
title: 'Source',
data: 'source',
defaultContent: ''
},
{
data: 'title',
defaultContent: ''
},
{
data: 'author',
defaultContent: ''
},
{
data: 'isbn',
defaultContent: ''
},
{
data: 'issn',
defaultContent: ''
},
{
data: 'date',
defaultContent: ''
}
]
};
// render functions don't get copied across when we make a dereferenced
// copy of them, so we have to reattach them once we have a copy
// Here we store them
var renders = {
title: function(data, type, row) {
return row.url ?
'<a href="'+row.url+'" target="_blank">'+row.title+'</a>' :
row.title;
},
source: function(data, type, row) {
return row.opac_url ?
'<a href="'+row.opac_url+'" target="_blank">'+row.source+'</a>' :
row.source;
}
};
services.forEach(function(service) {
// Create a deferenced copy of our table definition object
var tableDef = JSON.parse(JSON.stringify(tableTmpl));
// Iterate the table's columns array and add render functions
// as necessary
tableDef.columns.forEach(function(column) {
if (renders[column.data]) {
column.render = renders[column.data];
}
});
tableDef.ajax.dataSrc = function(data) {
var results = data.results.search_results;
// The source appears to be returning it's own pagination
// data
if (
data.hasOwnProperty('recordsFiltered') ||
data.hasOwnProperty('recordsTotal')
) {
return results;
}
// Set up our own pagination values based on what we just
// got back
ownPagination = true;
directionSet = false;
pageSize = results.length;
// These values are completely arbitrary, but they enable
// us to display pagination links
data.recordsFiltered = 5000,
data.recordsTotal = 5000;
return results;
};
tableDef.ajax.data = function(data) {
// Datatables sends a bunch of superfluous params
// that we don't want to litter our API schema
// with, so just remove them from the request
if (data.hasOwnProperty('columns')) {
delete data.columns;
}
if (data.hasOwnProperty('draw')) {
delete data.draw;
}
if (data.hasOwnProperty('order')) {
delete data.order;
}
if (data.hasOwnProperty('search')) {
delete data.search;
}
// If we're handling our own pagination, set the properties
// that DT will send in the request
if (ownPagination) {
start = forward ? start + pageSize : start - pageSize;
data.start = start;
data['length'] = pageSize;
}
// We may need to restrict the service IDs being queries, this
// needs to be handled in the plugin's API module
var restrict = $('#service_id_restrict').
attr('data-service_id_restrict_ids');
if (restrict && restrict.length > 0) {
data.restrict = restrict;
}
};
// Add any datatables config options passed from the service
// to the table definition
tableDef.ajax.url = service.endpoint + metadata;
if (service.hasOwnProperty('datatablesConfig')) {
var conf = service.datatablesConfig;
for (var key in conf) {
// The config from the service definition comes from a Perl
// hashref, therefore can't contain true/false, so we
// special case it
if (conf.hasOwnProperty(key)) {
if (conf[key] == 'false') {
// Special case false values
tableDef[key] = false;
} else if (conf[key] == 'true') {
// Special case true values
tableDef[key] = true;
} else {
// Copy the property value
tableDef[key] = conf[key];
}
}
}
}
// Create event watchers for the "next" and "previous" pagination
// links, this enables us to set the direction the next request is
// going in when we're doing our own pagination. We use "hover"
// because the click event is caught after the request has been
// sent
tableDef.drawCallback = function() {
$('.paginate_button.next:not(.disabled)',
this.api().table().container()
).on('hover', function() {
forward = true;
directionSet = true;
});
$('.paginate_button.previous:not(.disabled)',
this.api().table().container()
).on('hover', function() {
forward = false;
directionSet = true;
});
}
// Initialise the table
$('#'+service.id ).dataTable(
$.extend(true, {}, dataTablesDefaults, tableDef)
);
});
}
});