Skip to content

Commit 8176a02

Browse files
Sereza7manuelleduc
andauthored
XWIKI-6762: Add caption to images in Gallery Macro (#4716)
* Added the alt to the currentImage if it is different from the filename * Replaced a local variable declaration with a const * Fixed the localizations in the file. * Replaced the hard coded DOM with actual prototype construction. Co-authored-by: Manuel Leduc <[email protected]>
1 parent 13e94cb commit 8176a02

File tree

1 file changed

+57
-14
lines changed
  • xwiki-platform-core/xwiki-platform-web/xwiki-platform-web-war/src/main/webapp/resources/uicomponents/widgets/gallery

1 file changed

+57
-14
lines changed

xwiki-platform-core/xwiki-platform-web/xwiki-platform-web-war/src/main/webapp/resources/uicomponents/widgets/gallery/gallery.js

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,52 @@
1717
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
1818
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
1919
*/
20+
/*!
21+
#set ($l10nKeys = [
22+
'core.widgets.gallery.previousImage',
23+
'core.widgets.gallery.currentImage',
24+
'core.widgets.gallery.nextImage',
25+
'core.widgets.gallery.index.description',
26+
'core.widgets.gallery.maximize',
27+
'core.widgets.gallery.minimize'
28+
])
29+
#set ($l10n = {})
30+
#foreach ($key in $l10nKeys)
31+
#set ($discard = $l10n.put($key, $services.localization.render($key)))
32+
#end
33+
#[[*/
34+
// Start JavaScript-only code.
35+
(function(l10n) {
36+
"use strict";
2037
var XWiki = (function (XWiki) {
2138
// Start XWiki augmentation.
2239
XWiki.Gallery = Class.create({
2340
initialize : function(container) {
2441
this.images = this._collectImages(container);
25-
this.container = container.update(
26-
'<button class="maximize" title="${escapetool.xml($services.localization.render("core.widgets.gallery.maximize"))}"></button>' +
27-
'<button class="previous" title="${escapetool.xml($services.localization.render("core.widgets.gallery.previousImage"))}">&lt;</button>' +
28-
'<img class="currentImage" alt="${escapetool.xml($services.localization.render("core.widgets.gallery.currentImage"))}"/>' +
29-
'<button class="next" title="${escapetool.xml($services.localization.render("core.widgets.gallery.nextImage"))}">&gt;</button>' +
30-
'<div class="index" tabindex="0" title="${escapetool.xml($services.localization.render("core.widgets.gallery.index.description"))}" aria-description="${escapetool.xml($services.localization.render("core.widgets.gallery.index.description"))}">0 / 0</div>'
31-
);
42+
// Generate the different parts of the gallery
43+
let maximizeButton = new Element('button', {
44+
'class': 'maximize', 'title': l10n['core.widgets.gallery.maximize']});
45+
let previousButton = new Element('button', {
46+
'class': 'previous', 'title': l10n['core.widgets.gallery.previousImage']});
47+
previousButton.insert("&lt;");
48+
let currentImage = new Element('img',
49+
{'class': 'currentImage', 'title': l10n['core.widgets.gallery.currentImage']});
50+
let nextButton = new Element('button',
51+
{'class': 'next', 'title': l10n['core.widgets.gallery.nextImage']});
52+
nextButton.insert("&gt;");
53+
let imageIndex = new Element('div', {
54+
'class': 'index', 'tabindex': 0, 'title': l10n['core.widgets.gallery.index.description'],
55+
'aria-description': l10n['core.widgets.gallery.index.description']});
56+
imageIndex.insert("0 / 0");
57+
// Remove the content that's left in the container
58+
container.update("");
59+
// Add the gallery parts in the container, in the correct order.
60+
container.insert(maximizeButton);
61+
container.insert(previousButton);
62+
container.insert(currentImage);
63+
container.insert(nextButton);
64+
container.insert(imageIndex);
65+
this.container = container;
3266
this.container.addClassName('xGallery');
3367

3468
// Instead of an arbitrary element to catch focus, we use the index.
@@ -63,7 +97,7 @@ XWiki.Gallery = Class.create({
6397
var imageElements = container.select('img');
6498
for(var i = 0; i < imageElements.length; i++) {
6599
var imageElement = imageElements[i];
66-
images.push({url: imageElement.getAttribute('src'), title: imageElement.title});
100+
images.push({url: imageElement.getAttribute('src'), title: imageElement.title, alt: imageElement.alt});
67101
imageElement.removeAttribute('src');
68102
}
69103
return images;
@@ -117,8 +151,7 @@ XWiki.Gallery = Class.create({
117151
this.maximizeToggle.toggleClassName('maximize');
118152
this.maximizeToggle.toggleClassName('minimize');
119153
this.maximizeToggle.title = this.maximizeToggle.hasClassName('maximize') ?
120-
"${escapetool.javascript($services.localization.render('core.widgets.gallery.maximize'))}" :
121-
"${escapetool.javascript($services.localization.render('core.widgets.gallery.minimize'))}";
154+
l10n['core.widgets.gallery.maximize'] : l10n['core.widgets.gallery.minimize'];
122155
this.container.toggleClassName('maximized');
123156
$(document.documentElement).toggleClassName('maximized');
124157
// When a keyboard shortcut is used, the gallery is not focused by default. In order to keep the screen at the
@@ -133,11 +166,19 @@ XWiki.Gallery = Class.create({
133166
}
134167
// Update only if it's a different image. Some browsers, e.g. Chrome, don't fire the load event if the image URL
135168
// doesn't change. Another trick would be to reset the src attribute before setting the actual URL (set to '').
136-
if (this.currentImage.src != this.images[index].url) {
169+
const imageData = this.images[index];
170+
if (this.currentImage.src !== imageData.url) {
137171
this.currentImage.style.visibility = 'hidden';
138172
Element.addClassName(this.currentImage.parentNode, 'loading');
139-
this.currentImage.title = this.images[index].title;
140-
this.currentImage.src = this.images[index].url;
173+
this.currentImage.title = imageData.title;
174+
const filename = decodeURI(imageData.url.split('/').pop().split('?')[0]);
175+
// If the alt is just the name of the file, we instead fall back on the human-readable currentImage translation.
176+
if (filename !== imageData.alt) {
177+
this.currentImage.alt = imageData.alt;
178+
} else {
179+
this.currentImage.alt = l10n['core.widgets.gallery.currentImage'];
180+
}
181+
this.currentImage.src = imageData.url;
141182
}
142183
this.index = index;
143184
this.indexDisplay.update((index + 1) + ' / ' + this.images.length);
@@ -166,4 +207,6 @@ if (XWiki.contextaction !== 'export') {
166207

167208
// End XWiki augmentation.
168209
return XWiki;
169-
}(XWiki || {}));
210+
}(XWiki || {}));
211+
// End JavaScript-only code.
212+
}).apply(']]#', $jsontool.serialize([$l10n]));

0 commit comments

Comments
 (0)