Skip to content

Commit 35ce1a4

Browse files
committed
Converterd JS from jquery to plain js
1 parent 37b60fd commit 35ce1a4

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

view/frontend/templates/general.phtml

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
.htmlsitemap-index-index .sitemap-item-type {margin-bottom: 40px;}
3030
.htmlsitemap-index-index .sitemap-item-type > h2 {margin: 0 0 20px;}
31-
.htmlsitemap-index-index .sitemap-item-type .highlight-result {color: red;}
31+
[class*="htmlsitemap-"] .highlight-result {color: red;}
3232

3333
/* Category */
3434
[class*="htmlsitemap-"] .widget.block-categories .accordion {margin-bottom: 20px;}
@@ -60,64 +60,69 @@
6060

6161
</style>
6262
<?php $script = "
63-
require(['jquery'], function($) {
64-
63+
document.addEventListener('DOMContentLoaded', function() {
6564
var searchQuery = '';
6665
6766
initSearch();
6867
6968
function initSearch() {
70-
$('#html-sitemap-search').keyup(function(el) {
71-
var val = el.target.value;
69+
var searchInput = document.getElementById('html-sitemap-search');
70+
71+
// Handle keyup event on the search input
72+
searchInput.addEventListener('keyup', function(event) {
73+
var val = event.target.value;
7274
73-
if (searchQuery != val.toLocaleLowerCase()) {
75+
if (searchQuery !== val.toLocaleLowerCase()) {
7476
findItems(val);
7577
}
7678
});
7779
78-
$('.sitemap-item-section a').each(function (key, el) {
80+
// Store original content for each link
81+
var links = document.querySelectorAll('.sitemap-item-section a');
82+
links.forEach(function(el) {
7983
el.originalTextContent = el.innerText ? el.innerHTML : el.textContent;
8084
});
8185
}
8286
8387
function findItems(query) {
84-
searchQuery = query.replace(/^\s+/, '').replace(/\s+$/, '').toLowerCase();
88+
searchQuery = query.trim().toLowerCase();
8589
86-
$('.sitemap-item-section').each(function(key, el) {
90+
var sections = document.querySelectorAll('.sitemap-item-section');
91+
sections.forEach(function(section) {
8792
var noResults = true;
8893
89-
$(this).find('a').each(function(key, el) {
94+
var links = section.querySelectorAll('a');
95+
links.forEach(function(el) {
9096
el.textContent = el.originalTextContent.replace(/&amp;/g, '&');
91-
if (searchQuery.replace(/\s+/, '') != '') {
92-
if (el.originalTextContent.toLowerCase().indexOf(searchQuery) == -1) {
93-
$(el).hide();
97+
98+
if (searchQuery !== '') {
99+
if (el.originalTextContent.toLowerCase().indexOf(searchQuery) === -1) {
100+
el.style.display = 'none';
94101
} else {
95-
$(el).show();
102+
el.style.display = '';
96103
noResults = false;
97-
highlightResult($(el).get(0), searchQuery, 'highlight-result');
104+
highlightResult(el, searchQuery, 'highlight-result');
98105
}
99106
} else {
100-
$(el).show();
107+
el.style.display = '';
101108
noResults = false;
102109
}
103110
});
104111
105-
if (noResults) {
106-
$(this).hide();
107-
} else {
108-
$(this).show();
109-
}
112+
section.style.display = noResults ? 'none' : '';
110113
});
111114
}
112115
113116
function highlightResult(element, term, className) {
117+
114118
className = className || 'highlight';
115119
term = (term || '').toUpperCase();
116-
if (term.replace(/\s+/, '') == '') {
117-
return false
120+
121+
if ('' === term) {
122+
return false;
118123
}
119124
120-
if (element.nodeType == Node.TEXT_NODE) {
125+
if (element.nodeType === Node.TEXT_NODE) {
121126
var pos = element.data.toUpperCase().indexOf(term);
122127
if (pos >= 0) {
123128
var searchResult = element.splitText(pos),
@@ -127,15 +132,13 @@
127132
spanWrapper.className = className;
128133
spanWrapper.appendChild(searchResult.cloneNode(true));
129134
searchResult.parentNode.replaceChild(spanWrapper, searchResult);
130-
131135
}
132-
} else if (element.nodeType == Node.ELEMENT_NODE && element.childNodes && !/(script|style)/i.test(element.tagName)) {
136+
} else if (element.nodeType === Node.ELEMENT_NODE && element.childNodes && !/(script|style)/i.test(element.tagName)) {
133137
for (var i = 0; i < element.childNodes.length; ++i) {
134138
i += highlightResult(element.childNodes[i], term, className);
135139
}
136140
}
137141
};
138142
});
139143
";?>
140-
141144
<?= /* @noEscape */ $mfSecureRenderer->renderTag('script', [], $script, false) ?>

0 commit comments

Comments
 (0)