source/_assets/js/main.js   A
last analyzed

Complexity

Total Complexity 15
Complexity/F 1.25

Size

Lines of Code 50
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 32
c 0
b 0
f 0
dl 0
loc 50
rs 10
mnd 3
bc 3
fnc 12
bpm 0.25
cpm 1.25
noi 0
1
import share from './functions/share';
2
3
window.share = share;
4
5
window.toggleMenu = () => document
6
  .querySelector('.side-menu')
7
  .classList
8
  .toggle('side-menu--visible');
9
10
const loadImage = (img) => img.setAttribute('src', img.getAttribute('data-src'));
11
const removeClass = (className) => (elm) => elm.classList.remove(className);
12
13
if (typeof window.IntersectionObserver == 'undefined') {
14
  window.initDisqus && window.initDisqus();
15
16
  setTimeout(() => {
17
    const images = document.querySelectorAll('img.lazy-image');
18
    images.forEach(loadImage);
19
    images.forEach(removeClass('lazy-image'));
20
  }, 300);
21
}
22
23
if (typeof window.IntersectionObserver != 'undefined') {
24
  window.imagesObserver = new window.IntersectionObserver((entries, observer) => {
25
    entries.filter((entry) => entry.isIntersecting).forEach((entry) => {
26
      const target = entry.target;
27
      loadImage(target);
28
      removeClass('lazy-image')(target);
29
30
      observer.unobserve(target);
31
    });
32
  }, {
33
    rootMargin: '-20px',
34
  });
35
36
  document.querySelectorAll('img.lazy-image').forEach((img) => window.imagesObserver.observe(img));
37
38
  const commentsSection = document.querySelector('.episode__comments');
39
  if (commentsSection) {
40
    window.commentsObserver = new window.IntersectionObserver((entries, observer) => {
41
      entries.filter((entry) => entry.isIntersecting).forEach((entry) => {
42
        window.initDisqus && window.initDisqus();
43
44
        observer.unobserve(entry.target);
45
      });
46
    });
47
48
    window.commentsObserver.observe(commentsSection);
49
  }
50
}
51