summaryrefslogtreecommitdiff
path: root/_includes/scripts/components/lightbox.js
blob: 51689c2e5b36c049c1d94b31fc998effc9f2f5cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{%- include scripts/utils/imagesLoad.js -%}
(function () {
  var SOURCES = window.TEXT_VARIABLES.sources;
  window.Lazyload.js(SOURCES.jquery, function() {
    var $pageGalleryModal = $('.js-page-gallery-modal');
    var $images = $('.page__content').find('img:not(.lightbox-ignore)');
    window.imagesLoad($images).then(function() {
      /* global Gallery */
      var pageGalleryModal = $pageGalleryModal.modal({ onChange: handleModalChange });
      var gallery = null;
      var modalVisible = false;
      var i, items = [], image, item;
      if($images && $images.length > 0) {
        for (i = 0; i < $images.length; i++) {
          image = $images.eq(i);
          if (image.get(0).naturalWidth > 800) {
            items.push({ src: image.attr('src'), w: image.get(0).naturalWidth, h: image.get(0).naturalHeight, $el: image});
          }
        }
      }

      if(items.length > 0) {
        gallery = new Gallery('.gallery', items);
        gallery.setOptions({ disabled: !modalVisible });
        gallery.init();
        for (i = 0; i < items.length; i++) {
          item = items[i];
          item.$el && (item.$el.addClass('popup-image'), item.$el.on('click', (function() {
            var index = i;
            return function() {
              pageGalleryModal.show();
              gallery.setOptions({ initialSlide: index });
              gallery.refresh(true, { animation: false });
            };
          })()));
        }
      }

      function handleModalChange(visible) {
        modalVisible = visible;
        gallery && gallery.setOptions({ disabled: !modalVisible });
      }

      $pageGalleryModal.on('click', function() {
        pageGalleryModal.hide();
      });
    });
  });
})();