Skip to content

Commit

Permalink
DMF-6369: Factorize search listener js code
Browse files Browse the repository at this point in the history
  • Loading branch information
jayblanc committed Dec 16, 2024
1 parent 0b11c9a commit 2185f02
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 56 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Required module dependencies
* rating
* search
* topstories
* bookmarks
* skins
* default-skins

How to compile
---
Expand Down
18 changes: 18 additions & 0 deletions src/main/resources/javascript/custom/trackSearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function registerSearchListener(formId) {
if (window.wem) {
document.addEventListener('DOMContentLoaded', () => {
document.getElementById(formId).addEventListener('submit', function (event) {
console.log(event.target);
const searchTermInput = event.target.elements['searchTerm'];
const languageInput = event.target.querySelector('[name="src_languages.values"]');
const searchEvent = window.wem.buildSearchEvent(formId, event.target, searchTermInput.value, languageInput.value);
window.wem.collectEvent(searchEvent,
() => console.debug('[Collecting search event] search formEvent sent'),
() => console.debug('[Collecting search event] oups search formEvent was not handled properly')
);
});
});
} else {
console.debug('Wem does not exist');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@
</div>
</div>
</div>
</c:if>
</c:if>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<template:addResources type="css" resources="plugins/skyforms/sky-forms.css"/>
<template:addResources type="css" resources="plugins/skyforms/custom/custom-sky-forms.css"/>
<template:addResources type="javascript" resources="custom/advancedSearchForm.js"/>
<template:addResources type="javascript" resources="custom/trackSearch.js"/>

<jcr:nodeProperty name="jcr:title" node="${currentNode}" var="title"/>
<c:if test="${not empty title.string}">
Expand Down Expand Up @@ -128,21 +129,4 @@
</div>
</div>
</div>
<script>
if (window.wem) {
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('advancedSearchForm').addEventListener('submit', function (event) {
console.log(event.target);
const searchTermInput = event.target.elements['searchTerm'];
const languageInput = event.target.querySelector('[name="src_languages.values"]');
const searchEvent = window.wem.buildSearchEvent('advancedSearchForm', event.target, searchTermInput.value, languageInput.value);
window.wem.collectEvent(searchEvent,
() => console.debug('[Collecting search event] search formEvent sent'),
() => console.debug('[Collecting search event] oups search formEvent was not handled properly')
);
});
});
} else {
console.debug('Wem does not exist');
}
</script>
<script>registerSearchListener('advancedSearchForm');</script>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<%--@elvariable id="url" type="org.jahia.services.render.URLGenerator"--%>

<template:addResources type="css" resources="pages/page_search.css"/>
<template:addResources type="javascript" resources="custom/trackSearch.js"/>
<template:addCacheDependency uuid="${currentNode.properties.result.string}"/>
<c:if test="${not empty currentNode.properties.result.node}">
<c:url value='${url.base}${currentNode.properties.result.node.path}.html' var="searchUrl"/>
Expand Down Expand Up @@ -50,21 +51,4 @@
</div>
</div>
</c:if>
<script>
if (window.wem) {
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('searchForm').addEventListener('submit', function (event) {
console.log(event.target);
const searchTermInput = event.target.elements['searchTerm'];
const languageInput = event.target.querySelector('[name="src_languages.values"]');
const searchEvent = window.wem.buildSearchEvent('searchForm', event.target, searchTermInput.value, languageInput.value);
window.wem.collectEvent(searchEvent,
() => console.debug('[Collecting search event] search formEvent sent'),
() => console.debug('[Collecting search event] oups search formEvent was not handled properly')
);
});
});
} else {
console.debug('Wem does not exist');
}
</script>
<script>registerSearchListener('searchForm');</script>
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<%@ taglib prefix="template" uri="http://www.jahia.org/tags/templateLib" %>

<template:addResources type="css" resources="simplesearchform.css"/>

<template:addResources type="javascript" resources="custom/trackSearch.js"/>
<template:addCacheDependency uuid="${currentNode.properties.result.string}"/>
<c:if test="${not empty currentNode.properties.result.node}">
<c:url value='${url.base}${currentNode.properties.result.node.path}.html' var="searchUrl"/>
Expand All @@ -43,21 +43,4 @@
</s:form>
</div>
</c:if>
<script>
if (window.wem) {
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('topSearchForm').addEventListener('submit', function (event) {
console.log(event.target);
const searchTermInput = event.target.elements['searchTerm'];
const languageInput = event.target.querySelector('[name="src_languages.values"]');
const searchEvent = window.wem.buildSearchEvent('topSearchForm', event.target, searchTermInput.value, languageInput.value);
window.wem.collectEvent(searchEvent,
() => console.debug('[Collecting search event] search formEvent sent'),
() => console.debug('[Collecting search event] oups search formEvent was not handled properly')
);
});
});
} else {
console.debug('Wem does not exist');
}
</script>
<script>registerSearchListener('topSearchForm');</script>

0 comments on commit 2185f02

Please sign in to comment.