1
|
/*
|
2
|
---
|
3
|
|
4
|
description: A plugin for enabling autocomplete of a text input or textarea.
|
5
|
|
6
|
authors:
|
7
|
- Fábio Miranda Costa
|
8
|
|
9
|
requires:
|
10
|
- Core/Class.Extras
|
11
|
- Core/Element.Event
|
12
|
- Core/Element.Style
|
13
|
- More/Element.Forms
|
14
|
|
15
|
license: MIT-style license
|
16
|
|
17
|
provides: [Meio.Autocomplete]
|
18
|
|
19
|
...
|
20
|
*/
|
21
|
|
22
|
(function(global, $){
|
23
|
|
24
|
var browser = Browser; // better compression and faster
|
25
|
|
26
|
// Custom Events
|
27
|
|
28
|
// thanks Jan Kassens
|
29
|
Object.append(Element.NativeEvents, {
|
30
|
'paste': 2, 'input': 2
|
31
|
});
|
32
|
Element.Events.paste = {
|
33
|
base : (browser.opera || (browser.firefox && browser.version < 3)) ? 'input' : 'paste',
|
34
|
condition: function(e){
|
35
|
this.fireEvent('paste', e, 1);
|
36
|
return false;
|
37
|
}
|
38
|
};
|
39
|
|
40
|
// the key event that repeats
|
41
|
Element.Events.keyrepeat = {
|
42
|
base : (browser.firefox || browser.opera) ? 'keypress' : 'keydown',
|
43
|
condition: Function.from(true)
|
44
|
};
|
45
|
|
46
|
// Autocomplete itself
|
47
|
|
48
|
var Meio = global.Meio || {};
|
49
|
var globalCache;
|
50
|
|
51
|
var keysThatDontChangeValueOnKeyUp = {
|
52
|
9: 1, // tab
|
53
|
16: 1, // shift
|
54
|
17: 1, // control
|
55
|
18: 1, // alt
|
56
|
224: 1, // command (meta onkeypress)
|
57
|
91: 1, // command (meta onkeydown)
|
58
|
37: 1, // left
|
59
|
38: 1, // up
|
60
|
39: 1, // right
|
61
|
40: 1 // down
|
62
|
};
|
63
|
|
64
|
var encode = function(str){
|
65
|
return str.replace(/"/g, '"').replace(/'/g, ''');
|
66
|
};
|
67
|
|
68
|
Meio.Widget = new Class({
|
69
|
|
70
|
initialize: function(){
|
71
|
this.elements = {};
|
72
|
},
|
73
|
|
74
|
addElement: function(name, obj){
|
75
|
this.elements[name] = obj;
|
76
|
},
|
77
|
|
78
|
addEventToElement: function(name, eventName, event){
|
79
|
this.elements[name].addEvent(eventName, event.bind(this));
|
80
|
},
|
81
|
|
82
|
addEventsToElement: function(name, events){
|
83
|
for (var eventName in events){
|
84
|
this.addEventToElement(name, eventName, events[eventName]);
|
85
|
}
|
86
|
},
|
87
|
|
88
|
attach: function(){
|
89
|
for (var element in this.elements){
|
90
|
this.elements[element].attach();
|
91
|
}
|
92
|
},
|
93
|
|
94
|
detach: function(){
|
95
|
for (var element in this.elements){
|
96
|
this.elements[element].detach();
|
97
|
}
|
98
|
},
|
99
|
|
100
|
destroy: function(){
|
101
|
for (var element in this.elements){
|
102
|
this.elements[element] && this.elements[element].destroy();
|
103
|
}
|
104
|
}
|
105
|
});
|
106
|
|
107
|
Meio.Autocomplete = new Class({
|
108
|
|
109
|
Extends: Meio.Widget,
|
110
|
|
111
|
Implements: [Options, Events],
|
112
|
|
113
|
options: {
|
114
|
|
115
|
delay: 200,
|
116
|
minChars: 0,
|
117
|
cacheLength: 20,
|
118
|
selectOnTab: true,
|
119
|
maxVisibleItems: 10,
|
120
|
cacheType: 'shared', // 'shared' or 'own'
|
121
|
|
122
|
filter: {
|
123
|
/*
|
124
|
its posible to pass the filters directly or by passing a type and optionaly a path.
|
125
|
|
126
|
filter: function(text, data){}
|
127
|
formatMatch: function(text, data, i){}
|
128
|
formatItem: function(text, data){}
|
129
|
|
130
|
or
|
131
|
|
132
|
type: 'startswith' or 'contains' // can be any defined on the Meio.Autocomplete.Filter object
|
133
|
path: 'a.b.c' // path to the text value on each object thats contained on the data array
|
134
|
*/
|
135
|
},
|
136
|
|
137
|
/*
|
138
|
onNoItemToList: function(elements){},
|
139
|
onSelect: function(elements, value){},
|
140
|
onDeselect: function(elements){},
|
141
|
*/
|
142
|
|
143
|
fieldOptions: {}, // see Element options
|
144
|
listOptions: {}, // see List options
|
145
|
requestOptions: {}, // see DataRequest options
|
146
|
urlOptions: {} // see URL options
|
147
|
|
148
|
},
|
149
|
|
150
|
initialize: function(input, data, options, listInstance){
|
151
|
this.parent();
|
152
|
this.setOptions(options);
|
153
|
this.active = 0;
|
154
|
|
155
|
this.filters = Meio.Autocomplete.Filter.get(this.options.filter);
|
156
|
|
157
|
this.addElement('list', listInstance || new Meio.Element.List(this.options.listOptions));
|
158
|
this.addListEvents();
|
159
|
|
160
|
this.addElement('field', new Meio.Element.Field(input, this.options.fieldOptions));
|
161
|
this.addFieldEvents();
|
162
|
|
163
|
this.addSelectEvents();
|
164
|
|
165
|
this.attach();
|
166
|
this.initCache();
|
167
|
this.initData(data);
|
168
|
},
|
169
|
|
170
|
addFieldEvents: function(){
|
171
|
this.addEventsToElement('field', {
|
172
|
'beforeKeyrepeat': function(e){
|
173
|
this.active = 1;
|
174
|
var e_key = e.key, list = this.elements.list;
|
175
|
if (e_key == 'up' || e_key == 'down' || (e_key == 'enter' && list.showing)) e.preventDefault();
|
176
|
},
|
177
|
'delayedKeyrepeat': function(e){
|
178
|
var e_key = e.key, field = this.elements.field;
|
179
|
field.keyPressControl[e_key] = true;
|
180
|
switch (e_key){
|
181
|
case 'up': case 'down':
|
182
|
this.focusItem(e_key);
|
183
|
break;
|
184
|
case 'enter':
|
185
|
this.setInputValue();
|
186
|
break;
|
187
|
case 'tab':
|
188
|
if (this.options.selectOnTab) this.setInputValue();
|
189
|
field.keyPressControl[e_key] = false; // tab blurs the input so the keyup event wont happen at the same input you made a keydown
|
190
|
break;
|
191
|
case 'esc':
|
192
|
this.elements.list.hide();
|
193
|
break;
|
194
|
default:
|
195
|
this.setupList();
|
196
|
}
|
197
|
this.oldInputedText = field.node.get('value');
|
198
|
},
|
199
|
'keyup': function(e){
|
200
|
var field = this.elements.field;
|
201
|
if (!keysThatDontChangeValueOnKeyUp[e.code]){
|
202
|
if (!field.keyPressControl[e.key]) this.setupList();
|
203
|
field.keyPressControl[e.key] = false;
|
204
|
}
|
205
|
},
|
206
|
'focus': function(){
|
207
|
this.active = 1;
|
208
|
var list = this.elements.list;
|
209
|
list.focusedItem = null;
|
210
|
list.positionNextTo(this.elements.field.node);
|
211
|
},
|
212
|
'click': function(){
|
213
|
if (++this.active > 2 && !this.elements.list.showing){
|
214
|
this.forceSetupList();
|
215
|
}
|
216
|
},
|
217
|
'blur': function(e){
|
218
|
this.active = 0;
|
219
|
var list = this.elements.list;
|
220
|
if (list.shouldNotBlur){
|
221
|
this.elements.field.node.setCaretPosition('end');
|
222
|
list.shouldNotBlur = false;
|
223
|
if (list.focusedItem) list.hide();
|
224
|
} else {
|
225
|
list.hide();
|
226
|
}
|
227
|
},
|
228
|
'paste': function(){
|
229
|
return this.setupList();
|
230
|
}
|
231
|
});
|
232
|
},
|
233
|
|
234
|
addListEvents: function(){
|
235
|
this.addEventsToElement('list', {
|
236
|
'mousedown': function(e){
|
237
|
if (this.active && !e.dontHide) this.setInputValue();
|
238
|
}
|
239
|
});
|
240
|
},
|
241
|
|
242
|
update: function(){
|
243
|
var data = this.data, list = this.elements.list;
|
244
|
var cacheKey = data.getKey(), cached = this.cache.get(cacheKey), html;
|
245
|
if (cached){
|
246
|
html = cached.html;
|
247
|
this.itemsData = cached.data;
|
248
|
} else {
|
249
|
data = data.get();
|
250
|
var itemsHtml = [], itemsData = [], classes = list.options.classes, text = this.inputedText;
|
251
|
var filter = this.filters.filter, formatMatch = this.filters.formatMatch, formatItem = this.filters.formatItem;
|
252
|
for (var row, i = 0, n = 0; row = data[i++];) if (filter.call(this, text, row)){
|
253
|
itemsHtml.push(
|
254
|
'<li title="', encode(formatMatch.call(this, text, row)),
|
255
|
'" data-index="', n,
|
256
|
'" class="', (n%2 ? classes.even : classes.odd), '">',
|
257
|
formatItem.call(this, text, row, n),
|
258
|
'</li>'
|
259
|
);
|
260
|
itemsData.push(row);
|
261
|
n++;
|
262
|
}
|
263
|
html = itemsHtml.join('');
|
264
|
this.cache.set(cacheKey, {html: html, data: itemsData});
|
265
|
this.itemsData = itemsData;
|
266
|
}
|
267
|
list.focusedItem = null;
|
268
|
this.fireEvent('deselect', [this.elements]);
|
269
|
list.list.set('html', html);
|
270
|
if (this.options.maxVisibleItems) list.applyMaxHeight(this.options.maxVisibleItems);
|
271
|
},
|
272
|
|
273
|
setupList: function(){
|
274
|
this.inputedText = this.elements.field.node.get('value');
|
275
|
if (this.inputedText !== this.oldInputedText){
|
276
|
this.forceSetupList(this.inputedText);
|
277
|
} else {
|
278
|
this.elements.list.hide();
|
279
|
}
|
280
|
return true;
|
281
|
},
|
282
|
|
283
|
forceSetupList: function(inputedText){
|
284
|
inputedText = inputedText || this.elements.field.node.get('value');
|
285
|
if (inputedText.length >= this.options.minChars){
|
286
|
clearInterval(this.prepareTimer);
|
287
|
this.prepareTimer = this.data.prepare.delay(this.options.delay, this.data, this.inputedText);
|
288
|
}
|
289
|
},
|
290
|
|
291
|
dataReady: function(){
|
292
|
this.update();
|
293
|
if (this.onUpdate){
|
294
|
this.onUpdate();
|
295
|
this.onUpdate = null;
|
296
|
}
|
297
|
var list = this.elements.list;
|
298
|
if (list.list.get('html')){
|
299
|
if (this.active) list.show();
|
300
|
} else {
|
301
|
this.fireEvent('noItemToList', [this.elements]);
|
302
|
list.hide();
|
303
|
}
|
304
|
},
|
305
|
|
306
|
setInputValue: function(){
|
307
|
var list = this.elements.list;
|
308
|
if (list.focusedItem){
|
309
|
var text = list.focusedItem.get('title');
|
310
|
this.elements.field.node.set('value', text);
|
311
|
var index = list.focusedItem.get('data-index');
|
312
|
this.fireEvent('select', [this.elements, this.itemsData[index], text, index]);
|
313
|
}
|
314
|
list.hide();
|
315
|
},
|
316
|
|
317
|
focusItem: function(direction){
|
318
|
var list = this.elements.list;
|
319
|
if (list.showing){
|
320
|
list.focusItem(direction);
|
321
|
} else {
|
322
|
this.forceSetupList();
|
323
|
this.onUpdate = function(){ list.focusItem(direction); };
|
324
|
}
|
325
|
},
|
326
|
|
327
|
addSelectEvents: function(){
|
328
|
this.addEvents({
|
329
|
select: function(elements){
|
330
|
elements.field.addClass('selected');
|
331
|
},
|
332
|
deselect: function(elements){
|
333
|
elements.field.removeClass('selected');
|
334
|
}
|
335
|
});
|
336
|
},
|
337
|
|
338
|
initData: function(data){
|
339
|
this.data = (typeOf(data) == 'string') ?
|
340
|
new Meio.Autocomplete.Data.Request(data, this.cache, this.elements.field, this.options.requestOptions, this.options.urlOptions) :
|
341
|
new Meio.Autocomplete.Data(data, this.cache);
|
342
|
this.data.addEvent('ready', this.dataReady.bind(this));
|
343
|
},
|
344
|
|
345
|
initCache: function(){
|
346
|
var cacheLength = this.options.cacheLength;
|
347
|
if (this.options.cacheType == 'shared'){
|
348
|
this.cache = globalCache;
|
349
|
this.cache.setMaxLength(cacheLength);
|
350
|
} else { // 'own'
|
351
|
this.cache = new Meio.Autocomplete.Cache(cacheLength);
|
352
|
}
|
353
|
},
|
354
|
|
355
|
refreshCache: function(cacheLength){
|
356
|
this.cache.refresh();
|
357
|
this.cache.setMaxLength(cacheLength || this.options.cacheLength);
|
358
|
},
|
359
|
|
360
|
refreshAll: function(cacheLength, urlOptions){
|
361
|
// TODO, do you really need to refresh the url? see a better way of doing this
|
362
|
this.refreshCache(cacheLength);
|
363
|
this.data.refreshKey(urlOptions);
|
364
|
}
|
365
|
|
366
|
});
|
367
|
|
368
|
// This is the same autocomplete class but it acts like a normal select element.
|
369
|
// When you select an option from the autocomplete it will set the value of a given element (valueField)
|
370
|
// with the return of the valueFilter.
|
371
|
// if the syncAtInit option is set to true, it will synchonize the value of the autocomplete with the corresponding data
|
372
|
// from the valueField's value.
|
373
|
// to understand better see the user specs.
|
374
|
|
375
|
Meio.Autocomplete.Select = new Class({
|
376
|
|
377
|
Extends: Meio.Autocomplete,
|
378
|
|
379
|
options: {
|
380
|
syncName: 'id', // if falsy it wont sync at start
|
381
|
valueField: null,
|
382
|
valueFilter: function(data){
|
383
|
return data.id;
|
384
|
}
|
385
|
},
|
386
|
|
387
|
// overwritten
|
388
|
initialize: function(input, data, options, listInstance){
|
389
|
this.parent(input, data, options, listInstance);
|
390
|
this.valueField = $(this.options.valueField);
|
391
|
|
392
|
if (!this.valueField) return;
|
393
|
|
394
|
this.syncWithValueField(data);
|
395
|
},
|
396
|
|
397
|
syncWithValueField: function(data){
|
398
|
var value = this.getValueFromValueField();
|
399
|
|
400
|
if (value && this.options.syncName){
|
401
|
this.addParameter(data);
|
402
|
this.addDataReadyEvent(value);
|
403
|
this.data.prepare(this.elements.field.node.get('value'));
|
404
|
} else {
|
405
|
this.addValueFieldEvents();
|
406
|
}
|
407
|
},
|
408
|
|
409
|
addValueFieldEvents: function(){
|
410
|
this.addEvents({
|
411
|
'select': function(elements, data){
|
412
|
this.valueField.set('value', this.options.valueFilter.call(this, data));
|
413
|
},
|
414
|
'deselect': function(elements){
|
415
|
this.valueField.set('value', '');
|
416
|
}
|
417
|
});
|
418
|
},
|
419
|
|
420
|
addParameter: function(data){
|
421
|
this.parameter = {
|
422
|
name: this.options.syncName,
|
423
|
value: function(){
|
424
|
return this.valueField.value;
|
425
|
}.bind(this)
|
426
|
};
|
427
|
if (this.data.url) this.data.url.addParameter(this.parameter);
|
428
|
},
|
429
|
|
430
|
addDataReadyEvent: function(value){
|
431
|
var self = this;
|
432
|
var runOnce = function(){
|
433
|
self.addValueFieldEvents();
|
434
|
var values = this.get();
|
435
|
for (var i = values.length; i--;){
|
436
|
if (self.options.valueFilter.call(self, values[i]) == value){
|
437
|
var text = self.filters.formatMatch.call(self, '', values[i], 0);
|
438
|
self.elements.field.node.set('value', text);
|
439
|
self.fireEvent('select', [self.elements, values[i], text, i]);
|
440
|
break;
|
441
|
}
|
442
|
}
|
443
|
if (this.url) this.url.removeParameter(self.parameter);
|
444
|
this.removeEvent('ready', runOnce);
|
445
|
};
|
446
|
this.data.addEvent('ready', runOnce);
|
447
|
},
|
448
|
|
449
|
getValueFromValueField: function(){
|
450
|
return this.valueField.get('value');
|
451
|
}
|
452
|
|
453
|
});
|
454
|
|
455
|
// Transforms a select on an autocomplete field
|
456
|
|
457
|
Meio.Autocomplete.Select.One = new Class({
|
458
|
|
459
|
Extends: Meio.Autocomplete.Select,
|
460
|
|
461
|
options: {
|
462
|
filter: {
|
463
|
path: 'text' // path to the text value on each object thats contained on the data array
|
464
|
}
|
465
|
},
|
466
|
|
467
|
//overwritten
|
468
|
initialize: function(select, options, listInstance){
|
469
|
this.select = $(select);
|
470
|
this.replaceSelect();
|
471
|
this.parent(this.field, this.createDataArray(), Object.merge(options || {}, {
|
472
|
valueField: this.select,
|
473
|
valueFilter: function(data){ return data.value; }
|
474
|
}), listInstance);
|
475
|
},
|
476
|
|
477
|
replaceSelect: function(){
|
478
|
var selectedOption = this.select.getSelected()[0];
|
479
|
this.field = new Element('input', {type: 'text'});
|
480
|
var optionValue = selectedOption.get('value');
|
481
|
if (optionValue || optionValue === 0) this.field.set('value', selectedOption.get('html'));
|
482
|
this.select.setStyle('display', 'none');
|
483
|
this.field.inject(this.select, 'after');
|
484
|
},
|
485
|
|
486
|
createDataArray: function(){
|
487
|
var selectOptions = this.select.options, data = [];
|
488
|
for(var i = 0, selectOption, optionValue; selectOption = selectOptions[i++];){
|
489
|
optionValue = selectOption.value;
|
490
|
if (optionValue || optionValue === 0) data.push({value: optionValue, text: selectOption.innerHTML});
|
491
|
}
|
492
|
return data;
|
493
|
},
|
494
|
|
495
|
addValueFieldEvents: function(){
|
496
|
this.addEvents({
|
497
|
'select': function(elements, data, text, index){
|
498
|
var option = this.valueField.getElement('option[value="' + this.options.valueFilter.call(this, data) + '"]');
|
499
|
if (option) option.selected = true;
|
500
|
},
|
501
|
'deselect': function(elements){
|
502
|
var option = this.valueField.getSelected()[0];
|
503
|
if (option) option.selected = false;
|
504
|
}
|
505
|
});
|
506
|
},
|
507
|
|
508
|
getValueFromValueField: function(){
|
509
|
return this.valueField.getSelected()[0].get('value');
|
510
|
}
|
511
|
|
512
|
});
|
513
|
|
514
|
Meio.Element = new Class({
|
515
|
|
516
|
Implements: [Events],
|
517
|
|
518
|
initialize: function(node){
|
519
|
this.setNode(node);
|
520
|
this.createBoundEvents();
|
521
|
this.attach();
|
522
|
},
|
523
|
|
524
|
setNode: function(node){
|
525
|
this.node = node ? $(node) || $$(node)[0] : this.render();
|
526
|
},
|
527
|
|
528
|
createBoundEvents: function(){
|
529
|
this.bound = {};
|
530
|
this.boundEvents.each(function(evt){
|
531
|
this.bound[evt] = function(e){
|
532
|
this.fireEvent('before' + evt.capitalize(), e);
|
533
|
this[evt] && this[evt](e);
|
534
|
this.fireEvent(evt, e);
|
535
|
return true;
|
536
|
}.bind(this);
|
537
|
}, this);
|
538
|
},
|
539
|
|
540
|
attach: function(){
|
541
|
for (var e in this.bound){
|
542
|
this.node.addEvent(e, this.bound[e]);
|
543
|
}
|
544
|
},
|
545
|
|
546
|
detach: function(){
|
547
|
for (var e in this.bound){
|
548
|
this.node.removeEvent(e, this.bound[e]);
|
549
|
}
|
550
|
},
|
551
|
|
552
|
addClass: function(type){
|
553
|
this.node.addClass(this.options.classes[type]);
|
554
|
},
|
555
|
|
556
|
removeClass: function(type){
|
557
|
this.node.removeClass(this.options.classes[type]);
|
558
|
},
|
559
|
|
560
|
toElement: function(){
|
561
|
this.node;
|
562
|
},
|
563
|
|
564
|
render: function(){}
|
565
|
|
566
|
});
|
567
|
|
568
|
Meio.Element.Field = new Class({
|
569
|
|
570
|
Extends: Meio.Element,
|
571
|
|
572
|
Implements: [Options],
|
573
|
|
574
|
options: {
|
575
|
classes: {
|
576
|
loading: 'ma-loading',
|
577
|
selected: 'ma-selected'
|
578
|
}
|
579
|
},
|
580
|
|
581
|
initialize: function(field, options){
|
582
|
this.keyPressControl = {};
|
583
|
this.boundEvents = ['paste', 'focus', 'blur', 'click', 'keyup', 'keyrepeat'];
|
584
|
if (browser.ie6) this.boundEvents.push('keypress'); // yeah super ugly, but what can be awesome with ie?
|
585
|
this.setOptions(options);
|
586
|
this.parent(field);
|
587
|
|
588
|
$(global).addEvent('unload', function(){
|
589
|
if (this.node) this.node.set('autocomplete', 'on'); // if autocomplete is off when you reload the page the input value gets erased
|
590
|
}.bind(this));
|
591
|
},
|
592
|
|
593
|
setNode: function(element){
|
594
|
this.parent(element);
|
595
|
this.node.set('autocomplete', 'off');
|
596
|
},
|
597
|
|
598
|
// this let me get the value of the input on keydown and keypress
|
599
|
keyrepeat: function(e){
|
600
|
clearInterval(this.keyrepeatTimer);
|
601
|
this.keyrepeatTimer = this._keyrepeat.delay(1, this, e);
|
602
|
},
|
603
|
|
604
|
_keyrepeat: function(e){
|
605
|
this.fireEvent('delayedKeyrepeat', e);
|
606
|
},
|
607
|
|
608
|
destroy: function(){
|
609
|
this.detach();
|
610
|
this.node.removeAttribute('autocomplete');
|
611
|
},
|
612
|
|
613
|
// ie6 only, uglyness
|
614
|
// this fix the form being submited on the press of the enter key
|
615
|
keypress: function(e){
|
616
|
if (e.key == 'enter') this.bound.keyrepeat(e);
|
617
|
}
|
618
|
|
619
|
});
|
620
|
|
621
|
Meio.Element.List = new Class({
|
622
|
|
623
|
Extends: Meio.Element,
|
624
|
|
625
|
Implements: [Options],
|
626
|
|
627
|
options: {
|
628
|
width: 'field', // you can pass any other value settable by set('width') to the list container
|
629
|
classes: {
|
630
|
container: 'ma-container',
|
631
|
hover: 'ma-hover',
|
632
|
odd: 'ma-odd',
|
633
|
even: 'ma-even'
|
634
|
}
|
635
|
},
|
636
|
|
637
|
initialize: function(options){
|
638
|
this.boundEvents = ['mousedown', 'mouseover'];
|
639
|
this.setOptions(options);
|
640
|
this.parent();
|
641
|
this.focusedItem = null;
|
642
|
},
|
643
|
|
644
|
applyMaxHeight: function(maxVisibleItems){
|
645
|
var listChildren = this.list.childNodes;
|
646
|
var node = listChildren[maxVisibleItems - 1] || (listChildren.length ? listChildren[listChildren.length - 1] : null);
|
647
|
if (!node) return;
|
648
|
node = $(node);
|
649
|
// uggly hack to fix the height of the autocomplete list
|
650
|
for (var i = 2; i--;) this.node.setStyle('height', node.getCoordinates(this.list).bottom);
|
651
|
},
|
652
|
|
653
|
mouseover: function(e){
|
654
|
var item = this.getItemFromEvent(e), hoverClass = this.options.classes.hover;
|
655
|
if (!item) return true;
|
656
|
if (this.focusedItem) this.focusedItem.removeClass(hoverClass);
|
657
|
item.addClass(hoverClass);
|
658
|
this.focusedItem = item;
|
659
|
this.fireEvent('focusItem', [this.focusedItem]);
|
660
|
},
|
661
|
|
662
|
mousedown: function(e){
|
663
|
e.preventDefault();
|
664
|
this.shouldNotBlur = true;
|
665
|
if (!(this.focusedItem = this.getItemFromEvent(e))){
|
666
|
e.dontHide = true;
|
667
|
return true;
|
668
|
}
|
669
|
this.focusedItem.removeClass(this.options.classes.hover);
|
670
|
},
|
671
|
|
672
|
focusItem: function(direction){
|
673
|
var hoverClass = this.options.classes.hover, newFocusedItem;
|
674
|
if (this.focusedItem){
|
675
|
if ((newFocusedItem = this.focusedItem[direction == 'up' ? 'getPrevious' : 'getNext']())){
|
676
|
this.focusedItem.removeClass(hoverClass);
|
677
|
newFocusedItem.addClass(hoverClass);
|
678
|
this.focusedItem = newFocusedItem;
|
679
|
this.scrollFocusedItem(direction);
|
680
|
}
|
681
|
} else {
|
682
|
if ((newFocusedItem = this.list.getFirst())){
|
683
|
newFocusedItem.addClass(hoverClass);
|
684
|
this.focusedItem = newFocusedItem;
|
685
|
}
|
686
|
}
|
687
|
},
|
688
|
|
689
|
scrollFocusedItem: function(direction){
|
690
|
var focusedItemCoordinates = this.focusedItem.getCoordinates(this.list),
|
691
|
scrollTop = this.node.scrollTop;
|
692
|
if (direction == 'down'){
|
693
|
var delta = focusedItemCoordinates.bottom - this.node.getStyle('height').toInt();
|
694
|
if ((delta - scrollTop) > 0){
|
695
|
this.node.scrollTop = delta;
|
696
|
}
|
697
|
} else {
|
698
|
var top = focusedItemCoordinates.top;
|
699
|
if (scrollTop && scrollTop > top){
|
700
|
this.node.scrollTop = top;
|
701
|
}
|
702
|
}
|
703
|
},
|
704
|
|
705
|
getItemFromEvent: function(e){
|
706
|
var target = e.target;
|
707
|
while (target && target.tagName.toLowerCase() != 'li'){
|
708
|
if (target === this.node) return null;
|
709
|
target = target.parentNode;
|
710
|
}
|
711
|
return $(target);
|
712
|
},
|
713
|
|
714
|
render: function(){
|
715
|
var node = new Element('div', {'class': this.options.classes.container});
|
716
|
if (node.bgiframe) node.bgiframe({top: 0, left: 0});
|
717
|
this.list = new Element('ul').inject(node);
|
718
|
$(document.body).grab(node);
|
719
|
return node;
|
720
|
},
|
721
|
|
722
|
positionNextTo: function(fieldNode){
|
723
|
var width = this.options.width, listNode = this.node;
|
724
|
var elPosition = fieldNode.getCoordinates();
|
725
|
listNode.setStyle('width', width == 'field' ? fieldNode.getWidth().toInt() - listNode.getStyle('border-left-width').toInt() - listNode.getStyle('border-right-width').toInt() : width);
|
726
|
listNode.setPosition({x: elPosition.left, y: elPosition.bottom});
|
727
|
},
|
728
|
|
729
|
show: function(){
|
730
|
this.node.scrollTop = 0;
|
731
|
this.node.setStyle('visibility', 'visible');
|
732
|
this.showing = true;
|
733
|
},
|
734
|
|
735
|
hide: function(){
|
736
|
this.showing = false;
|
737
|
this.node.setStyle('visibility', 'hidden');
|
738
|
}
|
739
|
|
740
|
});
|
741
|
|
742
|
Meio.Autocomplete.Filter = {
|
743
|
|
744
|
filters: {},
|
745
|
|
746
|
get: function(options){
|
747
|
var type = options.type, keys = (options.path || '').split('.');
|
748
|
var filters = (type && this.filters[type]) ? this.filters[type](this, keys) : options;
|
749
|
return Object.merge(this.defaults(keys), filters);
|
750
|
},
|
751
|
|
752
|
define: function(name, options){
|
753
|
this.filters[name] = options;
|
754
|
},
|
755
|
|
756
|
defaults: function(keys){
|
757
|
var self = this;
|
758
|
return {
|
759
|
filter: function(text, data){
|
760
|
return text ? self._getValueFromKeys(data, keys).test(new RegExp(text.escapeRegExp(), 'i')) : true;
|
761
|
},
|
762
|
formatMatch: function(text, data){
|
763
|
return self._getValueFromKeys(data, keys);
|
764
|
},
|
765
|
formatItem: function(text, data, i){
|
766
|
return text ? self._getValueFromKeys(data, keys).replace(new RegExp('(' + text.escapeRegExp() + ')', 'gi'), '<strong>$1</strong>') : self._getValueFromKeys(data, keys);
|
767
|
}
|
768
|
};
|
769
|
},
|
770
|
|
771
|
_getValueFromKeys: function(obj, keys){
|
772
|
var key, value = obj;
|
773
|
for (var i = 0; key = keys[i++];) value = value[key];
|
774
|
return value;
|
775
|
}
|
776
|
|
777
|
};
|
778
|
|
779
|
Meio.Autocomplete.Filter.define('contains', function(self, keys){return {};});
|
780
|
Meio.Autocomplete.Filter.define('startswith', function(self, keys){
|
781
|
return {
|
782
|
filter: function(text, data){
|
783
|
return text ? self._getValueFromKeys(data, keys).test(new RegExp('^' + text.escapeRegExp(), 'i')) : true;
|
784
|
}
|
785
|
};
|
786
|
});
|
787
|
|
788
|
Meio.Autocomplete.Data = new Class({
|
789
|
|
790
|
Implements: [Options, Events],
|
791
|
|
792
|
initialize: function(data, cache){
|
793
|
this._cache = cache;
|
794
|
this.data = data;
|
795
|
this.dataString = JSON.encode(this.data);
|
796
|
},
|
797
|
|
798
|
get: function(){
|
799
|
return this.data;
|
800
|
},
|
801
|
|
802
|
getKey: function(){
|
803
|
return this.cachedKey;
|
804
|
},
|
805
|
|
806
|
prepare: function(text){
|
807
|
this.cachedKey = this.dataString + (text || '');
|
808
|
this.fireEvent('ready');
|
809
|
},
|
810
|
|
811
|
cache: function(key, data){
|
812
|
this._cache.set(key, data);
|
813
|
},
|
814
|
|
815
|
refreshKey: function(){}
|
816
|
|
817
|
});
|
818
|
|
819
|
Meio.Autocomplete.Data.Request = new Class({
|
820
|
|
821
|
Extends: Meio.Autocomplete.Data,
|
822
|
|
823
|
options: {
|
824
|
noCache: true,
|
825
|
formatResponse: function(jsonResponse){
|
826
|
return jsonResponse;
|
827
|
}
|
828
|
},
|
829
|
|
830
|
initialize: function(url, cache, element, options, urlOptions){
|
831
|
this.setOptions(options);
|
832
|
this.rawUrl = url;
|
833
|
this._cache = cache;
|
834
|
this.element = element;
|
835
|
this.urlOptions = urlOptions;
|
836
|
this.refreshKey();
|
837
|
this.createRequest();
|
838
|
},
|
839
|
|
840
|
prepare: function(text){
|
841
|
this.cachedKey = this.url.evaluate(text);
|
842
|
if (this._cache.has(this.cachedKey)){
|
843
|
this.fireEvent('ready');
|
844
|
} else {
|
845
|
this.request.send({url: this.cachedKey});
|
846
|
}
|
847
|
},
|
848
|
|
849
|
createRequest: function(){
|
850
|
var self = this;
|
851
|
this.request = new Request.JSON(this.options);
|
852
|
this.request.addEvents({
|
853
|
request: function(){
|
854
|
self.element.addClass('loading');
|
855
|
},
|
856
|
complete: function(){
|
857
|
self.element.removeClass('loading');
|
858
|
},
|
859
|
success: function(jsonResponse){
|
860
|
self.data = self.options.formatResponse(jsonResponse);
|
861
|
self.fireEvent('ready');
|
862
|
}
|
863
|
});
|
864
|
},
|
865
|
|
866
|
refreshKey: function(urlOptions){
|
867
|
urlOptions = Object.merge(this.urlOptions, {url: this.rawUrl}, urlOptions || {});
|
868
|
this.url = new Meio.Autocomplete.Data.Request.URL(urlOptions.url, urlOptions);
|
869
|
}
|
870
|
|
871
|
});
|
872
|
|
873
|
Meio.Autocomplete.Data.Request.URL = new Class({
|
874
|
|
875
|
Implements: [Options],
|
876
|
|
877
|
options: {
|
878
|
queryVarName: 'q',
|
879
|
extraParams: null,
|
880
|
max: 20
|
881
|
},
|
882
|
|
883
|
initialize: function(url, options){
|
884
|
this.setOptions(options);
|
885
|
this.rawUrl = url;
|
886
|
this.url = url;
|
887
|
this.url += this.url.contains('?') ? '&' : '?';
|
888
|
this.dynamicExtraParams = [];
|
889
|
var params = Array.from(this.options.extraParams);
|
890
|
for (var i = params.length; i--;){
|
891
|
this.addParameter(params[i]);
|
892
|
}
|
893
|
if (this.options.max) this.addParameter('limit=' + this.options.max);
|
894
|
},
|
895
|
|
896
|
evaluate: function(text){
|
897
|
text = text || '';
|
898
|
var params = this.dynamicExtraParams, url = [];
|
899
|
url.push(this.options.queryVarName + '=' + encodeURIComponent(text));
|
900
|
for (var i = params.length; i--;){
|
901
|
url.push(encodeURIComponent(params[i].name) + '=' + encodeURIComponent(Function.from(params[i].value)()));
|
902
|
}
|
903
|
return this.url + url.join('&');
|
904
|
},
|
905
|
|
906
|
addParameter: function(param){
|
907
|
if (param.nodeType == 1 || typeOf(param.value) == 'function'){
|
908
|
this.dynamicExtraParams.push(param);
|
909
|
} else {
|
910
|
this.url += ((typeOf(param) == 'string') ? param : encodeURIComponent(param.name) + '=' + encodeURIComponent(param.value)) + '&';
|
911
|
}
|
912
|
},
|
913
|
|
914
|
// TODO remove non dynamic parameters
|
915
|
removeParameter: function(param){
|
916
|
this.dynamicExtraParams.erase(param);
|
917
|
}
|
918
|
|
919
|
});
|
920
|
|
921
|
Meio.Autocomplete.Cache = new Class({
|
922
|
|
923
|
initialize: function(maxLength){
|
924
|
this.refresh();
|
925
|
this.setMaxLength(maxLength);
|
926
|
},
|
927
|
|
928
|
set: function(key, value){
|
929
|
if (!this.cache[key]){
|
930
|
if (this.getLength() >= this.maxLength){
|
931
|
var keyToRemove = this.pos.shift();
|
932
|
this.cache[keyToRemove] = null;
|
933
|
delete this.cache[keyToRemove];
|
934
|
}
|
935
|
this.cache[key] = value;
|
936
|
this.pos.push(key);
|
937
|
}
|
938
|
return this;
|
939
|
},
|
940
|
|
941
|
get: function(key){
|
942
|
return this.cache[key || ''] || null;
|
943
|
},
|
944
|
|
945
|
has: function(key){
|
946
|
return !!this.get(key);
|
947
|
},
|
948
|
|
949
|
getLength: function(){
|
950
|
return this.pos.length;
|
951
|
},
|
952
|
|
953
|
refresh: function(){
|
954
|
this.cache = {};
|
955
|
this.pos = [];
|
956
|
},
|
957
|
|
958
|
setMaxLength: function(maxLength){
|
959
|
this.maxLength = Math.max(maxLength, 1);
|
960
|
}
|
961
|
|
962
|
});
|
963
|
|
964
|
globalCache = new Meio.Autocomplete.Cache();
|
965
|
|
966
|
global.Meio = Meio;
|
967
|
|
968
|
})(this, document.id || $);
|