Parent: [180eab] (diff)

Child: [0f9a52] (diff)

Download this file

combobox.js    136 lines (125 with data), 4.5 kB

  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
(function($) {
$.widget('ui.combobox', {
options: {
source_url: null // caller must provide this
},
_create: function() {
var input,
that = this,
wasOpen = false,
loaded = false, // options list loaded with ajax already?
select = this.element.hide(),
selected = select.children(':selected'),
value = selected.val() ? selected.text() : "",
wrapper = this.wrapper = $('<span>')
.addClass('ui-combobox')
.insertAfter(select);
function populateSelect(data) {
for (var i = 0; i < data.options.length; i++) {
var label = data.options[i].label,
value = data.options[i].value;
$('<option>' + label + '</option>')
.val(value)
.appendTo(select);
}
loaded = true;
input.autocomplete('search', input.val()); // trigger search to re-render options
}
// Load options list with ajax and populate underlying select with loaded data
$.get(this.options.source_url, populateSelect);
function removeIfInvalid(element) {
var value = $(element).val(),
matcher = new RegExp('^' + $.ui.autocomplete.escapeRegex(value) + '$'),
valid = false;
select.children('option').each(function() {
if ($(this).val().match(matcher)) {
this.selected = valid = true;
input.val(this.text);
return false;
}
});
if (!valid) {
$(element).val('');
select.val('');
input.data('autocomplete').term = '';
}
}
input = $('<input>')
.appendTo(wrapper)
.val(value)
.attr('title', '')
.addClass('ui-state-default ui-combobox-input')
.autocomplete({
delay: 0,
minLength: 0,
source: function (request, response) {
if (!loaded) {
response([{
label: 'Loading...',
value: '',
option: {item: ''}
}]);
return;
}
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), 'i');
response(select.children('option').map(function() {
var text = $(this).text();
if (this.value && (!request.term || matcher.test(text))) {
return {
label: text.replace(
new RegExp(
'(?![^&;]+;)(?!<[^<>]*)(' +
$.ui.autocomplete.escapeRegex(request.term) +
')(?![^<>]*>)(?![^&;]+;)', 'gi'
), '<strong>$1</strong>'),
value: text,
option: this
};
}
}));
},
select: function(event, ui) {
ui.item.option.selected = true;
that._trigger('selected', event, {item: ui.item.option});
},
change: function(event, ui) {
if (!ui.item) {
removeIfInvalid(this);
}
}
}).addClass('ui-widget ui-widget-content ui-corner-left');
input.data('autocomplete')._renderItem = function(ul, item) {
return $('<li>')
.data('item.autocomplete', item)
.append('<a>' + item.label + '</a>')
.appendTo(ul);
};
$('<a>')
.attr('tabIndex', -1)
.attr('title', 'Show all options')
.appendTo(wrapper)
.button({
icons: {
primary: 'ui-icon-triangle-1-s'
},
text: false
})
.removeClass('ui-corner-all')
.addClass('ui-corner-right ui-combobox-toggle')
.mousedown(function() {
wasOpen = input.autocomplete('widget').is(':visible');
})
.click(function() {
input.focus();
if (wasOpen) {
return;
}
input.autocomplete('search', '');
});
},
_destroy: function() {
this.wrapper.remove();
this.element.show();
}
});
})(jQuery);