Skip to content

Commit f61b0ba

Browse files
committed
MAGE-986 Handle new window clicks on Autocomplete
1 parent 645ddbd commit f61b0ba

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

view/frontend/web/js/autocomplete.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ define([
5757

5858
navigator: {
5959
navigate({itemUrl}) {
60-
console.log("Local navigate:", itemUrl);
6160
window.location.assign(itemUrl);
6261
},
6362
navigateNewTab({itemUrl}) {
@@ -121,7 +120,7 @@ define([
121120

122121
handleAutocompleteSubmit({ state: { query } }) {
123122
if (query && !this.hasRedirect) {
124-
window.location.href = this.getSearchResultsUrl(query);
123+
this.navigator.navigate({ itemUrl: this.getSearchResultsUrl(query) });
125124
}
126125
},
127126

@@ -788,16 +787,26 @@ define([
788787
}
789788
},
790789

790+
/**
791+
* Only clickable links can open in a new window - else popup blockers may be triggered
792+
* @param event
793+
* @returns {boolean}
794+
*/
795+
canRedirectToNewWindow(event) {
796+
return algoliaConfig.autocomplete.redirects.openInNewWindow
797+
&& !(event instanceof SubmitEvent)
798+
&& !(event instanceof KeyboardEvent);
799+
},
800+
791801
buildRedirectPlugin() {
792802
const onRedirect = (redirects, { event, navigator, state }) => {
793-
console.log("onRedirect called:", redirects);
794803
const item = redirects.find((r) => r.sourceId === 'products');
795804
const itemUrl = item?.urls?.[0];
796805
if (!itemUrl) return;
797806

798807
if (event.metaKey || event.ctrlKey) {
799808
navigator.navigateNewTab({ itemUrl, item, state });
800-
} else if (event.shiftKey) {
809+
} else if (event.shiftKey || this.canRedirectToNewWindow(event)) {
801810
navigator.navigateNewWindow({ itemUrl, item, state });
802811
} else {
803812
navigator.navigate({ itemUrl, item, state });

0 commit comments

Comments
 (0)