forked from lorenzoongithub/completely
-
Notifications
You must be signed in to change notification settings - Fork 0
/
complete.lime.js
524 lines (468 loc) · 23.2 KB
/
complete.lime.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
/**
* complete.lime 1.0.4
* MIT Licensing
* Copyright (c) 2013 Lorenzo Puccetti
*
* This Software shall be used for doing good things, not bad things.
*
* Forked complete.ly.js to complete.lime.js 2014-12-27, Lennart Borgman
* Copyright (c) 2015 Lennart Borgman (for my small additions only, of course)
*
**/
// fix-me: Something can loop sometimes after CR, but what??? Maybe it was the "change" event??
function completeLime(container, config) {
config = config || {};
config.fontSize = config.fontSize || '16px';
config.fontFamily = config.fontFamily || 'sans-serif';
config.height = config.height || '32px';
config.padding = config.padding || '8px';
config.promptInnerHTML = config.promptInnerHTML || '';
config.color = config.color || '#333';
config.hintColor = config.hintColor || '#aaa';
config.backgroundColor = config.backgroundColor || '#fff';
config.dropDownBorderColor = config.dropDownBorderColor || '#aaa';
config.dropDownZIndex = config.dropDownZIndex || '100'; // to ensure we are in front of everybody
config.dropDownOnHoverBackgroundColor = config.dropDownOnHoverBackgroundColor || '#ddd';
var txtInput = document.createElement('input');
txtInput.type ='search';
txtInput.spellcheck = false;
txtInput.style.fontSize = config.fontSize;
txtInput.style.fontFamily = config.fontFamily;
txtInput.style.height = config.height;
txtInput.style.padding = config.padding;
txtInput.style.color = config.color;
txtInput.style.backgroundColor = config.backgroundColor;
txtInput.style.width = '100%';
txtInput.style.outline = '0';
txtInput.style.border = '0';
txtInput.style.margin = '0';
txtInput.style.verticalAlign = 'middle';
var txtHint = txtInput.cloneNode();
txtHint.tabIndex = '-1';
txtHint.disabled='';
txtHint.style.position = 'absolute';
txtHint.style.top = '0';
txtHint.style.left = '0';
txtHint.style.borderColor = 'transparent';
txtHint.style.boxShadow = 'none';
txtHint.style.color = config.hintColor;
txtInput.placeholder = 'Enter search string';
txtInput.autofocus = true;
txtInput.autocomplete ='search';
txtInput.style.backgroundColor ='transparent';
txtInput.style.position = 'relative';
var wrapper = document.createElement('div');
wrapper.style.position = 'relative';
wrapper.style.outline = '0';
wrapper.style.border = '0';
wrapper.style.margin = '0';
wrapper.style.padding = '0';
var prompt = document.createElement('div');
prompt.style.position = 'absolute';
prompt.style.outline = '0';
prompt.style.margin = '0';
prompt.style.padding = '0';
prompt.style.border = '0';
prompt.style.fontSize = config.fontSize;
prompt.style.fontFamily = config.fontFamily;
prompt.style.color = config.color;
prompt.style.backgroundColor = config.backgroundColor;
prompt.style.top = '0';
prompt.style.left = '0';
prompt.style.overflow = 'hidden';
prompt.innerHTML = config.promptInnerHTML;
prompt.style.background = 'transparent';
if (document.body === undefined) {
throw 'document.body is undefined. The library was wired up incorrectly.';
}
document.body.appendChild(prompt);
var w = prompt.getBoundingClientRect().right; // works out the width of the prompt.
wrapper.appendChild(prompt);
prompt.style.visibility = 'visible';
prompt.style.left = '-'+w+'px';
wrapper.style.marginLeft= w+'px';
wrapper.appendChild(txtHint);
wrapper.appendChild(txtInput);
var dropDown = document.createElement('div');
dropDown.style.position = 'absolute';
dropDown.style.visibility = 'hidden';
dropDown.style.outline = '0';
dropDown.style.margin = '0';
dropDown.style.padding = '0';
dropDown.style.textAlign = 'left';
dropDown.style.fontSize = config.fontSize;
dropDown.style.fontFamily = config.fontFamily;
dropDown.style.backgroundColor = config.backgroundColor;
dropDown.style.zIndex = config.dropDownZIndex;
dropDown.style.cursor = 'default';
dropDown.style.borderStyle = 'solid';
dropDown.style.borderWidth = '1px';
dropDown.style.borderColor = config.dropDownBorderColor;
dropDown.style.overflowX= 'hidden';
dropDown.style.whiteSpace = 'pre';
// dropDown.style.overflowY = 'scroll'; // note: this might be ugly when the scrollbar is not required. however in this way the width of the dropDown takes into account
dropDown.style.overflowY = 'auto'; // note: was too ugly, let the browser handle it! ;-)
var createDropDownController = function(elem) {
var rows = [];
var ix = 0;
var oldIndex = -1;
// See: https://developer.mozilla.org/en/docs/Web/API/EventTarget.addEventListener
// Fix-me: The outline does not show as expected, but it is there.
var mouseOverHandler = function(ths) { ths.style.outline = '1px solid #00f'; }
var mouseOutHandler = function(ths) { ths.style.outline = '0'; }
var mouseDownHandler = function(ths) { p.hide(); p.onmouseselection(ths.__hint); }
var p = {
hide : function() { elem.style.visibility = 'hidden'; },
isHidden : function() { return (elem.style.visibility === 'hidden'); },
refresh : function(token, array) {
elem.style.visibility = 'hidden';
ix = 0;
elem.innerHTML ='';
var vph = (window.innerHeight || document.documentElement.clientHeight);
var rect = elem.parentNode.getBoundingClientRect();
var distanceToTop = rect.top - 6; // heuristic give 6px
var distanceToBottom = vph - rect.bottom -6; // distance from the browser border.
rows = [];
var tokenRegex = new RegExp("^"+token,"i");
var paddingLeft = config.padding;
var paddingRight = config.padding;
for (var i=0;i<array.length;i++) {
if (!tokenRegex.test(array[i])) { continue; } // <-- case independent match
var divRow =document.createElement('div');
divRow.style.color = config.color;
divRow.style.lineHeight = config.height;
divRow.style.paddingLeft = paddingLeft;
divRow.style.paddingRight = paddingRight;
divRow.tabIndex = '0';
divRow.addEventListener("mouseover", function(){ mouseOverHandler(this); });
divRow.addEventListener("mouseout", function(){ mouseOutHandler(this); });
divRow.addEventListener("mousedown", function(){ mouseDownHandler(this); });
divRow.__hint = array[i];
divRow.innerHTML = token+'<b>'+array[i].substring(token.length)+'</b>';
rows.push(divRow);
elem.appendChild(divRow);
}
if (rows.length===0) { return; } // nothing to show.
// do not show dropDown if it has only one element which matches what we display.
if (rows.length===1 && token === rows[0].__hint) { return; }
// if (rows.length<2) return; // Fix-me: Maybe better to show this? Touch. Completion.
p.highlight(0);
if (distanceToTop > distanceToBottom*3) { // Heuristic (only when the distance to the to top is 4 times more than distance to the bottom
// elem.style.maxHeight = distanceToTop+'px'; // we display the dropDown on the top of the input text
elem.style.top ='';
elem.style.bottom ='100%';
} else {
elem.style.top = '100%';
elem.style.bottom = '';
// elem.style.maxHeight = distanceToBottom+'px';
}
elem.style.visibility = 'visible';
},
highlight : function(index) {
if (oldIndex !=-1 && rows[oldIndex]) {
rows[oldIndex].style.backgroundColor = config.backgroundColor;
}
rows[index].style.backgroundColor = config.dropDownOnHoverBackgroundColor; // <-- should be config
oldIndex = index;
},
move : function(step) { // moves the selection either up or down (unless it's not possible) step is either +1 or -1.
if (elem.style.visibility === 'hidden') return ''; // nothing to move if there is no dropDown. (this happens if the user hits escape and then down or up)
if (ix+step === -1 || ix+step === rows.length) return rows[ix].__hint; // NO CIRCULAR SCROLLING.
ix+=step;
p.highlight(ix);
return rows[ix].__hint;//txtShadow.value = uRows[uIndex].__hint ;
},
onmouseselection : function() {} // it will be overwritten.
};
return p;
}
function scrollToEnd() { txtInput.scrollLeft = 9000; txtHint.scrollLeft = txtInput.scrollLeft; }
var dropDownController = createDropDownController(dropDown);
dropDownController.onmouseselection = function(text) {
// txtInput.value = txtHint.value = leftSide+text;
txtHint.value = leftSide+text;
copyCarefully2txtInput(leftSide+text);
scrollToEnd();
rs.onChange(txtInput.value); // <-- forcing it.
registerOnTextChangeOldValue = txtInput.value; // <-- ensure that mouse down will not show the dropDown now.
// Something seems wrong in Chrome (and in IE).
// txtInput becomes the document.activeElement, but still focus is not there.
// Fix-me: File a bug report.
setTimeout(function() { txtInput.focus(); },0);
}
wrapper.appendChild(dropDown);
container.appendChild(wrapper);
var spacer;
var leftSide; // <-- it will contain the leftSide part of the textfield (the bit that was already autocompleted)
function calculateWidthForText(text) {
if (spacer === undefined) { // on first call only.
spacer = document.createElement('span');
spacer.style.visibility = 'hidden';
spacer.style.position = 'fixed';
spacer.style.outline = '0';
spacer.style.margin = '0';
spacer.style.padding = '0';
spacer.style.border = '0';
spacer.style.left = '0';
spacer.style.whiteSpace = 'pre';
spacer.style.fontSize = config.fontSize;
spacer.style.fontFamily = config.fontFamily;
spacer.style.fontWeight = 'normal';
document.body.appendChild(spacer);
}
// Used to encode an HTML string into a plain text.
// taken from http://stackoverflow.com/questions/1219860/javascript-jquery-html-encoding
spacer.innerHTML = String(text).replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
return spacer.getBoundingClientRect().right;
}
// Ensure case in input and hint are the same:
function setHintValue(txt) {
var str = txtInput.value;
txtHint.value = str + txt.substring(str.length);
}
function onEnterActions() {
if (rs.isTouchDevice) txtInput.blur(); // Get rid of virtual keyboard.
rs.onEnter( dropDown.style.visibility != 'hidden' );
}
var rs = {
onArrowDown : function() {}, // defaults to no action.
onArrowUp : function() {}, // defaults to no action.
onEnter : function() {}, // defaults to no action.
onTab : function() {}, // defaults to no action.
onChange: function() { rs.repaint() }, // defaults to repainting.
abortAuto: function() {}, // for aborting auto comp still in progress after CR
startFrom: 0,
options: [],
wrapper : wrapper, // For easy access to the HTML elements to the final user (customizations)
input : txtInput, // For easy access to the HTML elements to the final user (customizations)
hint : txtHint, // For easy access to the HTML elements to the final user (customizations)
dropDown : dropDown, // For easy access to the HTML elements to the final user (customizations)
prompt : prompt,
// Fix-me: I want to detect here is virtual keyboard only, but there is no way to do that. Yet.
isTouchDevice : "ontouchstart" in window,
setText : function(text) {
txtHint.value = text;
// txtInput.value = text;
copyCarefully2txtInput(text);
},
getText : function() {
return txtInput.value;
},
hideDropDown : function() {
dropDownController.hide();
},
repaint : function() {
var text = txtInput.value;
var startFrom = rs.startFrom;
var options = rs.options;
var optionsLength = options.length;
// breaking text in leftSide and token.
var token = text.substring(startFrom);
leftSide = text.substring(0,startFrom);
if (token.length + leftSide.length === 0) {
dropDownController.hide();
return;
}
// updating the hint.
txtHint.value ='';
// console.log("txtHint.value = '', 1 updating hint", txtInput.value);
var tokenRegex = new RegExp("^"+token,"i");
for (var i=0;i<optionsLength;i++) {
var opt = options[i];
if (tokenRegex.test(opt)) { // <-- case independent match
// txtHint.value = leftSide +opt;
setHintValue( leftSide + opt );
break;
}
}
// moving the dropDown and refreshing it.
dropDown.style.left = calculateWidthForText(leftSide)+'px';
dropDownController.refresh(token, rs.options);
}
};
var registerOnTextChangeOldValue;
/**
* Register a callback function to detect changes to the content of the input-type-text.
* Those changes are typically followed by user's action: a key-stroke event but sometimes it might be a mouse click.
**/
var registerOnTextChange = function(txt, callback) {
registerOnTextChangeOldValue = txt.value;
var handler = function() {
var value = txt.value;
if (registerOnTextChangeOldValue !== value) {
registerOnTextChangeOldValue = value;
callback(value);
}
};
// For user's actions, we listen to both input events and key up events
// It appears that input events are not enough so we defensively listen to key up events too.
// source: http://help.dottoro.com/ljhxklln.php
//
// The cost of listening to three sources should be negligible
// as the handler will invoke callback function only if the
// text.value was effectively changed.
//
//
txt.addEventListener("input", handler, false);
txt.addEventListener('keyup', handler, false);
txt.addEventListener('change', handler, false);
};
registerOnTextChange(txtInput,function(text) { // note the function needs to be wrapped as API-users will define their onChange
rs.onChange(text);
});
// Avoid triggering a change event if we can. I suspect this
// caused some looping, but I do not really understand what
// happened. I just saw something was looping.
function copyCarefully2txtInput(txt) {
if (txtInput.value === txt) return;
txtInput.value = txt;
}
var keyDownHandler = function(e) {
// Fix-me: replace when this is supported:
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent.key
// console.log(e.keyCode, e.key);
var keyCode = e.keyCode;
if (keyCode == 33) { return; } // page up (do nothing)
if (keyCode == 34) { return; } // page down (do nothing);
if (keyCode == 27) { //escape
e.preventDefault(); // We will empty the input field ourself - timing etc
if (!dropDownController.isHidden()) {
dropDownController.hide();
} else {
// txtInput.value = "";
copyCarefully2txtInput("");
}
txtHint.value = txtInput.value; // ensure that no hint is left.
txtInput.focus();
return;
}
if (keyCode == 39 || keyCode == 35 || keyCode == 9) { // right, end, tab (autocomplete triggered)
if (keyCode == 9) {
// for tabs we need to ensure that we override the
// default behaviour: move to the next focusable
// HTML-element since users might want to re-enable its
// default behaviour or handle the call somehow.
rs.onTab(); // tab was called with no action.
return;
}
if (txtHint.value.length > 0) { // if there is a hint
dropDownController.hide();
// txtInput.value = txtHint.value;
copyCarefully2txtInput(txtHint.value);
scrollToEnd();
var hasTextChanged = registerOnTextChangeOldValue != txtInput.value
registerOnTextChangeOldValue = txtInput.value; // <-- to avoid dropDown to appear again.
// for example imagine the array contains the
// following words: bee, beef, beetroot user has hit
// enter to get 'bee' it would be prompted with the
// dropDown again (as beef and beetroot also match)
if (hasTextChanged) {
rs.onChange(txtInput.value); // <-- forcing it.
}
}
return;
}
if (keyCode == 13) { // enter (autocomplete triggered)
if (txtHint.value.length == 0) { // if there is a hint
// if (rs.isTouchDevice) txtInput.blur(); // Get rid of virtual keyboard.
// rs.onEnter();
// console.log("13 hint length == 0, drop vis=", dropDown.style.visibility);
onEnterActions();
} else {
var wasDropDownHidden = (dropDown.style.visibility == 'hidden');
dropDownController.hide();
if (wasDropDownHidden) {
// console.log("13 dropdown was hidden");
txtHint.value = txtInput.value; // ensure that no hint is left.
txtInput.focus();
// if (rs.isTouchDevice) txtInput.blur(); // Get rid of virtual keyboard.
// rs.onEnter();
onEnterActions();
return;
}
// console.log("13 dropdown was visible");
// txtInput.value = txtHint.value;
copyCarefully2txtInput(txtHint.value);
scrollToEnd();
var hasTextChanged = registerOnTextChangeOldValue != txtInput.value
registerOnTextChangeOldValue = txtInput.value; // <-- to avoid dropDown to appear again.
// for example imagine the array contains the
// following words: bee, beef, beetroot user has hit
// enter to get 'bee' it would be prompted with the
// dropDown again (as beef and beetroot also match)
if (hasTextChanged) {
rs.onChange(txtInput.value); // <-- forcing it.
} else {
// Fix-me: abort auto
rs.abortAuto();
}
}
return;
}
if (keyCode == 40) { // down
var m = dropDownController.move(+1);
if (m == '') { rs.onArrowDown(); }
// txtHint.value = leftSide+m;
setHintValue( leftSide + m );
e.preventDefault();
e.stopPropagation();
return;
}
if (keyCode == 38 ) { // up
var m = dropDownController.move(-1);
if (m == '') { rs.onArrowUp(); }
// txtHint.value = leftSide+m;
setHintValue( leftSide + m );
e.preventDefault();
e.stopPropagation();
return;
}
// it's important to reset the txtHint on key down.
// think: user presses a letter (e.g. 'x') and never releases... you get (xxxxxxxxxxxxxxxxx)
// and you would see still the hint
txtHint.value =''; // resets the txtHint. (it might be updated onKeyUp)
// console.log("txtHint.value = '', 2 resetting hint on key down", txtInput.value);
};
var clickHandler = function() {
// In some browsers there is an "x" to the left in the input
// field (when the type is "search") which clears the
// field. Reset the txtHint if the input is empty.
//
// This seems to be necessary to do in a timer, at least in Chrome and IE now.
// console.log("txtInput clicked", txtInput.value);
setTimeout(function() {
if (txtInput.value.length === 0) { txtHint.value =''; }
dropDownController.hide();
}, 0);
};
var blurHandler = function() {
if (!rs.isTouchDevice) dropDownController.hide();
txtHint.value ='';
// console.log("txtHint.value = '', 3 blur", txtInput.value);
};
var scrollHandlerDoIt = function() {
// console.log("scroll", txtInput.scrollLeft, txtInput.scrollTop);
txtHint.scrollLeft = txtInput.scrollLeft;
txtHint.scrollTop = txtInput.scrollTop;
};
var scrollHandler = (function() {
var timer;
return function(txt) {
if (timer) return;
timer = setTimeout(function() { scrollHandlerDoIt(); timer = null; }, 50);
};
})();
txtInput.addEventListener("blur", function() { scrollHandler(); blurHandler(); });
txtInput.addEventListener("change", function() { scrollHandler(); });
txtInput.addEventListener("click", function() { scrollHandler(); clickHandler(); });
txtInput.addEventListener("focus", function() { scrollHandler(); rs.repaint(); });
txtInput.addEventListener("input", function() { scrollHandler(); });
txtInput.addEventListener("keydown", function(e) { scrollHandler(); keyDownHandler(e); }, false);
txtInput.addEventListener("keyup", function() { scrollHandler(); });
// txtInput.addEventListener("scroll", function() { scrollHandler(); });
return rs;
}