-
Notifications
You must be signed in to change notification settings - Fork 16
#66 Add openseadragon-bookmark url plugin #77
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
base: 3.x
Are you sure you want to change the base?
Changes from all commits
1ae9659
51ec570
ffa0b67
a1a05b4
6d99755
510339c
1587882
be89e9a
fe3c041
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -124,5 +124,6 @@ | |
| }); | ||
| } | ||
|
|
||
| return viewer; | ||
| }; | ||
| })(once); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # OpenSeadragon Bookmark URL | ||
|
|
||
| Enables the OpenSeadragon Bookmark URL plugin that tracks OpenSeadragon zoom level and coordinates to allow sharing of a display via URL. | ||
|
|
||
| This module assumes you have added a string field to a media type that can hold the bookmark fragment, e.g. | ||
| `file` with `field_bookmark_url`. | ||
|
|
||
| ## Requirements | ||
| - [OpenSeadragon](https://www.drupal.org/project/openseadragon) | ||
|
|
||
| ## Installation | ||
|
|
||
| Install as you would normally install a contributed Drupal module. For further information, see | ||
| [Installing Drupal Modules](https://www.drupal.org/docs/extending-drupal/installing-modules). | ||
|
|
||
| ## Configuration | ||
| Currently, no UI exists for configuration. | ||
|
|
||
| Create an openseadragon_bookmark_url.settings.yml file and set `bookmark_url_bundle` to the media type, e.g. `file`, and `bookmark_url_field` to the field. The value from the specific media and field are inject into the Drupal settings available to javascript. | ||
| * `bookmark_url_bundle`: `file` | ||
| * `bookmark_url_field`: `field_bookmark_url` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Default configuration for the OpenSeadragon Bookmark URL submodule. | ||
| # These values are intentionally empty; an administrator must choose the | ||
| # appropriate content type and field for their site via the settings form. | ||
| bookmark_url_bundle: '' | ||
| bookmark_url_field: '' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Schema for openseadragon_bookmark_url.settings config object. | ||
|
|
||
| openseadragon_bookmark_url.settings: | ||
| type: config_object | ||
| label: 'OpenSeadragon Bookmark URL settings' | ||
| mapping: | ||
| bookmark_url_bundle: | ||
| type: string | ||
| label: 'Content type (bundle) machine name' | ||
| constraints: | ||
| # Must be a valid (media) bundle machine name or empty string. | ||
| Regex: | ||
| pattern: '/^[a-z0-9_]*$/' | ||
| message: 'The bundle must be a valid machine name (lowercase letters, digits, and underscores only).' | ||
|
|
||
| bookmark_url_field: | ||
| type: string | ||
| label: 'Field machine name that stores the bookmark URL' | ||
| constraints: | ||
| Regex: | ||
| pattern: '/^[a-z0-9_]*$/' | ||
| message: 'The field name must be a valid machine name (lowercase letters, digits, and underscores only).' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /*jslint browser: true*/ | ||
| /*global OpenSeadragon, Drupal*/ | ||
| /** | ||
| * @file | ||
| * Attaches openseadragon-bookmark-url to OpenSeadragon viewer. | ||
| */ | ||
| (function($, Drupal, drupalSettings, once) { | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * The DOM element that represents the Singleton Instance of this class. | ||
| * @type {string} | ||
| */ | ||
| var base = '#openseadragon-viewer'; | ||
|
|
||
| /** | ||
| * Invoke bookmark url plugin. | ||
| */ | ||
| Drupal.behaviors.openSeadragonBookmarkURL = { | ||
| attach: function(context, settings) { | ||
| Object.keys(settings.openseadragon).forEach(function(osdViewerId) { | ||
| // Use custom element #id if set. | ||
| base = '#' + osdViewerId; | ||
| once('openSeadragonViewerBookmarkURL', base, context).forEach(function () { | ||
| // Load bookmark url, but without page tracking, to allow hash to be set. | ||
| Drupal.openSeadragonViewer[base].bookmarkUrl({trackPage: false}); | ||
| if (window.location.hash === '') { | ||
| var hash = settings.openseadragon[osdViewerId].openseadragonBookmarkUrl; | ||
| if (hash !== '') { | ||
| window.location.hash = hash; | ||
| } | ||
| } | ||
| // With hash set or not, once OSD loaded, begin page tracking. | ||
| Drupal.openSeadragonViewer[base].addOnceHandler('open', function(){ | ||
| Drupal.openSeadragonViewer[base].bookmarkUrl({trackPage: true}); | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| })($, Drupal, drupalSettings, once); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| name: 'OpenSeadragon Bookmark URL' | ||
| type: module | ||
| description: 'Wrapper for https://github.com/openseadragon/bookmark-url library' | ||
| package: Media | ||
| core_version_requirement: ^10 || ^11 | ||
| dependencies: | ||
| - openseadragon:openseadragon |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| plugin: | ||
| remote: https://github.com/openseadragon/bookmark-url | ||
| version: "0.0.5" | ||
| license: | ||
| name: BSD-3-Clause | ||
| url: https://github.com/openseadragon/bookmark-url/blob/master/LICENSE | ||
| gpl-compatible: true | ||
| js: | ||
| https://cdn.jsdelivr.net/gh/openseadragon/bookmark-url@13c534c6ad7078fa44675f23185b75ee760d659c/openseadragon-bookmark-url.js: | ||
| type: external | ||
| minified: false | ||
| attributes: | ||
| integrity: "sha256-RDZWd2DzG9Asb3Pf3U0UrC0wKEGVJaC3D00Z9mqN63w=" | ||
| crossorigin: anonymous | ||
| openseadragon_bookmark_url: | ||
| js: | ||
| js/drupal-openseadragon-bookmark-url.js: {} | ||
| dependencies: | ||
| - core/drupal | ||
| - core/once | ||
| - core/drupalSettings | ||
| - openseadragon/openseadragon | ||
| - openseadragon_bookmark_url/plugin |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,38 @@ | ||||||||||||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||
| * @file | ||||||||||||||||||||||||||||||||||||
| * Attach bookmark-url.js. | ||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||
| * Attach openseadragon_bookmark_url js if openseadragon_formatter used. | ||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||
| * Implements hook_preprocess_HOOK(). | ||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||
| * {@inheritDoc} | ||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||
| function openseadragon_bookmark_url_preprocess_field(&$variables) { | ||||||||||||||||||||||||||||||||||||
| if (isset($variables['items'])) { | ||||||||||||||||||||||||||||||||||||
| foreach ($variables['items'] as $item) { | ||||||||||||||||||||||||||||||||||||
| if (isset($item['content']['#theme']) && $item['content']['#theme'] == 'openseadragon_formatter') { | ||||||||||||||||||||||||||||||||||||
| $variables['#attached']['library'][] = 'openseadragon_bookmark_url/openseadragon_bookmark_url'; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Inject bookmark URL into settings for later reference in js. | ||||||||||||||||||||||||||||||||||||
| $config = \Drupal::config('openseadragon_bookmark_url.settings'); | ||||||||||||||||||||||||||||||||||||
| $bookmark_url_bundle = $config->get('bookmark_url_bundle'); | ||||||||||||||||||||||||||||||||||||
| $bookmark_url_field = $config->get('bookmark_url_field'); | ||||||||||||||||||||||||||||||||||||
| if (isset($item['content']['#entity'])) { | ||||||||||||||||||||||||||||||||||||
| $entity = $item['content']['#entity']; | ||||||||||||||||||||||||||||||||||||
| if ($entity->bundle() == $bookmark_url_bundle) { | ||||||||||||||||||||||||||||||||||||
| $openseadragon_viewer_id = 'openseadragon-viewer-' . $entity->id(); | ||||||||||||||||||||||||||||||||||||
| if ($entity->hasField($bookmark_url_field)) { | ||||||||||||||||||||||||||||||||||||
| $fragment = $entity->get($bookmark_url_field)->value; | ||||||||||||||||||||||||||||||||||||
| $variables['#attached']['drupalSettings']['openseadragon'][$openseadragon_viewer_id]['openseadragonBookmarkUrl'] = $fragment; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1
to
+38
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Can we just use libraries-extend on openseadragon/openseadragon in this module's libraries file?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAICT For now, I've used
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah - I guess modules have to use hook_library_info_alter and can't leverage |
||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| services: | ||
| openseadragon_bookmark_url.config: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is used? If so can we drop this services file and |
||
| class: Drupal\openseadragon_bookmark_url\BookmarkUrlConfig | ||
| arguments: | ||
| - '@config.factory' | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Drupal\openseadragon_bookmark_url; | ||
|
|
||
| use Drupal\Core\Config\ConfigFactoryInterface; | ||
| use Drupal\Core\Config\ImmutableConfig; | ||
|
|
||
| /** | ||
| * Typed accessor service for openseadragon_bookmark_url.settings. | ||
| * | ||
| * Usage (from a service or controller): | ||
| * @code | ||
| * $config = \Drupal::service('openseadragon_bookmark_url.config'); | ||
| * $bundle = $config->getBundle(); | ||
| * $field = $config->getField(); | ||
| * @endcode | ||
| */ | ||
| final class BookmarkUrlConfig { | ||
|
|
||
| private const CONFIG_NAME = 'openseadragon_bookmark_url.settings'; | ||
|
|
||
| /** | ||
| * The openseadragon bookmark url config. | ||
| * | ||
| * @var \Drupal\Core\Config\ImmutableConfig | ||
| */ | ||
| private ImmutableConfig $config; | ||
|
|
||
| /** | ||
| * Constructs a BookmarkUrlConfig instance. | ||
| * | ||
| * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory | ||
| * The Drupal config factory service. | ||
| */ | ||
| public function __construct(ConfigFactoryInterface $configFactory) { | ||
| $this->config = $configFactory->get(self::CONFIG_NAME); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the configured node bundle machine name, or an empty string. | ||
| */ | ||
| public function getBundle(): string { | ||
| return $this->config->get('bookmark_url_bundle') ?? ''; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the configured field machine name, or an empty string. | ||
| */ | ||
| public function getField(): string { | ||
| return $this->config->get('bookmark_url_field') ?? ''; | ||
| } | ||
|
|
||
| /** | ||
| * Returns TRUE when both bundle and field have been set by an administrator. | ||
| */ | ||
| public function isConfigured(): bool { | ||
| return $this->getBundle() !== '' && $this->getField() !== ''; | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure that referencing what I assume is supposed to be
jQueryas$here is safe. On the other hand, it doesn't seem to be used, so it probably shouldn't be here in the first place?drupalSettingsdoesn't appear to be used directly, either.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, yeah, the attempt to use
$globally should cause warnings about unresolvable symbols, given Drupal loads jQuery with.noConflict()by default.Somewhat related: If the aliasing of
jQueryto$isn't necessary (because jQuery isn't actually being used), then the function wrapping the behavior definition as a whole isn't necessary.