PageSourceSearch

https://coratolive.it/wp-content/themes/livenetwork-theme/inc/pharmacy/pharmacy-archive.js?ver=1787240211

js coratolive.it collected 2026-09-25 01:14:27 UTC 6,104 bytes, 199 lines download raw bytes

1(function () {
2  "use strict";
3
4  function qs(sel, root) {
5    return (root || document).querySelector(sel);
6  }
7
8  function log(...args) {
9    // metti a false se non vuoi log
10    console.log("[LNV Pharmacy]", ...args);
11  }
12
13  function warn(...args) {
14    console.warn("[LNV Pharmacy]", ...args);
15  }
16
17  function err(...args) {
18    console.error("[LNV Pharmacy]", ...args);
19  }
20
21  function getConfig() {
22    if (!window.LNV_PH_ARCH) {
23      warn("Config LNV_PH_ARCH mancante. Controlla wp_localize_script.");
24      return null;
25    }
26    if (!window.LNV_PH_ARCH.ajaxurl) {
27      warn("LNV_PH_ARCH.ajaxurl mancante.");
28      return null;
29    }
30    if (!window.LNV_PH_ARCH.nonce) {
31      warn("LNV_PH_ARCH.nonce mancante.");
32      return null;
33    }
34    return window.LNV_PH_ARCH;
35  }
36
37  function setLoading(btn, isLoading, textLoading) {
38    if (!btn) return;
39    btn.disabled = !!isLoading;
40    btn.setAttribute("aria-busy", isLoading ? "true" : "false");
41    if (isLoading) {
42      btn.dataset._oldText = btn.textContent;
43      btn.textContent = textLoading || "Caricamento...";
44    } else if (btn.dataset._oldText) {
45      btn.textContent = btn.dataset._oldText;
46      delete btn.dataset._oldText;
47    }
48  }
49
50  // helper: prova a trovare un "contenitore" dove appendere l'HTML
51function findAppendTarget(btn) {
52  const block = btn.closest(".ln-pharmacy-archive");
53  if (!block) return document.body;
54  return block.querySelector(".ln-pharmacy-archive-chunks") || block;
55}
56
57
58  async function postAjax(url, data) {
59    const body = new URLSearchParams();
60    Object.keys(data).forEach((k) => body.append(k, data[k]));
61
62    const res = await fetch(url, {
63      method: "POST",
64      headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" },
65      body: body.toString(),
66      credentials: "same-origin",
67    });
68
69    const text = await res.text();
70
71    // admin-ajax quando fallisce spesso torna "0"
72    if (text.trim() === "0") {
73      return { ok: false, error: "Admin-Ajax ha risposto 0 (action/nonce/capability?)", raw: text };
74    }
75
76    // se il tuo PHP fa wp_send_json_success/error => è JSON
77    try {
78      const json = JSON.parse(text);
79      return { ok: true, json };
80    } catch (e) {
81      // se invece ritorni HTML puro
82      return { ok: true, html: text };
83    }
84  }
85
86  async function handleLoadMore(btn) {
87    const cfg = getConfig();
88    if (!cfg) return;
89
90    const from = btn.getAttribute("data-from") || "";
91    const days = btn.getAttribute("data-days") || (cfg.days || 30);
92
93    if (!from) {
94      warn("data-from mancante sul bottone. Esempio: data-from='2026-01-21'");
95      alert("Configurazione mancante: data-from non impostato sul bottone.");
96      return;
97    }
98
99    // action: DEVE combaciare con add_action('wp_ajax_XXXXX', ...)
100    // Se nel tuo PHP l'action ha un nome diverso, cambialo QUI.
101    const action = btn.getAttribute("data-action") || "lnv_pharmacy_more";
102
103const payload = {
104  action,
105  nonce: cfg.nonce,
106  start: from,      // ✅ invece di from: from
107  days: String(days),
108};
109
110    log("Click load more", payload);
111
112    setLoading(btn, true, "Carico altri giorni...");
113
114    try {
115      const out = await postAjax(cfg.ajaxurl, payload);
116
117      if (!out.ok) {
118        err("AJAX failed:", out.error, out.raw || "");
119        alert("Errore caricamento: " + out.error);
120        return;
121      }
122
123      // Caso 1: JSON standard { success:true, data:{ html, next_from, has_more } }
124      if (out.json && typeof out.json.success !== "undefined") {
125        if (!out.json.success) {
126          const msg = (out.json.data && (out.json.data.message || out.json.data)) || "Errore server";
127          err("Server error:", msg, out.json);
128          alert(String(msg));
129          return;
130        }
131
132const data = out.json.data || {};
133const html = data.html || "";
134const nextFrom = data.next_from || "";
135const hasMore = (typeof data.has_more === "boolean") ? data.has_more : true;
136
137if (nextFrom) btn.setAttribute("data-from", nextFrom);
138if (!hasMore) btn.style.display = "none";
139
140        if (!html) {
141          warn("Risposta JSON ok ma html vuoto.", out.json);
142        }
143
144        const target = findAppendTarget(btn);
145        // Se nel tuo PHP ritorni solo i <section> dei nuovi giorni, qui li appendi
146        const tmp = document.createElement("div");
147        tmp.innerHTML = html;
148
149        // Prova a appendere solo i nuovi day-block se ci sono
150        const blocks = tmp.querySelectorAll(".ln-day-block, section.ln-day-block");
151        if (blocks && blocks.length) {
152          blocks.forEach((b) => target.appendChild(b));
153        } else {
154          // fallback: append tutto
155          while (tmp.firstChild) target.appendChild(tmp.firstChild);
156        }
157
158        if (nextFrom) btn.setAttribute("data-from", nextFrom);
159
160        if (!hasMore) {
161          btn.style.display = "none";
162        }
163
164        return;
165      }
166
167      // Caso 2: il PHP ritorna HTML puro (senza wp_send_json)
168      if (out.html) {
169        const target = findAppendTarget(btn);
170        const tmp = document.createElement("div");
171        tmp.innerHTML = out.html;
172        while (tmp.firstChild) target.appendChild(tmp.firstChild);
173
174        // in HTML puro devi calcolare next_from lato client o inserirlo lato server
175        warn("Risposta HTML puro: assicurati di aggiornare data-from (server o client).");
176        return;
177      }
178
179      warn("Risposta inattesa:", out);
180    } catch (e) {
181      err("Eccezione fetch:", e);
182      alert("Errore di rete o JS: apri la Console per dettagli.");
183    } finally {
184      setLoading(btn, false);
185    }
186  }
187
188  // Event delegation: funziona SEMPRE
189  document.addEventListener("click", function (e) {
190    const btn = e.target.closest(".ln-pharmacy-load-more");
191    if (!btn) return;
192
193    e.preventDefault();
194    handleLoadMore(btn);
195  });
196
197  // sanity log: ti dice se il file è caricato davvero
198  log("pharmacy-archive.js caricato ✅");
199})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.