@algolia/autocomplete-js 1.11.1 → 1.12.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.
@@ -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.0 | 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.0';
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";
@@ -3305,6 +3333,7 @@
3305
3333
  navigator: props.navigator
3306
3334
  }, setters));
3307
3335
  function onStoreStateChange(_ref) {
3336
+ var _state$context, _state$context$algoli;
3308
3337
  var prevState = _ref.prevState,
3309
3338
  state = _ref.state;
3310
3339
  props.onStateChange(_objectSpread$3({
@@ -3313,6 +3342,13 @@
3313
3342
  refresh: refresh,
3314
3343
  navigator: props.navigator
3315
3344
  }, setters));
3345
+ 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) {
3346
+ var plugin = createAlgoliaInsightsPlugin({
3347
+ __autocomplete_clickAnalytics: false
3348
+ });
3349
+ props.plugins.push(plugin);
3350
+ subscribePlugins([plugin]);
3351
+ }
3316
3352
  }
3317
3353
  function refresh() {
3318
3354
  return onInput(_objectSpread$3({
@@ -3327,34 +3363,40 @@
3327
3363
  store: store
3328
3364
  }, setters));
3329
3365
  }
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;
3366
+ function subscribePlugins(plugins) {
3367
+ plugins.forEach(function (plugin) {
3368
+ var _plugin$subscribe;
3369
+ return (_plugin$subscribe = plugin.subscribe) === null || _plugin$subscribe === void 0 ? void 0 : _plugin$subscribe.call(plugin, _objectSpread$3(_objectSpread$3({}, setters), {}, {
3370
+ navigator: props.navigator,
3371
+ refresh: refresh,
3372
+ onSelect: function onSelect(fn) {
3373
+ subscribers.push({
3374
+ onSelect: fn
3375
+ });
3376
+ },
3377
+ onActive: function onActive(fn) {
3378
+ subscribers.push({
3379
+ onActive: fn
3380
+ });
3381
+ },
3382
+ onResolve: function onResolve(fn) {
3383
+ subscribers.push({
3384
+ onResolve: fn
3385
+ });
3386
+ }
3387
+ }));
3388
+ });
3389
+ }
3390
+ function isAlgoliaInsightsPluginEnabled() {
3391
+ return props.plugins.some(function (plugin) {
3392
+ return plugin.name === 'aa.algoliaInsightsPlugin';
3393
+ });
3394
+ }
3395
+ if (props.insights && !isAlgoliaInsightsPluginEnabled()) {
3396
+ var insightsParams = typeof props.insights === 'boolean' ? {} : props.insights;
3334
3397
  props.plugins.push(createAlgoliaInsightsPlugin(insightsParams));
3335
3398
  }
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
- });
3399
+ subscribePlugins(props.plugins);
3358
3400
  injectMetadata({
3359
3401
  metadata: getMetadata({
3360
3402
  plugins: props.plugins,