-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortcombobox.js
109 lines (96 loc) · 4.57 KB
/
sortcombobox.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
(function ($) {
if ($ == null) return;
$.fn.sortcombobox = function (options) {
var settings = $.extend({
// These are the defaults.
SearchedText: '--Searched--',
OthersText: '<hr >'
}, options);
this.filter('select').each(function (index) {
function CreateOption(val, text, ignoreEvent) {
var option = $('<div data-value="' + val + '" data-text="' + text + '">' + text + '</div>')
.addClass('SortComboboxOption');
if (!ignoreEvent) {
option.mouseenter(function () { $(this).addClass('SortComboboxSelectedOption'); })
.mouseleave(function () { $(this).removeClass('SortComboboxSelectedOption'); })
.click(function (e) {
input.val($(this).data('text')).trigger('input');
select.val($(this).data('value')).change();
optionsWrapper.hide();
});
} else {
option.append('<hr>');
}
return option;
}
var select = $(this),
inside = false,
oldValue = '',
wrapper = $('<div></div>').addClass('SortComboboxWrapper'),
optionsObj = [],
input = $('<input>').attr('type', 'text').focus(function () {
optionsWrapper.show();
input.val('');
}).blur(function () {
if (!inside) {
optionsWrapper.hide();
var text = input.val().toUpperCase(),
selectedOptions = optionsObj.filter(function (o) { return o.text == text });
if (selectedOptions.length === 1 && selectedOptions[0].value !== select.val()) {
select.val(selectedOptions[0].value).change();
} else {
input.val(select.children(':selected').text()).trigger('input');
}
}
}),
optionsWrapper = $('<div></div>').addClass('SortComboboxOptionsWrapper');
select.find('option').each(function (index, option) {
optionsObj.push({ text: this.text, value: this.value });
optionsWrapper.append(CreateOption(this.value, this.text));
});
wrapper = select.css({ 'display': 'none' }).wrap(wrapper).parent().mouseenter(function () { inside = true; }).mouseleave(function () { inside = false; });
wrapper.append(input).append(optionsWrapper);
select.change(function (event, isOutside) {
if (isOutside !== undefined) {
input.val(select.children(':selected').text()).trigger('input');
}
}).on('disable', function () {
input.attr('disabled', 'disabled');
}).on('enable', function () {
input.removeAttr('disabled');
});
input.keydown(function (event) {
switch (event.keyCode) {
case 9: inside = false; break;
case 13: inside = false; input.blur(); break;
case 104: //up
case 38:
break;
case 98: // down
case 40:
break;
}
}).on('input propertychange', function (event) {
var text = this.value.toUpperCase(),
search = [],
other = [];
if (oldValue === text) return;
oldValue = text;
for (var i in optionsObj) {
var o = optionsObj[i];
if (o.text.indexOf(text) > -1) {
search.push(CreateOption(o.value, o.text));
} else {
other.push(CreateOption(o.value, o.text));
}
}
//if (search.length > 0) search.splice(0, 0, CreateOption(settings.SearchedText, settings.SearchedText, true));
if (other.length > 0) other.splice(0, 0, CreateOption("", "", true));
optionsWrapper.children().remove();
optionsWrapper.append(search).append(other);
})
.val(select.children(':selected').text()).trigger('input');
});
return this;
}
})(jQuery);