-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathanswers-search-bar.js
508 lines (448 loc) · 15.6 KB
/
answers-search-bar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/** @module */
import Core from './core/core';
import cssVars from 'css-vars-ponyfill';
import {
Renderers,
DOM,
SearchParams
} from './ui/index';
import ErrorReporter from './core/errors/errorreporter';
import { AnalyticsReporter } from './core';
import Storage from './core/storage/storage';
import { AnswersComponentError } from './core/errors/errors';
import StorageKeys from './core/storage/storagekeys';
import QueryTriggers from './core/models/querytriggers';
import SearchConfig from './core/models/searchconfig';
import ComponentManager from './ui/components/componentmanager';
import { SANDBOX, PRODUCTION, LOCALE, QUERY_SOURCE } from './core/constants';
import { isValidContext } from './core/utils/apicontext';
import { urlWithoutQueryParamsAndHash } from './core/utils/urlutils';
import Filter from './core/models/filter';
import { SEARCH_BAR_COMPONENTS_REGISTRY } from './ui/components/search-bar-only-registry';
import createImpressionEvent from './core/analytics/createimpressionevent';
/** @typedef {import('./core/services/errorreporterservice').default} ErrorReporterService */
/** @typedef {import('./core/services/analyticsreporterservice').default} AnalyticsReporterService */
/**
* @typedef Services
* @property {ErrorReporterService} errorReporterService
*/
const DEFAULTS = {
environment: PRODUCTION,
locale: LOCALE,
querySource: QUERY_SOURCE,
analyticsEventsEnabled: true
};
/**
* The class that powers the stand-alone SearchBar integration. Note that this class
* follows the singleton pattern.
*/
class AnswersSearchBar {
constructor () {
if (!AnswersSearchBar.setInstance(this)) {
return AnswersSearchBar.getInstance();
}
/**
* A reference of the renderer to use for the components
* This is provided during initialization.
* @type {Renderer}
*/
this.renderer = new Renderers.Handlebars();
/**
* A local reference to the component manager
* @type {ComponentManager}
*/
this.components = new ComponentManager(SEARCH_BAR_COMPONENTS_REGISTRY);
/**
* A local reference to the core api
* @type {Core}
*/
this.core = null;
/**
* A callback function to invoke once the library is ready.
* Typically fired after templates are fetched from server for rendering.
*/
this._onReady = function () {};
/**
* @type {boolean}
* @private
*/
this._eligibleForAnalytics = false;
/**
* @type {Services}
* @private
*/
this._services = null;
/**
* @type {AnalyticsReporterService}
* @private
*/
this._analyticsReporterService = null;
}
/**
* Attempts to set the singleton instance of the {@link AnswersSearchBar}.
* If there is already an instance of this class, the method returns False.
* If the provided instance was set successfully, the method returns True.
*
* @param {AnswersSearchBar} instance
* @returns {boolean}
*/
static setInstance (instance) {
if (!this.instance) {
this.instance = instance;
return true;
}
return false;
}
static getInstance () {
return this.instance;
}
/**
* Initializes the stand-alone SearchBar using the provided configuration.
*
* @param {Object} config The Answers configuration.
*/
init (config) {
const parsedConfig = this.parseConfig(config);
this.validateConfig(parsedConfig);
parsedConfig.search = new SearchConfig(parsedConfig.search);
const storage = new Storage({
persistedValueParser: this._parsePersistentStorageValue
});
storage.init(window.location.search);
storage.set(StorageKeys.SEARCH_CONFIG, parsedConfig.search);
storage.set(StorageKeys.LOCALE, parsedConfig.locale);
storage.set(StorageKeys.QUERY_SOURCE, parsedConfig.querySource);
// Check if sessionsOptIn data is stored in the URL. If it is, prefer that over
// what is in parsedConfig.
const sessionOptIn = storage.get(StorageKeys.SESSIONS_OPT_IN);
if (!sessionOptIn) {
storage.set(
StorageKeys.SESSIONS_OPT_IN,
{ value: parsedConfig.sessionTrackingEnabled, setDynamically: false });
} else {
// If sessionsOptIn was stored in the URL, it was stored only as a string.
// Parse this value and add it back to storage.
storage.set(
StorageKeys.SESSIONS_OPT_IN,
{ value: (/^true$/i).test(sessionOptIn), setDynamically: true });
}
parsedConfig.noResults && storage.set(StorageKeys.NO_RESULTS_CONFIG, parsedConfig.noResults);
const isSuggestQueryTrigger =
storage.get(StorageKeys.QUERY_TRIGGER) === QueryTriggers.SUGGEST;
if (storage.has(StorageKeys.QUERY) && !isSuggestQueryTrigger) {
storage.set(StorageKeys.QUERY_TRIGGER, QueryTriggers.QUERY_PARAMETER);
}
const context = storage.get(StorageKeys.API_CONTEXT);
if (context && !isValidContext(context)) {
storage.delete(StorageKeys.API_CONTEXT);
console.error(`Context parameter "${context}" is invalid, omitting from the search.`);
}
if (storage.get(StorageKeys.REFERRER_PAGE_URL) === undefined) {
storage.set(
StorageKeys.REFERRER_PAGE_URL,
urlWithoutQueryParamsAndHash(document.referrer)
);
}
this._services = getServices(parsedConfig, storage);
this._eligibleForAnalytics = parsedConfig.businessId != null;
if (this._eligibleForAnalytics) {
this._analyticsReporterService = new AnalyticsReporter(
parsedConfig.experienceKey,
parsedConfig.experienceVersion,
parsedConfig.businessId,
parsedConfig.analyticsEventsEnabled,
parsedConfig.analyticsOptions,
parsedConfig.environment,
parsedConfig.cloudChoice);
this.components.setAnalyticsReporter(this._analyticsReporterService);
}
this.core = new Core({
token: parsedConfig.token,
apiKey: parsedConfig.apiKey,
storage: storage,
experienceKey: parsedConfig.experienceKey,
fieldFormatters: parsedConfig.fieldFormatters,
experienceVersion: parsedConfig.experienceVersion,
locale: parsedConfig.locale,
analyticsReporter: this._analyticsReporterService,
onVerticalSearch: parsedConfig.onVerticalSearch,
onUniversalSearch: parsedConfig.onUniversalSearch,
environment: parsedConfig.environment,
componentManager: this.components,
cloudChoice: parsedConfig.cloudChoice
});
if (parsedConfig.onStateChange && typeof parsedConfig.onStateChange === 'function') {
parsedConfig.onStateChange(
Object.fromEntries(storage.getAll()),
this.core.storage.getCurrentStateUrlMerged());
}
this.components
.setCore(this.core)
.setRenderer(this.renderer);
this._setDefaultInitialSearch(parsedConfig.search);
if (parsedConfig.visitor) {
this.setVisitor(parsedConfig.visitor);
} else {
this.core.init();
}
this._onReady = parsedConfig.onReady || function () {};
this.renderer.init(parsedConfig.templateBundle, this._getInitLocale());
this._handlePonyfillCssVariables(parsedConfig.disableCssVariablesPonyfill)
.finally(() => {
this._onReady();
if (this._analyticsReporterService) {
const impressionEvent = createImpressionEvent({
verticalKey: parsedConfig.search?.verticalKey,
standAlone: true
});
this._analyticsReporterService.report(impressionEvent, { includeQueryId: false });
}
});
}
domReady (cb) {
DOM.onReady(cb);
}
onReady (cb) {
this._onReady = cb;
return this;
}
/**
* Parses the config provided by the user. In the parsed config, any options not supplied by the
* user are given default values.
* @param {Object} config The user supplied config.
*/
parseConfig (config) {
const parsedConfig = Object.assign({}, DEFAULTS, config);
let sessionTrackingEnabled = true;
if (typeof config.sessionTrackingEnabled === 'boolean') {
sessionTrackingEnabled = config.sessionTrackingEnabled;
}
parsedConfig.sessionTrackingEnabled = sessionTrackingEnabled;
if (parsedConfig.apiKey) {
const sandboxPrefix = `${SANDBOX}-`;
if (!config.environment) {
parsedConfig.apiKey.includes(sandboxPrefix)
? parsedConfig.environment = SANDBOX
: parsedConfig.environment = PRODUCTION;
}
parsedConfig.apiKey = parsedConfig.apiKey.replace(sandboxPrefix, '');
}
return parsedConfig;
}
/**
* Validates the Answers config object to ensure things like api key and experience key are
* properly set.
* @param {Object} config The Answers config.
*/
validateConfig (config) {
// TODO (tmeyer): Extract this method into it's own class. Investigate the use of JSON schema
// to validate these configs.
if (typeof config.apiKey !== 'string' && typeof config.token !== 'string') {
throw new Error('Missing required `apiKey` or `token`. Type must be {string}');
}
if (typeof config.apiKey === 'string' && typeof config.token === 'string') {
throw new Error('Both apiKey and token are present. Only one authentication method should be provided');
}
if (typeof config.experienceKey !== 'string') {
throw new Error('Missing required `experienceKey`. Type must be {string}');
}
if (config.onVerticalSearch && typeof config.onVerticalSearch !== 'function') {
throw new Error('onVerticalSearch must be a function. Current type is: ' + typeof config.onVerticalSearch);
}
if (config.onUniversalSearch && typeof config.onUniversalSearch !== 'function') {
throw new Error('onUniversalSearch must be a function. Current type is: ' + typeof config.onUniversalSearch);
}
}
addComponent (type, opts) {
if (typeof opts === 'string') {
opts = {
container: opts
};
}
try {
this.components.create(type, opts).mount();
} catch (e) {
throw new AnswersComponentError('Failed to add component', type, e);
}
return this;
}
/**
* @returns {boolean} Whether analytics events are opted in or out
*/
getAnalyticsOptIn () {
return this._analyticsReporterService?.getAnalyticsOptIn();
}
/**
* Opt in or out of analytic events
* @param {boolean} analyticsEventsEnabled
*/
setAnalyticsOptIn (analyticsEventsEnabled) {
this._analyticsReporterService.setAnalyticsOptIn(analyticsEventsEnabled);
}
/**
* Opt in or out of convertion tracking analytics
* @param {boolean} optIn
*/
setConversionsOptIn (optIn) {
if (this._eligibleForAnalytics) {
this._analyticsReporterService.setConversionTrackingEnabled(optIn);
}
}
/**
* Opt in or out of session cookies
* @param {boolean} optIn
*/
setSessionsOptIn (optIn) {
this.core.storage.set(
StorageKeys.SESSIONS_OPT_IN, { value: optIn, setDynamically: true });
}
/**
* Sets a search query on initialization for vertical searchers that have a
* defaultInitialSearch provided, if the user hasn't already provided their
* own via URL param. A default initial search should not be persisted in the URL,
* so we do a regular set instead of a setWithPersist here.
*
* @param {SearchConfig} searchConfig
* @private
*/
_setDefaultInitialSearch (searchConfig) {
if (searchConfig.defaultInitialSearch == null) {
return;
}
const prepopulatedQuery = this.core.storage.get(StorageKeys.QUERY);
if (prepopulatedQuery != null) {
return;
}
this.core.storage.set(StorageKeys.QUERY_TRIGGER, QueryTriggers.INITIALIZE);
this.core.storage.set(StorageKeys.QUERY, searchConfig.defaultInitialSearch);
}
/**
* Sets the geolocation tag in storage, overriding other inputs. Do not use in conjunction
* with other components that will set the geolocation internally.
* @param {number} lat
* @param {number} long
*/
setGeolocation (lat, lng) {
this.core.storage.set(StorageKeys.GEOLOCATION, {
lat, lng, radius: 0
});
}
/*
* Adds context as a parameter for the query API calls.
* @param {Object} context The context object passed in the API calls
*/
setContext (context) {
const contextString = JSON.stringify(context);
if (!isValidContext(contextString)) {
console.error(`Context parameter "${context}" is invalid, omitting from the search.`);
return;
}
this.core.storage.set(StorageKeys.API_CONTEXT, contextString);
}
/**
* Gets the locale that ANSWERS was initialized to
*
* @returns {string}
*/
_getInitLocale () {
return this.core.storage.get(StorageKeys.LOCALE);
}
/**
* A promise that resolves when ponyfillCssVariables resolves,
* or resolves immediately if ponyfill is disabled
* @param {boolean} option to opt out of the css variables ponyfill
* @return {Promise} resolves after ponyfillCssVariables, or immediately if disabled
*/
_handlePonyfillCssVariables (ponyfillDisabled) {
if (ponyfillDisabled) {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
this.ponyfillCssVariables({
onFinally: () => {
resolve();
}
});
});
}
/*
* Updates the css styles with new current variables. This is useful when the css
* variables are updated dynamically (e.g. through js) or if the css variables are
* added after the ANSWERS.init
*
* To solve issues with non-zero max-age cache controls for link/script assets in IE11,
* we add a cache busting parameter so that XMLHttpRequests succeed.
*
* @param {Object} config Additional config to pass to the ponyfill
*/
ponyfillCssVariables (config = {}) {
cssVars({
onlyLegacy: true,
onError: config.onError || function () {},
onSuccess: config.onSuccess || function () {},
onFinally: config.onFinally || function () {},
onBeforeSend: (xhr, node, url) => {
try {
const uriWithCacheBust = new URL(url);
const params = new SearchParams(uriWithCacheBust.search);
params.set('_', new Date().getTime());
uriWithCacheBust.search = params.toString();
xhr.open('GET', uriWithCacheBust.toString());
} catch (e) {
// Catch the error and continue if the URL provided in the asset is not a valid URL
}
}
});
}
/**
* Parses a value from persistent storage, which stores strings,
* into the shape the SDK expects.
* TODO(SLAP-1111): Move this into a dedicated file/class.
*
* @param {string} key
* @param {string} value
* @returns {string|number|Filter}
*/
_parsePersistentStorageValue (key, value) {
switch (key) {
case StorageKeys.PERSISTED_FILTER:
return Filter.from(JSON.parse(value));
case StorageKeys.PERSISTED_LOCATION_RADIUS:
return parseFloat(value);
case StorageKeys.PERSISTED_FACETS:
case StorageKeys.SORT_BYS:
return JSON.parse(value);
default:
return value;
}
}
setVisitor (visitor) {
if (visitor.id) {
this._analyticsReporterService?.setVisitor(visitor);
this.core.init({ visitor: visitor });
} else {
console.error(`Invalid visitor. Visitor was not set because "${JSON.stringify(visitor)}" does not have an id.`);
}
}
}
/**
* @param {Object} config
* @param {Storage} storage
* @returns {Services}
*/
function getServices (config, storage) {
return {
errorReporterService: new ErrorReporter(
{
apiKey: config.apiKey,
experienceKey: config.experienceKey,
experienceVersion: config.experienceVersion,
printVerbose: config.debug,
sendToServer: !config.suppressErrorReports,
environment: config.environment
},
storage)
};
}
const ANSWERS = new AnswersSearchBar();
export default ANSWERS;