aboutsummaryrefslogtreecommitdiff
path: root/static/scripts/index.js
blob: b1b006d0f5ee7855d8c05ad4251f79fd6daa5eb1 (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
// const modal = document.querySelector("dialog");
const showModalBtns = document.querySelectorAll(".show-modal");
const closeModalBtns = document.querySelectorAll(".close-modal");
const clipboard = document.querySelector("#copy-ingredients");

if(clipboard) {
  clipboard.addEventListener("click", () => {

    // Copy data to clipboard
    const ingredientsContent = document.querySelector("#ingredients").textContent.trim();
    const contentArr = ingredientsContent.split("\n");
    const ingredientsString = contentArr.map((el) => {
       if(el.trim() == "") return;
       else return el.trim();
    }).join("\n")
    navigator.clipboard.writeText(ingredientsString);

    // Show success message momentarily
    clipboard.parentElement.insertAdjacentHTML("afterend", "<span id='copied-message'> ingredients copied!</span>");
  
    setTimeout(() => {
       document.querySelector("#copied-message").remove(); 
    }, 2000)
  })
}


// Open and close modals on /account page
if(showModalBtns){
  showModalBtns.forEach((btn) => {
    btn.addEventListener("click", () => {

      // Open dialog box
      btn.nextElementSibling.showModal();

    })
  })
}

if(closeModalBtns){
  closeModalBtns.forEach((btn) => {
    btn.addEventListener("click", () => {

      // Open dialog box
      btn.parentNode.close();

    })
  })
}