-
Notifications
You must be signed in to change notification settings - Fork 41
/
datalist.polyfill.js
214 lines (190 loc) · 8.03 KB
/
datalist.polyfill.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
// https://github.com/Fyrd/purejs-datalist-polyfill
// license: MIT
(function(document) {
var IE_SELECT_ATTRIBUTE = 'data-datalist';
var LIST_CLASS = 'datalist-polyfill';
var ACTIVE_CLASS = 'datalist-polyfill__active';
var datalistSupported = !!(document.createElement('datalist') && window.HTMLDataListElement);
var ua = navigator.userAgent;
// Android does not have actual support
var isAndroidBrowser = ua.match(/Android/) && !ua.match(/(Firefox|Chrome|Opera|OPR)/);
if( datalistSupported && !isAndroidBrowser ) {
return;
}
var inputs = document.querySelectorAll('input[list]');
var triggerEvent = function(elem, eventType) {
var event;
if (document.createEvent) {
event = document.createEvent("HTMLEvents");
event.initEvent(eventType, true, true);
elem.dispatchEvent(event);
} else {
event = document.createEventObject();
event.eventType = eventType;
elem.fireEvent("on" + eventType, event);
}
};
for( var i = 0; i < inputs.length; i++ ) {
var input = inputs[i];
var listId = input.getAttribute('list');
var datalist = document.getElementById(listId);
if( !datalist ) {
console.error('No datalist found for input: ' + listId);
return;
}
// Only visible to <= IE9
var childSelect = document.querySelector('select[' + IE_SELECT_ATTRIBUTE + '="' + listId + '"]');
var parent = childSelect || datalist;
var listItems = parent.getElementsByTagName('option');
convert(input, datalist, listItems);
if( childSelect ) {
childSelect.parentNode.removeChild( childSelect );
}
}
function convert(input, datalist, listItems) {
var fakeList = document.createElement('ul');
var visibleItems = null;
fakeList.id = listId;
fakeList.className = LIST_CLASS;
document.body.appendChild( fakeList );
var scrollValue = 0;
// Used to prevent reflow
var tempItems = document.createDocumentFragment();
for( var i = 0; i < listItems.length; i++ ) {
var item = listItems[i];
var li = document.createElement('li');
li.innerText = item.value;
tempItems.appendChild( li );
}
fakeList.appendChild( tempItems );
var fakeItems = fakeList.childNodes;
var eachItem = function(callback) {
for( var i = 0; i < fakeItems.length; i++ ) {
callback(fakeItems[i]);
}
};
var listen = function(elem, event, func) {
if( elem.addEventListener ) {
elem.addEventListener(event, func, false);
} else {
elem.attachEvent('on' + event, func);
}
};
datalist.parentNode.removeChild( datalist );
listen(input, 'focus', function() {
// Reset scroll
fakeList.scrollTop = 0;
scrollValue = 0;
});
listen(input, 'blur', function(evt) {
// If this fires immediately, it prevents click-to-select from working
setTimeout(function() {
fakeList.style.display = 'none';
eachItem( function(item) {
// Note: removes all, not just ACTIVE_CLASS, but should be safe
item.className = '';
});
}, 100);
});
var positionList = function() {
fakeList.style.top = input.offsetTop + input.offsetHeight + 'px';
fakeList.style.left = input.offsetLeft + 'px';
fakeList.style.width = input.offsetWidth + 'px';
};
var itemSelected = function(item) {
input.value = item.innerText;
triggerEvent(input, 'change');
setTimeout(function() {
fakeList.style.display = 'none';
}, 100);
};
var buildList = function(e) {
// Build datalist
fakeList.style.display = 'block';
positionList();
visibleItems = [];
eachItem( function(item) {
// Note: removes all, not just ACTIVE_CLASS, but should be safe
var query = input.value.toLowerCase();
var isFound = query.length && item.innerText.toLowerCase().indexOf( query ) > -1;
if( isFound ) {
visibleItems.push( item );
}
item.style.display = isFound ? 'block' : 'none';
} );
};
listen(input, 'keyup', buildList);
listen(input, 'focus', buildList);
// Don't want to use :hover in CSS so doing this instead
// really helps with arrow key navigation
eachItem( function(item) {
// Note: removes all, not just ACTIVE_CLASS, but should be safe
listen(item, 'mouseover', function(evt) {
eachItem( function(_item) {
_item.className = item == _item ? ACTIVE_CLASS : '';
});
});
listen(item, 'mouseout', function(evt) {
item.className = '';
});
// Mousedown fires before native 'change' event is triggered
// So we use this instead of click so only the new value is passed to 'change'
listen(item, 'mousedown', function(evt) {
itemSelected(item);
});
});
listen(window, 'resize', positionList);
listen(input, 'keydown', function(e) {
var activeItem = fakeList.querySelector("." + ACTIVE_CLASS);
if( !visibleItems.length ) {
return;
}
var lastVisible = visibleItems[ visibleItems.length-1 ];
var datalistItemsHeight = lastVisible.offsetTop + lastVisible.offsetHeight;
// up/down arrows
var isUp = e.keyCode == 38;
var isDown = e.keyCode == 40;
if ( (isUp || isDown) ) {
if( isDown && !activeItem ) {
visibleItems[0].className = ACTIVE_CLASS;
} else if (activeItem) {
var prevVisible = null;
var nextVisible = null;
for( var i = 0; i < visibleItems.length; i++ ) {
var visItem = visibleItems[i];
if( visItem == activeItem ) {
prevVisible = visibleItems[i-1];
nextVisible = visibleItems[i+1];
break;
}
}
activeItem.className = '';
if ( isUp ) {
if( prevVisible ) {
prevVisible.className = ACTIVE_CLASS;
if ( prevVisible.offsetTop < fakeList.scrollTop ) {
fakeList.scrollTop -= prevVisible.offsetHeight;
}
} else {
visibleItems[visibleItems.length - 1].className = ACTIVE_CLASS;
}
}
if ( isDown ) {
if( nextVisible ) {
nextVisible.className = ACTIVE_CLASS;
if( nextVisible.offsetTop + nextVisible.offsetHeight > fakeList.scrollTop + fakeList.offsetHeight ) {
fakeList.scrollTop += nextVisible.offsetHeight;
}
} else {
visibleItems[0].className = ACTIVE_CLASS;
}
}
}
}
// return or tab key
if ( activeItem && (e.keyCode == 13 || e.keyCode == 9) ){
itemSelected(activeItem);
}
});
}
}(document));