forked from fabiomcosta/mootools-meio-autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Meio.Autocomplete.js
1337 lines (1041 loc) · 37.4 KB
/
Meio.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
---
description: Port of bgiframe plugin for mootools
authors:
- Fábio Miranda Costa
requires:
- Core/Class.Extras
license: MIT-style license Original plugin copyright Copyright (c) 2006 Brandon Aaron (http://brandonaaron.net) Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses. Version 2.1.1
provides: [BGIFrame]
...
*/
(function(global, $) {
var isIE6 = Browser.ie6; // better compression and faster
var BgIframe = new Class({
Implements: Options,
options: {
top : 'auto',
left : 'auto',
width : 'auto',
height : 'auto',
opacity : true,
src : 'javascript:false;'
},
initialize: function(element, options) {
if (!isIE6) return;
this.setOptions(options);
this.element = $(element);
var firstChild = this.element.getFirst();
if (!(firstChild && firstChild.hasClass('bgiframe'))) {
this.element.grab(document.createElement(this.render()), 'top');
}
},
toPx: function(n) {
return isFinite(n) ? n + 'px' : n;
},
render: function() {
var options = this.options;
return ['<iframe class="bgiframe" frameborder="0" tabindex="-1" src="', options.src, '" ',
'style="display:block;position:absolute;z-index:-1;',
(options.opacity !== false ? 'filter:alpha(opacity=\'0\');' : ''),
'top:', (options.top === 'auto' ? 'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')' : this.toPx(options.top)), ';',
'left:', (options.left === 'auto' ? 'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')' : this.toPx(options.left)), ';',
'width:', (options.width === 'auto' ? 'expression(this.parentNode.offsetWidth+\'px\')' : this.toPx(options.width)), ';',
'height:', (options.height === 'auto' ? 'expression(this.parentNode.offsetHeight+\'px\')' : this.toPx(options.height)), ';',
'"/>'].join('');
}
});
Element.implement('bgiframe', function(options) {
if (isIE6) new BgIframe(this, options);
return this;
});
})(this, document.id || this.$);
/*
---
description: A plugin for enabling autocomplete of a text input or textarea.
authors:
- Fábio Miranda Costa
requires:
- Core/Class.Extras
- Core/Element.Event
- Core/Element.Style
- More/Element.Forms
license: MIT-style license
provides: [Meio]
...
*/
(function(global, $) {
var browser = Browser; // better compression and faster
// Custom Events
// thanks Jan Kassens
Object.append(Element.NativeEvents, {
'paste': 2, 'input': 2
});
Element.Events.paste = {
base : (browser.opera || (browser.firefox && browser.version < 3)) ? 'input' : 'paste',
condition: function(e) {
this.fireEvent('paste', e, 1);
return false;
}
};
// the key event that repeats
Element.Events.keyrepeat = {
base : (browser.firefox || browser.opera) ? 'keypress' : 'keydown',
condition: Function.from(true)
};
// Meio namespace
global.Meio = global.Meio || {};
}(this, document.id || this.$));
/*
---
description: A plugin for enabling autocomplete of a text input or textarea.
authors:
- Fábio Miranda Costa
requires:
- Meio.Base
license: MIT-style license
provides: [Meio.Widget]
...
*/
Meio.Widget = new Class({
initialize: function() {
this.elements = {};
},
addElement: function(name, obj) {
this.elements[name] = obj;
},
addEventToElement: function(name, eventName, event) {
this.elements[name].addEvent(eventName, event.bind(this));
},
addEventsToElement: function(name, events) {
for (var eventName in events) {
this.addEventToElement(name, eventName, events[eventName]);
}
},
attach: function() {
for (var element in this.elements) {
this.elements[element].attach();
}
},
detach: function() {
for (var element in this.elements) {
this.elements[element].detach();
}
},
destroy: function() {
for (var element in this.elements) {
if (this.elements[element]) {
this.elements[element].destroy();
}
}
}
});
/*
---
description: A plugin for enabling autocomplete of a text input or textarea.
authors:
- Fábio Miranda Costa
requires:
- Meio.Base
license: MIT-style license
provides: [Meio.Element]
...
*/
(function(global, $) {
global.Meio.Element = new Class({
Implements: [Events],
initialize: function(node) {
this.setNode(node);
this.createBoundEvents();
this.attach();
},
setNode: function(node) {
this.node = node ? $(node) || $$(node)[0] : this.render();
},
createBoundEvents: function() {
this.bound = {};
this.boundEvents.each(function(evt) {
this.bound[evt] = function(e) {
this.fireEvent('before' + evt.capitalize(), e);
if (this[evt]) this[evt](e);
this.fireEvent(evt, e);
return true;
}.bind(this);
}, this);
},
attach: function() {
for (var e in this.bound) {
this.node.addEvent(e, this.bound[e]);
}
},
detach: function() {
for (var e in this.bound) {
this.node.removeEvent(e, this.bound[e]);
}
},
addClass: function(type) {
this.node.addClass(this.options.classes[type]);
},
removeClass: function(type) {
this.node.removeClass(this.options.classes[type]);
},
toElement: function() {
return this.node;
},
render: function() {}
});
}(this, document.id || this.$));
/*
---
description: A plugin for enabling autocomplete of a text input or textarea.
authors:
- Fábio Miranda Costa
requires:
- Meio.Element
license: MIT-style license
provides: [Meio.Element.Field]
...
*/
(function(global, $) {
global.Meio.Element.Field = new Class({
Extends: global.Meio.Element,
Implements: [Options],
options: {
classes: {
loading: 'ma-loading',
selected: 'ma-selected'
}
},
initialize: function(field, options) {
this.keyPressControl = {};
this.boundEvents = ['paste', 'focus', 'blur', 'click', 'keyup', 'keyrepeat'];
if (Browser.ie6) this.boundEvents.push('keypress'); // ie6 doesnt fire keydown on enter
this.setOptions(options);
this.parent(field);
$(global).addEvent('unload', function() {
// if autocomplete is off when you reload the page the input value gets erased
if (this.node) $(this.node).set('autocomplete', 'on');
}.bind(this));
},
setNode: function(element) {
this.parent(element);
this.node.set('autocomplete', 'off');
},
// this let me get the value of the input on keydown and keypress
keyrepeat: function(e) {
clearInterval(this.keyrepeatTimer);
this.keyrepeatTimer = this._keyrepeat.delay(1, this, e);
},
_keyrepeat: function(e) {
this.fireEvent('delayedKeyrepeat', e);
},
destroy: function() {
this.detach();
this.node.removeAttribute('autocomplete');
},
// ie6 only, uglyness
// this fix the form being submited on the press of the enter key
keypress: function(e) {
if (e.key === 'enter') this.bound.keyrepeat(e);
}
});
}(this, document.id || this.$));
/*
---
description: A plugin for enabling autocomplete of a text input or textarea.
authors:
- Fábio Miranda Costa
requires:
- Meio.Element
license: MIT-style license
provides: [Meio.Element.List]
...
*/
(function(global, $) {
global.Meio.Element.List = new Class({
Extends: global.Meio.Element,
Implements: [Options],
options: {
width: 'field', // you can pass any other value settable by set('width') to the list container
container: 'body',
classes: {
container: 'ma-container',
hover: 'ma-hover',
odd: 'ma-odd',
even: 'ma-even'
}
},
initialize: function(options) {
this.boundEvents = ['mousedown', 'mouseover'];
this.setOptions(options);
this.parent();
this.focusedItem = null;
},
applyMaxHeight: function(maxVisibleItems) {
var listChildren = this.list.childNodes;
var node = listChildren[maxVisibleItems - 1] || (listChildren.length ? listChildren[listChildren.length - 1] : null);
if (!node) return;
node = $(node);
// uggly hack to fix the height of the autocomplete list
for (var i = 2; i--;) this.node.setStyle('height', node.getCoordinates(this.list).bottom);
},
mouseover: function(e) {
var item = this.getItemFromEvent(e), hoverClass = this.options.classes.hover;
if (!item) return true;
if (this.focusedItem) this.focusedItem.removeClass(hoverClass);
item.addClass(hoverClass);
this.focusedItem = item;
this.fireEvent('focusItem', [this.focusedItem]);
},
mousedown: function(e) {
e.preventDefault();
this.shouldNotBlur = true;
if (!(this.focusedItem = this.getItemFromEvent(e))) {
e.dontHide = true;
return true;
}
this.focusedItem.removeClass(this.options.classes.hover);
},
focusItem: function(direction) {
var hoverClass = this.options.classes.hover, newFocusedItem;
if (this.focusedItem) {
if ((newFocusedItem = this.focusedItem[direction == 'up' ? 'getPrevious' : 'getNext']())) {
this.focusedItem.removeClass(hoverClass);
newFocusedItem.addClass(hoverClass);
this.focusedItem = newFocusedItem;
this.scrollFocusedItem(direction);
}
} else {
if ((newFocusedItem = this.list.getFirst())) {
newFocusedItem.addClass(hoverClass);
this.focusedItem = newFocusedItem;
}
}
},
scrollFocusedItem: function(direction) {
var focusedItemCoordinates = this.focusedItem.getCoordinates(this.list),
scrollTop = this.node.scrollTop;
if (direction == 'down') {
var delta = focusedItemCoordinates.bottom - this.node.getStyle('height').toInt();
if ((delta - scrollTop) > 0) {
this.node.scrollTop = delta;
}
} else {
var top = focusedItemCoordinates.top;
if (scrollTop && scrollTop > top) {
this.node.scrollTop = top;
}
}
},
getItemFromEvent: function(e) {
var target = e.target;
while (target && target.tagName.toLowerCase() != 'li') {
if (target === this.node) return null;
target = target.parentNode;
}
return $(target);
},
render: function() {
var node = new Element('div', {'class': this.options.classes.container});
if (node.bgiframe) node.bgiframe({top: 0, left: 0});
this.list = new Element('ul').inject(node);
$$(this.options.container)[0].grab(node);
return node;
},
positionNextTo: function(fieldNode) {
var width = this.options.width, listNode = this.node;
var elPosition = fieldNode.getCoordinates();
listNode.setStyle('width', width == 'field' ? fieldNode.getWidth().toInt() - listNode.getStyle('border-left-width').toInt() - listNode.getStyle('border-right-width').toInt() : width);
listNode.setPosition({x: elPosition.left, y: elPosition.bottom});
},
show: function() {
this.node.scrollTop = 0;
this.node.setStyle('visibility', 'visible');
this.showing = true;
},
hide: function() {
this.showing = false;
this.node.setStyle('visibility', 'hidden');
}
});
}(this, document.id || this.$));
/*
---
description: A plugin for enabling autocomplete of a text input or textarea.
authors:
- Fábio Miranda Costa
requires:
- Meio.Widget
- Meio.Element.List
- Meio.Element.Field
license: MIT-style license
provides: [Meio.Autocomplete]
...
*/
(function(global, $) {
var Meio = global.Meio;
var keysThatDontChangeValueOnKeyUp = {
9: 1, // tab
16: 1, // shift
17: 1, // control
18: 1, // alt
224: 1, // command (meta onkeypress)
91: 1, // command (meta onkeydown)
37: 1, // left
38: 1, // up
39: 1, // right
40: 1 // down
};
var encode = function(str) {
return str.replace(/"/g, '"').replace(/'/g, ''');
};
Meio.Autocomplete = new Class({
Extends: Meio.Widget,
Implements: [Options, Events],
options: {
delay: 200,
minChars: 0,
cacheLength: 20,
selectOnTab: true,
maxVisibleItems: 10,
cacheType: 'shared', // 'shared' or 'own'
filter: {
/*
its posible to pass the filters directly or by passing a type and optionaly a path.
filter: function(text, data) {}
formatMatch: function(text, data, i) {}
formatItem: function(text, data) {}
or
type: 'startswith' or 'contains' // can be any defined on the Meio.Autocomplete.Filter object
path: 'a.b.c' // path to the text value on each object thats contained on the data array
*/
},
/*
onNoItemToList: function(elements) {},
onSelect: function(elements, value) {},
onDeselect: function(elements) {},
*/
fieldOptions: {}, // see Element options
listOptions: {}, // see List options
requestOptions: {}, // see DataRequest options
urlOptions: {} // see URL options
},
initialize: function(input, data, options, listInstance) {
this.parent();
this.setOptions(options);
this.active = 0;
this.filters = Meio.Autocomplete.Filter.get(this.options.filter);
this.addElement('list', listInstance || new Meio.Element.List(this.options.listOptions));
this.addListEvents();
this.addElement('field', new Meio.Element.Field(input, this.options.fieldOptions));
this.addFieldEvents();
this.addSelectEvents();
this.attach();
this.initCache();
this.initData(data);
},
addFieldEvents: function() {
this.addEventsToElement('field', {
'beforeKeyrepeat': function(e) {
this.active = 1;
var e_key = e.key, list = this.elements.list;
if (e_key == 'up' || e_key == 'down' || (e_key == 'enter' && list.showing)) e.preventDefault();
},
'delayedKeyrepeat': function(e) {
var e_key = e.key, field = this.elements.field;
field.keyPressControl[e_key] = true;
switch (e_key) {
case 'up': case 'down':
this.focusItem(e_key);
break;
case 'enter':
this.setInputValue();
break;
case 'tab':
if (this.options.selectOnTab) this.setInputValue();
field.keyPressControl[e_key] = false; // tab blurs the input so the keyup event wont happen at the same input you made a keydown
break;
case 'esc':
this.elements.list.hide();
break;
default:
this.setupList();
}
this.oldInputedText = field.node.get('value');
},
'keyup': function(e) {
var field = this.elements.field;
if (!keysThatDontChangeValueOnKeyUp[e.code]) {
if (!field.keyPressControl[e.key]) this.setupList();
field.keyPressControl[e.key] = false;
}
},
'focus': function() {
this.active = 1;
var list = this.elements.list;
list.focusedItem = null;
list.positionNextTo(this.elements.field.node);
},
'click': function() {
if (++this.active > 2 && !this.elements.list.showing) {
this.forceSetupList();
}
},
'blur': function(e) {
this.active = 0;
var list = this.elements.list;
if (list.shouldNotBlur) {
this.elements.field.node.setCaretPosition('end');
list.shouldNotBlur = false;
if (list.focusedItem) list.hide();
} else {
list.hide();
}
},
'paste': function() {
return this.setupList();
}
});
},
addListEvents: function() {
this.addEventsToElement('list', {
'mousedown': function(e) {
if (this.active && !e.dontHide) this.setInputValue();
}
});
},
update: function() {
var data = this.data, list = this.elements.list;
var cacheKey = data.getKey(), cached = this.cache.get(cacheKey), html;
if (cached) {
html = cached.html;
this.itemsData = cached.data;
} else {
data = data.get();
var itemsHtml = [], itemsData = [], classes = list.options.classes, text = this.inputedText;
var filter = this.filters.filter, formatMatch = this.filters.formatMatch, formatItem = this.filters.formatItem;
for (var row, i = 0, n = 0; row = data[i++];) if (filter.call(this, text, row)) {
itemsHtml.push(
'<li title="', encode(formatMatch.call(this, text, row)),
'" data-index="', n,
'" class="', (n%2 ? classes.even : classes.odd), '">',
formatItem.call(this, text, row, n),
'</li>'
);
itemsData.push(row);
n++;
}
html = itemsHtml.join('');
this.cache.set(cacheKey, {html: html, data: itemsData});
this.itemsData = itemsData;
}
list.focusedItem = null;
this.fireEvent('deselect', [this.elements]);
list.list.set('html', html);
if (this.options.maxVisibleItems) list.applyMaxHeight(this.options.maxVisibleItems);
},
setupList: function() {
this.inputedText = this.elements.field.node.get('value');
if (this.inputedText !== this.oldInputedText) {
this.forceSetupList(this.inputedText);
} else {
this.elements.list.hide();
}
return true;
},
forceSetupList: function(inputedText) {
inputedText = inputedText || this.elements.field.node.get('value');
if (inputedText.length >= this.options.minChars) {
clearInterval(this.prepareTimer);
this.prepareTimer = this.data.prepare.delay(this.options.delay, this.data, this.inputedText);
}
},
dataReady: function() {
this.update();
if (this.onUpdate) {
this.onUpdate();
this.onUpdate = null;
}
var list = this.elements.list;
if (list.list.get('html')) {
if (this.active) list.show();
} else {
this.fireEvent('noItemToList', [this.elements]);
list.hide();
}
},
setInputValue: function() {
var list = this.elements.list;
if (list.focusedItem) {
var text = list.focusedItem.get('title');
this.elements.field.node.set('value', text);
var index = list.focusedItem.get('data-index');
this.fireEvent('select', [this.elements, this.itemsData[index], text, index]);
}
list.hide();
},
focusItem: function(direction) {
var list = this.elements.list;
if (list.showing) {
list.focusItem(direction);
} else {
this.forceSetupList();
this.onUpdate = function() { list.focusItem(direction); };
}
},
addSelectEvents: function() {
this.addEvents({
select: function(elements) {
elements.field.addClass('selected');
},
deselect: function(elements) {
elements.field.removeClass('selected');
}
});
},
initData: function(data) {
this.data = (typeOf(data) == 'string') ?
new Meio.Autocomplete.Data.Request(data, this.cache, this.elements.field, this.options.requestOptions, this.options.urlOptions) :
(typeOf(data) == 'function') ? new Meio.Autocomplete.Data.Source(data, this.cache, this.elements.field) :
new Meio.Autocomplete.Data(data, this.cache);
this.data.addEvent('ready', this.dataReady.bind(this));
},
initCache: function() {
var cacheLength = this.options.cacheLength;
if (this.options.cacheType == 'shared') {
this.cache = Meio.Autocomplete.Cache.instance;
this.cache.setMaxLength(cacheLength);
} else { // 'own'
this.cache = new Meio.Autocomplete.Cache(cacheLength);
}
},
refreshCache: function(cacheLength) {
this.cache.refresh();
this.cache.setMaxLength(cacheLength || this.options.cacheLength);
},
refreshAll: function(cacheLength, urlOptions) {
// TODO, do you really need to refresh the url? see a better way of doing this
this.refreshCache(cacheLength);
this.data.refreshKey(urlOptions);
}
});
}(this, document.id || this.$));
/*
---
description:
This is the same autocomplete class but it acts like a normal select element.
When you select an option from the autocomplete it will set the value of a given element (valueField)
with the return of the valueFilter.
if the syncAtInit option is set to true, it will synchonize the value of the autocomplete with the corresponding data
from the valueField's value.
to understand better see the user specs.
authors:
- Fábio Miranda Costa
requires:
- Meio.Autocomplete
license: MIT-style license
provides: [Meio.Autocomplete.Select]
...
*/
(function(global, $) {
global.Meio.Autocomplete.Select = new Class({
Extends: global.Meio.Autocomplete,
options: {
syncName: 'id', // if falsy it wont sync at start
valueField: null,
valueFilter: function(data) {
return data.id;
}
},
// overwritten
initialize: function(input, data, options, listInstance) {
this.parent(input, data, options, listInstance);
this.valueField = $(this.options.valueField);
if (!this.valueField) return;
this.syncWithValueField(data);
},
syncWithValueField: function(data) {
var value = this.getValueFromValueField();
if (value && this.options.syncName) {
this.addParameter(data);
this.addDataReadyEvent(value);
this.data.prepare(this.elements.field.node.get('value'));
} else {
this.addValueFieldEvents();
}
},
addValueFieldEvents: function() {
this.addEvents({
'select': function(elements, data) {
this.valueField.set('value', this.options.valueFilter.call(this, data));
},
'deselect': function(elements) {
this.valueField.set('value', '');
}
});
},
addParameter: function(data) {
this.parameter = {
name: this.options.syncName,
value: function() {
return this.valueField.value;
}.bind(this)
};
if (this.data.url) this.data.url.addParameter(this.parameter);
},
addDataReadyEvent: function(value) {
var self = this;
var runOnce = function() {
self.addValueFieldEvents();
var values = this.get();
for (var i = values.length; i--;) {
if (self.options.valueFilter.call(self, values[i]) == value) {
var text = self.filters.formatMatch.call(self, '', values[i], 0);
self.elements.field.node.set('value', text);
self.fireEvent('select', [self.elements, values[i], text, i]);
break;
}
}
if (this.url) this.url.removeParameter(self.parameter);
this.removeEvent('ready', runOnce);
};
this.data.addEvent('ready', runOnce);
},
getValueFromValueField: function() {
return this.valueField.get('value');
}
});
}(this, document.id || this.$));
/*
---
description: Transforms a select on an autocomplete field.
authors:
- Fábio Miranda Costa
requires:
- Meio.Autocomplete.Select
license: MIT-style license
provides: [Meio.Autocomplete.Select.One]
...
*/
(function(global, $) {
Meio.Autocomplete.Select.One = new Class({
Extends: Meio.Autocomplete.Select,
options: {
filter: {
path: 'text' // path to the text value on each object thats contained on the data array
}
},
//overwritten
initialize: function(select, options, listInstance) {
this.select = $(select);
this.replaceSelect();
this.parent(this.field, this.createDataArray(), Object.merge(options || {}, {
valueField: this.select,
valueFilter: function(data) {
return data.value;
}
}), listInstance);
},
replaceSelect: function() {
var selectedOption = this.select.getSelected()[0];
this.field = new Element('input', {type: 'text'});
var optionValue = selectedOption.get('value');
if (optionValue || optionValue === 0) {
this.field.set('value', selectedOption.get('html'));
}
this.select.setStyle('display', 'none');
this.field.inject(this.select, 'after');
},
createDataArray: function() {
var selectOptions = this.select.options, data = [];
for (var i = 0, selectOption, optionValue; selectOption = selectOptions[i++];) {
optionValue = selectOption.value;
if (optionValue || optionValue === 0) data.push({value: optionValue, text: selectOption.innerHTML});
}
return data;
},
addValueFieldEvents: function() {
this.addEvents({
'select': function(elements, data, text, index) {
var option = this.valueField.getElement('option[value="' + this.options.valueFilter.call(this, data) + '"]');
if (option) option.selected = true;
},
'deselect': function(elements) {
var option = this.valueField.getSelected()[0];
if (option) option.selected = false;
}
});
},
getValueFromValueField: function() {
return this.valueField.getSelected()[0].get('value');
}
});
}(this, document.id || this.$));
/*
---
description: A plugin for enabling autocomplete of a text input or textarea.
authors:
- Fábio Miranda Costa
requires:
- Meio.Autocomplete
license: MIT-style license
provides: [Meio.Autocomplete.Filter]
...
*/
Meio.Autocomplete.Filter = {
filters: {},
get: function(options) {
var type = options.type, keys = (options.path || '').split('.');
var filters = (type && this.filters[type]) ? this.filters[type](this, keys) : options;
return Object.merge(this.defaults(keys), filters);
},
define: function(name, options) {
this.filters[name] = options;
},
defaults: function(keys) {