-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombobox.js
201 lines (196 loc) · 6.09 KB
/
combobox.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
/*
ComboBox Object
http://www.zoonman.com/projects/combobox/
Copyright (c) 2011, Tkachev Philipp
All rights reserved.
BSD License
*/
ComboBox = function(object_name) {
// Edit element cache
this.edit = document.getElementById(object_name);
// Items Container
var ddl = document.getElementById(object_name)
.parentNode
.getElementsByTagName('DIV');
this.dropdownlist = ddl[0];
// Current Item
this.currentitem = null;
// Current Item Index
this.currentitemindex = null;
// Visible Items Count
this.visiblecount = 0;
// Closure Object
var parobject = this;
// Picker
var pick = document.getElementById(object_name)
.parentNode
.getElementsByTagName('SPAN');
pick[0].onclick = function() {
parobject.edit.focus();
};
// Show Items when edit get focus
this.edit.onfocus = function() {
parobject.dropdownlist.style.display = 'block';
};
// Hide Items when edit lost focus
this.edit.onblur = function() {
if (allowLoose) {
setTimeout(function() {
parobject.dropdownlist.style.display = 'none';
},
150
);
}
};
var allowLoose = true;
// IE fix
parobject.dropdownlist.onmousedown = function(event) {
allowLoose = false;
return false;
};
parobject.dropdownlist.onmouseup = function(event) {
setTimeout(function() {
allowLoose = true;
}, 150);
return false;
};
// Get Items
this.listitems = this.dropdownlist.getElementsByTagName('A');
for (var i = 0; i < this.listitems.length; i++) {
var t = i;
// Binding Click Event
this.listitems[i].onclick = function() {
var upv = this.innerHTML;
upv = upv.replace(/\<b\>/ig, '');
upv = upv.replace(/\<\/b\>/ig, '');
parobject.edit.value = upv;
parobject.dropdownlist.style.display = 'none';
return false;
};
// Binding OnMouseOver Event
this.listitems[i].onmouseover = function(e) {
for (var i = 0; i < parobject.listitems.length; i++) {
if (this === parobject.listitems[i]) {
if (parobject.currentitem) {
parobject.currentitem.className = parobject.currentitem.className.replace(
/light/g,
''
);
}
parobject.currentitem = parobject.listitems[i];
parobject.currentitemindex = i;
parobject.currentitem.className += ' light';
}
}
}
}
// Binding OnKeyDown Event
this.edit.onkeydown = function(e) {
e = e || window.event;
// Move Selection Up
if (e.keyCode === 38) {
// up
var cn = 0;
if (parobject.visiblecount > 0) {
if (parobject.visiblecount === 1) {
parobject.currentitemindex = parobject.listitems.length - 1;
}
do {
parobject.currentitemindex--;
cn++;
}
while (parobject.currentitemindex > 0 && parobject.listitems[parobject.currentitemindex].style.display === 'none');
if (parobject.currentitemindex < 0){
parobject.currentitemindex = parobject.listitems.length - 1;
}
if (parobject.currentitem) {
parobject.currentitem.className = parobject.currentitem.className.replace(
/light/g,
''
)
}
parobject.currentitem = parobject.listitems[parobject.currentitemindex];
parobject.currentitem.className += ' light';
parobject.currentitem.scrollIntoView(false);
}
e.cancelBubble = true;
if (navigator.appName !== 'Microsoft Internet Explorer') {
e.preventDefault();
e.stopPropagation();
}
return false;
}
// Move Selection Down
else if (e.keyCode === 40) {
// down
var ic = 0;
if (parobject.visiblecount > 0) {
do {
parobject.currentitemindex++;
}
while (parobject.currentitemindex < parobject.listitems.length && parobject.listitems[parobject.currentitemindex].style.display === 'none');
if (parobject.currentitemindex >= parobject.listitems.length){parobject.currentitemindex = 0;
}
if (parobject.currentitem) {
parobject.currentitem.className = parobject.currentitem.className.replace(
/light/g,
''
)
}
parobject.currentitem = parobject.listitems[parobject.currentitemindex];
parobject.currentitem.className += ' light';
parobject.currentitem.scrollIntoView(false);
}
e.cancelBubble = true;
if (navigator.appName != 'Microsoft Internet Explorer') {
e.preventDefault();
e.stopPropagation();
}
return false;
}
};
this.edit.onkeyup = function(e) {
e = e || window.event;
if (e.keyCode === 13) {
// enter
if (parobject.visiblecount != 0) {
var upv = parobject.currentitem.innerHTML;
upv = upv.replace(/\<b\>/ig, '');
upv = upv.replace(/\<\/b\>/ig, '');
parobject.edit.value = upv;
}
parobject.dropdownlist.style.display = 'none';
e.cancelBubble = true;
return false;
}
else {
parobject.dropdownlist.style.display = 'block';
parobject.visiblecount = 0;
if (parobject.edit.value === '') {
for (var i = 0; i < parobject.listitems.length; i++) {
parobject.listitems[i].style.display = 'block';
parobject.visiblecount++;
var pv = parobject.listitems[i].innerHTML;
pv = pv.replace(/\<b\>/ig, '');
parobject.listitems[i].innerHTML = pv.replace(/\<\/b\>/ig, '');
}
}
else {
var re = new RegExp('(' + parobject.edit.value + ')', "i");
for (var i = 0; i < parobject.listitems.length; i++) {
var pv = parobject.listitems[i].innerHTML;
pv = pv.replace(/\<b\>/ig, '');
pv = pv.replace(/\<\/b\>/ig, '');
if (re.test(pv)) {
parobject.listitems[i].style.display = 'block';
parobject.visiblecount++;
parobject.listitems[i].innerHTML = pv.replace(re, '<b>$1</b>');
}
else {
parobject.listitems[i].style.display = 'none';
}
}
}
}
}
};