Skip to content

Commit 5f554d3

Browse files
committed
feat: Support coveo on mobile + show in header
1 parent 5105e9f commit 5f554d3

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

assets/js/coveo.js

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ async function getsearchObj() {
2121
}
2222

2323
async function atomicCoveo() {
24+
/* Fetch the credentials */
2425
await customElements.whenDefined('atomic-search-interface');
25-
const searchInterface = document.querySelector('#search-v2');
26-
const searchInterfaceStandalone =
27-
document.querySelector('#search-standalone');
2826
const token = localStorage.getItem('coveo_jwt_v1');
2927
const org_id = localStorage.getItem('coveo_org_id_v1');
3028
let searchObj = { token, org_id };
@@ -35,8 +33,15 @@ async function atomicCoveo() {
3533
localStorage.setItem('coveo_org_id_v1', searchObj.org_id);
3634
}
3735

38-
if (searchInterface) {
39-
await searchInterface.initialize({
36+
/* Initialize the interfaces with credentials */
37+
const searchPageInterface = document.querySelector('#search-v2');
38+
const searchBarHeader = document.querySelector('#search-standalone-header');
39+
const searchBarSidebar = document.querySelector('#search-standalone-sidebar');
40+
const sidebar = document.querySelector('#sidebar-layout');
41+
let searchbar;
42+
43+
if (searchPageInterface) {
44+
await searchPageInterface.initialize({
4045
accessToken: token,
4146
organizationId: org_id,
4247
analytics: { analyticsMode: 'legacy' },
@@ -48,22 +53,29 @@ async function atomicCoveo() {
4853
return request;
4954
},
5055
});
51-
searchInterface.executeFirstSearch();
52-
}
56+
searchPageInterface.executeFirstSearch();
57+
} else {
58+
// If there is a searchbar, only initialize the searchbar for the sidebar.
59+
if (sidebar) {
60+
searchbar = searchBarSidebar;
61+
} else {
62+
searchbar = searchBarHeader;
63+
}
5364

54-
await searchInterfaceStandalone.initialize({
55-
accessToken: token,
56-
organizationId: org_id,
57-
analytics: { analyticsMode: 'legacy' },
58-
preprocessRequest: (request, clientOrigin, metadata) => {
59-
const body = JSON.parse(request.body);
60-
body.q = `<@- ${body.q} -@>`;
61-
request.body = JSON.stringify(body);
65+
await searchbar.initialize({
66+
accessToken: token,
67+
organizationId: org_id,
68+
analytics: { analyticsMode: 'legacy' },
69+
preprocessRequest: (request, clientOrigin, metadata) => {
70+
const body = JSON.parse(request.body);
71+
body.q = `<@- ${body.q} -@>`;
72+
request.body = JSON.stringify(body);
6273

63-
return request;
64-
},
65-
});
66-
searchInterfaceStandalone.executeFirstSearch();
74+
return request;
75+
},
76+
});
77+
searchbar.executeFirstSearch();
78+
}
6779
}
6880

6981
async function legacyCoveo() {

layouts/_default/docs.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
{{ end }}
1616

1717
<section class="main-layout">
18-
<div class="sidebar-layout">
18+
<div class="sidebar-layout" id="sidebar-layout">
1919
<nav data-mf="true" id="sidebar-v2" class="sidebar" style="display:none;">
2020
{{ partial "sidebar-v2.html" . }}
2121
</nav>

layouts/partials/header.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
</a>
77
{{ if ( not ( in .Site.Params.buildtype "package" ) ) }}
88
<div class="navbar navbar-nav">
9-
<!-- Standalone search box. -->
9+
<!-- Standalone search box. -->
1010
<div id="searchbox">
1111
<!--div class="CoveoAnalytics" data-search-hub="HUB_ES_Nginx_Docs_And_Org"></div-->
1212
<div class="CoveoSearchbox" data-placeholder='Search NGINX Docs...'></div>
1313
</div>
14+
<atomic-search-interface id="search-standalone-header" data-mf="true" style="display:none;">
15+
<atomic-search-box redirection-url="/search.html">
16+
</atomic-search-box>
17+
</atomic-search-interface>
1418
</div>
1519
{{ end }}
1620
{{ if ( in .Site.Params.buildtype "package" ) }}

layouts/partials/sidebar-v2.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
{{ $productIdentifier := index ((split $relPermalink "/")) 1 }}
2121
{{ $productName := index $productMap $productIdentifier }}
2222

23-
<atomic-search-interface id="search-standalone" data-mf="true" style="display:none;">
23+
<atomic-search-interface id="search-standalone-sidebar" data-mf="true" style="display:none;">
2424
<atomic-search-box redirection-url="/search.html">
2525
</atomic-search-box>
2626
</atomic-search-interface>

0 commit comments

Comments
 (0)