1
|
(function ($) {
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
Drupal.behaviors.autocomplete = {
|
7
|
attach: function (context) {
|
8
|
var $context = $(context);
|
9
|
var acdb = [];
|
10
|
$context.find('input.autocomplete').once('autocomplete', function () {
|
11
|
var uri = this.value;
|
12
|
if (!acdb[uri]) {
|
13
|
acdb[uri] = new Drupal.ACDB(uri);
|
14
|
}
|
15
|
var $input = $context.find('#' + this.id.substr(0, this.id.length - 13))
|
16
|
.attr('autocomplete', 'OFF')
|
17
|
.attr('aria-autocomplete', 'list');
|
18
|
$context.find($input[0].form).submit(Drupal.autocompleteSubmit);
|
19
|
$input.parents('.form-item')
|
20
|
.attr('role', 'application')
|
21
|
.append($('<span class="element-invisible" aria-live="assertive"></span>')
|
22
|
.attr('id', $input.attr('id') + '-autocomplete-aria-live')
|
23
|
);
|
24
|
new Drupal.jsAC($input, acdb[uri], $context);
|
25
|
});
|
26
|
}
|
27
|
};
|
28
|
|
29
|
|
30
|
|
31
|
|
32
|
|
33
|
Drupal.autocompleteSubmit = function () {
|
34
|
$('.form-autocomplete > .dropdown').each(function () {
|
35
|
this.owner.hidePopup();
|
36
|
});
|
37
|
|
38
|
|
39
|
|
40
|
return true;
|
41
|
};
|
42
|
|
43
|
|
44
|
|
45
|
|
46
|
Drupal.jsAC.prototype.highlight = function (node) {
|
47
|
if (this.selected) {
|
48
|
$(this.selected).removeClass('active');
|
49
|
}
|
50
|
$(node).addClass('active');
|
51
|
this.selected = node;
|
52
|
$(this.ariaLive).html($(this.selected).html());
|
53
|
};
|
54
|
|
55
|
|
56
|
|
57
|
|
58
|
Drupal.jsAC.prototype.unhighlight = function (node) {
|
59
|
$(node).removeClass('active');
|
60
|
this.selected = false;
|
61
|
$(this.ariaLive).empty();
|
62
|
};
|
63
|
|
64
|
|
65
|
|
66
|
|
67
|
Drupal.jsAC.prototype.populatePopup = function () {
|
68
|
var $input = $(this.input);
|
69
|
|
70
|
if (this.popup) {
|
71
|
$(this.popup).remove();
|
72
|
}
|
73
|
this.selected = false;
|
74
|
this.popup = $('<div class="dropdown"></div>')[0];
|
75
|
this.popup.owner = this;
|
76
|
$input.parent().after(this.popup);
|
77
|
|
78
|
|
79
|
this.db.owner = this;
|
80
|
this.db.search(this.input.value);
|
81
|
};
|
82
|
|
83
|
|
84
|
|
85
|
|
86
|
Drupal.jsAC.prototype.found = function (matches) {
|
87
|
|
88
|
if (!this.input.value.length) {
|
89
|
return false;
|
90
|
}
|
91
|
|
92
|
|
93
|
var ul = $('<ul class="dropdown-menu"></ul>');
|
94
|
var ac = this;
|
95
|
ul.css({
|
96
|
display: 'block',
|
97
|
right: 0
|
98
|
});
|
99
|
for (var key in matches) {
|
100
|
$('<li></li>')
|
101
|
.html($('<a href="#"></a>').html(matches[key]).on('click', function (e) { e.preventDefault(); }))
|
102
|
.on('mousedown', function () { ac.hidePopup(this); })
|
103
|
.on('mouseover', function () { ac.highlight(this); })
|
104
|
.on('mouseout', function () { ac.unhighlight(this); })
|
105
|
.data('autocompleteValue', key)
|
106
|
.appendTo(ul);
|
107
|
}
|
108
|
|
109
|
|
110
|
if (this.popup) {
|
111
|
if (ul.children().length) {
|
112
|
$(this.popup).empty().append(ul).show();
|
113
|
$(this.ariaLive).html(Drupal.t('Autocomplete popup'));
|
114
|
}
|
115
|
else {
|
116
|
$(this.popup).css({ visibility: 'hidden' });
|
117
|
this.hidePopup();
|
118
|
}
|
119
|
}
|
120
|
};
|
121
|
|
122
|
Drupal.jsAC.prototype.setStatus = function (status) {
|
123
|
var $throbber = $(this.input).parent().find('.glyphicon-refresh, .autocomplete-throbber').first();
|
124
|
var throbbingClass = $throbber.is('.autocomplete-throbber') ? 'throbbing' : 'glyphicon-spin';
|
125
|
switch (status) {
|
126
|
case 'begin':
|
127
|
$throbber.addClass(throbbingClass);
|
128
|
$(this.ariaLive).html(Drupal.t('Searching for matches...'));
|
129
|
break;
|
130
|
case 'cancel':
|
131
|
case 'error':
|
132
|
case 'found':
|
133
|
$throbber.removeClass(throbbingClass);
|
134
|
break;
|
135
|
}
|
136
|
};
|
137
|
|
138
|
|
139
|
var oldPrototype = Drupal.jsAC.prototype;
|
140
|
|
141
|
|
142
|
|
143
|
|
144
|
Drupal.jsAC = function ($input, db, context) {
|
145
|
var ac = this;
|
146
|
|
147
|
|
148
|
|
149
|
|
150
|
|
151
|
|
152
|
this.$context = context && $(context) || $(document);
|
153
|
|
154
|
this.input = $input[0];
|
155
|
this.ariaLive = this.$context.find('#' + this.input.id + '-autocomplete-aria-live');
|
156
|
this.db = db;
|
157
|
$input
|
158
|
.keydown(function (event) { return ac.onkeydown(this, event); })
|
159
|
.keyup(function (event) { ac.onkeyup(this, event); })
|
160
|
.blur(function () { ac.hidePopup(); ac.db.cancel(); });
|
161
|
};
|
162
|
|
163
|
|
164
|
Drupal.jsAC.prototype = oldPrototype;
|
165
|
|
166
|
})(jQuery);
|