This repository has been archived by the owner on Sep 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeahead.jquery.js.php
130 lines (121 loc) · 3.21 KB
/
typeahead.jquery.js.php
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
<?php header("Content-Type: application/javascript"); include("sql.inc.php"); ?>
var kontenview_konten = [];
$.get("/accounts.json.php", {year: "<?php print($year); ?>"}, function (data) {kontenview_konten = data; }, "json");
function getKontoInput(prefix, maxlength) {
function _suggestions(q) {
return kontenview_konten
.filter(function (k) {return k.code.indexOf(prefix) === 0 && (maxlength === undefined || maxlength == 0 || k.code.length == prefix.length + maxlength) && (k.code.indexOf(q) >= 0 || k.label.toLowerCase().indexOf(q.toLowerCase()) >= 0); })
.map(function (k) {return k.code.substr(prefix.length) + " " + k.label;});
}
return getTypeAheadInput(_suggestions);
}
function getTypeAheadInput(suggestions) {
var _input = $("<input>").addClass("form-control");
new InputTypeAhead(_input, suggestions);
return _input;
}
function InputTypeAhead(inputq, suggestions) {
this.suggestions = suggestions;
this.inputq = inputq;
this.overlay = $("<div>");
this.list = $("<ul>").css({background: "white", listStyle: "none", padding: "0px"}).appendTo(this.overlay);
this.data = [];
this.current = -1;
this.active = false;
this.ignoreKey = false;
this.interval = null;
this.inputq
.keydown(this.keyDown.bind(this))
.blur(this.onBlur.bind(this))
.focus(this.onFocus.bind(this))
.keyup(this.onChange.bind(this));
$("body").append(this.overlay);
}
InputTypeAhead.prototype = {
keyDown: function(e) {
if(!this.active) return;
this.ignoreKey = true;
switch(e.keyCode) {
case 40: //down
e.preventDefault();
this._next();
break;
case 38: //down
e.preventDefault();
this._prev();
break;
case 13: //enter
if (this.active && this.current >= 0 && this.current < this.data.length) {
this.inputq.val(this.data[this.current]);
this._close();
e.preventDefault();
}
break;
case 27: //esc
this._close();
break;
default:
this.ignoreKey = false;
}
},
onBlur: function() {
this._close();
},
onChange: function() {
if(this.ignoreKey) {
this.ignoreKey= false;
return;
}
var q = this.inputq.val();
if(q.trim() == "") {
this._close();
} else {
this.search(q);
}
},
onFocus: function() {
this.onChange();
},
search: function(q) {
this._open(this.suggestions(q));
},
_renderData: function(data) {
this.overlay.css({
position: "absolute",
left: (this.inputq.offset().left) + "px",
top: (this.inputq.offset().top + this.inputq.outerHeight()) + "px",
width: (this.inputq.outerWidth()) + "px",
});
this.list.empty();
this.data = data;
this.list.append(data.map(function (_val) {return $("<li>").text(_val); }));
},
_select: function(i) {
if(!this.active) return;
if(i < 0 || i >= this.data.length) return;
var lis = this.list.children("li");
lis.css("backgroundColor", "white");
$(lis[i]).css("backgroundColor", "#ccc");
this.current = i;
},
_next: function() {
this._select(this.current+1);
},
_prev: function() {
this._select(this.current-1);
},
_open: function(data) {
if(data.length == 0) {
this._close();
return;
}
this._renderData(data);
this._select(0);
this.overlay.show();
this.active = true;
},
_close: function() {
this.overlay.hide();
this.active = false;
}
}