@algolia/autocomplete-js 1.11.0 → 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.
@@ -101,13 +101,12 @@ export function renderPanel(render, _ref2) {
101
101
  }, propGetters.getListProps(_objectSpread({
102
102
  state: state,
103
103
  props: autocomplete.getListProps({
104
- sourceIndex: sourceIndex
104
+ source: source
105
105
  })
106
106
  }, autocompleteScopeApi))), items.map(function (item) {
107
107
  var itemProps = autocomplete.getItemProps({
108
108
  item: item,
109
- source: source,
110
- sourceIndex: sourceIndex
109
+ source: source
111
110
  });
112
111
  return renderer.createElement("li", _extends({
113
112
  key: itemProps.id,
@@ -1,4 +1,4 @@
1
- /*! @algolia/autocomplete-js 1.11.0 | 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.0';
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) {
@@ -1370,6 +1380,17 @@
1370
1380
  };
1371
1381
  }
1372
1382
 
1383
+ /**
1384
+ * Returns a full element id for an autocomplete element.
1385
+ *
1386
+ * @param autocompleteInstanceId The id of the autocomplete instance
1387
+ * @param elementId The specific element id
1388
+ * @param source The source of the element, when it needs to be scoped
1389
+ */
1390
+ function getAutocompleteElementId(autocompleteInstanceId, elementId, source) {
1391
+ return [autocompleteInstanceId, source === null || source === void 0 ? void 0 : source.sourceId, elementId].filter(Boolean).join('-').replace(/\s/g, '');
1392
+ }
1393
+
1373
1394
  function isOrContainsNode(parent, child) {
1374
1395
  return parent === child || parent.contains(child);
1375
1396
  }
@@ -1680,7 +1701,7 @@
1680
1701
  autoFocus: false,
1681
1702
  defaultActiveItemId: null,
1682
1703
  stallThreshold: 300,
1683
- insights: false,
1704
+ insights: undefined,
1684
1705
  environment: environment,
1685
1706
  shouldPanelOpen: function shouldPanelOpen(_ref) {
1686
1707
  var state = _ref.state;
@@ -2167,7 +2188,8 @@
2167
2188
  setIsOpen = setters.setIsOpen,
2168
2189
  setQuery = setters.setQuery,
2169
2190
  setActiveItemId = setters.setActiveItemId,
2170
- setStatus = setters.setStatus;
2191
+ setStatus = setters.setStatus,
2192
+ setContext = setters.setContext;
2171
2193
  setQuery(query);
2172
2194
  setActiveItemId(props.defaultActiveItemId);
2173
2195
  if (!query && props.openOnFocus === false) {
@@ -2217,6 +2239,20 @@
2217
2239
  return preResolve(itemsOrDescription, source.sourceId, store.getState());
2218
2240
  });
2219
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
+ }
2220
2256
  return postResolve(responses, sources, store);
2221
2257
  }).then(function (collections) {
2222
2258
  return reshape({
@@ -2262,6 +2298,9 @@
2262
2298
  });
2263
2299
  return store.pendingRequests.add(request);
2264
2300
  }
2301
+ function isSearchResponseWithAutomaticInsightsFlag(items) {
2302
+ return !Array.isArray(items) && Boolean(items === null || items === void 0 ? void 0 : items._automaticInsights);
2303
+ }
2265
2304
 
2266
2305
  function _typeof$7(obj) {
2267
2306
  "@babel/helpers - typeof";
@@ -2358,7 +2397,8 @@
2358
2397
  if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
2359
2398
  // eslint-disable-next-line no-inner-declarations
2360
2399
  var triggerScrollIntoView = function triggerScrollIntoView() {
2361
- var nodeItem = props.environment.document.getElementById("".concat(props.id, "-item-").concat(store.getState().activeItemId));
2400
+ var highlightedItem = getActiveItem(store.getState());
2401
+ var nodeItem = props.environment.document.getElementById(getAutocompleteElementId(props.id, "item-".concat(store.getState().activeItemId), highlightedItem === null || highlightedItem === void 0 ? void 0 : highlightedItem.source));
2362
2402
  if (nodeItem) {
2363
2403
  if (nodeItem.scrollIntoViewIfNeeded) {
2364
2404
  nodeItem.scrollIntoViewIfNeeded(false);
@@ -2545,9 +2585,8 @@
2545
2585
  _excluded2 = ["inputElement", "formElement", "panelElement"],
2546
2586
  _excluded3 = ["inputElement"],
2547
2587
  _excluded4 = ["inputElement", "maxLength"],
2548
- _excluded5 = ["sourceIndex"],
2549
- _excluded6 = ["sourceIndex"],
2550
- _excluded7 = ["item", "source", "sourceIndex"];
2588
+ _excluded5 = ["source"],
2589
+ _excluded6 = ["item", "source"];
2551
2590
  function ownKeys$6(object, enumerableOnly) {
2552
2591
  var keys = Object.keys(object);
2553
2592
  if (Object.getOwnPropertySymbols) {
@@ -2692,8 +2731,11 @@
2692
2731
  role: 'combobox',
2693
2732
  'aria-expanded': store.getState().isOpen,
2694
2733
  'aria-haspopup': 'listbox',
2695
- 'aria-owns': store.getState().isOpen ? "".concat(props.id, "-list") : undefined,
2696
- 'aria-labelledby': "".concat(props.id, "-label")
2734
+ 'aria-owns': store.getState().isOpen ? store.getState().collections.map(function (_ref2) {
2735
+ var source = _ref2.source;
2736
+ return getAutocompleteElementId(props.id, 'list', source);
2737
+ }).join(' ') : undefined,
2738
+ 'aria-labelledby': getAutocompleteElementId(props.id, 'label')
2697
2739
  }, rest);
2698
2740
  };
2699
2741
  var getFormProps = function getFormProps(providedProps) {
@@ -2743,22 +2785,25 @@
2743
2785
  }
2744
2786
  store.dispatch('focus', null);
2745
2787
  }
2746
- var _ref2 = providedProps || {};
2747
- _ref2.inputElement;
2748
- var _ref2$maxLength = _ref2.maxLength,
2749
- maxLength = _ref2$maxLength === void 0 ? 512 : _ref2$maxLength,
2750
- rest = _objectWithoutProperties$1(_ref2, _excluded4);
2788
+ var _ref3 = providedProps || {};
2789
+ _ref3.inputElement;
2790
+ var _ref3$maxLength = _ref3.maxLength,
2791
+ maxLength = _ref3$maxLength === void 0 ? 512 : _ref3$maxLength,
2792
+ rest = _objectWithoutProperties$1(_ref3, _excluded4);
2751
2793
  var activeItem = getActiveItem(store.getState());
2752
2794
  var userAgent = ((_props$environment$na = props.environment.navigator) === null || _props$environment$na === void 0 ? void 0 : _props$environment$na.userAgent) || '';
2753
2795
  var shouldFallbackKeyHint = isSamsung(userAgent);
2754
2796
  var enterKeyHint = props.enterKeyHint || (activeItem !== null && activeItem !== void 0 && activeItem.itemUrl && !shouldFallbackKeyHint ? 'go' : 'search');
2755
2797
  return _objectSpread$6({
2756
2798
  'aria-autocomplete': 'both',
2757
- 'aria-activedescendant': store.getState().isOpen && store.getState().activeItemId !== null ? "".concat(props.id, "-item-").concat(store.getState().activeItemId) : undefined,
2758
- 'aria-controls': store.getState().isOpen ? "".concat(props.id, "-list") : undefined,
2759
- 'aria-labelledby': "".concat(props.id, "-label"),
2799
+ 'aria-activedescendant': store.getState().isOpen && store.getState().activeItemId !== null ? getAutocompleteElementId(props.id, "item-".concat(store.getState().activeItemId), activeItem === null || activeItem === void 0 ? void 0 : activeItem.source) : undefined,
2800
+ 'aria-controls': store.getState().isOpen ? store.getState().collections.map(function (_ref4) {
2801
+ var source = _ref4.source;
2802
+ return getAutocompleteElementId(props.id, 'list', source);
2803
+ }).join(' ') : undefined,
2804
+ 'aria-labelledby': getAutocompleteElementId(props.id, 'label'),
2760
2805
  value: store.getState().completion || store.getState().query,
2761
- id: "".concat(props.id, "-input"),
2806
+ id: getAutocompleteElementId(props.id, 'input'),
2762
2807
  autoComplete: 'off',
2763
2808
  autoCorrect: 'off',
2764
2809
  autoCapitalize: 'off',
@@ -2804,26 +2849,20 @@
2804
2849
  }
2805
2850
  }, rest);
2806
2851
  };
2807
- var getAutocompleteId = function getAutocompleteId(instanceId, sourceId) {
2808
- return typeof sourceId !== 'undefined' ? "".concat(instanceId, "-").concat(sourceId) : instanceId;
2809
- };
2810
- var getLabelProps = function getLabelProps(providedProps) {
2811
- var _ref3 = providedProps || {},
2812
- sourceIndex = _ref3.sourceIndex,
2813
- rest = _objectWithoutProperties$1(_ref3, _excluded5);
2852
+ var getLabelProps = function getLabelProps(rest) {
2814
2853
  return _objectSpread$6({
2815
- htmlFor: "".concat(getAutocompleteId(props.id, sourceIndex), "-input"),
2816
- id: "".concat(getAutocompleteId(props.id, sourceIndex), "-label")
2854
+ htmlFor: getAutocompleteElementId(props.id, 'input'),
2855
+ id: getAutocompleteElementId(props.id, 'label')
2817
2856
  }, rest);
2818
2857
  };
2819
2858
  var getListProps = function getListProps(providedProps) {
2820
- var _ref4 = providedProps || {},
2821
- sourceIndex = _ref4.sourceIndex,
2822
- rest = _objectWithoutProperties$1(_ref4, _excluded6);
2859
+ var _ref5 = providedProps || {},
2860
+ source = _ref5.source,
2861
+ rest = _objectWithoutProperties$1(_ref5, _excluded5);
2823
2862
  return _objectSpread$6({
2824
2863
  role: 'listbox',
2825
- 'aria-labelledby': "".concat(getAutocompleteId(props.id, sourceIndex), "-label"),
2826
- id: "".concat(getAutocompleteId(props.id, sourceIndex), "-list")
2864
+ 'aria-labelledby': getAutocompleteElementId(props.id, 'label'),
2865
+ id: getAutocompleteElementId(props.id, 'list', source)
2827
2866
  }, rest);
2828
2867
  };
2829
2868
  var getPanelProps = function getPanelProps(rest) {
@@ -2842,10 +2881,9 @@
2842
2881
  var getItemProps = function getItemProps(providedProps) {
2843
2882
  var item = providedProps.item,
2844
2883
  source = providedProps.source,
2845
- sourceIndex = providedProps.sourceIndex,
2846
- rest = _objectWithoutProperties$1(providedProps, _excluded7);
2884
+ rest = _objectWithoutProperties$1(providedProps, _excluded6);
2847
2885
  return _objectSpread$6({
2848
- id: "".concat(getAutocompleteId(props.id, sourceIndex), "-item-").concat(item.__autocomplete_id),
2886
+ id: getAutocompleteElementId(props.id, "item-".concat(item.__autocomplete_id), source),
2849
2887
  role: 'option',
2850
2888
  'aria-selected': store.getState().activeItemId === item.__autocomplete_id,
2851
2889
  onMouseMove: function onMouseMove(event) {
@@ -3295,6 +3333,7 @@
3295
3333
  navigator: props.navigator
3296
3334
  }, setters));
3297
3335
  function onStoreStateChange(_ref) {
3336
+ var _state$context, _state$context$algoli;
3298
3337
  var prevState = _ref.prevState,
3299
3338
  state = _ref.state;
3300
3339
  props.onStateChange(_objectSpread$3({
@@ -3303,6 +3342,13 @@
3303
3342
  refresh: refresh,
3304
3343
  navigator: props.navigator
3305
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
+ }
3306
3352
  }
3307
3353
  function refresh() {
3308
3354
  return onInput(_objectSpread$3({
@@ -3317,34 +3363,40 @@
3317
3363
  store: store
3318
3364
  }, setters));
3319
3365
  }
3320
- if (options.insights && !props.plugins.some(function (plugin) {
3321
- return plugin.name === 'aa.algoliaInsightsPlugin';
3322
- })) {
3323
- 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;
3324
3397
  props.plugins.push(createAlgoliaInsightsPlugin(insightsParams));
3325
3398
  }
3326
- props.plugins.forEach(function (plugin) {
3327
- var _plugin$subscribe;
3328
- return (_plugin$subscribe = plugin.subscribe) === null || _plugin$subscribe === void 0 ? void 0 : _plugin$subscribe.call(plugin, _objectSpread$3(_objectSpread$3({}, setters), {}, {
3329
- navigator: props.navigator,
3330
- refresh: refresh,
3331
- onSelect: function onSelect(fn) {
3332
- subscribers.push({
3333
- onSelect: fn
3334
- });
3335
- },
3336
- onActive: function onActive(fn) {
3337
- subscribers.push({
3338
- onActive: fn
3339
- });
3340
- },
3341
- onResolve: function onResolve(fn) {
3342
- subscribers.push({
3343
- onResolve: fn
3344
- });
3345
- }
3346
- }));
3347
- });
3399
+ subscribePlugins(props.plugins);
3348
3400
  injectMetadata({
3349
3401
  metadata: getMetadata({
3350
3402
  plugins: props.plugins,
@@ -4705,13 +4757,12 @@
4705
4757
  }, propGetters.getListProps(_objectSpread2({
4706
4758
  state: state,
4707
4759
  props: autocomplete.getListProps({
4708
- sourceIndex: sourceIndex
4760
+ source: source
4709
4761
  })
4710
4762
  }, autocompleteScopeApi))), items.map(function (item) {
4711
4763
  var itemProps = autocomplete.getItemProps({
4712
4764
  item: item,
4713
- source: source,
4714
- sourceIndex: sourceIndex
4765
+ source: source
4715
4766
  });
4716
4767
  return renderer.createElement("li", _extends({
4717
4768
  key: itemProps.id,