Skip to content
Snippets Groups Projects
Commit 28d87c6a authored by Demian Katz's avatar Demian Katz
Browse files

Make OpenURL loading AJAX-compatible.

- Resolves VUFIND-1167 (OpenUrl-Resolver in HoldingsTab not embedded when loadInitialTabWithAjax=true)
- Includes refactoring of openurl.js to use new "VuFind namespace object plug-in" format.
- Includes test
parent 659a01e1
Branches
Tags
No related merge requests found
...@@ -74,15 +74,17 @@ class LinkResolverTest extends \VuFindTest\Unit\MinkTestCase ...@@ -74,15 +74,17 @@ class LinkResolverTest extends \VuFindTest\Unit\MinkTestCase
* Set up the record page for OpenURL testing. * Set up the record page for OpenURL testing.
* *
* @param array $openUrlExtras Extra settings for the [OpenURL] config section. * @param array $openUrlExtras Extra settings for the [OpenURL] config section.
* @param array $extraConfigs Top-level config.ini overrides
* *
* @return Element * @return Element
*/ */
protected function setupRecordPage($openUrlExtras = []) protected function setupRecordPage($openUrlExtras = [], $extraConfigs = [])
{ {
// Set up configs // Set up configs
$this->changeConfigs( $this->changeConfigs(
[ [
'config' => $this->getConfigIniOverrides($openUrlExtras), 'config' =>
$extraConfigs + $this->getConfigIniOverrides($openUrlExtras),
] ]
); );
...@@ -95,14 +97,17 @@ class LinkResolverTest extends \VuFindTest\Unit\MinkTestCase ...@@ -95,14 +97,17 @@ class LinkResolverTest extends \VuFindTest\Unit\MinkTestCase
/** /**
* Click an OpenURL on the page and assert the expected results. * Click an OpenURL on the page and assert the expected results.
* *
* @param Element $page Current page object * @param Element $page Current page object
* @param bool $click Should we click the link (true), or is it autoloading?
* *
* @return void * @return void
*/ */
protected function assertOpenUrl(Element $page) protected function assertOpenUrl(Element $page, $click = true)
{ {
// Click the OpenURL link: // Click the OpenURL link:
$this->findCss($page, '.fulltext')->click(); if ($click) {
$this->findCss($page, '.fulltext')->click();
}
$this->snooze(); $this->snooze();
// Confirm that the expected fake demo driver data is there: // Confirm that the expected fake demo driver data is there:
...@@ -132,7 +137,7 @@ class LinkResolverTest extends \VuFindTest\Unit\MinkTestCase ...@@ -132,7 +137,7 @@ class LinkResolverTest extends \VuFindTest\Unit\MinkTestCase
} }
/** /**
* Test a link in the search results. * Test a link in the search results (default behavior, click required).
* *
* @return void * @return void
*/ */
...@@ -158,7 +163,35 @@ class LinkResolverTest extends \VuFindTest\Unit\MinkTestCase ...@@ -158,7 +163,35 @@ class LinkResolverTest extends \VuFindTest\Unit\MinkTestCase
} }
/** /**
* Test a link on the record page. * Test a link in the search results (optional autoloading enabled).
*
* @return void
*/
public function testLinkInSearchResultsWithAutoloading()
{
// Set up configs
$this->changeConfigs(
[
'config' => $this->getConfigIniOverrides(
['embed_auto_load' => true]
),
]
);
// Search for a known record:
$session = $this->getMinkSession();
$session->visit($this->getVuFindUrl() . '/Search/Home');
$page = $session->getPage();
$this->findCss($page, '.searchForm [name="lookfor"]')
->setValue('id:testsample1');
$this->findCss($page, '.btn.btn-primary')->click();
// Verify the OpenURL
$this->assertOpenUrl($page, false /* do not click link */);
}
/**
* Test that link is missing from the record page by default.
* *
* @return void * @return void
*/ */
...@@ -171,7 +204,7 @@ class LinkResolverTest extends \VuFindTest\Unit\MinkTestCase ...@@ -171,7 +204,7 @@ class LinkResolverTest extends \VuFindTest\Unit\MinkTestCase
} }
/** /**
* Test a link on the record page. * Test a link on the record page (in core metadata).
* *
* @return void * @return void
*/ */
...@@ -183,7 +216,7 @@ class LinkResolverTest extends \VuFindTest\Unit\MinkTestCase ...@@ -183,7 +216,7 @@ class LinkResolverTest extends \VuFindTest\Unit\MinkTestCase
} }
/** /**
* Test a link on the record page. * Test a link on the record page (in holdings tab).
* *
* @return void * @return void
*/ */
...@@ -193,4 +226,19 @@ class LinkResolverTest extends \VuFindTest\Unit\MinkTestCase ...@@ -193,4 +226,19 @@ class LinkResolverTest extends \VuFindTest\Unit\MinkTestCase
$page = $this->setupRecordPage(['show_in_holdings' => true]); $page = $this->setupRecordPage(['show_in_holdings' => true]);
$this->assertOpenUrl($page); $this->assertOpenUrl($page);
} }
/**
* Test a link on the record page (in holdings tab w/ AJAX loading).
*
* @return void
*/
public function testLinkOnRecordPageWithLinkInHoldingsAndAjaxTabLoading()
{
// By default, no OpenURL on record page:
$page = $this->setupRecordPage(
['show_in_holdings' => true],
['Site' => ['loadInitialTabWithAjax' => true]]
);
$this->assertOpenUrl($page);
}
} }
/*global VuFind, setupOpenUrlLinks, checkItemStatuses, checkSaveStatuses */ /*global VuFind, checkItemStatuses, checkSaveStatuses */
VuFind.combinedSearch = (function() { VuFind.combinedSearch = (function() {
var init = function(container, url) { var init = function(container, url) {
container.load(url, '', function(responseText) { container.load(url, '', function(responseText) {
if (responseText.length == 0) { if (responseText.length == 0) {
container.hide(); container.hide();
} else { } else {
setupOpenUrlLinks(container); VuFind.openurl.init(container);
checkItemStatuses(container); checkItemStatuses(container);
checkSaveStatuses(container); checkSaveStatuses(container);
} }
......
/*global extractClassParams, VuFind */ /*global extractClassParams, VuFind */
VuFind.register('openurl', function() {
var _loadResolverLinks = function($target, openUrl, searchClassId) {
$target.addClass('ajax_availability');
var url = VuFind.path + '/AJAX/JSON?' + $.param({method:'getResolverLinks',openurl:openUrl,searchClassId:searchClassId});
$.ajax({
dataType: 'json',
url: url
})
.done(function(response) {
$target.removeClass('ajax_availability').empty().append(response.data);
})
.fail(function(response, textStatus) {
$target.removeClass('ajax_availability').addClass('text-danger').empty();
if (textStatus == 'abort' || typeof response.responseJSON === 'undefined') { return; }
$target.append(response.responseJSON.data);
});
}
function loadResolverLinks($target, openUrl, searchClassId) { var _embedOpenUrlLinks = function(element) {
$target.addClass('ajax_availability'); // Extract the OpenURL associated with the clicked element:
var url = VuFind.path + '/AJAX/JSON?' + $.param({method:'getResolverLinks',openurl:openUrl,searchClassId:searchClassId}); var openUrl = element.children('span.openUrl:first').attr('title');
$.ajax({
dataType: 'json',
url: url
})
.done(function(response) {
$target.removeClass('ajax_availability').empty().append(response.data);
})
.fail(function(response, textStatus) {
$target.removeClass('ajax_availability').addClass('text-danger').empty();
if (textStatus == 'abort' || typeof response.responseJSON === 'undefined') { return; }
$target.append(response.responseJSON.data);
});
}
function embedOpenUrlLinks(element) {
// Extract the OpenURL associated with the clicked element:
var openUrl = element.children('span.openUrl:first').attr('title');
// Hide the controls now that something has been clicked: // Hide the controls now that something has been clicked:
var controls = element.parents('.openUrlControls'); var controls = element.parents('.openUrlControls');
controls.removeClass('openUrlEmbed').addClass('hidden'); controls.removeClass('openUrlEmbed').addClass('hidden');
// Locate the target area for displaying the results: // Locate the target area for displaying the results:
var target = controls.next('div.resolver'); var target = controls.next('div.resolver');
// If the target is already visible, a previous click has populated it; // If the target is already visible, a previous click has populated it;
// don't waste time doing redundant work. // don't waste time doing redundant work.
if (target.hasClass('hidden')) { if (target.hasClass('hidden')) {
loadResolverLinks(target.removeClass('hidden'), openUrl, element.data('search-class-id')); _loadResolverLinks(target.removeClass('hidden'), openUrl, element.data('search-class-id'));
}
} }
}
// Assign actions to the OpenURL links. This can be called with a container e.g. when // Assign actions to the OpenURL links. This can be called with a container e.g. when
// combined results fetched with AJAX are loaded. // combined results fetched with AJAX are loaded.
function setupOpenUrlLinks(container) var init = function(container)
{ {
if (typeof(container) == 'undefined') { if (typeof(container) == 'undefined') {
container = $('body'); container = $('body');
}
// assign action to the openUrlWindow link class
container.find('a.openUrlWindow').unbind('click').click(function() {
var params = extractClassParams(this);
var settings = params.window_settings;
window.open($(this).attr('href'), 'openurl', settings);
return false;
});
// assign action to the openUrlEmbed link class
container.find('.openUrlEmbed a').unbind('click').click(function() {
_embedOpenUrlLinks($(this));
return false;
});
container.find('.openUrlEmbed.openUrlEmbedAutoLoad a').trigger('click');
} }
return {init: init}
// assign action to the openUrlWindow link class
container.find('a.openUrlWindow').click(function() {
var params = extractClassParams(this);
var settings = params.window_settings;
window.open($(this).attr('href'), 'openurl', settings);
return false;
});
// assign action to the openUrlEmbed link class
container.find('.openUrlEmbed a').click(function() {
embedOpenUrlLinks($(this));
return false;
});
container.find('.openUrlEmbed.openUrlEmbedAutoLoad a').trigger('click');
}
$(document).ready(function() {
setupOpenUrlLinks();
}); });
<? <?
$this->headScript()->appendFile("openurl.js"); echo $this->inlineScript(\Zend\View\Helper\HeadScript::FILE, 'openurl.js', 'SET');
$classes = ''; $classes = '';
if ($this->openUrlEmbed) { if ($this->openUrlEmbed) {
$classes = "fulltext"; $classes = "fulltext";
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment