-
Notifications
You must be signed in to change notification settings - Fork 2
/
combobox.js
159 lines (132 loc) · 3.9 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
$(function () {
// Add popup and hooks for text inputs.
$("input.combobox").each(function () {
var $this = $(this).wrap('<span class="combobox-parent">');
var visible = $this.is(":visible");
$('<div class="combobox-popup">').insertAfter($this).hide();
$this
.keyup(comboboxTextInput)
.blur(comboboxHidePopup)
.change(comboboxTextUpdateValue);
if (!visible) $span.hide();
});
// Change select comboboxes to be text then add hooks.
$("select.combobox").each(function () {
var $this = $(this);
var visible = $this.is(":visible");
var $combo = $('<div class="combobox-popup">');
// Populate combobox
$this.children("option").each(function () {
var $child = $(this);
var help = $child.attr("data-help") || false;
var $row = $('<div class="combobox-row">')
.append(
$('<span>').text($child.text())
).data({
"text": $child.text(),
"value": $child.val(),
"help": help,
}).val($child.val())
.appendTo($combo)
.click(comboboxSelectSelectOption);
if (help) $('<span class="help">').text(help).appendTo($row);
})
// Replace select with a span that has an input field.
var $parent = $('<span>')
.attr({
"id": $this.attr("id"),
"class": $this.attr("class"),
"value": $this.val(),
})
.addClass("combobox-parent")
.append([
$('<input>').attr({
"class": $this.attr("class"),
"style": $this.attr("style"),
"placeholder": $this.attr("placeholder")
}).width($this.show().width())
// Attach hooks to input
.focus(comboboxSelectFilterPopup)
.keyup(comboboxSelectFilterPopup)
.blur(comboboxHidePopup)
.change(comboboxSelectUpdateValue),
$combo.hide()
]);
$this.replaceWith($parent);
if (!visible) $parent.hide();
});
});
function comboboxTextInput() {
var $this = $(this);
window[$this.attr("data-combo-search")]($this.val(), function (result) {
var $combo = $this.parent().children('.combobox-popup');
$combo.empty();
for (var i = 0; i < result.length; i++) {
$('<div class="combobox-row">')
.append(result[i])
.appendTo($combo)
.data("value", result[i].data("value"))
.click(comboboxTextSelectOption);
}
var pos = $this.position();
pos.top += $this.height();
$combo.show();
if ($combo.children(".combobox-row:visible").length == 0) {
$combo.hide();
}
});
}
function comboboxTextSelectOption() {
var $this = $(this), val = $this.data("value");
var $input = $this.parent().hide().parent().children("input").val(val).change();
var func = $input.attr("data-combo-set");
if (func && func in window) window[func](val);
}
function comboboxSelectSelectOption() {
var $this = $(this);
var $parent = $this.parent().hide().parent();
$parent.val($this.data("value")).change();
$parent.children("input").val($this.data("text"));
}
function comboboxSelectFilterPopup() {
var $this = $(this);
var val = $this.val().toLowerCase().replace(/\s/g, "");
var $combo = $this.parent().children(".combobox-popup").show();
function inside(x) {
x = x.toLowerCase().replace(/\s/g, "");
return x.indexOf(val) >= 0;
}
$combo.children(".combobox-row").each(function () {
var $row = $(this);
if (inside($row.data("text")) || inside($row.data("value"))) {
$row.show();
} else {
$row.hide();
}
});
if ($combo.children(".combobox-row:visible").length == 0) {
$combo.hide();
}
}
function comboboxHidePopup() {
var $elem = $(this).parent().children(".combobox-popup");
setTimeout($elem.hide.bind($elem), 200);
}
function comboboxTextUpdateValue() {
var $this = $(this);
$this.parent().val($this.val());
}
function comboboxSelectUpdateValue() {
var $this = $(this);
var $parent = $this.parent();
var val = $this.val();
// First check if there is an entry for this.
$parent.find(".combobox-row").each(function () {
var $row = $(this);
if ($row.text() == val) {
val = $row.data("value");
}
})
$parent.val(val);
$parent.find(".combobox-popup").hide();
}