@@ -42,6 +42,8 @@ This JavaScript library adds a fast and fully-featured auto-completion menu to y
42
42
- [ Custom source] ( #custom-source )
43
43
- [ Security] ( #security )
44
44
- [ User-generated data: protecting against XSS] ( #user-generated-data-protecting-against-xss )
45
+ - [ FAQ] ( #faq )
46
+ - [ How can I ` Control ` -click on results and have them open in a new tab?] ( #how-can-i-control-click-on-results-and-have-them-open-in-a-new-tab )
45
47
- [ Events] ( #events )
46
48
- [ API] ( #api )
47
49
- [ jQuery] ( #jquery-1 )
@@ -134,8 +136,8 @@ var autocomplete = require('autocomplete.js');
134
136
}
135
137
}
136
138
}
137
- ]).on (' autocomplete:selected' , function (event , suggestion , dataset ) {
138
- console .log (suggestion, dataset);
139
+ ]).on (' autocomplete:selected' , function (event , suggestion , dataset , context ) {
140
+ console .log (event , suggestion, dataset, context );
139
141
});
140
142
</script >
141
143
```
@@ -164,8 +166,8 @@ var autocomplete = require('autocomplete.js');
164
166
}
165
167
}
166
168
}
167
- ]).on (' autocomplete:selected' , function (event , suggestion , dataset ) {
168
- console .log (suggestion, dataset);
169
+ ]).on (' autocomplete:selected' , function (event , suggestion , dataset , context ) {
170
+ console .log (event , suggestion, dataset, context );
169
171
});
170
172
</script >
171
173
```
@@ -203,7 +205,7 @@ var autocomplete = require('autocomplete.js');
203
205
};
204
206
205
207
$scope .$on (' autocomplete:selected' , function (event , suggestion , dataset ) {
206
- console .log (suggestion, dataset);
208
+ console .log (event , suggestion, dataset, context );
207
209
});
208
210
}]);
209
211
</script >
@@ -592,6 +594,35 @@ If you did specify custom highlighting pre/post tags, you can specify them as 2n
592
594
}
593
595
```
594
596
597
+ ## FAQ
598
+
599
+ ### How can I ` Control ` -click on results and have them open in a new tab?
600
+
601
+ You'll need to update your suggestion templates to make them as ` <a href> ` links
602
+ and not simple divs. ` Control ` -clicking on them will trigger the default browser
603
+ behavior and open suggestions in a new tab.
604
+
605
+ To also support keyboard navigation, you'll need to listen to the
606
+ ` autocomplete:selected ` event and change ` window.location ` to the destination
607
+ URL.
608
+
609
+ Note that you might need to check the value of ` context.selectionMethod ` in
610
+ ` autocomplete:selected ` first. If it's equal to ` click ` , you should ` return `
611
+ early, otherwise your main window will ** also** follow the link.
612
+
613
+ Here is an example of how it would look like:
614
+
615
+ ``` javascript
616
+ autocomplete (…).on (' autocomplete:selected' , function (event , suggestion , dataset , context ) {
617
+ // Do nothing on click, as the browser will already do it
618
+ if (context .selectionMethod === ' click' ) {
619
+ return ;
620
+ }
621
+ // Change the page, for example, on other events
622
+ window .location .assign (suggestion .url );
623
+ });
624
+ ```
625
+
595
626
## Events
596
627
597
628
The autocomplete component triggers the following custom events.
@@ -615,9 +646,11 @@ The autocomplete component triggers the following custom events.
615
646
the dataset the suggestion belongs to.
616
647
617
648
* ` autocomplete:selected ` – Triggered when a suggestion from the dropdown menu is
618
- selected. The event handler will be invoked with 3 arguments: the jQuery
619
- event object, the suggestion object, and the name of the dataset the
620
- suggestion belongs to.
649
+ selected. The event handler will be invoked with the following arguments: the jQuery
650
+ event object, the suggestion object, the name of the dataset the
651
+ suggestion belongs to and a ` context ` object. The ` context ` contains
652
+ a ` .selectionMethod ` key that can be either ` click ` , ` enterKey ` , ` tabKey ` or
653
+ ` blur ` , depending how the suggestion was selected.
621
654
622
655
* ` autocomplete:cursorremoved ` – Triggered when the cursor leaves the selections
623
656
or its current index is lower than 0
0 commit comments