diff --git a/css/focal_point_preview.css b/css/focal_point_preview.css new file mode 100644 index 0000000..50ff21b --- /dev/null +++ b/css/focal_point_preview.css @@ -0,0 +1,46 @@ +#focal-point-preview { + height: 550px; + text-align: center; +} + +#focal-point-preview-image { + max-height: 500px; + max-width: 100%; + box-shadow: 4px 4px 10px rgba(0,0,0,0.75); +} + +#focal-point-derivatives { + height: 14em; + white-space: nowrap; + overflow-x: scroll; + overflow-y: hidden; +} + +.focal-point-derivative-preview { + display: inline-block; + text-align: center; + font-size: 1em; + margin: 1em 1em 1em 0; + padding: 0 1em 1em 1em; + border: 1px solid #CCCCCC; + background: #EEEEEE; + min-height: 11em; + vertical-align: top; +} + +.focal-point-derivative-preview.active { + background: #CCCCCC; + border-color: #999999; +} + +.focal-point-derivative-preview-image { + max-height: 8em; + max-width: 100%; +} + +.focal-point-preview-wrapper .note { + display: block; + color: #999999; + font-size: .8em; + text-align: center; +} \ No newline at end of file diff --git a/focal_point.libraries.yml b/focal_point.libraries.yml index c9f4847..40e6456 100644 --- a/focal_point.libraries.yml +++ b/focal_point.libraries.yml @@ -10,3 +10,11 @@ drupal.focal_point: - core/jquery.ui - core/jquery.ui.draggable - core/drupal + +drupal.focal_point_preview: + version: VERSION + js: + js/focal_point_preview.js: {} + css: + theme: + css/focal_point_preview.css: {} \ No newline at end of file diff --git a/focal_point.module b/focal_point.module index d56ab61..8d1d3e7 100644 --- a/focal_point.module +++ b/focal_point.module @@ -14,6 +14,23 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\crop\Entity\Crop; /** + * Implements hook_theme(); + */ +function focal_point_theme($existing, $type, $theme, $path) { + return array( + 'focal_point_preview_page' => array( + 'variables' => array( + 'original_image' => NULL, + 'derivative_images' => [], + 'focal_point' => '', + 'preview_image_note' => '', + 'derivative_image_note' => '', + ), + ), + ); +} + +/** * Implements hook_entity_presave(). * * Saves the focal point value for the image file entity about to be saved. diff --git a/js/focal_point_preview.js b/js/focal_point_preview.js new file mode 100644 index 0000000..a94ccf4 --- /dev/null +++ b/js/focal_point_preview.js @@ -0,0 +1,67 @@ +/** + * @file + * Javascript functionality for the focal point preview page. + */ + +(function($, Drupal) { + 'use strict'; + + /** + * Focal Point preview. + */ + Drupal.behaviors.focalPointPreview = { + attach: function(context, settings) { + var $focalPointDerivativePreviews = $(".focal-point-derivative-preview"); + + var $focalPointImagePreview = $("#focal-point-preview-image"); + var $focalPointImagePreviewLabel = $("#focal-point-preview-label"); + + var originalImageURL = $focalPointImagePreview.attr('src'); + var originalImagePreviewLabel = $focalPointImagePreviewLabel.html(); + + $focalPointDerivativePreviews.each(function () { + $(this).click(function(event) { + // Before adding the active class, remove the active class from all + // derivative previews in case one is already active. + $(".focal-point-derivative-preview").removeClass('active'); + $(this).addClass('active'); + + // Set the main preview label and image to this derivative since it + // was just clicked. + var imageSrc = $(this).find("img").attr('src'); + var imageLabel = $(this).find('h3').html(); + $focalPointImagePreviewLabel.html(imageLabel); + $focalPointImagePreview.attr('src', imageSrc); + + // Prevent the window click event from running. + event.stopPropagation(); + }); + }); + + $(window).click(function(event) { + resetPreview(); + }); + $(window).keyup(function(event) { + // Check if the esc key was pressed. + if (event.keyCode === 27) { + resetPreview(); + } + }); + + /** + * Reset the main preview image. + * + * Remove the active class from all derivative image previews and then + * reset the main preview image and label. + */ + function resetPreview() { + $focalPointDerivativePreviews.removeClass('active'); + + $focalPointImagePreviewLabel.html(originalImagePreviewLabel); + $focalPointImagePreview.attr('src', originalImageURL); + } + + } + + }; +})(jQuery, Drupal); diff --git a/src/Controller/FocalPointPreviewController.php b/src/Controller/FocalPointPreviewController.php index 5e429aa..240e30e 100644 --- a/src/Controller/FocalPointPreviewController.php +++ b/src/Controller/FocalPointPreviewController.php @@ -21,7 +21,11 @@ class FocalPointPreviewController extends ControllerBase { * {@inheritdoc} */ public function content($fid, $focal_point_value) { - $output = []; + $output = [ + '#attached' => [ + 'library' => ['focal_point/drupal.focal_point_preview'] + ], + ]; $image = $this->entityTypeManager()->getStorage('file')->load($fid); $styles = $this->getFocalPointImageStyles(); @@ -32,24 +36,44 @@ class FocalPointPreviewController extends ControllerBase { // the focal point value. image_path_flush($image->getFileUri()); + $derivative_images = []; + + $original_image = [ + '#theme' => 'image', + '#uri' => $image->getFileUri(), + '#alt' => t('Focal Point Preview Image'), + '#attributes' => [ + 'id' => 'focal-point-preview-image', + ], + ]; + foreach ($styles as $style) { $style_label = $style->get('label'); $url = $this->buildUrl($style, $image, $focal_point_value); - // @todo: Should probably create a template for this. - $output[$style->getName()] = [ - 'title' => [ - '#type' => 'markup', - '#markup' => '