photo.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. document.writeln("<!-- Root element of PhotoSwipe. Must have class pswp. -->");
  2. document.writeln("<div class=\"pswp\" tabindex=\"-1\" role=\"dialog\" aria-hidden=\"true\">");
  3. document.writeln("");
  4. document.writeln(" <!-- Background of PhotoSwipe.");
  5. document.writeln(" It\'s a separate element as animating opacity is faster than rgba(). -->");
  6. document.writeln(" <div class=\"pswp__bg\"><\/div>");
  7. document.writeln("");
  8. document.writeln(" <!-- Slides wrapper with overflow:hidden. -->");
  9. document.writeln(" <div class=\"pswp__scroll-wrap\">");
  10. document.writeln("");
  11. document.writeln(" <!-- Container that holds slides.");
  12. document.writeln(" PhotoSwipe keeps only 3 of them in the DOM to save memory.");
  13. document.writeln(" Don\'t modify these 3 pswp__item elements, data is added later on. -->");
  14. document.writeln(" <div class=\"pswp__container\">");
  15. document.writeln(" <div class=\"pswp__item\"><\/div>");
  16. document.writeln(" <div class=\"pswp__item\"><\/div>");
  17. document.writeln(" <div class=\"pswp__item\"><\/div>");
  18. document.writeln(" <\/div>");
  19. document.writeln("");
  20. document.writeln(" <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->");
  21. document.writeln(" <div class=\"pswp__ui pswp__ui--hidden\">");
  22. document.writeln("");
  23. document.writeln(" <div class=\"pswp__top-bar\">");
  24. document.writeln("");
  25. document.writeln(" <!-- Controls are self-explanatory. Order can be changed. -->");
  26. document.writeln("");
  27. document.writeln(" <div class=\"pswp__counter\"><\/div>");
  28. document.writeln("");
  29. document.writeln(" <button class=\"pswp__button pswp__button--close\" title=\"Close (Esc)\"><\/button>");
  30. document.writeln("");
  31. document.writeln(" <button class=\"pswp__button pswp__button--share\" title=\"Share\"><\/button>");
  32. document.writeln("");
  33. document.writeln(" <button class=\"pswp__button pswp__button--fs\" title=\"Toggle fullscreen\"><\/button>");
  34. document.writeln("");
  35. document.writeln(" <button class=\"pswp__button pswp__button--zoom\" title=\"Zoom in\/out\"><\/button>");
  36. document.writeln("");
  37. document.writeln(" <!-- Preloader demo http:\/\/codepen.io\/dimsemenov\/pen\/yyBWoR -->");
  38. document.writeln(" <!-- element will get class pswp__preloader--active when preloader is running -->");
  39. document.writeln(" <div class=\"pswp__preloader\">");
  40. document.writeln(" <div class=\"pswp__preloader__icn\">");
  41. document.writeln(" <div class=\"pswp__preloader__cut\">");
  42. document.writeln(" <div class=\"pswp__preloader__donut\"><\/div>");
  43. document.writeln(" <\/div>");
  44. document.writeln(" <\/div>");
  45. document.writeln(" <\/div>");
  46. document.writeln(" <\/div>");
  47. document.writeln("");
  48. document.writeln(" <div class=\"pswp__share-modal pswp__share-modal--hidden pswp__single-tap\">");
  49. document.writeln(" <div class=\"pswp__share-tooltip\"><\/div>");
  50. document.writeln(" <\/div>");
  51. document.writeln("");
  52. document.writeln(" <button class=\"pswp__button pswp__button--arrow--left\" title=\"Previous (arrow left)\">");
  53. document.writeln(" <\/button>");
  54. document.writeln("");
  55. document.writeln(" <button class=\"pswp__button pswp__button--arrow--right\" title=\"Next (arrow right)\">");
  56. document.writeln(" <\/button>");
  57. document.writeln("");
  58. document.writeln(" <div class=\"pswp__caption\">");
  59. document.writeln(" <div class=\"pswp__caption__center\"><\/div>");
  60. document.writeln(" <\/div>");
  61. document.writeln("");
  62. document.writeln(" <\/div>");
  63. document.writeln("");
  64. document.writeln(" <\/div>");
  65. document.writeln("");
  66. document.writeln("<\/div>");
  67. (function() {
  68. var initPhotoSwipeFromDOM = function(gallerySelector) {
  69. var parseThumbnailElements = function(el) {
  70. var thumbElements = el.childNodes,
  71. numNodes = thumbElements.length,
  72. items = [],
  73. el,
  74. childElements,
  75. thumbnailEl,
  76. size,
  77. item;
  78. for(var i = 0; i < numNodes; i++) {
  79. el = thumbElements[i];
  80. // include only element nodes
  81. if(el.nodeType !== 1) {
  82. continue;
  83. }
  84. childElements = el.children;
  85. size = el.getAttribute('data-size').split('x');
  86. // create slide object
  87. item = {
  88. src: el.getAttribute('href'),
  89. w: parseInt(size[0], 10),
  90. h: parseInt(size[1], 10),
  91. author: el.getAttribute('data-author')
  92. };
  93. item.el = el; // save link to element for getThumbBoundsFn
  94. if(childElements.length > 0) {
  95. item.msrc = childElements[0].getAttribute('src'); // thumbnail url
  96. if(childElements.length > 1) {
  97. item.title = childElements[1].innerHTML; // caption (contents of figure)
  98. }
  99. }
  100. var mediumSrc = el.getAttribute('data-med');
  101. if(mediumSrc) {
  102. size = el.getAttribute('data-med-size').split('x');
  103. // "medium-sized" image
  104. item.m = {
  105. src: mediumSrc,
  106. w: parseInt(size[0], 10),
  107. h: parseInt(size[1], 10)
  108. };
  109. }
  110. // original image
  111. item.o = {
  112. src: item.src,
  113. w: item.w,
  114. h: item.h
  115. };
  116. items.push(item);
  117. }
  118. return items;
  119. };
  120. // find nearest parent element
  121. var closest = function closest(el, fn) {
  122. return el && ( fn(el) ? el : closest(el.parentNode, fn) );
  123. };
  124. var onThumbnailsClick = function(e) {
  125. e = e || window.event;
  126. e.preventDefault ? e.preventDefault() : e.returnValue = false;
  127. var eTarget = e.target || e.srcElement;
  128. var clickedListItem = closest(eTarget, function(el) {
  129. return el.tagName === 'A';
  130. });
  131. if(!clickedListItem) {
  132. return;
  133. }
  134. var clickedGallery = clickedListItem.parentNode;
  135. var childNodes = clickedListItem.parentNode.childNodes,
  136. numChildNodes = childNodes.length,
  137. nodeIndex = 0,
  138. index;
  139. for (var i = 0; i < numChildNodes; i++) {
  140. if(childNodes[i].nodeType !== 1) {
  141. continue;
  142. }
  143. if(childNodes[i] === clickedListItem) {
  144. index = nodeIndex;
  145. break;
  146. }
  147. nodeIndex++;
  148. }
  149. if(index >= 0) {
  150. openPhotoSwipe( index, clickedGallery );
  151. }
  152. return false;
  153. };
  154. var photoswipeParseHash = function() {
  155. var hash = window.location.hash.substring(1),
  156. params = {};
  157. if(hash.length < 5) { // pid=1
  158. return params;
  159. }
  160. var vars = hash.split('&');
  161. for (var i = 0; i < vars.length; i++) {
  162. if(!vars[i]) {
  163. continue;
  164. }
  165. var pair = vars[i].split('=');
  166. if(pair.length < 2) {
  167. continue;
  168. }
  169. params[pair[0]] = pair[1];
  170. }
  171. if(params.gid) {
  172. params.gid = parseInt(params.gid, 10);
  173. }
  174. return params;
  175. };
  176. var openPhotoSwipe = function(index, galleryElement, disableAnimation, fromURL) {
  177. var pswpElement = document.querySelectorAll('.pswp')[0],
  178. gallery,
  179. options,
  180. items;
  181. items = parseThumbnailElements(galleryElement);
  182. // define options (if needed)
  183. options = {
  184. galleryUID: galleryElement.getAttribute('data-pswp-uid'),
  185. getThumbBoundsFn: function(index) {
  186. // See Options->getThumbBoundsFn section of docs for more info
  187. var thumbnail = items[index].el.children[0],
  188. pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
  189. rect = thumbnail.getBoundingClientRect();
  190. return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
  191. },
  192. addCaptionHTMLFn: function(item, captionEl, isFake) {
  193. if(!item.title) {
  194. captionEl.children[0].innerText = '';
  195. return false;
  196. }
  197. captionEl.children[0].innerHTML = item.title + '<br/><small>Photo: ' + item.author + '</small>';
  198. return true;
  199. }
  200. };
  201. // options for control bar
  202. options.shareEl = false;
  203. options.fullscreenEl = false;
  204. if(fromURL) {
  205. if(options.galleryPIDs) {
  206. // parse real index when custom PIDs are used
  207. // http://photoswipe.com/documentation/faq.html#custom-pid-in-url
  208. for(var j = 0; j < items.length; j++) {
  209. if(items[j].pid == index) {
  210. options.index = j;
  211. break;
  212. }
  213. }
  214. } else {
  215. options.index = parseInt(index, 10) - 1;
  216. }
  217. } else {
  218. options.index = parseInt(index, 10);
  219. }
  220. // exit if index not found
  221. if( isNaN(options.index) ) {
  222. return;
  223. }
  224. // Pass data to PhotoSwipe and initialize it
  225. gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
  226. // see: http://photoswipe.com/documentation/responsive-images.html
  227. var realViewportWidth,
  228. useLargeImages = false,
  229. firstResize = true,
  230. imageSrcWillChange;
  231. gallery.listen('beforeResize', function() {
  232. var dpiRatio = window.devicePixelRatio ? window.devicePixelRatio : 1;
  233. dpiRatio = Math.min(dpiRatio, 2.5);
  234. realViewportWidth = gallery.viewportSize.x * dpiRatio;
  235. if(realViewportWidth >= 1200 || (!gallery.likelyTouchDevice && realViewportWidth > 800) || screen.width > 1200 ) {
  236. if(!useLargeImages) {
  237. useLargeImages = true;
  238. imageSrcWillChange = true;
  239. }
  240. } else {
  241. if(useLargeImages) {
  242. useLargeImages = false;
  243. imageSrcWillChange = true;
  244. }
  245. }
  246. if(imageSrcWillChange && !firstResize) {
  247. gallery.invalidateCurrItems();
  248. }
  249. if(firstResize) {
  250. firstResize = false;
  251. }
  252. imageSrcWillChange = false;
  253. });
  254. gallery.listen('gettingData', function(index, item) {
  255. if( useLargeImages ) {
  256. item.src = item.o.src;
  257. item.w = item.o.w;
  258. item.h = item.o.h;
  259. } else {
  260. item.src = item.m.src;
  261. item.w = item.m.w;
  262. item.h = item.m.h;
  263. }
  264. });
  265. gallery.init();
  266. };
  267. // select all gallery elements
  268. var galleryElements = document.querySelectorAll( gallerySelector );
  269. for(var i = 0, l = galleryElements.length; i < l; i++) {
  270. galleryElements[i].setAttribute('data-pswp-uid', i+1);
  271. galleryElements[i].onclick = onThumbnailsClick;
  272. }
  273. // Parse URL and open gallery if it contains #&pid=3&gid=1
  274. var hashData = photoswipeParseHash();
  275. if(hashData.pid && hashData.gid) {
  276. openPhotoSwipe( hashData.pid, galleryElements[ hashData.gid - 1 ], true, true );
  277. }
  278. };
  279. initPhotoSwipeFromDOM('.demo-gallery');
  280. })();