-
Notifications
You must be signed in to change notification settings - Fork 1
/
depage-autocomplete.js
309 lines (273 loc) · 9.53 KB
/
depage-autocomplete.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/**
* @require framework/shared/jquery-1.8.3.js
*
* @file depage-autocomplete
*
* Depage AutoComplete plugin to supply user with hints or data while filling forms.
*
* Provide a url in the options to dynamically load via AJAX into the corresponding unordered list.
*
* Fires a "selected" event when the item is picked
*
* copyright (c) 2006-2012 Frank Hellenkamp [[email protected]]
*
* @author Ben Wallis
*/
(function($){
// add focus expression for jquery > 1.6
$.expr[':'].focus = function( elem ) {
return elem === document.activeElement && ( elem.type || elem.href );
};
if(!$.depage){
$.depage = {};
}
/**
* autocomplete
*
* @param el - file input
* @param index
* @param options
*/
$.depage.autocomplete = function(el, index, options){
// To avoid scope issues, use 'base' instead of 'this' to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("depage.autocomplete", base);
// List element associated with input
base.$list = null;
base.$form = null;
base.$items = null;
var $body = $('body');
base.visible = false;
// {{{ init
/**
* Init
*
* Get the plugin options.
*
* @return void
*/
base.init = function(){
base.options = $.extend({}, $.depage.autocomplete.defaultOptions, options);
base.options.list_id = base.options.list_id || (base.$el.parents("p.input-text").attr("id") + "-list");
// disable browser autocomplete
base.$el.attr("autocomplete", "off");
base.setup();
base.autocomplete();
};
// }}}
// {{{ select
/*
* Select
*
* Set the list-item as selected, and hide the autocompelete list.
*
* @param $item - $('li')
*/
var select = function(e, $content) {
$content.removeClass("hover");
base.$el.val($content.find('.content').text());
base.hide();
base.$el.trigger("selected", [$content]);
};
// }}}
// {{{ autocomplete()
/**
* autocomplete
*
* Binds to keypress and loads list element with options.
*
* @return void
*/
base.autocomplete = function(){
if(base.options.url) {
base.$el.bind("keyup.autocomplete", function(e) {
// @TODO check search term if changed from last search
// @TODO add timer so that we do not search for every keypress, when someone types fast
var code = e.keyCode ? e.keyCode : e.which;
if(!(code == 40 || code == 38 || code == 13 || code == 27)) { // ignore arrow and enter keys
var url = $("base").attr("href") + $('html').attr('lang') + base.options.url + "?ajax=true" + "&value=" + $(this).val();
base.$items = null;
$.get(url , null, function(data) {
base.$el.trigger("load.autocomplete", [$(data)]);
});
}
});
}
};
/// }}}
// {{{ setup()
/**
* Setup UL
*
* Clicking <li> adds contents to the input element.
*
*/
base.setup = function(){
base.$list = $("#" + base.options.list_id);
base.$form = base.$el.parents("form");
if (!base.$list.length){
// add a hidden <ul> for the autocomplete list if it doesn not already exist
base.$list = $("<ul class='autocomplete' />")
.attr({
"id" : base.options.list_id
})
.css({
"position" : "absolute",
"left" : base.$el.offset().left,
"top" : base.$el.offset().top + base.$el.height(),
"z-index" : "1000",
"background-color" : "#FFF",
"width" : base.$el.width()
})
.hide();
$body.prepend(base.$list);
}
/*
* Load
*
* Bind to the autocomplete load event and setup the dynamic functionality.
*/
base.$el.bind("load.autocomplete", function(e, $newItems) {
base.$list.empty();
base.$items = null;
if (!$newItems) {
return;
}
$newItems = $newItems.children("li");
// truncate the list
if(base.options.max_items){
$newItems = $newItems.slice(0,base.options.max_items -1);
}
base.$items = $newItems;
// append the list items...
base.$list.append(base.$items);
// on click select the list item.
base.$items.click(function(e) {
select(e, $(this));
return false;
});
if(base.$items.length) {
if (base.$items.filter(".hover").length === 0) {
$(base.$items[0]).addClass("hover");
}
// add hover class on mouse over
base.$items.hover(function(){
base.$items.filter(".hover").removeClass("hover");
$(this).addClass("hover");
});
base.show();
} else {
base.hide();
}
});
// Bind to keyup events on the input
base.$el.bind("keyup.autocomplete", function(e) {
var code = e.keyCode ? e.keyCode : e.which;
if (code == 27) {
// escape key
base.hide();
base.$el.val("");
}
if (base.$items) {
// find the selected list item
var $item = base.$items.filter(".hover").removeClass("hover");
if ($item.length){
switch (code) {
case 40 : // arrow down
$item = $item.next();
if (!$item.length) $item = base.$items.first();
break;
case 38 : // arrow up
$item = $item.prev();
if (!$item.length) $item = base.$items.last();
break;
case 13 : // enter key
select(e, $item);
base.$el.val("");
break;
}
} else {
// default to the first item
$item = $(base.$items[0]);
}
// show the hover class on the selected itm
$item.addClass("hover");
}
return false;
});
base.$form.bind("submit.autocomplete", function(e) {
if (base.$el.is("input:focus")) {
// stop submission when the input has the focus to capture submission on enter
e.stopPropagation();
e.preventDefault();
return false;
}
});
};
// }}}
// {{{ base.show()
/**
* Base Show
*/
base.show = function() {
if (base.visible) {
return;
}
// we have items so position and show the list
base.$list
.css({
"left" : base.$el.offset().left,
"top" : base.$el.offset().top + base.$el.height()
})
.show();
/**
* Remove menu on click out
*/
$body.bind('click.autocomplete', function(e) {
if (e.target.type !== 'submit') {
base.hide();
}
});
base.visible = true;
return false;
};
// }}}
// {{{ base.hide()
/**
* Base Hide
*/
base.hide = function() {
if (!base.visible) {
return false;
}
$body.unbind('click.autocomplete');
base.$list.hide();
base.visible = false;
return false;
};
// }}}
base.init();
};
// }}}
/**
* Default Options
*
* url - the ajax lander url
* html5 - if false this will force the autoloader to work via ajax
*/
$.depage.autocomplete.defaultOptions = {
url : false,
list_id : false,
max_items : 8
};
$.fn.depageAutoComplete = function(options){
return this.each(function(index){
(new $.depage.autocomplete(this, index, options));
});
};
})(jQuery);
/* vim:set ft=javascript sw=4 sts=4 fdm=marker : */