Skip to content

Commit

Permalink
feat: Reimplement the Zooming Image Tool
Browse files Browse the repository at this point in the history
This recreates the Zooming Image Tool template for the HTML block.  It
does it in such a way that doesn't depend on any external resources:
both the loupe code and sample image are inlined.

Some benefits to this version are:

* We can now maintain the loupe javascript code properly

* Because the javascript is included in the contents of the block
  itself, the course author can customize it as needed

* As opposed to the previous iteration, the magnified image URL is now
  optional: if it's not present, the regular image will be used for
  magnification

This also removes some CSS left over from the previous iteration.
  • Loading branch information
arbrandes committed Dec 11, 2024
1 parent ee30f1b commit 7586e11
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 7 deletions.
7 changes: 0 additions & 7 deletions cms/static/sass/elements/_vendor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@
z-index: 100000 !important;
}

//jQuery loupeAndLightbox Plugin
.zooming-image-place {
.larger {
left: 0 !important;
bottom: 100% !important;
}
}
// ====================

// reset styles to remove ui-lightness jquery ui theme from the tabs component (used in the add component problem tab menu)
Expand Down
202 changes: 202 additions & 0 deletions xmodule/templates/html/zooming_image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
---
metadata:
display_name: Zooming Image Tool
data: |
<p>Use the Zooming Image Tool to enable learners to see details of large, complex images. With the tool, the learner can move the mouse pointer over a part of the image to enlarge it and see more detail.</p>
<p>To set it up, first upload the regular image file and, optionally, a magnified image file to your course. Then refer to them with the following HTML code, replacing the values in <i>italics</i> accordingly:</p>
<pre>
&lt;div class="zooming-image-container" style="position: relative;"&gt;
&lt;a class="zooming-image" <strong>data-src</strong>="<i style="color: blue">(Optional) URL to the magnified image</i>"&gt;
&lt;img <strong>src</strong>="<i style="color: blue">URL to the regular image</i>" /&gt;
&lt;/a&gt;
&lt;/div&gt;
</pre>
<p>If a magnified image is not provided, the regular one will be used at its native size.</p>
<p>Feel free to modify the example below for your own use, but take care not to remove the included Javascript.</p>
<!-- MODIFY HERE: start -->
<div class="zooming-image-container" style="position: relative;">
<a class="zooming-image" data-src="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0iI3NjaGlybSIgLz4KIDx1c2UgdHJhbnNmb3JtPSJzY2FsZSgwLjAyOCkiIGZpbHRlcj0idXJsKCNzMCkiIHhsaW5rOmhyZWY9IiNzY2hpcm0iIC8+CiA8L2c+Cjwvc3ZnPg==" />
</a>
</div>
<!-- MODIFY HERE: end -->
<!-- DO NOT REMOVE: start -->
<script type="text/javascript">//<![CDATA[
(function($) {
$.fn.loupe = function(options) {
var settings = $.extend({}, $.fn.loupe.defaults, options);
return this.each(function() {
var $this = $(this),
$targetImage = $this.find('> img'),
$magnifiedImage = $('<img />'),
$loupe = $('<div class="larger" style="overflow: hidden;">'),
$errorMessage = $('<div class="lal_errorMessage">'),
$loader = $('<div class="lal_loader">Loading...</div>');
$this.css({
cursor:'default'
});
$targetImage.css({
cursor: 'pointer'
});
$magnifiedImage.css({
position: 'absolute',
maxWidth: 'none'
});
$loupe.css({
cursor: 'none',
display: 'none',
border: settings.border,
height: settings.height,
overflow: 'hidden',
position: 'absolute',
bottom: 0,
left: 0,
width: settings.width,
zIndex: settings.zIndex,
borderRadius: '50%',
background: '#FFF',
});
$errorMessage
.text(settings.errorMessage)
.css({
height:settings.height,
width:settings.width
});
$loader.css({
height:settings.height,
width:settings.width
});
$this.click(function(event) {
event.preventDefault();
});
$targetImage.click(function(event) {
if(!$loupe.hasClass('visible')) {
var left = event.pageX,
top = event.pageY;
if(!$magnifiedImage.hasClass('appended')) {
getMagnifiedImage();
}
setTimeout(function() {
appendLoupe();
magnify(left, top);
}, 100);
}
});
$targetImage.mousemove(function(event) {
var left = event.pageX,
top = event.pageY,
offsetTop = $targetImage.offset().top-($loupe.width()/2),
offsetLeft = $targetImage.offset().left-($loupe.height()/2),
offsetBottom = $targetImage.offset().top+$targetImage.height()+($loupe.height()/2),
offsetRight = $targetImage.offset().left+$targetImage.width()+($loupe.width()/2);
if(left < offsetLeft || left > offsetRight || top < offsetTop || top > offsetBottom) {
$targetImage.css({cursor:'default'});
} else {
$targetImage.css({cursor:'crosshair'});
magnify(left, top);
}
}).click(function() {
detachLoupe();
}).mouseleave(function() {
pulseLoupe();
});
// Detach when clicking outside of the loupe
$(document).click(function(event) {
if($loupe.hasClass('visible')) {
detachLoupe();
}
});
// Resize with window
$(window).resize(function() {
if($loupe.is(':visible')) {
$magnifiedImage.css({
left:-($magnifiedImage.width()/2)+($loupe.width()/2),
top:-($magnifiedImage.height()/2)+($loupe.height()/2)
});
}
});
function magnify(left, top) {
var heightDiff = $magnifiedImage.height()/$targetImage.height(),
widthDiff = $magnifiedImage.width()/$targetImage.width(),
magnifierTop = (-(top - $targetImage.offset().top)*heightDiff)+(settings.height/2),
magnifierLeft = (-(left - $targetImage.offset().left)*widthDiff)+(settings.width/2);
$magnifiedImage.css({
top:magnifierTop,
left:magnifierLeft
});
};
function appendLoupe() {
$loupe
.appendTo($('.zooming-image-container'))
.append($magnifiedImage)
.fadeIn(settings.fadeSpeed, function() {
$(this).addClass('visible');
});
};
function getMagnifiedImage() {
var src = $this.data('src') ? $this.data('src') : $targetImage.attr('src');
$loader.appendTo($loupe);
$magnifiedImage
.load(function() {
$(this).addClass('appended');
$loader.detach();
})
.error(function () {
$(this).hide();
$loupe
.append($errorMessage)
.addClass('lal_loadError');
$loader.detach();
})
.attr('src', src);
};
function detachLoupe() {
$loupe.fadeOut(settings.fadeSpeed, function() {
$(this)
.removeClass('visible')
.detach();
});
};
function pulseLoupe() {
$loupe.fadeTo(150, 0.25, function() {
$(this).fadeTo(150, 1.0);
});
};
});
};
$.fn.loupe.defaults = {
zIndex: 1000,
width: 350,
height: 350,
border: '2px solid #ccc',
fadeSpeed: 250,
errorMessage: 'Image load error'
};
})(jQuery);
$('.zooming-image').loupe();
//]]></script>
<!-- DO NOT REMOVE: end -->

0 comments on commit 7586e11

Please sign in to comment.