From 5f554d372ee749060010a47eec8b1f861907879e Mon Sep 17 00:00:00 2001 From: Lam Nguyen Date: Thu, 27 Mar 2025 12:03:23 -0700 Subject: [PATCH 1/3] feat: Support coveo on mobile + show in header --- assets/js/coveo.js | 50 ++++++++++++++++++++------------ layouts/_default/docs.html | 2 +- layouts/partials/header.html | 6 +++- layouts/partials/sidebar-v2.html | 2 +- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/assets/js/coveo.js b/assets/js/coveo.js index 9b7a462d..9f38c09d 100644 --- a/assets/js/coveo.js +++ b/assets/js/coveo.js @@ -21,10 +21,8 @@ async function getsearchObj() { } async function atomicCoveo() { + /* Fetch the credentials */ await customElements.whenDefined('atomic-search-interface'); - const searchInterface = document.querySelector('#search-v2'); - const searchInterfaceStandalone = - document.querySelector('#search-standalone'); const token = localStorage.getItem('coveo_jwt_v1'); const org_id = localStorage.getItem('coveo_org_id_v1'); let searchObj = { token, org_id }; @@ -35,8 +33,15 @@ async function atomicCoveo() { localStorage.setItem('coveo_org_id_v1', searchObj.org_id); } - if (searchInterface) { - await searchInterface.initialize({ + /* Initialize the interfaces with credentials */ + const searchPageInterface = document.querySelector('#search-v2'); + const searchBarHeader = document.querySelector('#search-standalone-header'); + const searchBarSidebar = document.querySelector('#search-standalone-sidebar'); + const sidebar = document.querySelector('#sidebar-layout'); + let searchbar; + + if (searchPageInterface) { + await searchPageInterface.initialize({ accessToken: token, organizationId: org_id, analytics: { analyticsMode: 'legacy' }, @@ -48,22 +53,29 @@ async function atomicCoveo() { return request; }, }); - searchInterface.executeFirstSearch(); - } + searchPageInterface.executeFirstSearch(); + } else { + // If there is a searchbar, only initialize the searchbar for the sidebar. + if (sidebar) { + searchbar = searchBarSidebar; + } else { + searchbar = searchBarHeader; + } - await searchInterfaceStandalone.initialize({ - accessToken: token, - organizationId: org_id, - analytics: { analyticsMode: 'legacy' }, - preprocessRequest: (request, clientOrigin, metadata) => { - const body = JSON.parse(request.body); - body.q = `<@- ${body.q} -@>`; - request.body = JSON.stringify(body); + await searchbar.initialize({ + accessToken: token, + organizationId: org_id, + analytics: { analyticsMode: 'legacy' }, + preprocessRequest: (request, clientOrigin, metadata) => { + const body = JSON.parse(request.body); + body.q = `<@- ${body.q} -@>`; + request.body = JSON.stringify(body); - return request; - }, - }); - searchInterfaceStandalone.executeFirstSearch(); + return request; + }, + }); + searchbar.executeFirstSearch(); + } } async function legacyCoveo() { diff --git a/layouts/_default/docs.html b/layouts/_default/docs.html index 6bc7f917..97ea795a 100644 --- a/layouts/_default/docs.html +++ b/layouts/_default/docs.html @@ -15,7 +15,7 @@ {{ end }}
-