Skip to content

Commit a41919a

Browse files
committed
Handling direct link, instead of search and then go to the suggestion.
1 parent 434c1f1 commit a41919a

File tree

2 files changed

+51
-28
lines changed

2 files changed

+51
-28
lines changed
Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,50 @@
11
(function ($) {
2-
/**
2+
3+
/**
4+
* Hides the autocomplete suggestions, original function.
5+
*/
6+
Drupal.jsAC.prototype.hidePopupOrig = function (keycode) {
7+
// Select item if the right key or mousebutton was pressed.
8+
if (this.selected && ((keycode && keycode != 46 && keycode != 8 && keycode != 27) || !keycode)) {
9+
this.input.value = $(this.selected).data('autocompleteValue');
10+
}
11+
// Hide popup.
12+
var popup = this.popup;
13+
if (popup) {
14+
this.popup = null;
15+
$(popup).fadeOut('fast', function () { $(popup).remove(); });
16+
}
17+
this.selected = false;
18+
$(this.ariaLive).empty();
19+
};
20+
21+
/**
322
* Hides the autocomplete suggestions.
423
*/
5-
Drupal.jsAC.prototype.hidePopup = function (keycode) {
6-
// Select item if the right key or mousebutton was pressed.
7-
if (this.selected && ((keycode && keycode != 46 && keycode != 8 && keycode != 27) || !keycode)) {
8-
this.input.value = $('a', this.selected).attr("href");
9-
return $('a', this.selected).attr("href");
10-
}
24+
Drupal.jsAC.prototype.hidePopup = function (keycode, event) {
25+
if ($(this.input).attr('elasticsearch-autocomplete')) {
26+
// Select item if the right key or mousebutton was pressed.
27+
if (this.selected && ((keycode && keycode != 46 && keycode != 8 && keycode != 27) || !keycode)) {
28+
this.input.value = $('a', this.selected).text();
29+
if (typeof event != 'undefined') {
30+
event.preventDefault();
31+
}
32+
33+
window.location = $('a', this.selected).attr("href");
34+
}
1135

12-
// Hide popup.
13-
var popup = this.popup;
14-
if (popup) {
15-
this.popup = null;
16-
$(popup).fadeOut('fast', function () { $(popup).remove(); });
36+
// Hide popup.
37+
var popup = this.popup;
38+
if (popup) {
39+
this.popup = null;
40+
$(popup).fadeOut('fast', function () { $(popup).remove(); });
41+
}
42+
this.selected = false;
43+
$(this.ariaLive).empty();
44+
}
45+
else {
46+
this.hidePopupOrig(keycode);
1747
}
18-
this.selected = false;
19-
$(this.ariaLive).empty();
2048
};
2149

2250
/**
@@ -33,6 +61,7 @@ Drupal.behaviors.elasticsearch_autocomplete = {
3361
}
3462
var $input = $(this)
3563
.attr('autocomplete', 'OFF')
64+
.attr('elasticsearch-autocomplete', true)
3665
.attr('aria-autocomplete', 'list');
3766
$($input[0].form).unbind();
3867
$($input[0].form).submit(Drupal.ELAutocompleteSubmit);
@@ -52,20 +81,13 @@ Drupal.behaviors.elasticsearch_autocomplete = {
5281
* Prevents the form from submitting if the suggestions popup is open
5382
* and closes the suggestions popup when doing so.
5483
*/
55-
Drupal.ELAutocompleteSubmit = function () {
84+
Drupal.ELAutocompleteSubmit = function (e) {
5685
var href = '';
5786
$('#autocomplete').each(function () {
58-
href = this.owner.hidePopup();
87+
this.owner.hidePopup(null, e);
5988
});
6089

61-
if (href == '' || typeof href == 'undefined') {
62-
return false;;
63-
}
64-
else {
65-
window.location = href;
66-
}
67-
68-
return false;
90+
return true;
6991
};
7092
})(jQuery);
7193

modules/elasticsearch_connector_search_api/service.inc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,18 +1779,19 @@ class SearchApiElasticsearchConnector extends SearchApiAbstractService {
17791779
if (isset($response['results'])) {
17801780
$items = $index->loadItems(array_keys($response['results']));
17811781
foreach ($items as $id => $item) {
1782-
$node_title = $index->datasource()->getItemLabel($item);
1782+
$item_title = $index->datasource()->getItemLabel($item);
1783+
$url_item = $index->datasource()->getItemUrl($item);
17831784
if (!empty($search->options['custom']['link_suggestions'])) {
1784-
$matches[$node_title] = l($node_title, 'node/' . $item->nid);
1785+
$matches[$item_title] = l($item_title, $url_item['path'], array('html' => TRUE));
17851786
}
17861787
else {
1787-
$matches[$node_title] = $node_title;
1788+
$matches[$item_title] = $item_title;
17881789
}
17891790
}
17901791

17911792
if ($matches) {
17921793
// Eliminate suggestions that are too short or already in the query.
1793-
foreach ($matches as $name => $node_title) {
1794+
foreach ($matches as $name => $item_title) {
17941795
if (drupal_strlen($name) < 3 || isset($keys_array[$name])) {
17951796
unset($matches[$name]);
17961797
}

0 commit comments

Comments
 (0)