PageSourceSearch

https://emprendedores.es/wp-content/themes/mkt228/assets/js/modules/mkt-listing-load-more.js?ver=1790841376

js emprendedores.es collected 2026-10-01 08:53:59 UTC 6,226 bytes, 211 lines download raw bytes

1(function () {
2  "use strict";
3
4  function getArchivePageFromUrl() {
5    var pathMatch = window.location.pathname.match(/\/page\/(\d+)\/?$/);
6
7    if (pathMatch && pathMatch[1]) {
8      return parseInt(pathMatch[1], 10) || 1;
9    }
10
11    var searchParams = new URLSearchParams(window.location.search);
12    var paged = parseInt(searchParams.get("paged") || searchParams.get("page") || "1", 10);
13
14    return paged > 0 ? paged : 1;
15  }
16
17  function createLoadMoreButton(listing) {
18    var actions = document.createElement("div");
19    var button = document.createElement("button");
20    var customButtonClasses = listing.dataset.buttonClasses || "";
21    var customLabel = listing.dataset.loadMoreLabel || MKT.listing_load_more_label || "Cargar mas";
22
23    actions.className = "mkt-listing__actions";
24    button.className = "mkt-listing__load-more " + customButtonClasses;
25    button.type = "button";
26    button.textContent = customLabel;
27
28    actions.appendChild(button);
29    listing.appendChild(actions);
30
31    return button;
32  }
33
34  function removeLoadMoreButton(listing) {
35    var actions = listing.querySelector(".mkt-listing__actions");
36
37    if (actions) {
38      actions.remove();
39    }
40  }
41
42  function appendItemsWithAnimation(itemsContainer, html) {
43    if (!html) {
44      return [];
45    }
46
47    var template = document.createElement("template");
48    var prefersReducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
49    var newItems = [];
50
51    template.innerHTML = html.trim();
52    newItems = Array.from(template.content.children);
53
54    if (!newItems.length) {
55      return [];
56    }
57
58    if (prefersReducedMotion) {
59      newItems.forEach(function (item) {
60        itemsContainer.appendChild(item);
61      });
62
63      return newItems;
64    }
65
66    newItems.forEach(function (item, index) {
67      item.classList.add("mkt-listing__item-enter");
68      item.style.setProperty("--mkt-listing-enter-delay", String(index * 120) + "ms");
69      itemsContainer.appendChild(item);
70    });
71
72    requestAnimationFrame(function () {
73      window.setTimeout(function () {
74        newItems.forEach(function (item) {
75          item.classList.add("is-visible");
76        });
77      }, 140);
78    });
79
80    newItems.forEach(function (item) {
81      item.addEventListener(
82        "transitionend",
83        function handleTransitionEnd(event) {
84          if (event.propertyName !== "opacity") {
85            return;
86          }
87
88          item.classList.remove("mkt-listing__item-enter", "is-visible");
89          item.style.removeProperty("--mkt-listing-enter-delay");
90          item.removeEventListener("transitionend", handleTransitionEnd);
91        },
92        { once: true }
93      );
94    });
95
96    return newItems;
97  }
98
99  function scrollToNewItems(newItems) {
100    if (!newItems || !newItems.length) {
101      return;
102    }
103
104    var prefersReducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
105    var firstItem = newItems[0];
106    var rect = firstItem.getBoundingClientRect();
107    var viewportHeight = window.innerHeight || document.documentElement.clientHeight || 0;
108    var offset = 48;
109
110    if (rect.top <= viewportHeight * 0.55) {
111      return;
112    }
113
114    window.scrollTo({
115      top: window.scrollY + rect.top - offset,
116      behavior: prefersReducedMotion ? "auto" : "smooth",
117    });
118  }
119
120  function initListing(listing) {
121    if (!listing || listing.dataset.mktListingReady === "true") {
122      return;
123    }
124
125    var itemsContainer = listing.querySelector(".mkt-listing__items");
126    var button = null;
127    var hasMore = listing.classList.contains("mkt-listing--has-more");
128
129    if (!itemsContainer) {
130      return;
131    }
132
133    listing.dataset.mktListingReady = "true";
134
135    if (hasMore) {
136      button = createLoadMoreButton(listing);
137    }
138
139    if (!button) {
140      return;
141    }
142
143    button.addEventListener("click", function () {
144      if (button.disabled) {
145        return;
146      }
147
148      var currentPage = parseInt(listing.dataset.currentPage || "", 10) || getArchivePageFromUrl();
149      var postsPerPage = parseInt(listing.dataset.postsPerPage || "", 10);
150      var template = listing.dataset.template || "sample-card-multipurpose";
151      var itemClasses = listing.dataset.itemClasses || "mkt-listing__item";
152      var context = listing.dataset.context || "{}";
153      var templateContext = listing.dataset.templateContext || "{}";
154      var nonce = MKT.listing_load_more_nonce || "";
155      var nextPage = currentPage + 1;
156      var formData = new FormData();
157
158      button.disabled = true;
159      button.classList.add("is-loading");
160
161      formData.append("action", "mkt_load_more_listing");
162      formData.append("nonce", nonce);
163      formData.append("template", template);
164      formData.append("item_classes", itemClasses);
165      formData.append("page", String(nextPage));
166      formData.append("context", context);
167      formData.append("template_context", templateContext);
168
169      if (postsPerPage > 0) {
170        formData.append("posts_per_page", String(postsPerPage));
171      }
172
173      fetch(MKT.ajax_url, {
174        method: "POST",
175        credentials: "same-origin",
176        body: formData,
177      })
178        .then(function (response) {
179          return response.json();
180        })
181        .then(function (response) {
182          if (!response || !response.success || !response.data) {
183            throw new Error("Load more failed");
184          }
185
186          if (response.data.html) {
187            var newItems = appendItemsWithAnimation(itemsContainer, response.data.html);
188            scrollToNewItems(newItems);
189          }
190
191          listing.dataset.currentPage = String(response.data.current_page || nextPage);
192          listing.classList.toggle("mkt-listing--has-more", !!response.data.has_more);
193
194          if (!response.data.has_more) {
195            removeLoadMoreButton(listing);
196          } else {
197            button.disabled = false;
198            button.classList.remove("is-loading");
199          }
200        })
201        .catch(function () {
202          button.disabled = false;
203          button.classList.remove("is-loading");
204        });
205    });
206  }
207
208  document.addEventListener("DOMContentLoaded", function () {
209    document.querySelectorAll(".mkt-listing").forEach(initListing);
210  });
211})();

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.