-
Notifications
You must be signed in to change notification settings - Fork 3
/
pwpswp.js
135 lines (104 loc) · 3.82 KB
/
pwpswp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
(function (window, document, undefined) {
var galleries,
nonmodals,
pswpElement
function init() {
pswpElement = document.querySelector('.pswp')
galleries = document.querySelectorAll('.pwpswp-gallery')
galleries = galleries ? Array.prototype.slice.call(galleries) : null
galleries = galleries.map(function(galleryElement, index, array) {
return {
element : galleryElement,
items : buildItemsArray(galleryElement)
}
})
galleries.forEach(function(gallery, index, galleriesArray) {
var gid = index + 1
gallery.items.forEach(function(item, index, itemsArray) {
var pid = index + 1
var options = getGalleryOptions(gid)
options.getThumbBoundsFn = function(index) {
thumbnail = item.itemElement.querySelector('img')
var pageYScroll = window.pageYOffset || document.documentElement.scrollTop
var rect = thumbnail.getBoundingClientRect()
return {x:rect.left, y:rect.top + pageYScroll, w:rect.width}
}
item.linkElement.addEventListener('click', function(event) {
event.preventDefault()
openGallery(gid, pid, options)
// var pswpInstance = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, itemsArray, options)
// pswpInstance.init()
})
})
})
var urlParams = getUrlParams()
if(urlParams !== null && urlParams.gid >= 0 && urlParams.pid >= 0) {
openGallery(urlParams.gid, urlParams.pid, getGalleryOptions(urlParams.gid))
}
}
/**
* buildItemsArray
*
* @param {*} galleryElement
*/
function buildItemsArray(galleryElement) {
var items = galleryElement.querySelectorAll('.pwpswp-gallery__item')
items = items ? Array.prototype.slice.call(items) : null
items = items.map(function(itemElement, index, array) {
var linkElement = itemElement.querySelector('.pwpswp-gallery__link'),
size = linkElement.getAttribute('data-size').split("x")
return {
itemElement : itemElement,
linkElement : linkElement,
src : linkElement.getAttribute('href'),
w : parseInt(size[0]),
h : parseInt(size[1]),
msrc : linkElement.getAttribute('data-lores-src')
}
})
return items
}
/**
* openGallery
*
* @param {*} gid
* @param {*} pid
* @param {*} options
*/
function openGallery(gid, pid, options) {
itemsArray = galleries[gid-1].items
options.index = pid - 1
var pswpInstance = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, itemsArray, options)
pswpInstance.init()
}
/**
*
* @param {*} gid
*/
function getGalleryOptions(gid) {
var options = {}
var usrOptions = JSON.parse(galleries[gid-1].element.getAttribute('data-pswp-options'))
for (var attr in usrOptions) {
options[attr] = usrOptions[attr]
}
return options
}
/**
* getUrlParams
*
* @return
*/
function getUrlParams() {
var params = {}
var hash = window.location.hash.substring(1)
if(hash) {
params.gid = parseInt(hash.match(/&gid=([0-9]+)/)[1])
params.pid = parseInt(hash.match(/&pid=([0-9]+)/)[1])
return params
}
return null
}
document.addEventListener("DOMContentLoaded", function() {
init()
})
})(window, document)