@salesforcedevs/dx-components 0.69.0 → 0.70.0-alpha.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
+ }
@@ -1,20 +1,43 @@
1
- import qs from "query-string";
1
+ import qs, { ParsedQuery } from "query-string";
2
+ /*
3
+ we import this util function below to get/set the query params as hyphenated versions of their camelcase counterparts.
4
+ */
5
+ import kebabCase from "lodash.kebabcase";
2
6
 
3
7
  export function getUrlParamValue(key: string) {
4
- const urlParams = qs.parse(window.location.search);
8
+ const urlParams: ParsedQuery<string> = qs.parse(window.location.search);
9
+ const hyphenatedKey = kebabCase(key);
5
10
 
6
- return urlParams[key] ? urlParams[key]?.split(",") : [];
11
+ if (hyphenatedKey in urlParams) {
12
+ return (urlParams[hyphenatedKey] as string)
13
+ .split(",")
14
+ .map((urlParam) => urlParam.replace("-", " "));
15
+ }
16
+
17
+ return [];
7
18
  }
8
19
 
9
- export function setUrlParamValue(key: string, value: any) {
10
- const newUrl = new URL(window.location.href);
11
- if (value) {
12
- newUrl.search = qs.stringify(
13
- { [key]: value },
14
- { arrayFormat: "comma" }
15
- );
16
- } else {
17
- newUrl.searchParams.delete(key);
20
+ export function setUrlParamValue(options: { [key: string]: string[] }) {
21
+ const url = new URL(window.location.href);
22
+
23
+ const newParams = {} as any;
24
+ const urlParams = qs.parse(window.location.search) as any;
25
+
26
+ for (const [key, value] of Object.entries(options)) {
27
+ if (value?.length) {
28
+ newParams[kebabCase(key)] = value.map((v) => kebabCase(v));
29
+ } else {
30
+ delete urlParams[kebabCase(key)];
31
+ }
18
32
  }
19
- window.history.replaceState(null, "", newUrl.href);
33
+
34
+ url.search = qs.stringify(
35
+ {
36
+ ...urlParams,
37
+ ...newParams
38
+ },
39
+ { arrayFormat: "comma" }
40
+ );
41
+
42
+ window.history.replaceState(null, "", url.href);
20
43
  }
package/LICENSE DELETED
@@ -1,12 +0,0 @@
1
- Copyright (c) 2020, Salesforce.com, Inc.
2
- All rights reserved.
3
-
4
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
-
6
- * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
-
8
- * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
-
10
- * Neither the name of Salesforce.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
-
12
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.