Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the mirador config more easily alterable, Drupal-side. #22

Merged
merged 5 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions islandora_mirador.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ viewer:
theme:
css/mirador.css: {}
dependencies:
- core/once
- core/drupal
- core/drupalSettings
- islandora_mirador/mirador
32 changes: 28 additions & 4 deletions islandora_mirador.module
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ function islandora_mirador_theme() {
}

/**
* Implements template_preprocess_*().
* Preprocess variables for mirador templates.
*/
function template_preprocess_mirador(&$variables) {
$variables['mirador_view_id'] = Html::getUniqueId($variables['mirador_view_id']);
$variables['#attached']['drupalSettings']['mirador_view_id'] = $variables['mirador_view_id'];

/**
* @var \Drupal\islandora_mirador\IslandoraMiradorPluginManager
Expand All @@ -41,11 +40,11 @@ function template_preprocess_mirador(&$variables) {
/**
* @var \Drupal\Core\Config\ImmutableConfig
*/
$config = Drupal::service('config.factory')->get('islandora_mirador.settings');
$config = \Drupal::service('config.factory')->get('islandora_mirador.settings');

$mirador_plugins = $mirador_plugin_manager->getDefinitions();
$enabled_plugins = $config->get('mirador_enabled_plugins');
$variables['#attached']['drupalSettings']['mirador_enabled_plugins'] = array_filter(array_values($enabled_plugins));
$variables['#attached']['drupalSettings']['mirador']['enabled_plugins'] = array_filter(array_values($enabled_plugins));

$window_config = [];
foreach ($mirador_plugins as $plugin_id => $plugin_definition) {
Expand All @@ -57,7 +56,32 @@ function template_preprocess_mirador(&$variables) {
$plugin_instance->windowConfigAlter($window_config);
}
}

// XXX: Maintain the original properties, for now.
$variables['#attached']['drupalSettings']['mirador_view_id'] = $variables['mirador_view_id'];
$variables['#attached']['drupalSettings']['mirador_window_settings'] = $window_config;
$variables['#attached']['drupalSettings']['mirador_enabled_plugins'] = $variables['#attached']['drupalSettings']['mirador']['enabled_plugins'];

// mirador.window_settings ... isn't presently used on its own?
$variables['#attached']['drupalSettings']['mirador']['window_settings'] = $window_config;

// mirador.viewers is an associative array mapping CSS/jQuery/once selectors
// to objects of mirador configuration.
$variables['#attached']['drupalSettings']['mirador']['viewers']["#{$variables['mirador_view_id']}"] = [
'id' => $variables['mirador_view_id'],
'manifests' => [
$variables['iiif_manifest_url'] => [
'provider' => 'Islandora'
],
],
'window' => $window_config,
'windows' => [
[
'manifestId' => $variables['iiif_manifest_url'],
'thumbnailNavigationPosition' => 'far-bottom',
],
]
];
}

/**
Expand Down
57 changes: 15 additions & 42 deletions js/mirador_viewer.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,30 @@
/*jslint browser: true*/
/*global Mirador, textOverlayPlugin, Drupal*/
/*jslint browser: true, esversion: 6 */
/*global Mirador, Drupal, once*/
/**
* @file
* Displays Mirador viewer.
*/
(function ($, Drupal) {
(function (Drupal, once) {
'use strict';

/**
* If initialized.
* @type {boolean}
*/
var initialized;
/**
* Unique HTML id.
* @type {string}
*/
var base;

function init(context,settings){
if (!initialized){
initialized = true;

/*
Mirador
*/
var miradorInstance = Mirador.viewer({
"id": base,
"manifests": {
[settings.iiif_manifest_url]: {provider: "Islandora"}
},
"window": settings.mirador_window_settings,
"windows": [
{
"manifestId": settings.iiif_manifest_url,
"thumbnailNavigationPosition": 'far-bottom'
}
]
}, window.miradorPlugins);
}
}
Drupal.Mirador = Drupal.Mirador || {};

/**
* Initialize the Mirador Viewer.
*/
Drupal.behaviors.Mirador = {
attach: function (context, settings) {
base = settings.mirador_view_id;
init(context,settings);
Object.entries(settings.mirador.viewers).forEach(entry => {
const [base, values] = entry;
once('mirador-viewer', base, context).forEach(() =>
Mirador.viewer(values, window.miradorPlugins || {})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The window.miradorPlugins thing here seems like we could have saner expectations? Like, have the integration itself include it itself, so there's not this arbitrary JS extension point?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With something like roblib/mirador-integration-islandora#1, could possibly avoid having to touch window.miradorPlugins here? Could just become:

Suggested change
Mirador.viewer(values, window.miradorPlugins || {})
Mirador.viewer(values)

);
});
},
detach: function () {
detach: function (context, settings) {
Object.entries(settings.mirador.viewers).forEach(entry => {
const [base, ] = entry;
once.remove('mirador-viewer', base, context);
});
}
};

})(jQuery, Drupal);
})(Drupal, once);
12 changes: 4 additions & 8 deletions src/Plugin/Block/MiradorBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,10 @@ public function build() {
$build = [
"#title" => $this->t('Mirador Viewer'),
"#description" => $this->t("A div for mirador viewer"),
"#theme" => "mirador",
'#mirador_view_id' => $id,
'#iiif_manifest_url' => $manifest_url,
"#attached" => [
'drupalSettings' => [
'iiif_manifest_url' => $manifest_url,
'mirador_view_id' => $id,
],
'viewer' => [
"#theme" => "mirador",
'#mirador_view_id' => $id,
'#iiif_manifest_url' => $manifest_url,
],
];

Expand Down
17 changes: 10 additions & 7 deletions src/Plugin/Field/FieldFormatter/MiradorImageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\islandora_mirador\Plugin\Field\FieldFormatter;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
Expand Down Expand Up @@ -119,27 +120,29 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
if (empty($files)) {
return $elements;
}

$iiif_url = $this->configFactory->get('islandora_mirador.settings')->get('iiif_manifest_url');
$token_service = $this->token;
foreach ($files as $file) {
$cache_meta = CacheableMetadata::createFromObject($file);

$medias = $this->utils->getReferencingMedia($file->id());
$first_media = array_values($medias)[0];
$cache_meta->addCacheableDependency($first_media);
$node = $first_media->get('field_media_of')->entity;
$cache_meta->addCacheableDependency($node);
$id = 'mirador_' . $node->id();
$manifest_url = $token_service->replace($iiif_url, ['node' => $node]);
$elements[] = [
$renderable = [
'#theme' => 'mirador',
'#mirador_view_id' => $id,
'#iiif_manifest_url' => $manifest_url,
'#attached' => [
'drupalSettings' => [
'iiif_manifest_url' => $manifest_url,
'mirador_view_id' => $id,
],
],
'#settings' => $settings,
];
$cache_meta->applyTo($renderable);
$elements[] = $renderable;
}

return $elements;
}

Expand Down