Develop and Download Open Source Software

Browse Subversion Repository

Contents of /js/input_assist.js

Parent Directory Parent Directory | Revision Log Revision Log


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

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