Skip to content

Commit

Permalink
Load linked resource interface asynchronously (#2176)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimsafley authored Jan 28, 2025
1 parent e2b2ccb commit 865009a
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 43 deletions.
54 changes: 54 additions & 0 deletions application/asset/js/linked-resources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
$(document).ready(function () {

// Render linked resources.
const renderLinkedResources = function(page, resourceProperty) {
const url = container.data('url');
const siteId = container.data('siteId');
const query = {};
if (siteId) {
query.site_id = siteId;
}
if (page) {
query.page = page;
}
if (resourceProperty) {
query.resource_property = resourceProperty
}
$.get(url, query, function(data) {
container.html(data);
});
};

// Render linked resources on initial load.
const container = $('#linked-resources-container');
renderLinkedResources();

// Handle next and previous clicks.
$(container).on('click', 'a.next, a.previous', function(e) {
e.preventDefault();
const thisButton = $(this);
// Note that we can use any base URL for this purpose.
const url = new URL(thisButton.attr('href'), 'http://foo');
renderLinkedResources(
url.searchParams.get('page'),
url.searchParams.get('resource_property')
);
});

// Handle page form submission.
$(container).on('submit', 'form', function(e) {
e.preventDefault();
const thisForm = $(this);
const searchParams = new URLSearchParams(thisForm.serialize());
renderLinkedResources(
searchParams.get('page'),
searchParams.get('resource_property')
);
});

// Handle resource property select.
$(container).on('change', '#resource-property-select', function(e) {
const thisSelect = $(this);
renderLinkedResources('1', thisSelect.val());
});
});
2 changes: 2 additions & 0 deletions application/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@
'Omeka\Controller\Admin\Item' => Service\Controller\Admin\ItemControllerFactory::class,
'Omeka\Controller\SiteAdmin\Index' => Service\Controller\SiteAdmin\IndexControllerFactory::class,
'Omeka\Controller\Site\Page' => Service\Controller\Site\PageControllerFactory::class,
'Omeka\Controller\LinkedResources' => Service\Controller\LinkedResourcesControllerFactory::class,
],
],
'controller_plugins' => [
Expand Down Expand Up @@ -489,6 +490,7 @@
'passwordRequirements' => Service\ViewHelper\PasswordRequirementsFactory::class,
'resourcePageBlocks' => Service\ViewHelper\ResourcePageBlocksFactory::class,
'browse' => Service\ViewHelper\BrowseFactory::class,
'linkedResources' => Service\ViewHelper\LinkedResourcesFactory::class,
],
'shared' => [
'resourcePageBlocks' => false,
Expand Down
13 changes: 13 additions & 0 deletions application/config/routes.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,19 @@
],
],
],
'linked-resources' => [
'type' => \Laminas\Router\Http\Segment::class,
'options' => [
'route' => '/linked-resources/:resource-id',
'defaults' => [
'controller' => 'Omeka\Controller\LinkedResources',
'action' => 'index',
],
'constraints' => [
'resource-id' => '\d+',
],
],
],
],
],
];
22 changes: 22 additions & 0 deletions application/src/Controller/LinkedResourcesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace Omeka\Controller;

use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;

class LinkedResourcesController extends AbstractActionController
{
public function __construct()
{
}

public function indexAction()
{
$resource = $this->api()->read('resources', $this->params('resource-id'))->getContent();

$view = new ViewModel;
$view->setTerminal(true);
$view->setVariable('resource', $resource);
return $view;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Omeka\Service\Controller;

use Interop\Container\ContainerInterface;
use Omeka\Controller\LinkedResourcesController;
use Laminas\ServiceManager\Factory\FactoryInterface;

class LinkedResourcesControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $services, $requestedName, array $options = null)
{
return new LinkedResourcesController;
}
}
15 changes: 15 additions & 0 deletions application/src/Service/ViewHelper/LinkedResourcesFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Omeka\Service\ViewHelper;

use Omeka\View\Helper\LinkedResources;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;

class LinkedResourcesFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $services, $requestedName, array $options = null)
{
return new LinkedResources;
}
}
23 changes: 23 additions & 0 deletions application/src/View/Helper/LinkedResources.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Omeka\View\Helper;

use Laminas\View\Helper\AbstractHelper;
use Omeka\Api\Representation\AbstractResourceEntityRepresentation;

class LinkedResources extends AbstractHelper
{
public function __construct()
{
}

public function __invoke(AbstractResourceEntityRepresentation $resource, int $siteId = null)
{
$view = $this->getView();
$view->headScript()->appendFile($view->assetUrl('js/linked-resources.js', 'Omeka'));
return sprintf(
'<div id="linked-resources-container" data-url="%s" data-site-id="%s"></div>',
$view->url('linked-resources', ['resource-id' => $resource->id()]),
$view->escapeHtml($siteId)
);
}
}
11 changes: 0 additions & 11 deletions application/view/common/linked-resources.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,3 @@ $caption = sprintf(
<?php echo ($totalCount > $perPage) ? '<div class="linked-footer">' . $pagination . '</div>' : ''; ?>

</div>

<script>
const propertySelect = $('#resource-property-select');
const url = propertySelect.data('url');
const fragment = propertySelect.data('fragment');
propertySelect.on('change', function(e) {
const selectedOption = propertySelect.find(':selected');
const resourceProperty = selectedOption.val();
window.location = url + '?' + $.param({resource_property: resourceProperty}) + '#' + fragment;
});
</script>
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
<?php
$options = [
'page' => $this->params()->fromQuery('page', 1),
'perPage' => 25,
'resourceProperty' => $this->params()->fromQuery('resource_property'),
];
$siteId = null;
if ($this->siteSetting('exclude_resources_not_in_site')) {
$options['siteId'] = $this->currentSite()->id();
$siteId = $this->currentSite()->id();
}
$subjectValues = $resource->displaySubjectValues($options);
?>
<?php if ($subjectValues): ?>
<div id="resources-linked">
<h3><?php echo $this->translate('Linked resources'); ?></h3>
<?php echo $subjectValues; ?>
</div>
<?php if ($resource->subjectValueTotalCount(null, null, $siteId)): ?>
<?php echo $this->linkedResources($resource, $siteId); ?>
<?php endif; ?>
16 changes: 5 additions & 11 deletions application/view/omeka/admin/item-set/show.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,12 @@ $sectionNavs = [

<div id="resources-linked" class="section">
<?php if ($itemSet->subjectValueTotalCount()): ?>
<p><?php echo $translate('The following resources link to this item set:'); ?></p>
<?php echo $itemSet->displaySubjectValues([
'page' => $this->params()->fromQuery('page', 1),
'perPage' => 25,
'resourceProperty' => $this->params()->fromQuery('resource_property'),
]); ?>
<?php echo $this->linkedResources($itemSet); ?>
<?php else: ?>
<div class="no-resources">
<p><?php echo $translate('No resources link to this item set.'); ?></p>
</div>
<?php endif; ?>
</div>
<div class="no-resources">
<p><?php echo $translate('No resources link to this item set.'); ?></p>
</div>
<?php endif; ?>

<div class="active sidebar">
<div class="meta-group">
Expand Down
15 changes: 6 additions & 9 deletions application/view/omeka/admin/item/show.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,13 @@ $itemMedia = $item->media();

<div id="resources-linked" class="section">
<?php if ($item->subjectValueTotalCount()): ?>
<?php echo $item->displaySubjectValues([
'page' => $this->params()->fromQuery('page', 1),
'perPage' => 25,
'resourceProperty' => $this->params()->fromQuery('resource_property'),
]); ?>
<?php echo $this->linkedResources($item); ?>
<?php else: ?>
<div class="no-resources">
<p><?php echo $translate('No resources link to this item.'); ?></p>
</div>
<?php endif; ?>
<div class="no-resources">
<p><?php echo $translate('No resources link to this item.'); ?></p>
</div>
<?php endif; ?>

</div>

<div class="sidebar active">
Expand Down
5 changes: 5 additions & 0 deletions application/view/omeka/linked-resources/index.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php echo $resource->displaySubjectValues([
'page' => $this->params()->fromQuery('page', 1),
'perPage' => 25,
'resourceProperty' => $this->params()->fromQuery('resource_property'),
]); ?>

0 comments on commit 865009a

Please sign in to comment.