| 1 |
import {Lightbox} from "./Lightbox"; |
| 2 |
import * as Util from "../Util"; |
| 3 |
import {Core} from "../Core"; |
| 4 |
|
| 5 |
export class PhotonicPhotoSwipe5 extends Lightbox { |
| 6 |
constructor() { |
| 7 |
super(); |
| 8 |
} |
| 9 |
|
| 10 |
soloImages() { |
| 11 |
const a = document.querySelectorAll('a[href]'); |
| 12 |
const solos = Array.from(a).filter(elem => /(\.jpg|\.jpeg|\.bmp|\.gif|\.png)/i.test(elem.getAttribute('href'))) |
| 13 |
.filter(elem => !elem.classList.contains('photonic-lb')); |
| 14 |
solos.forEach(solo => { |
| 15 |
solo.classList.add("photonic-photoswipe"); |
| 16 |
solo.classList.add("photonic-photoswipe-solo"); |
| 17 |
}); |
| 18 |
return solos; |
| 19 |
}; |
| 20 |
|
| 21 |
hostedVideo(a) { |
| 22 |
const html5 = a.getAttribute('href').match(new RegExp(/(\.mp4|\.webm|\.ogg)/i)); |
| 23 |
let css = a.classList.contains('photonic-lb'); |
| 24 |
|
| 25 |
if (html5 !== null && !css) { |
| 26 |
a.classList.add("photoswipe-html5-video"); |
| 27 |
let videos = document.querySelector('#photonic-html5-videos'); |
| 28 |
if (videos === null) { |
| 29 |
videos = document.createElement('div'); |
| 30 |
videos.style.display = 'none'; |
| 31 |
videos.setAttribute('id', 'photonic-html5-videos'); |
| 32 |
document.body.appendChild(videos); |
| 33 |
} |
| 34 |
|
| 35 |
videos.insertAdjacentHTML('beforeend', '<div id="photonic-html5-video-' + this.videoIndex + '"><video controls preload="none"><source src="' + a.getAttribute('href') + '" type="video/mp4">Your browser does not support HTML5 video.</video></div>'); |
| 36 |
|
| 37 |
a.setAttribute('data-html5-href', a.getAttribute('href')); |
| 38 |
a.setAttribute('href', '#photonic-html5-video-' + this.videoIndex); |
| 39 |
if (!a.getAttribute('id')) { |
| 40 |
a.setAttribute('id', 'photonic-html5-video-a-' + this.videoIndex); |
| 41 |
} |
| 42 |
this.videoIndex++; |
| 43 |
} |
| 44 |
}; |
| 45 |
|
| 46 |
changeVideoURL(element, regular, embed) { |
| 47 |
element.setAttribute('href', embed); |
| 48 |
}; |
| 49 |
|
| 50 |
modifyAdditionalVideoProperties(anchor) { |
| 51 |
if (anchor != null && anchor instanceof Element && anchor.tagName === 'A') { |
| 52 |
if (anchor.classList.contains('photoswipe5-video')) { |
| 53 |
anchor.classList.remove('photoswipe5-video'); |
| 54 |
anchor.classList.add('photoswipe-video'); |
| 55 |
} |
| 56 |
|
| 57 |
if (anchor.getAttribute('id') == null) { |
| 58 |
anchor.setAttribute('id', 'photonic-photoswipe-video-' + this.videoIndex); |
| 59 |
this.videoIndex++; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
setLightboxCaption(lightbox) { |
| 65 |
const self = this; |
| 66 |
lightbox.on('uiRegister', function() { |
| 67 |
lightbox.pswp.ui.registerElement({ |
| 68 |
name: 'custom-caption', |
| 69 |
order: 9, |
| 70 |
isButton: false, |
| 71 |
appendTo: 'root', |
| 72 |
html: 'View', |
| 73 |
onInit: (el, pswp) => { |
| 74 |
lightbox.pswp.on('change', () => { |
| 75 |
const currSlideElement = lightbox.pswp.currSlide.data.element; |
| 76 |
let captionHTML = ''; |
| 77 |
if (currSlideElement) { |
| 78 |
const hiddenCaption = Util.HTMLSanitizer.SanitizeHTML(currSlideElement.getAttribute('data-title')); |
| 79 |
if (hiddenCaption) { |
| 80 |
captionHTML = hiddenCaption; |
| 81 |
} |
| 82 |
else if (currSlideElement.getAttribute('title')) { |
| 83 |
captionHTML = Util.getText(currSlideElement.getAttribute('title')); |
| 84 |
} |
| 85 |
else { |
| 86 |
captionHTML = Util.getText(currSlideElement.querySelector('img').getAttribute('alt')); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
if (captionHTML) { |
| 91 |
el.innerHTML = '<div class="photonic-pswp-caption">' + captionHTML + '</div>'; |
| 92 |
} |
| 93 |
else { |
| 94 |
el.innerHTML = ''; |
| 95 |
} |
| 96 |
}); |
| 97 |
} |
| 98 |
}); |
| 99 |
|
| 100 |
lightbox.pswp.ui.registerElement({ |
| 101 |
name: 'download-button', |
| 102 |
order: 8, |
| 103 |
isButton: true, |
| 104 |
tagName: 'a', |
| 105 |
|
| 106 |
// SVG with outline |
| 107 |
html: { |
| 108 |
isCustomSVG: true, |
| 109 |
inner: '<path d="M18.62 17.09V19H5.38v-1.91zm-2.97-6.96L17 11.45l-5 4.87-5-4.87 1.36-1.32 2.68 2.64V5h1.92v7.77z" id="pswp__icn-download"/>', |
| 110 |
outlineID: 'pswp__icn-download' |
| 111 |
}, |
| 112 |
|
| 113 |
onInit: (el, pswp) => { |
| 114 |
el.setAttribute('download', ''); |
| 115 |
el.setAttribute('target', '_blank'); |
| 116 |
el.setAttribute('rel', 'noopener'); |
| 117 |
|
| 118 |
pswp.on('change', () => { |
| 119 |
if (pswp.currSlide.data.element) { |
| 120 |
el.href = pswp.currSlide.data.element.getAttribute('data-photonic-download') ? pswp.currSlide.data.element.getAttribute('data-photonic-download') : pswp.currSlide.data.src; |
| 121 |
} |
| 122 |
}); |
| 123 |
} |
| 124 |
}); |
| 125 |
|
| 126 |
lightbox.pswp.ui.registerElement({ |
| 127 |
name: 'social-sharing', |
| 128 |
order: 7, |
| 129 |
isButton: false, |
| 130 |
html: 'Share', |
| 131 |
onInit: (el, pswp) => { |
| 132 |
lightbox.pswp.on('change', () => { |
| 133 |
if (Photonic_JS.social_media) { |
| 134 |
const currSlideElement = lightbox.pswp.currSlide.data.element; |
| 135 |
let url = location.href; |
| 136 |
if (currSlideElement) { |
| 137 |
let title = Util.getText(currSlideElement.getAttribute('data-title')); |
| 138 |
let image = currSlideElement.href; |
| 139 |
const social = self.socialIcons.replace(/{photonic_share_link}/g, encodeURIComponent(url)) |
| 140 |
.replace(/{photonic_share_title}/g, encodeURIComponent(title)) |
| 141 |
.replace(/{photonic_share_image}/g, encodeURIComponent(image)); |
| 142 |
el.innerHTML = '<div class="photonic-pswp-social">' + social + '</div>'; |
| 143 |
} |
| 144 |
} |
| 145 |
else { |
| 146 |
el.innerHTML = ''; |
| 147 |
} |
| 148 |
}); |
| 149 |
} |
| 150 |
}); |
| 151 |
}); |
| 152 |
} |
| 153 |
|
| 154 |
initialize(selector) { |
| 155 |
this.handleSolos(); |
| 156 |
const self = this; |
| 157 |
|
| 158 |
let selection; |
| 159 |
if (selector == null) { |
| 160 |
selection = document.querySelectorAll('.photonic-level-1-container'); |
| 161 |
} |
| 162 |
else if (selector instanceof NodeList) { |
| 163 |
selection = selector; |
| 164 |
} |
| 165 |
else if (selector instanceof Element) { |
| 166 |
selection = [selector]; |
| 167 |
} |
| 168 |
else { |
| 169 |
selection = document.querySelectorAll(selector); |
| 170 |
} |
| 171 |
|
| 172 |
selection.forEach(function (current) { |
| 173 |
const galleryId = current.getAttribute('id'); |
| 174 |
|
| 175 |
const lightbox = new PhotoSwipeLightbox({ |
| 176 |
gallery: '#' + galleryId, |
| 177 |
children: 'a.photonic-lb', |
| 178 |
pswpModule: PhotoSwipe, |
| 179 |
}); |
| 180 |
|
| 181 |
lightbox.on('itemData', (e) => { |
| 182 |
let thumbnail = e.itemData.element; |
| 183 |
if (thumbnail && thumbnail.dataset.photonicMediaType === 'video') { |
| 184 |
let href = thumbnail.getAttribute('href'); |
| 185 |
let html = document.querySelector(href).cloneNode(true); |
| 186 |
html.removeAttribute('id'); |
| 187 |
html.classList.add('photonic-video'); |
| 188 |
e.itemData = { |
| 189 |
html: html.outerHTML |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
self.setHash(thumbnail); |
| 194 |
}); |
| 195 |
|
| 196 |
lightbox.on('close', () => { |
| 197 |
self.unsetHash(); |
| 198 |
}); |
| 199 |
|
| 200 |
self.setLightboxCaption(lightbox); |
| 201 |
|
| 202 |
lightbox.init(); |
| 203 |
}); |
| 204 |
|
| 205 |
document.querySelectorAll('.photonic-photoswipe-solo').forEach((current, idx) => { |
| 206 |
const href = current.href; |
| 207 |
let itemData = { |
| 208 |
src: href |
| 209 |
}; |
| 210 |
if (current.getAttribute('data-pswp-width') && current.getAttribute('data-pswp-height')) { |
| 211 |
itemData.width = current.getAttribute('data-pswp-width'); |
| 212 |
itemData.height = current.getAttribute('data-pswp-height'); |
| 213 |
} |
| 214 |
|
| 215 |
const lightbox = new PhotoSwipeLightbox({ |
| 216 |
dataSource: [ |
| 217 |
itemData |
| 218 |
], |
| 219 |
pswpModule: PhotoSwipe, |
| 220 |
}); |
| 221 |
|
| 222 |
lightbox.on('beforeOpen', (e) => { |
| 223 |
const { pswp } = lightbox; |
| 224 |
|
| 225 |
let itemWidth, itemHeight; |
| 226 |
|
| 227 |
function soloImageDim() { |
| 228 |
return new Promise(resolve => { |
| 229 |
const img = new Image(); |
| 230 |
|
| 231 |
img.onload = () => { |
| 232 |
itemWidth = img.naturalWidth; |
| 233 |
itemHeight = img.naturalHeight; |
| 234 |
resolve(); |
| 235 |
}; |
| 236 |
|
| 237 |
img.src = href; |
| 238 |
}); |
| 239 |
} |
| 240 |
|
| 241 |
async function waitForDim() { |
| 242 |
await soloImageDim(); |
| 243 |
} |
| 244 |
|
| 245 |
waitForDim().then(() => { |
| 246 |
pswp.options.dataSource = [ |
| 247 |
{ |
| 248 |
src: href, |
| 249 |
width: itemWidth, |
| 250 |
height: itemHeight, |
| 251 |
}, |
| 252 |
]; |
| 253 |
pswp.refreshSlideContent(pswp.currSlide.index); |
| 254 |
}); |
| 255 |
}); |
| 256 |
|
| 257 |
self.setLightboxCaption(lightbox); |
| 258 |
|
| 259 |
lightbox.init(); |
| 260 |
current.onclick = (e) => { |
| 261 |
e.preventDefault(); |
| 262 |
lightbox.loadAndOpen(0); |
| 263 |
}; |
| 264 |
}); |
| 265 |
|
| 266 |
document.querySelectorAll('.photoswipe-video, .photoswipe-html5-video').forEach((current, idx) => { |
| 267 |
const href = current.href; |
| 268 |
const anchorId = current.getAttribute('id'); |
| 269 |
|
| 270 |
const lightbox = new PhotoSwipeLightbox({ |
| 271 |
gallery: '#' + anchorId, |
| 272 |
pswpModule: PhotoSwipe, |
| 273 |
}); |
| 274 |
|
| 275 |
let html; |
| 276 |
if (current.classList.contains('photoswipe-video')) { |
| 277 |
html = '<div class="photonic-video"><iframe class="pswp__video" width="640" height="480" src="' + href + '" frameborder="0" allowfullscreen></iframe></div>'; |
| 278 |
} |
| 279 |
else { |
| 280 |
html = document.querySelector(current.getAttribute('href')).cloneNode(true); |
| 281 |
html.removeAttribute('id'); |
| 282 |
html.classList.add('photonic-video'); |
| 283 |
html = html.outerHTML; |
| 284 |
} |
| 285 |
|
| 286 |
lightbox.on('itemData', (e) => { |
| 287 |
e.itemData = { |
| 288 |
html: html |
| 289 |
}; |
| 290 |
}); |
| 291 |
|
| 292 |
self.setLightboxCaption(lightbox); |
| 293 |
|
| 294 |
lightbox.init(); |
| 295 |
}); |
| 296 |
} |
| 297 |
|
| 298 |
initializeForNewContainer(containerId) { |
| 299 |
this.initialize(containerId); |
| 300 |
} |
| 301 |
} |
| 302 |
|