PageSourceSearch

https://scottishshale.co.uk/static/photoswipe/photoswipe-gallery.js

js scottishshale.co.uk collected 2026-10-02 11:26:37 UTC 6,449 bytes, 206 lines download raw bytes

1
2var initPhotoSwipeFromDOM = function (gallerySelector) {
3    // parse slide data (url, title, size ...) from DOM elements 
4    // (children of gallerySelector)
5    var parseThumbnailElements = function (el) {
6        var thumbElements = el.childNodes,
7            numNodes = thumbElements.length,
8            items = [],
9            figureEl,
10            linkEl,
11            size,
12            annotation,
13            item;
14
15        for (var i = 0; i < numNodes; i++) {
16
17            figureEl = thumbElements[i]; // <figure> element
18            // include only element nodes 
19            if (figureEl.nodeType !== 1) {
20                continue;
21            }
22            
23            linkEl = figureEl.children[0]; // <a> element
24
25            size = linkEl.getAttribute('data-size').split('x');
26            annotation  = linkEl.getAttribute('data-annotation')
27
28            // create slide object
29            item = {
30                src: linkEl.getAttribute('href'),
31                w: parseInt(size[0], 10),
32                h: parseInt(size[1], 10)
33            };
34
35            if (annotation) {
36                // <figcaption> content
37                item.title = annotation;
38            }
39
40            if (figureEl.children.length > 1) {
41                // <figcaption> content
42                item.title = figureEl.children[1].innerHTML;
43            }
44
45            item.el = figureEl; // save link to element for getThumbBoundsFn
46            items.push(item);    
47        }
48
49        return items;
50    };
51
52    // find nearest parent element
53    var closest = function closest(el, fn) {
54        return el && (fn(el) ? el : closest(el.parentNode, fn));
55    };
56
57    // triggers when user clicks on thumbnail
58    var onThumbnailsClick = function (e) {
59        if ((e.target).className.includes("tag")) return;
60        e = e || window.event;
61        e.preventDefault ? e.preventDefault() : e.returnValue = false;
62
63        var eTarget = e.target || e.srcElement;
64
65        // find root element of slide
66        var clickedListItem = closest(eTarget, function (el) {
67            return (el.tagName && el.tagName.toUpperCase() === 'SPAN' && el.classList.contains("ps-image"));
68        });
69
70        if (!clickedListItem) {
71            return;
72        }
73
74        // find index of clicked item by looping through all child nodes
75        // alternatively, you may define index via data- attribute
76        var clickedGallery = clickedListItem.parentNode,
77            childNodes = clickedListItem.parentNode.childNodes,
78            numChildNodes = childNodes.length,
79            nodeIndex = 0,
80            index;
81
82        for (var i = 0; i < numChildNodes; i++) {
83            if (childNodes[i].nodeType !== 1) {
84                continue;
85            }
86
87            if (childNodes[i] === clickedListItem) {
88                index = nodeIndex;
89                break;
90            }
91            nodeIndex++;
92        }
93
94
95
96        if (index >= 0) {
97            // open PhotoSwipe if valid index found
98            openPhotoSwipe(index, clickedGallery);
99        }
100        return false;
101    };
102
103    // parse picture index and gallery index from URL (#&pid=1&gid=2)
104    var photoswipeParseHash = function () {
105        var hash = window.location.hash.substring(1),
106            params = {};
107
108        if (hash.length < 5) {
109            return params;
110        }
111
112        var vars = hash.split('&');
113        for (var i = 0; i < vars.length; i++) {
114            if (!vars[i]) {
115                continue;
116            }
117            var pair = vars[i].split('=');
118            if (pair.length < 2) {
119                continue;
120            }
121            params[pair[0]] = pair[1];
122        }
123
124        if (params.gid) {
125            params.gid = parseInt(params.gid, 10);
126        }
127
128        return params;
129    };
130
131    var openPhotoSwipe = function (index, galleryElement, disableAnimation, fromURL) {
132        var pswpElement = document.querySelectorAll('.pswp')[0],
133            gallery,
134            options,
135            items;
136
137        items = parseThumbnailElements(galleryElement);
138
139        // define options (if needed)
140        options = {
141
142            // define gallery index (for URL)
143            galleryUID: galleryElement.getAttribute('data-pswp-uid'),
144            showHideOpacity:true, getThumbBoundsFn:false,
145
146            getThumbBoundsFn: function (index) {
147                // See Options -> getThumbBoundsFn section of documentation for more info
148                var thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnail
149                    pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
150                    rect = thumbnail.getBoundingClientRect();
151
152                return { x: rect.left, y: rect.top + pageYScroll, w: rect.width };
153            }
154
155        };
156
157        // PhotoSwipe opened from URL
158        if (fromURL) {
159            if (options.galleryPIDs) {
160                // parse real index when custom PIDs are used 
161                // http://photoswipe.com/documentation/faq.html#custom-pid-in-url
162                for (var j = 0; j < items.length; j++) {
163                    if (items[j].pid == index) {
164                        options.index = j;
165                        break;
166                    }
167                }
168            } else {
169                // in URL indexes start from 1
170                options.index = parseInt(index, 10) - 1;
171            }
172        } else {
173            options.index = parseInt(index, 10);
174        }
175
176        // exit if index not found
177        if (isNaN(options.index)) {
178            return;
179        }
180
181        if (disableAnimation) {
182            options.showAnimationDuration = 0;
183        }
184
185        // Pass data to PhotoSwipe and initialize it
186        gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
187        gallery.init();
188    };
189
190    // loop through all gallery elements and bind events
191    var galleryElements = document.querySelectorAll(gallerySelector);
192    for (var i = 0, l = galleryElements.length; i < l; i++) {
193        galleryElements[i].setAttribute('data-pswp-uid', i + 1);
194        galleryElements[i].onclick = onThumbnailsClick;
195    }
196
197    // Parse URL and open gallery if it contains #&pid=3&gid=1
198    var hashData = photoswipeParseHash();
199    
200    if (hashData.pid && hashData.gid) {
201        openPhotoSwipe(hashData.pid, galleryElements[hashData.gid - 1], true, true);
202    }
203};
204
205// execute above function
206initPhotoSwipeFromDOM('.my-gallery');

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.