From be8351486ad1e8c04d4d521a151d54861932ee0b Mon Sep 17 00:00:00 2001 From: Victor Häggqvist Date: Fri, 15 Jan 2016 01:27:40 +0100 Subject: caption plugin --- src/Plugins/Captions.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/Plugins/Captions.js (limited to 'src/Plugins') diff --git a/src/Plugins/Captions.js b/src/Plugins/Captions.js new file mode 100644 index 0000000..0286a82 --- /dev/null +++ b/src/Plugins/Captions.js @@ -0,0 +1,39 @@ +/** + * @author Victor Häggqvist + * @since 2016-01-16 + */ + +export class Captions { + + constructor() { + this.element = document.createElement('div'); + this.element.id = 'imagelightbox-caption'; + //this.element.innerHTML = 'Close'; + } + + register(lightbox) { + this.lightbox = lightbox; + lightbox.addOnLoadStartListener(this.hideCaption.bind(this)); + lightbox.addOnLoadEndListener(this.showCaption.bind(this)); + lightbox.addOnEndListener(this.hideCaption.bind(this)); + } + + showCaption() { + let img = this.lightbox.target.querySelector('img'); + + if (img === null) return; + + let caption = img.getAttribute('alt'); + if (caption !== null && caption.length > 0) { + this.element.innerHTML = img.alt; + document.body.appendChild(this.element); + } + } + + hideCaption() { + try { + document.body.removeChild(this.element); + } catch (e) {} + } + +} -- cgit v1.2.3