Develop and Download Open Source Software

Browse Subversion Repository

Contents of /js/input_assist.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (show annotations) (download) (as text)
Sat Dec 5 02:36:36 2009 UTC (14 years, 5 months ago) by berupon
File MIME type: application/x-javascript
File size: 7145 byte(s)
ライセンス表記
1
2 // Copyright (c) 2009 Katsuhisa Yuasa <berupon [at] gmail.com>
3 // License http://www.opensource.org/licenses/mit-license.html
4 //
5 // Inspired by suggest.js http://www.enjoyxstudy.com/javascript/suggest/
6 //
7
8 InputAssist = function (elem) {
9 this.init.apply(this, arguments);
10 };
11
12 InputAssist.prototype = {
13
14 init : function (elem) {
15 this.elem = elem;
16 this.listElem = null;
17 this.oldValue = elem.value;
18 this.timer = null;
19 this.interval = 300;
20 this.listClassName = "assist_list";
21 this.listItemClassName = "item";
22 this.activeListItemClassName = "active";
23 this.listActiveIndex = -1;
24 this.dataList = new Array();
25 this.matchedDataList = new Array();
26
27 elem.observe("focus", this.onFocus.bind(this));
28 elem.observe("blur", this.onBlur.bind(this));
29
30 elem.observe("keydown", this.onKeyDown.bind(this));
31 var keyevent = (Prototype.Browser.Opera || Prototype.Browser.Gecko) ? 'keypress' : 'keyup';
32 elem.observe(keyevent, this.onKeyUp.bind(this));
33 document.observe('mousemove', this.onMouseMove.bind(this));
34 },
35
36 onFocus : function (e) {
37 var elem = Event.element(e);
38 this.createListElem();
39 },
40
41 onBlur : function (e) {
42 this.hideList();
43 if (this.timer) {
44 clearTimeout(this.timer);
45 this.timer = null;
46 }
47 },
48
49 onKeyDown : function (e) {
50 switch (e.keyCode) {
51 case 9: // tab
52 if (1
53 && this.matchedDataList.length
54 && this.listActiveIndex >= 0
55 && this.listActiveIndex < this.matchedDataList.length
56 ) {
57 this.selectData(this.listActiveIndex);
58 break;
59 }
60 case 13: // enter
61 if (1
62 && this.matchedDataList.length
63 && this.listActiveIndex >= 0
64 && this.listActiveIndex < this.matchedDataList.length
65 ) {
66 this.selectData(this.listActiveIndex, true);
67 Event.stop(e);
68 break;
69 }
70 break;
71 }
72 },
73
74 selectData : function (idx, bFocus) {
75 setTimeout(
76 function () {
77 this.hideList();
78 this.setData(this.matchedDataList[idx]);
79 this.oldValue = this.elem.value;
80 this.listActiveIndex = -1;
81 if (bFocus) {
82 this.elem.focus();
83 }
84 }.bind(this),
85 10
86 );
87 },
88
89 onKeyUp : function (e) {
90 if (this.timer) {
91 switch (e.keyCode) {
92 case 16: // shift
93 case 17: // ctrl
94 case 18: // alt
95 break;
96 default:
97 clearTimeout(this.timer);
98 }
99 }
100 switch (e.keyCode) {
101 case 37: // left
102 break;
103 case 38: // up
104 this.shiftItemFocus(-1);
105 break;
106 case 39: // right
107 break;
108 case 40: // down
109 if (e.altKey) {
110 this.oldValue = "";
111 this.onTimer();
112 }else {
113 this.shiftItemFocus(1);
114 }
115 break;
116 case 27: // esc
117 this.hideList();
118 break;
119 case 13: // enter
120 case 9: // tab
121 case 16: // shift
122 case 17: // ctrl
123 case 18: // alt
124 break;
125 case 8: // backspace
126 case 46: // delete
127 if (this.elem.value == "") {
128 this.onTimer();
129 break;
130 }
131 default:
132 if (!e.altKey && !e.ctrlKey) {
133 if (this.oldValue == "") {
134 this.timer = setTimeout(this.onTimer.bind(this), this.interval/10);
135 }else {
136 this.timer = setTimeout(this.onTimer.bind(this), this.interval);
137 }
138 }
139 break;
140 }
141 },
142
143 onMouseMove : function (e) {
144 if (!this.listElem) {
145 return;
146 }
147 var nodes = this.listElem.childNodes;
148 if (nodes.length == 0) {
149 return;
150 }
151 var x = Event.pointerX(e);
152 var y = Event.pointerY(e);
153 var listItems = this.listElem.childNodes;
154 for (var i=0; i<listItems.length; ++i) {
155 var elem = listItems[i];
156 if (Position.within(elem, x, y)) {
157 this.moveItemFocus(i);
158 break;
159 }
160 }
161
162 },
163
164 shiftItemFocus : function (shift) {
165 if (!Element.visible(this.listElem)) {
166 return;
167 }
168 var nodes = this.listElem.childNodes;
169 if (nodes.length == 0) {
170 return;
171 }
172 var newActiveIndex = -1;
173 if (this.listActiveIndex == -1) {
174 if (shift < 0) {
175 newActiveIndex = nodes.length - 1;
176 }else {
177 newActiveIndex = 0;
178 }
179 }else {
180 newActiveIndex = this.listActiveIndex + shift;
181 if (newActiveIndex >= nodes.length) {
182 newActiveIndex = -1;
183 }
184 }
185 this.moveItemFocus(newActiveIndex);
186 },
187
188 moveItemFocus : function (newActiveIndex) {
189 if (this.listActiveIndex != newActiveIndex) {
190 var nodes = this.listElem.childNodes;
191 if (this.listActiveIndex != -1 && this.listActiveIndex < nodes.length) {
192 Element.removeClassName(nodes[this.listActiveIndex], this.activeListItemClassName);
193 }
194 if (newActiveIndex != -1) {
195 if (newActiveIndex < 0) {
196 alert(1);
197 }
198 Element.addClassName(nodes[newActiveIndex], this.activeListItemClassName);
199 }
200 this.listActiveIndex = newActiveIndex;
201 }
202 },
203
204 onTimer : function () {
205 if (this.elem.value.length == 0) {
206 this.hideList();
207 return;
208 }
209 if (this.elem != document.activeElement) {
210 return;
211 }
212 if (this.elem.value == this.oldValue) {
213 return;
214 }
215 this.setList();
216 this.oldValue = this.elem.value;
217 this.timer = setTimeout(this.onTimer.bind(this), this.interval);
218 },
219
220 onMouseDown : function(e) {
221 var x = Event.pointerX(e);
222 var y = Event.pointerY(e);
223 var listItems = this.listElem.childNodes;
224 for (var i=0; i<listItems.length; ++i) {
225 var elem = listItems[i];
226 if (Position.within(elem, x, y)) {
227 this.selectData(i, true);
228 break;
229 }
230 }
231 },
232
233 createListElem : function () {
234 if (this.listElem) {
235 return;
236 }
237 var listElem = document.createElement('DIV');
238 listElem.className = this.listClassName;
239 Element.hide(listElem);
240 Event.observe(listElem, "mousedown", this.onMouseDown.bind(this));
241 this.listElem = listElem;
242 this.elem.parentNode.insertBefore(listElem, this.elem.nextSibling);
243 },
244
245 addListElemItem : function (html) {
246 var listItem = document.createElement('DIV');
247 listItem.className = this.listItemClassName;
248 listItem.innerHTML = html;
249 this.listElem.appendChild(listItem);
250 },
251
252 setList : function () {
253 if (!this.listElem) {
254 return;
255 }
256 if (this.listElem.childNodes) {
257 for (var i=this.listElem.childNodes.length-1; i>=0; --i) {
258 this.listElem.removeChild(this.listElem.childNodes[i]);
259 }
260 }
261 this.dataList = this.provideDataList();
262 this.matchedDataList = new Array();
263 var results = new Array();
264 for (var i=0; i<this.dataList.length; ++i) {
265 var data = this.dataList[i];
266 var result = this.matchData(data);
267 if (result) {
268 this.matchedDataList.push(data);
269 results.push(result);
270 }
271 }
272 if (results.length != 0) {
273 for (var i=0; i<results.length; ++i) {
274 this.addListElemItem(results[i]);
275 }
276 var style = this.listElem.style;
277 var offset = Element.cumulativeOffset(this.elem);
278 style.left = offset[0] + "px";
279 style.top = (offset[1] + this.elem.offsetHeight) + "px";
280 style.minWidth = this.elem.offsetWidth + "px";
281 Element.show(this.listElem);
282 }else {
283 this.hideList();
284 }
285 },
286
287 hideList : function () {
288 if (!this.listElem) {
289 return;
290 }
291 this.oldValue = this.elem.value;
292 this.listActiveIndex = -1;
293 Element.hide(this.listElem);
294 }
295
296 };
297

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26