javascriptruby-on-railsphotoswipe

how to call a function inside a foreEach?


I added an eventListener on small pictures, when a picture is clicked, PhotoSwipe opens the whole gallery but it doesn't remind open...

If I uncomment //openPhotoSwipe() at the end of the js file, the PhotoSwipe gallery appears when the page is loaded, and the eventListener works as expected. But that's not what I want... I am new to javascript... Help and explanation would be much appreciated

 function openPhotoSwipe() {

    var pswpElement = document.querySelectorAll('.pswp')[0];

    var pics =  Array.from(document.getElementsByTagName('img'));

    var items = []

    pics.forEach(function(e){
                    items.push({src: e.src, w: 0, h: 0});
                    })
    var options = {

        history: false,
        focus: false,
        showAnimationDuration: 0,
        hideAnimationDuration: 0,
        closeOnScroll: false,
        closeOnClick: false,

    };

    var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);

    gallery.listen('gettingData', function(index, item) {
      if (item.w < 1 || item.h < 1) {
        var img = new Image(); 
        img.onload = function() {
            item.w = this.width;
            item.h = this.height;
            gallery.invalidateCurrItems();
            gallery.updateSize(true);
        }
        img.src = item.src;
      }
    });

    gallery.init();
};


   var open = Array.from(document.getElementsByClassName("open_gallery"));

open.forEach(function(e){
    e.addEventListener('click', openPhotoSwipe);
})
// openPhotoSwipe();

here is a part of my view:

   <div class="container">

    <h1><%= @article.title %></h1>
    <p><%= @article.description %></p>

    <div class="thumb_images">
        <% @article.attachments.each do | art| %>

        <%= link_to image_tag art.url, class: "open_gallery" %>

        <% end %>
    </div>

</div>


<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <div class="pswp__bg"></div>

    <div class="pswp__scroll-wrap">


        <div class="pswp__container">
            <% @article.attachments.each do | art| %>
                <div class="pswp__item">
                    <%= image_tag art.url %>
                </div>
            <% end %>
        </div>


        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut">
                        </div>
                    </div>
                    </div>
                </div>

            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip">
                </div> 
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center">
                </div>
            </div>

        </div>

    </div>

</div>

Solution

  • As you confirmed in the comment that my guess was correct that, your link_to basically doesn't created the desired HTML markup. So here I am putting the comment as answer. Write your link to method as below:

    <%= link_to "#", class: "open_gallery" do %>
      <%= image_tag art.url %>
    <%end%>