summaryrefslogtreecommitdiffstats
path: root/assets/js/share-sidebar.js
blob: 81e5f6ceef70f162ca0342dd25714216379411f7 (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
50
51
/**
 * share-sidebar.js
 * Social sharing sidebar with copy-to-clipboard
 */

export function initShareSidebar() {
  'use strict';

  const sidebar = document.getElementById('share-sidebar');
  const copyBtn = document.getElementById('share-copy');

  if (!sidebar) return;

  // Share button handlers
  const shareBtns = sidebar.querySelectorAll('.share-btn[data-platform]');
  shareBtns.forEach((btn) => {
    btn.addEventListener('click', () => {
      const platform = btn.getAttribute('data-platform');
      const url = sidebar.getAttribute('data-url');
      const title = sidebar.getAttribute('data-title');

      const shareUrls = {
        whatsapp: `https://wa.me/?text=${encodeURIComponent(title + ' ' + url)}`,
        telegram: `https://t.me/share/url?url=${encodeURIComponent(url)}&text=${encodeURIComponent(title)}`,
        linkedin: `https://www.linkedin.com/sharing/share-offsite/?url=${encodeURIComponent(url)}`,
        twitter: `https://twitter.com/intent/tweet?url=${encodeURIComponent(url)}&text=${encodeURIComponent(title)}`,
        facebook: `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(url)}`,
        reddit: `https://reddit.com/submit?url=${encodeURIComponent(url)}&title=${encodeURIComponent(title)}`,
        email: `mailto:?subject=${encodeURIComponent(title)}&body=${encodeURIComponent(url)}`,
      };

      if (shareUrls[platform]) {
        window.open(shareUrls[platform], '_blank');
      }
    });
  });

  // Copy to clipboard
  if (copyBtn) {
    copyBtn.addEventListener('click', () => {
      const url = sidebar.getAttribute('data-url');
      navigator.clipboard.writeText(url).then(() => {
        const originalText = copyBtn.innerHTML;
        copyBtn.innerHTML = '✓';
        setTimeout(() => {
          copyBtn.innerHTML = originalText;
        }, 2000);
      });
    });
  }
}