photoswipe-script.js 6.4 KB

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