@salesforcedevs/dx-components 0.69.0 → 0.70.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,187 @@
1
+ import { LightningElement, api, track } from "lwc";
2
+ import type * as CoveoSDK from "coveo-search-ui";
3
+
4
+ interface CoveoSearch {
5
+ state: typeof CoveoSDK.state;
6
+ get: typeof CoveoSDK.get;
7
+ $$: typeof CoveoSDK.$$;
8
+ InitializationEvents: typeof CoveoSDK.InitializationEvents;
9
+ QueryEvents: typeof CoveoSDK.QueryEvents;
10
+ SearchEndpoint: typeof CoveoSDK.SearchEndpoint;
11
+ TemplateCache: typeof CoveoSDK.TemplateCache;
12
+ UnderscoreTemplate: typeof CoveoSDK.UnderscoreTemplate;
13
+ init: typeof CoveoSDK.init;
14
+ IQuerySuccessEventArgs: CoveoSDK.IQuerySuccessEventArgs;
15
+ }
16
+
17
+ declare const Coveo: CoveoSearch;
18
+
19
+ function getPaginationState(event: CoveoSDK.IQuerySuccessEventArgs): {
20
+ numberOfPages: number;
21
+ currentPage: number;
22
+ } {
23
+ const pageSize = event.query.numberOfResults!;
24
+ const totalResults = event.results.totalCount!;
25
+ const numberOfPages = Math.ceil(totalResults / pageSize);
26
+
27
+ const currentPage = event.query.firstResult! / pageSize + 1;
28
+
29
+ return { numberOfPages, currentPage };
30
+ }
31
+
32
+ const resultsTemplatesInnerHtml = `
33
+ <script
34
+ id="myDocumentResultTemplate"
35
+ class="result-template"
36
+ type="text/html"
37
+ data-field-publicurl=""
38
+ >
39
+ <div class="dx-result">
40
+ <p class="dx-result-title">
41
+ <a
42
+ class="CoveoResultLink"
43
+ data-field="@publicurl"
44
+ ></a>
45
+ </p>
46
+ <p class="dx-result-excerpt CoveoExcerpt"></p>
47
+ </div>
48
+ </script>
49
+ <script
50
+ id="myDefaultResultTemplate"
51
+ class="result-template"
52
+ type="text/html"
53
+ >
54
+ <div class="dx-result">
55
+ <p class="dx-result-title">
56
+ <a class="CoveoResultLink"></a>
57
+ </p>
58
+ <p class="dx-result-excerpt CoveoExcerpt"></p>
59
+ </div>
60
+ </script>
61
+ `;
62
+
63
+ export default class SearchResults extends LightningElement {
64
+ @api coveoOrganizationId!: string;
65
+ @api coveoPublicAccessToken!: string;
66
+ @api coveoSearchPipeline!: string;
67
+
68
+ private root: HTMLElement | null = null;
69
+ private isInitialized = false;
70
+
71
+ @track _query: string = "";
72
+
73
+ @api
74
+ get searchQuery() {
75
+ return this._query;
76
+ }
77
+ set searchQuery(q: string) {
78
+ this._query = q;
79
+ if (this.isInitialized && this._query !== "") {
80
+ this.updateSearchQuery();
81
+ }
82
+ }
83
+
84
+ private totalResults: number | null = null;
85
+
86
+ private get title() {
87
+ return this.totalResults ? this.totalResults.toLocaleString() : "";
88
+ }
89
+
90
+ private query: string = "";
91
+ private hasFilters: boolean = false;
92
+
93
+ private get hasQuery(): boolean {
94
+ return this.query !== "";
95
+ }
96
+
97
+ private updateSearchQuery() {
98
+ Coveo.state(this.root!, "q", this._query);
99
+ }
100
+
101
+ private clearFilters() {
102
+ const BreadcrumbManager = Coveo.get(
103
+ this.root!.querySelector(".CoveoBreadcrumb") as HTMLElement
104
+ ) as any;
105
+ BreadcrumbManager.clearBreadcrumbs();
106
+ }
107
+
108
+ private currentPage: number = 1;
109
+ private totalPages: number = 1;
110
+
111
+ private goToPage(e: CustomEvent) {
112
+ const page = e.detail;
113
+ const Pager = Coveo.get(
114
+ this.root!.querySelector(".CoveoPager") as HTMLElement
115
+ ) as any;
116
+ Pager.setPage(page);
117
+ this.currentPage = page;
118
+ }
119
+
120
+ private dismissFiltersOverlay = () => {
121
+ const overlay = this.root!.querySelector(
122
+ ".coveo-dropdown-background-active"
123
+ ) as HTMLElement | null;
124
+ overlay?.click();
125
+ };
126
+
127
+ private attachListeners(root: HTMLElement) {
128
+ Coveo.$$(root).on(
129
+ Coveo.InitializationEvents.afterInitialization,
130
+ () => {
131
+ if (this._query !== "") {
132
+ Coveo.state(this.root!, "q", this.searchQuery);
133
+ }
134
+ if (Coveo.state(this.root!, "q") === "") {
135
+ Coveo.state(this.root!, "sort", "date descending");
136
+ }
137
+ this.isInitialized = true;
138
+ }
139
+ );
140
+
141
+ Coveo.$$(root).on(Coveo.QueryEvents.querySuccess, (event: any) => {
142
+ const { currentPage, numberOfPages } = getPaginationState(
143
+ event.detail
144
+ );
145
+ this.currentPage = currentPage;
146
+ this.totalPages = numberOfPages;
147
+ this.totalResults = event.detail.results.totalCount;
148
+ this.query = event.detail.query.q ?? "";
149
+ this.hasFilters = event.detail.query.facets.some((f: any) => {
150
+ return f.currentValues.some((cv: any) => {
151
+ return cv.state === "selected";
152
+ });
153
+ });
154
+ });
155
+ }
156
+
157
+ private initializeCoveo() {
158
+ this.root = this.template.querySelector(".CoveoSearchInterface")!;
159
+
160
+ const resultsList = this.template.querySelector(".CoveoResultList");
161
+ if (resultsList) {
162
+ resultsList.innerHTML = resultsTemplatesInnerHtml;
163
+ }
164
+
165
+ Coveo.SearchEndpoint.configureCloudV2Endpoint(
166
+ this.coveoOrganizationId,
167
+ this.coveoPublicAccessToken
168
+ );
169
+
170
+ this.attachListeners(this.root);
171
+
172
+ Coveo.init(this.root);
173
+ }
174
+
175
+ renderedCallback() {
176
+ if (!this.isInitialized) {
177
+ if (Object.prototype.hasOwnProperty.call(window, "Coveo")) {
178
+ this.initializeCoveo();
179
+ } else {
180
+ const script = document.querySelector("script.coveo-script");
181
+ script?.addEventListener("load", () => {
182
+ this.initializeCoveo();
183
+ });
184
+ }
185
+ }
186
+ }
187
+ }