PageSourceSearch

https://www.ejemplos.co/wp-content/themes/ejemplos_v5/js/navigation.js?ver=1.0.2

js ejemplos.co collected 2026-10-02 04:39:59 UTC 8,817 bytes, 270 lines download raw bytes

1/**
2 * File navigation.js.
3 *
4 * Handles toggling the navigation menu for small screens and enables TAB key
5 * navigation support for dropdown menus.
6 */
7
8document.addEventListener("DOMContentLoaded", e => {
9  consentCookieCheck();
10  langSwitchInit();
11});
12(function () {
13  const siteNavigation = document.getElementById("site-navigation");
14
15  // Return early if the navigation don't exist.
16  if (!siteNavigation) {
17    return;
18  }
19  const button = document.querySelector(".menu-toggle");
20
21  // Return early if the button don't exist.
22  if ("undefined" === typeof button) {
23    return;
24  }
25  const menu = siteNavigation.getElementsByTagName("ul")[0];
26
27  // Hide menu toggle button if menu is empty and return early.
28  if ("undefined" === typeof menu) {
29    button.style.display = "none";
30    return;
31  }
32  if (!menu.classList.contains("nav-menu")) {
33    menu.classList.add("nav-menu");
34  }
35
36  // Toggle the .toggled class and the aria-expanded value each time the button is clicked.
37  button.addEventListener("click", function () {
38    document.body.classList.toggle("menu-toggled");
39    // console.log(button.getAttribute( 'aria-expanded' ));
40
41    if (button.getAttribute("aria-expanded") === "true") {
42      button.setAttribute("aria-expanded", "false");
43    } else {
44      button.setAttribute("aria-expanded", "true");
45    }
46  });
47
48  // Get all the link elements within the menu.
49  const links = menu.getElementsByTagName("a");
50
51  // Get all the link elements with children within the menu.
52  const linksWithChildren = menu.querySelectorAll(".menu-item-has-children > a, .page_item_has_children > a");
53
54  // Toggle focus each time a menu link is focused or blurred.
55  for (const link of links) {
56    link.addEventListener("focus", toggleFocus, true);
57    link.addEventListener("blur", toggleFocus, true);
58  }
59
60  // Toggle focus each time a menu link with children receive a touch event.
61  for (const link of linksWithChildren) {
62    link.addEventListener("touchstart", toggleFocus, false);
63  }
64
65  /**
66   * Sets or removes .focus class on an element.
67   */
68  function toggleFocus() {
69    if (event.type === "focus" || event.type === "blur") {
70      let self = this;
71      // Move up through the ancestors of the current link until we hit .nav-menu.
72      while (!self.classList.contains("nav-menu")) {
73        // On li elements toggle the class .focus.
74        if ("li" === self.tagName.toLowerCase()) {
75          self.classList.toggle("focus");
76        }
77        self = self.parentNode;
78      }
79    }
80    if (event.type === "touchstart") {
81      const menuItem = this.parentNode;
82      event.preventDefault();
83      for (const link of menuItem.parentNode.children) {
84        if (menuItem !== link) {
85          link.classList.remove("focus");
86        }
87      }
88      menuItem.classList.toggle("focus");
89    }
90  }
91
92  // change target to link with rel = "external"
93
94  // for (link of document.querySelectorAll('a[rel=external')) {
95  // 	link.target = "_blank";
96  // }
97})();
98
99(function () {
100  /*--------------------------------------------------------------
101  ## Index Toggle
102  --------------------------------------------------------------*/
103
104  let indexBtn = document.querySelector(".index-toggle");
105  if (!indexBtn) return;
106  indexBtn.addEventListener("click", function () {
107    document.body.classList.toggle("index-toggled");
108    document.body.classList.remove("search-toggled");
109  });
110})();
111
112/*--------------------------------------------------------------
113## Detectar si ha escroleado mas allá del header
114--------------------------------------------------------------*/
115
116(function () {
117  window.addEventListener("scroll", function () {
118    if (window.pageYOffset > getHeaderHeight()) {
119      document.body.classList.add("scrolled");
120    } else {
121      document.body.classList.remove("scrolled");
122    }
123  });
124})();
125
126/*--------------------------------------------------------------
127## Search toggle
128--------------------------------------------------------------*/
129
130(function () {
131  let searchBtn = document.querySelector(".header-search .search-toggle");
132  if (!searchBtn) return;
133  searchBtn.addEventListener("click", function (e) {
134    document.body.classList.toggle("search-toggled");
135    document.body.classList.remove("index-toggled");
136    document.querySelector(".search-field").focus();
137  });
138})();
139
140/*--------------------------------------------------------------
141## Evitar busqueda vacías
142--------------------------------------------------------------*/
143
144(function () {
145  for (searchform of document.querySelectorAll("form.searchform")) {
146    searchform.addEventListener("submit", function (e) {
147      if (this.querySelector('[type="search"]').value == "") {
148        e.preventDefault();
149      }
150    });
151  }
152})();
153
154/*--------------------------------------------------------------
155## Share Links
156--------------------------------------------------------------*/
157
158(function () {
159  for (link of document.querySelectorAll("a.box-share")) {
160    link.addEventListener("click", function (e) {
161      e.preventDefault();
162      window.open(this.href, "", "resizable=no,status=no,location=no,toolbar=no,menubar=no,fullscreen=no,scrollbars=no,dependent=no,width=570,height=600");
163    });
164  }
165  for (link of document.querySelectorAll("a.copy-link")) {
166    link.addEventListener("click", function (e) {
167      e.preventDefault();
168      // console.log(e);
169      copyToClipboard(this.href, this.dataset.message);
170    });
171  }
172})();
173
174/*--------------------------------------------------------------
175## Crear Menu Mobile
176--------------------------------------------------------------*/
177
178(function () {
179  let siteHeader = document.querySelector(".site-header");
180  let navs = siteHeader.querySelectorAll("nav");
181  if (navs.length <= 0) return;
182  let menuMobileContainer = document.createElement("div");
183  let menuMobileContainerScroller = document.createElement("div");
184  menuMobileContainer.className = "menu-mobile-container";
185  menuMobileContainerScroller.className = "menu-mobile-container-scroller";
186  for (nav of navs) {
187    navClone = nav.cloneNode(true);
188    navClone.id = "";
189    for (const uls of navClone.querySelectorAll("ul")) {
190      uls.id = uls.id != "" ? uls.id + "-mobile" : "";
191    }
192    for (item of navClone.querySelectorAll(".menu-item-has-children")) {
193      let showSubmenu = document.createElement("span");
194      showSubmenu.className = "show-submenu icon icon-chevron-down";
195      item.appendChild(showSubmenu);
196      showSubmenu.addEventListener("click", function () {
197        this.parentNode.classList.toggle("open");
198      });
199    }
200    menuMobileContainerScroller.appendChild(navClone);
201    menuMobileContainer.appendChild(menuMobileContainerScroller);
202  }
203  siteHeader.appendChild(menuMobileContainer);
204})();
205function consentCookieCheck(params) {
206  // Select the node that will be observed for mutations
207  const targetNode = document.body;
208  // Options for the observer (which mutations to observe)
209  const config = {
210    attributes: true,
211    childList: true,
212    subtree: true
213  };
214
215  // Callback function to execute when mutations are observed
216  const callback = (mutationList, observer) => {
217    for (const mutation of mutationList) {
218      if (mutation.type === "childList") {
219        // console.log("A child node has been added or removed.");
220        mutation.addedNodes.forEach(element => {
221          // console.log(element);
222          if (element.id == 'sd-cmp') {
223            if (document.getElementById('cookie-notice')) {
224              document.getElementById('cookie-notice').remove();
225              console.log('#cookie-notice removed');
226              observer.disconnect();
227            }
228          }
229        });
230      }
231    }
232  };
233
234  // Create an observer instance linked to the callback function
235  const observer = new MutationObserver(callback);
236
237  // Start observing the target node for configured mutations
238  observer.observe(targetNode, config);
239  setTimeout(() => {
240    observer.disconnect();
241  }, 15000);
242}
243function langSwitchInit() {
244  document.querySelectorAll('.language-switcher.list').forEach((ls, i) => {
245    if (ls.querySelector('.current-lang')) {
246      const label = document.createElement('a');
247      label.className = 'current-label';
248      label.href = "#";
249      label.innerHTML = ls.querySelector('.current-lang').innerText;
250      ls.prepend(label);
251      ls.querySelector('.current-lang').remove();
252      label.addEventListener('click', e => {
253        e.preventDefault();
254      });
255      label.addEventListener('focus', e => {
256        ls.classList.add('active');
257      });
258      label.addEventListener('blur', e => {
259        setTimeout(function () {
260          ls.classList.remove('active');
261        }, 100);
262      });
263    }
264    ;
265    // remove english
266    if (ls.querySelector('.lang-item-en')) {
267      ls.querySelector('.lang-item-en').remove();
268    }
269  });
270}

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.