Advertisement
Guest User

Untitled

a guest
Mar 8th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. var initPhotoSwipeFromDOM = function(gallerySelector) {
  3.  
  4.     // parse slide data (url, title, size ...) from DOM elements
  5.     // (children of gallerySelector)
  6.     var parseThumbnailElements = function(el) {
  7.         var thumbElements = el.childNodes,
  8.             numNodes = thumbElements.length,
  9.             items = [],
  10.             figureEl,
  11.             linkEl,
  12.             size,
  13.             item;
  14.  
  15.         for(var i = 0; i < numNodes; i++) {
  16.  
  17.             figureEl = thumbElements[i]; // <figure> element
  18.  
  19.             // include only element nodes
  20.             if(figureEl.nodeType !== 1) {
  21.                 continue;
  22.             }
  23.  
  24.             linkEl = figureEl.children[0]; // <a> element
  25.  
  26.             size = linkEl.getAttribute('data-size').split('x');
  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.  
  36.  
  37.             if(figureEl.children.length > 1) {
  38.                 // <figcaption> content
  39.                 item.title = figureEl.children[1].innerHTML;
  40.             }
  41.  
  42.             if(linkEl.children.length > 0) {
  43.                 // <img> thumbnail element, retrieving thumbnail url
  44.                 item.msrc = linkEl.children[0].getAttribute('src');
  45.             }
  46.  
  47.             item.el = figureEl; // save link to element for getThumbBoundsFn
  48.             items.push(item);
  49.         }
  50.  
  51.         return items;
  52.     };
  53.  
  54.     // find nearest parent element
  55.     var closest = function closest(el, fn) {
  56.         return el && ( fn(el) ? el : closest(el.parentNode, fn) );
  57.     };
  58.  
  59.     // triggers when user clicks on thumbnail
  60.     var onThumbnailsClick = function(e) {
  61.         e = e || window.event;
  62.         e.preventDefault ? e.preventDefault() : e.returnValue = false;
  63.  
  64.         var eTarget = e.target || e.srcElement;
  65.  
  66.         // find root element of slide
  67.         var clickedListItem = closest(eTarget, function(el) {
  68.             return (el.tagName && el.tagName.toUpperCase() === 'FIGURE');
  69.         });
  70.  
  71.         if(!clickedListItem) {
  72.             return;
  73.         }
  74.  
  75.         // find index of clicked item by looping through all child nodes
  76.         // alternatively, you may define index via data- attribute
  77.         var clickedGallery = clickedListItem.parentNode,
  78.             childNodes = clickedListItem.parentNode.childNodes,
  79.             numChildNodes = childNodes.length,
  80.             nodeIndex = 0,
  81.             index;
  82.  
  83.         for (var i = 0; i < numChildNodes; i++) {
  84.             if(childNodes[i].nodeType !== 1) {
  85.                 continue;
  86.             }
  87.  
  88.             if(childNodes[i] === clickedListItem) {
  89.                 index = nodeIndex;
  90.                 break;
  91.             }
  92.             nodeIndex++;
  93.         }
  94.  
  95.  
  96.  
  97.         if(index >= 0) {
  98.             // open PhotoSwipe if valid index found
  99.             openPhotoSwipe( index, clickedGallery );
  100.         }
  101.         return false;
  102.     };
  103.  
  104.     // parse picture index and gallery index from URL (#&pid=1&gid=2)
  105.     var photoswipeParseHash = function() {
  106.         var hash = window.location.hash.substring(1),
  107.         params = {};
  108.  
  109.         if(hash.length < 5) {
  110.             return params;
  111.         }
  112.  
  113.         var vars = hash.split('&');
  114.         for (var i = 0; i < vars.length; i++) {
  115.             if(!vars[i]) {
  116.                 continue;
  117.             }
  118.             var pair = vars[i].split('=');  
  119.             if(pair.length < 2) {
  120.                 continue;
  121.             }          
  122.             params[pair[0]] = pair[1];
  123.         }
  124.  
  125.         if(params.gid) {
  126.             params.gid = parseInt(params.gid, 10);
  127.         }
  128.  
  129.         return params;
  130.     };
  131.  
  132.     var openPhotoSwipe = function(index, galleryElement, disableAnimation, fromURL) {
  133.         var pswpElement = document.querySelectorAll('.pswp')[0],
  134.             gallery,
  135.             options,
  136.             items;
  137.  
  138.         items = parseThumbnailElements(galleryElement);
  139.  
  140.         // define options (if needed)
  141.         options = {
  142.  
  143.             // define gallery index (for URL)
  144.             galleryUID: galleryElement.getAttribute('data-pswp-uid'),
  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.  
  193.     for(var i = 0, l = galleryElements.length; i < l; i++) {
  194.         galleryElements[i].setAttribute('data-pswp-uid', i+1);
  195.         galleryElements[i].onclick = onThumbnailsClick;
  196.     }
  197.  
  198.     // Parse URL and open gallery if it contains #&pid=3&gid=1
  199.     var hashData = photoswipeParseHash();
  200.     if(hashData.pid && hashData.gid) {
  201.         openPhotoSwipe( hashData.pid ,  galleryElements[ hashData.gid - 1 ], true, true );
  202.     }
  203. };
  204.  
  205. // execute above function
  206. initPhotoSwipeFromDOM('.my-gallery');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement