@algolia/autocomplete-core 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.
@@ -1,4 +1,4 @@
1
- /*! @algolia/autocomplete-core 1.11.0 | MIT License | © Algolia, Inc. and contributors | https://github.com/algolia/autocomplete */
1
+ /*! @algolia/autocomplete-core 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) :
@@ -291,7 +291,7 @@
291
291
  return undefined;
292
292
  }
293
293
 
294
- var version = '1.11.0';
294
+ var version = '1.12.0';
295
295
 
296
296
  var userAgents = [{
297
297
  segment: 'autocomplete-core',
@@ -824,7 +824,8 @@
824
824
  providedInsightsClient = _getOptions.insightsClient,
825
825
  onItemsChange = _getOptions.onItemsChange,
826
826
  onSelectEvent = _getOptions.onSelect,
827
- onActiveEvent = _getOptions.onActive;
827
+ onActiveEvent = _getOptions.onActive,
828
+ __autocomplete_clickAnalytics = _getOptions.__autocomplete_clickAnalytics;
828
829
  var insightsClient = providedInsightsClient;
829
830
  if (!providedInsightsClient) {
830
831
  safelyRunOnBrowser(function (_ref2) {
@@ -887,9 +888,9 @@
887
888
  function setInsightsContext(userToken) {
888
889
  setContext({
889
890
  algoliaInsightsPlugin: {
890
- __algoliaSearchParameters: _objectSpread({
891
+ __algoliaSearchParameters: _objectSpread(_objectSpread({}, __autocomplete_clickAnalytics ? {
891
892
  clickAnalytics: true
892
- }, userToken ? {
893
+ } : {}), userToken ? {
893
894
  userToken: userToken
894
895
  } : {}),
895
896
  insights: insights
@@ -954,27 +955,36 @@
954
955
  __autocomplete_pluginOptions: options
955
956
  };
956
957
  }
958
+ function getAlgoliaSources() {
959
+ var _context$algoliaInsig;
960
+ var algoliaSourceBase = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
961
+ var context = arguments.length > 1 ? arguments[1] : undefined;
962
+ return [].concat(_toConsumableArray(algoliaSourceBase), ['autocomplete-internal'], _toConsumableArray((_context$algoliaInsig = context.algoliaInsightsPlugin) !== null && _context$algoliaInsig !== void 0 && _context$algoliaInsig.__automaticInsights ? ['autocomplete-automatic'] : []));
963
+ }
957
964
  function getOptions(options) {
958
965
  return _objectSpread({
959
966
  onItemsChange: function onItemsChange(_ref8) {
960
967
  var insights = _ref8.insights,
961
- insightsEvents = _ref8.insightsEvents;
968
+ insightsEvents = _ref8.insightsEvents,
969
+ state = _ref8.state;
962
970
  insights.viewedObjectIDs.apply(insights, _toConsumableArray(insightsEvents.map(function (event) {
963
971
  return _objectSpread(_objectSpread({}, event), {}, {
964
- algoliaSource: [].concat(_toConsumableArray(event.algoliaSource || []), ['autocomplete-internal'])
972
+ algoliaSource: getAlgoliaSources(event.algoliaSource, state.context)
965
973
  });
966
974
  })));
967
975
  },
968
976
  onSelect: function onSelect(_ref9) {
969
977
  var insights = _ref9.insights,
970
- insightsEvents = _ref9.insightsEvents;
978
+ insightsEvents = _ref9.insightsEvents,
979
+ state = _ref9.state;
971
980
  insights.clickedObjectIDsAfterSearch.apply(insights, _toConsumableArray(insightsEvents.map(function (event) {
972
981
  return _objectSpread(_objectSpread({}, event), {}, {
973
- algoliaSource: [].concat(_toConsumableArray(event.algoliaSource || []), ['autocomplete-internal'])
982
+ algoliaSource: getAlgoliaSources(event.algoliaSource, state.context)
974
983
  });
975
984
  })));
976
985
  },
977
- onActive: noop
986
+ onActive: noop,
987
+ __autocomplete_clickAnalytics: true
978
988
  }, options);
979
989
  }
980
990
  function loadInsights(environment) {
@@ -1256,6 +1266,17 @@
1256
1266
  };
1257
1267
  }
1258
1268
 
1269
+ /**
1270
+ * Returns a full element id for an autocomplete element.
1271
+ *
1272
+ * @param autocompleteInstanceId The id of the autocomplete instance
1273
+ * @param elementId The specific element id
1274
+ * @param source The source of the element, when it needs to be scoped
1275
+ */
1276
+ function getAutocompleteElementId(autocompleteInstanceId, elementId, source) {
1277
+ return [autocompleteInstanceId, source === null || source === void 0 ? void 0 : source.sourceId, elementId].filter(Boolean).join('-').replace(/\s/g, '');
1278
+ }
1279
+
1259
1280
  function isOrContainsNode(parent, child) {
1260
1281
  return parent === child || parent.contains(child);
1261
1282
  }
@@ -1367,7 +1388,7 @@
1367
1388
  autoFocus: false,
1368
1389
  defaultActiveItemId: null,
1369
1390
  stallThreshold: 300,
1370
- insights: false,
1391
+ insights: undefined,
1371
1392
  environment: environment,
1372
1393
  shouldPanelOpen: function shouldPanelOpen(_ref) {
1373
1394
  var state = _ref.state;
@@ -1628,7 +1649,8 @@
1628
1649
  setIsOpen = setters.setIsOpen,
1629
1650
  setQuery = setters.setQuery,
1630
1651
  setActiveItemId = setters.setActiveItemId,
1631
- setStatus = setters.setStatus;
1652
+ setStatus = setters.setStatus,
1653
+ setContext = setters.setContext;
1632
1654
  setQuery(query);
1633
1655
  setActiveItemId(props.defaultActiveItemId);
1634
1656
  if (!query && props.openOnFocus === false) {
@@ -1678,6 +1700,20 @@
1678
1700
  return preResolve(itemsOrDescription, source.sourceId, store.getState());
1679
1701
  });
1680
1702
  })).then(resolve).then(function (responses) {
1703
+ var __automaticInsights = responses.some(function (_ref2) {
1704
+ var items = _ref2.items;
1705
+ return isSearchResponseWithAutomaticInsightsFlag(items);
1706
+ });
1707
+
1708
+ // No need to pollute the context if `__automaticInsights=false`
1709
+ if (__automaticInsights) {
1710
+ var _store$getState$conte;
1711
+ setContext({
1712
+ algoliaInsightsPlugin: _objectSpread2(_objectSpread2({}, ((_store$getState$conte = store.getState().context) === null || _store$getState$conte === void 0 ? void 0 : _store$getState$conte.algoliaInsightsPlugin) || {}), {}, {
1713
+ __automaticInsights: __automaticInsights
1714
+ })
1715
+ });
1716
+ }
1681
1717
  return postResolve(responses, sources, store);
1682
1718
  }).then(function (collections) {
1683
1719
  return reshape({
@@ -1723,6 +1759,9 @@
1723
1759
  });
1724
1760
  return store.pendingRequests.add(request);
1725
1761
  }
1762
+ function isSearchResponseWithAutomaticInsightsFlag(items) {
1763
+ return !Array.isArray(items) && Boolean(items === null || items === void 0 ? void 0 : items._automaticInsights);
1764
+ }
1726
1765
 
1727
1766
  var _excluded$1 = ["event", "props", "refresh", "store"];
1728
1767
  function onKeyDown(_ref) {
@@ -1734,7 +1773,8 @@
1734
1773
  if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
1735
1774
  // eslint-disable-next-line no-inner-declarations
1736
1775
  var triggerScrollIntoView = function triggerScrollIntoView() {
1737
- var nodeItem = props.environment.document.getElementById("".concat(props.id, "-item-").concat(store.getState().activeItemId));
1776
+ var highlightedItem = getActiveItem(store.getState());
1777
+ var nodeItem = props.environment.document.getElementById(getAutocompleteElementId(props.id, "item-".concat(store.getState().activeItemId), highlightedItem === null || highlightedItem === void 0 ? void 0 : highlightedItem.source));
1738
1778
  if (nodeItem) {
1739
1779
  if (nodeItem.scrollIntoViewIfNeeded) {
1740
1780
  nodeItem.scrollIntoViewIfNeeded(false);
@@ -1912,9 +1952,8 @@
1912
1952
  _excluded2 = ["inputElement", "formElement", "panelElement"],
1913
1953
  _excluded3 = ["inputElement"],
1914
1954
  _excluded4 = ["inputElement", "maxLength"],
1915
- _excluded5 = ["sourceIndex"],
1916
- _excluded6 = ["sourceIndex"],
1917
- _excluded7 = ["item", "source", "sourceIndex"];
1955
+ _excluded5 = ["source"],
1956
+ _excluded6 = ["item", "source"];
1918
1957
  function getPropGetters(_ref) {
1919
1958
  var props = _ref.props,
1920
1959
  refresh = _ref.refresh,
@@ -1983,8 +2022,11 @@
1983
2022
  role: 'combobox',
1984
2023
  'aria-expanded': store.getState().isOpen,
1985
2024
  'aria-haspopup': 'listbox',
1986
- 'aria-owns': store.getState().isOpen ? "".concat(props.id, "-list") : undefined,
1987
- 'aria-labelledby': "".concat(props.id, "-label")
2025
+ 'aria-owns': store.getState().isOpen ? store.getState().collections.map(function (_ref2) {
2026
+ var source = _ref2.source;
2027
+ return getAutocompleteElementId(props.id, 'list', source);
2028
+ }).join(' ') : undefined,
2029
+ 'aria-labelledby': getAutocompleteElementId(props.id, 'label')
1988
2030
  }, rest);
1989
2031
  };
1990
2032
  var getFormProps = function getFormProps(providedProps) {
@@ -2034,22 +2076,25 @@
2034
2076
  }
2035
2077
  store.dispatch('focus', null);
2036
2078
  }
2037
- var _ref2 = providedProps || {};
2038
- _ref2.inputElement;
2039
- var _ref2$maxLength = _ref2.maxLength,
2040
- maxLength = _ref2$maxLength === void 0 ? 512 : _ref2$maxLength,
2041
- rest = _objectWithoutProperties$1(_ref2, _excluded4);
2079
+ var _ref3 = providedProps || {};
2080
+ _ref3.inputElement;
2081
+ var _ref3$maxLength = _ref3.maxLength,
2082
+ maxLength = _ref3$maxLength === void 0 ? 512 : _ref3$maxLength,
2083
+ rest = _objectWithoutProperties$1(_ref3, _excluded4);
2042
2084
  var activeItem = getActiveItem(store.getState());
2043
2085
  var userAgent = ((_props$environment$na = props.environment.navigator) === null || _props$environment$na === void 0 ? void 0 : _props$environment$na.userAgent) || '';
2044
2086
  var shouldFallbackKeyHint = isSamsung(userAgent);
2045
2087
  var enterKeyHint = props.enterKeyHint || (activeItem !== null && activeItem !== void 0 && activeItem.itemUrl && !shouldFallbackKeyHint ? 'go' : 'search');
2046
2088
  return _objectSpread2({
2047
2089
  'aria-autocomplete': 'both',
2048
- 'aria-activedescendant': store.getState().isOpen && store.getState().activeItemId !== null ? "".concat(props.id, "-item-").concat(store.getState().activeItemId) : undefined,
2049
- 'aria-controls': store.getState().isOpen ? "".concat(props.id, "-list") : undefined,
2050
- 'aria-labelledby': "".concat(props.id, "-label"),
2090
+ '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,
2091
+ 'aria-controls': store.getState().isOpen ? store.getState().collections.map(function (_ref4) {
2092
+ var source = _ref4.source;
2093
+ return getAutocompleteElementId(props.id, 'list', source);
2094
+ }).join(' ') : undefined,
2095
+ 'aria-labelledby': getAutocompleteElementId(props.id, 'label'),
2051
2096
  value: store.getState().completion || store.getState().query,
2052
- id: "".concat(props.id, "-input"),
2097
+ id: getAutocompleteElementId(props.id, 'input'),
2053
2098
  autoComplete: 'off',
2054
2099
  autoCorrect: 'off',
2055
2100
  autoCapitalize: 'off',
@@ -2095,26 +2140,20 @@
2095
2140
  }
2096
2141
  }, rest);
2097
2142
  };
2098
- var getAutocompleteId = function getAutocompleteId(instanceId, sourceId) {
2099
- return typeof sourceId !== 'undefined' ? "".concat(instanceId, "-").concat(sourceId) : instanceId;
2100
- };
2101
- var getLabelProps = function getLabelProps(providedProps) {
2102
- var _ref3 = providedProps || {},
2103
- sourceIndex = _ref3.sourceIndex,
2104
- rest = _objectWithoutProperties$1(_ref3, _excluded5);
2143
+ var getLabelProps = function getLabelProps(rest) {
2105
2144
  return _objectSpread2({
2106
- htmlFor: "".concat(getAutocompleteId(props.id, sourceIndex), "-input"),
2107
- id: "".concat(getAutocompleteId(props.id, sourceIndex), "-label")
2145
+ htmlFor: getAutocompleteElementId(props.id, 'input'),
2146
+ id: getAutocompleteElementId(props.id, 'label')
2108
2147
  }, rest);
2109
2148
  };
2110
2149
  var getListProps = function getListProps(providedProps) {
2111
- var _ref4 = providedProps || {},
2112
- sourceIndex = _ref4.sourceIndex,
2113
- rest = _objectWithoutProperties$1(_ref4, _excluded6);
2150
+ var _ref5 = providedProps || {},
2151
+ source = _ref5.source,
2152
+ rest = _objectWithoutProperties$1(_ref5, _excluded5);
2114
2153
  return _objectSpread2({
2115
2154
  role: 'listbox',
2116
- 'aria-labelledby': "".concat(getAutocompleteId(props.id, sourceIndex), "-label"),
2117
- id: "".concat(getAutocompleteId(props.id, sourceIndex), "-list")
2155
+ 'aria-labelledby': getAutocompleteElementId(props.id, 'label'),
2156
+ id: getAutocompleteElementId(props.id, 'list', source)
2118
2157
  }, rest);
2119
2158
  };
2120
2159
  var getPanelProps = function getPanelProps(rest) {
@@ -2133,10 +2172,9 @@
2133
2172
  var getItemProps = function getItemProps(providedProps) {
2134
2173
  var item = providedProps.item,
2135
2174
  source = providedProps.source,
2136
- sourceIndex = providedProps.sourceIndex,
2137
- rest = _objectWithoutProperties$1(providedProps, _excluded7);
2175
+ rest = _objectWithoutProperties$1(providedProps, _excluded6);
2138
2176
  return _objectSpread2({
2139
- id: "".concat(getAutocompleteId(props.id, sourceIndex), "-item-").concat(item.__autocomplete_id),
2177
+ id: getAutocompleteElementId(props.id, "item-".concat(item.__autocomplete_id), source),
2140
2178
  role: 'option',
2141
2179
  'aria-selected': store.getState().activeItemId === item.__autocomplete_id,
2142
2180
  onMouseMove: function onMouseMove(event) {
@@ -2412,6 +2450,7 @@
2412
2450
  navigator: props.navigator
2413
2451
  }, setters));
2414
2452
  function onStoreStateChange(_ref) {
2453
+ var _state$context, _state$context$algoli;
2415
2454
  var prevState = _ref.prevState,
2416
2455
  state = _ref.state;
2417
2456
  props.onStateChange(_objectSpread2({
@@ -2420,6 +2459,13 @@
2420
2459
  refresh: refresh,
2421
2460
  navigator: props.navigator
2422
2461
  }, setters));
2462
+ 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) {
2463
+ var plugin = createAlgoliaInsightsPlugin({
2464
+ __autocomplete_clickAnalytics: false
2465
+ });
2466
+ props.plugins.push(plugin);
2467
+ subscribePlugins([plugin]);
2468
+ }
2423
2469
  }
2424
2470
  function refresh() {
2425
2471
  return onInput(_objectSpread2({
@@ -2434,34 +2480,40 @@
2434
2480
  store: store
2435
2481
  }, setters));
2436
2482
  }
2437
- if (options.insights && !props.plugins.some(function (plugin) {
2438
- return plugin.name === 'aa.algoliaInsightsPlugin';
2439
- })) {
2440
- var insightsParams = typeof options.insights === 'boolean' ? {} : options.insights;
2483
+ function subscribePlugins(plugins) {
2484
+ plugins.forEach(function (plugin) {
2485
+ var _plugin$subscribe;
2486
+ return (_plugin$subscribe = plugin.subscribe) === null || _plugin$subscribe === void 0 ? void 0 : _plugin$subscribe.call(plugin, _objectSpread2(_objectSpread2({}, setters), {}, {
2487
+ navigator: props.navigator,
2488
+ refresh: refresh,
2489
+ onSelect: function onSelect(fn) {
2490
+ subscribers.push({
2491
+ onSelect: fn
2492
+ });
2493
+ },
2494
+ onActive: function onActive(fn) {
2495
+ subscribers.push({
2496
+ onActive: fn
2497
+ });
2498
+ },
2499
+ onResolve: function onResolve(fn) {
2500
+ subscribers.push({
2501
+ onResolve: fn
2502
+ });
2503
+ }
2504
+ }));
2505
+ });
2506
+ }
2507
+ function isAlgoliaInsightsPluginEnabled() {
2508
+ return props.plugins.some(function (plugin) {
2509
+ return plugin.name === 'aa.algoliaInsightsPlugin';
2510
+ });
2511
+ }
2512
+ if (props.insights && !isAlgoliaInsightsPluginEnabled()) {
2513
+ var insightsParams = typeof props.insights === 'boolean' ? {} : props.insights;
2441
2514
  props.plugins.push(createAlgoliaInsightsPlugin(insightsParams));
2442
2515
  }
2443
- props.plugins.forEach(function (plugin) {
2444
- var _plugin$subscribe;
2445
- return (_plugin$subscribe = plugin.subscribe) === null || _plugin$subscribe === void 0 ? void 0 : _plugin$subscribe.call(plugin, _objectSpread2(_objectSpread2({}, setters), {}, {
2446
- navigator: props.navigator,
2447
- refresh: refresh,
2448
- onSelect: function onSelect(fn) {
2449
- subscribers.push({
2450
- onSelect: fn
2451
- });
2452
- },
2453
- onActive: function onActive(fn) {
2454
- subscribers.push({
2455
- onActive: fn
2456
- });
2457
- },
2458
- onResolve: function onResolve(fn) {
2459
- subscribers.push({
2460
- onResolve: fn
2461
- });
2462
- }
2463
- }));
2464
- });
2516
+ subscribePlugins(props.plugins);
2465
2517
  injectMetadata({
2466
2518
  metadata: getMetadata({
2467
2519
  plugins: props.plugins,