@algolia/autocomplete-js 1.11.1 → 1.12.1

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.
@@ -1,4 +1,4 @@
1
- /*! @algolia/autocomplete-js 1.11.1 | MIT License | © Algolia, Inc. and contributors | https://github.com/algolia/autocomplete */
1
+ /*! @algolia/autocomplete-js 1.12.1 | MIT License | © Algolia, Inc. and contributors | https://github.com/algolia/autocomplete */
2
2
  (function (global, factory) {
3
3
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
4
4
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
@@ -347,7 +347,7 @@
347
347
  return undefined;
348
348
  }
349
349
 
350
- var version = '1.11.1';
350
+ var version = '1.12.1';
351
351
 
352
352
  var userAgents$1 = [{
353
353
  segment: 'autocomplete-core',
@@ -880,7 +880,8 @@
880
880
  providedInsightsClient = _getOptions.insightsClient,
881
881
  onItemsChange = _getOptions.onItemsChange,
882
882
  onSelectEvent = _getOptions.onSelect,
883
- onActiveEvent = _getOptions.onActive;
883
+ onActiveEvent = _getOptions.onActive,
884
+ __autocomplete_clickAnalytics = _getOptions.__autocomplete_clickAnalytics;
884
885
  var insightsClient = providedInsightsClient;
885
886
  if (!providedInsightsClient) {
886
887
  safelyRunOnBrowser(function (_ref2) {
@@ -943,9 +944,9 @@
943
944
  function setInsightsContext(userToken) {
944
945
  setContext({
945
946
  algoliaInsightsPlugin: {
946
- __algoliaSearchParameters: _objectSpread$f({
947
+ __algoliaSearchParameters: _objectSpread$f(_objectSpread$f({}, __autocomplete_clickAnalytics ? {
947
948
  clickAnalytics: true
948
- }, userToken ? {
949
+ } : {}), userToken ? {
949
950
  userToken: userToken
950
951
  } : {}),
951
952
  insights: insights
@@ -1010,27 +1011,36 @@
1010
1011
  __autocomplete_pluginOptions: options
1011
1012
  };
1012
1013
  }
1014
+ function getAlgoliaSources() {
1015
+ var _context$algoliaInsig;
1016
+ var algoliaSourceBase = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
1017
+ var context = arguments.length > 1 ? arguments[1] : undefined;
1018
+ return [].concat(_toConsumableArray$5(algoliaSourceBase), ['autocomplete-internal'], _toConsumableArray$5((_context$algoliaInsig = context.algoliaInsightsPlugin) !== null && _context$algoliaInsig !== void 0 && _context$algoliaInsig.__automaticInsights ? ['autocomplete-automatic'] : []));
1019
+ }
1013
1020
  function getOptions(options) {
1014
1021
  return _objectSpread$f({
1015
1022
  onItemsChange: function onItemsChange(_ref8) {
1016
1023
  var insights = _ref8.insights,
1017
- insightsEvents = _ref8.insightsEvents;
1024
+ insightsEvents = _ref8.insightsEvents,
1025
+ state = _ref8.state;
1018
1026
  insights.viewedObjectIDs.apply(insights, _toConsumableArray$5(insightsEvents.map(function (event) {
1019
1027
  return _objectSpread$f(_objectSpread$f({}, event), {}, {
1020
- algoliaSource: [].concat(_toConsumableArray$5(event.algoliaSource || []), ['autocomplete-internal'])
1028
+ algoliaSource: getAlgoliaSources(event.algoliaSource, state.context)
1021
1029
  });
1022
1030
  })));
1023
1031
  },
1024
1032
  onSelect: function onSelect(_ref9) {
1025
1033
  var insights = _ref9.insights,
1026
- insightsEvents = _ref9.insightsEvents;
1034
+ insightsEvents = _ref9.insightsEvents,
1035
+ state = _ref9.state;
1027
1036
  insights.clickedObjectIDsAfterSearch.apply(insights, _toConsumableArray$5(insightsEvents.map(function (event) {
1028
1037
  return _objectSpread$f(_objectSpread$f({}, event), {}, {
1029
- algoliaSource: [].concat(_toConsumableArray$5(event.algoliaSource || []), ['autocomplete-internal'])
1038
+ algoliaSource: getAlgoliaSources(event.algoliaSource, state.context)
1030
1039
  });
1031
1040
  })));
1032
1041
  },
1033
- onActive: noop
1042
+ onActive: noop,
1043
+ __autocomplete_clickAnalytics: true
1034
1044
  }, options);
1035
1045
  }
1036
1046
  function loadInsights(environment) {
@@ -1691,7 +1701,7 @@
1691
1701
  autoFocus: false,
1692
1702
  defaultActiveItemId: null,
1693
1703
  stallThreshold: 300,
1694
- insights: false,
1704
+ insights: undefined,
1695
1705
  environment: environment,
1696
1706
  shouldPanelOpen: function shouldPanelOpen(_ref) {
1697
1707
  var state = _ref.state;
@@ -2178,7 +2188,8 @@
2178
2188
  setIsOpen = setters.setIsOpen,
2179
2189
  setQuery = setters.setQuery,
2180
2190
  setActiveItemId = setters.setActiveItemId,
2181
- setStatus = setters.setStatus;
2191
+ setStatus = setters.setStatus,
2192
+ setContext = setters.setContext;
2182
2193
  setQuery(query);
2183
2194
  setActiveItemId(props.defaultActiveItemId);
2184
2195
  if (!query && props.openOnFocus === false) {
@@ -2228,6 +2239,20 @@
2228
2239
  return preResolve(itemsOrDescription, source.sourceId, store.getState());
2229
2240
  });
2230
2241
  })).then(resolve).then(function (responses) {
2242
+ var __automaticInsights = responses.some(function (_ref2) {
2243
+ var items = _ref2.items;
2244
+ return isSearchResponseWithAutomaticInsightsFlag(items);
2245
+ });
2246
+
2247
+ // No need to pollute the context if `__automaticInsights=false`
2248
+ if (__automaticInsights) {
2249
+ var _store$getState$conte;
2250
+ setContext({
2251
+ algoliaInsightsPlugin: _objectSpread$8(_objectSpread$8({}, ((_store$getState$conte = store.getState().context) === null || _store$getState$conte === void 0 ? void 0 : _store$getState$conte.algoliaInsightsPlugin) || {}), {}, {
2252
+ __automaticInsights: __automaticInsights
2253
+ })
2254
+ });
2255
+ }
2231
2256
  return postResolve(responses, sources, store);
2232
2257
  }).then(function (collections) {
2233
2258
  return reshape({
@@ -2273,6 +2298,9 @@
2273
2298
  });
2274
2299
  return store.pendingRequests.add(request);
2275
2300
  }
2301
+ function isSearchResponseWithAutomaticInsightsFlag(items) {
2302
+ return !Array.isArray(items) && Boolean(items === null || items === void 0 ? void 0 : items._automaticInsights);
2303
+ }
2276
2304
 
2277
2305
  function _typeof$7(obj) {
2278
2306
  "@babel/helpers - typeof";
@@ -2439,7 +2467,7 @@
2439
2467
  } else if (event.key === 'Tab') {
2440
2468
  store.dispatch('blur', null);
2441
2469
 
2442
- // Hitting the `Escape` key signals the end of a user interaction with the
2470
+ // Hitting the `Tab` key signals the end of a user interaction with the
2443
2471
  // autocomplete. At this point, we should ignore any requests that are still
2444
2472
  // pending and could reopen the panel once they resolve, because that would
2445
2473
  // result in an unsolicited UI behavior.
@@ -3192,6 +3220,7 @@
3192
3220
  // we lose track of the highlighted index. (Query-suggestions use-case)
3193
3221
  action.props.openOnFocus === true ? action.props.defaultActiveItemId : null,
3194
3222
  status: 'idle',
3223
+ completion: null,
3195
3224
  query: ''
3196
3225
  });
3197
3226
  }
@@ -3305,6 +3334,7 @@
3305
3334
  navigator: props.navigator
3306
3335
  }, setters));
3307
3336
  function onStoreStateChange(_ref) {
3337
+ var _state$context, _state$context$algoli;
3308
3338
  var prevState = _ref.prevState,
3309
3339
  state = _ref.state;
3310
3340
  props.onStateChange(_objectSpread$3({
@@ -3313,6 +3343,13 @@
3313
3343
  refresh: refresh,
3314
3344
  navigator: props.navigator
3315
3345
  }, setters));
3346
+ if (!isAlgoliaInsightsPluginEnabled() && (_state$context = state.context) !== null && _state$context !== void 0 && (_state$context$algoli = _state$context.algoliaInsightsPlugin) !== null && _state$context$algoli !== void 0 && _state$context$algoli.__automaticInsights && props.insights !== false) {
3347
+ var plugin = createAlgoliaInsightsPlugin({
3348
+ __autocomplete_clickAnalytics: false
3349
+ });
3350
+ props.plugins.push(plugin);
3351
+ subscribePlugins([plugin]);
3352
+ }
3316
3353
  }
3317
3354
  function refresh() {
3318
3355
  return onInput(_objectSpread$3({
@@ -3327,34 +3364,40 @@
3327
3364
  store: store
3328
3365
  }, setters));
3329
3366
  }
3330
- if (options.insights && !props.plugins.some(function (plugin) {
3331
- return plugin.name === 'aa.algoliaInsightsPlugin';
3332
- })) {
3333
- var insightsParams = typeof options.insights === 'boolean' ? {} : options.insights;
3367
+ function subscribePlugins(plugins) {
3368
+ plugins.forEach(function (plugin) {
3369
+ var _plugin$subscribe;
3370
+ return (_plugin$subscribe = plugin.subscribe) === null || _plugin$subscribe === void 0 ? void 0 : _plugin$subscribe.call(plugin, _objectSpread$3(_objectSpread$3({}, setters), {}, {
3371
+ navigator: props.navigator,
3372
+ refresh: refresh,
3373
+ onSelect: function onSelect(fn) {
3374
+ subscribers.push({
3375
+ onSelect: fn
3376
+ });
3377
+ },
3378
+ onActive: function onActive(fn) {
3379
+ subscribers.push({
3380
+ onActive: fn
3381
+ });
3382
+ },
3383
+ onResolve: function onResolve(fn) {
3384
+ subscribers.push({
3385
+ onResolve: fn
3386
+ });
3387
+ }
3388
+ }));
3389
+ });
3390
+ }
3391
+ function isAlgoliaInsightsPluginEnabled() {
3392
+ return props.plugins.some(function (plugin) {
3393
+ return plugin.name === 'aa.algoliaInsightsPlugin';
3394
+ });
3395
+ }
3396
+ if (props.insights && !isAlgoliaInsightsPluginEnabled()) {
3397
+ var insightsParams = typeof props.insights === 'boolean' ? {} : props.insights;
3334
3398
  props.plugins.push(createAlgoliaInsightsPlugin(insightsParams));
3335
3399
  }
3336
- props.plugins.forEach(function (plugin) {
3337
- var _plugin$subscribe;
3338
- return (_plugin$subscribe = plugin.subscribe) === null || _plugin$subscribe === void 0 ? void 0 : _plugin$subscribe.call(plugin, _objectSpread$3(_objectSpread$3({}, setters), {}, {
3339
- navigator: props.navigator,
3340
- refresh: refresh,
3341
- onSelect: function onSelect(fn) {
3342
- subscribers.push({
3343
- onSelect: fn
3344
- });
3345
- },
3346
- onActive: function onActive(fn) {
3347
- subscribers.push({
3348
- onActive: fn
3349
- });
3350
- },
3351
- onResolve: function onResolve(fn) {
3352
- subscribers.push({
3353
- onResolve: fn
3354
- });
3355
- }
3356
- }));
3357
- });
3400
+ subscribePlugins(props.plugins);
3358
3401
  injectMetadata({
3359
3402
  metadata: getMetadata({
3360
3403
  plugins: props.plugins,