Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 18615
1
/*
2
Put this file in /static/js/load-photoswipe.js
3
Documentation and licence at https://github.com/liwenyip/hugo-easy-gallery/
4
*/
5
6
/* Show an alert if this js file has been loaded twice */
7
if (window.loadphotoswipejs) {
8
window.alert("You've loaded load-photoswipe.js twice. See https://github.com/liwenyip/hugo-easy-gallery/issues/6")
9
}
10
var loadphotoswipejs = 1
11
12
/* TODO: Make the share function work */
13
$( document ).ready(function() {
14
/*
15
Initialise Photoswipe
16
*/
17
var items = []; // array of slide objects that will be passed to PhotoSwipe()
18
// for every figure element on the page:
19
$('figure').each( function() {
20
if ($(this).attr('class') == 'no-photoswipe') return true; // ignore any figures where class="no-photoswipe"
21
// get properties from child a/img/figcaption elements,
22
var $figure = $(this),
23
$a = $figure.find('a'),
24
$img = $figure.find('img'),
25
$src = $a.attr('href'),
26
$title = $img.attr('alt'),
27
$msrc = $img.attr('src');
28
// if data-size on <a> tag is set, read it and create an item
29
if ($a.data('size')) {
30
var $size = $a.data('size').split('x');
31
var item = {
32
src : $src,
33
w : $size[0],
34
h : $size[1],
35
title : $title,
36
msrc : $msrc
37
};
38
console.log("Using pre-defined dimensions for " + $src);
39
// if not, set temp default size then load the image to check actual size
40
} else {
41
var item = {
42
src : $src,
43
w : 800, // temp default size
44
h : 600, // temp default size
45
title : $title,
46
msrc : $msrc
47
};
48
console.log("Using default dimensions for " + $src);
49
// load the image to check its dimensions
50
// update the item as soon as w and h are known (check every 30ms)
51
var img = new Image();
52
img.src = $src;
53
var wait = setInterval(function() {
54
var w = img.naturalWidth,
55
h = img.naturalHeight;
56
if (w && h) {
57
clearInterval(wait);
58
item.w = w;
59
item.h = h;
60
console.log("Got actual dimensions for " + img.src);
61
}
62
}, 30);
63
}
64
// Save the index of this image then add it to the array
65
var index = items.length;
66
items.push(item);
67
// Event handler for click on a figure
68
$figure.on('click', function(event) {
69
event.preventDefault(); // prevent the normal behaviour i.e. load the <a> hyperlink
70
// Get the PSWP element and initialise it with the desired options
71
var $pswp = $('.pswp')[0];
72
var options = {
73
index: index,
74
bgOpacity: 0.8,
75
showHideOpacity: true
76
}
77
new PhotoSwipe($pswp, PhotoSwipeUI_Default, items, options).init();
78
});
79
});
80
});
81