Update jquery.tabComplete.js

This commit is contained in:
Mattias Erming 2014-04-08 19:40:54 +02:00
parent d6b7a5331a
commit da74cdece4
2 changed files with 25 additions and 9 deletions

View File

@ -67,7 +67,7 @@ $(function() {
chat.find(".messages").scrollGlue({animate: 400}).scrollToBottom();
chat.find(".window")
.find("input")
.tabComplete(commands)
.tabComplete(commands, {appendSpace: true})
.end()
.first()
.bringToTop()
@ -94,7 +94,7 @@ $(function() {
).find(".window")
.last()
.find("input")
.tabComplete(commands)
.tabComplete(commands, {appendSpace: true})
.end()
.bringToTop()
.find(".messages")

View File

@ -5,18 +5,26 @@
* Copyright (c) 2014 Mattias Erming <mattias@mattiaserming.com>
* Licensed under the MIT License.
*
* Version 0.1.0
* Version 0.2.0
*/
(function($) {
$.fn.tabComplete = function(list) {
$.fn.tabComplete = function(list, options) {
var settings = $.extend({
appendSpace: false,
caseSensitive: false,
}, options);
var self = this;
if (self.size() > 1) {
return self.each(function() {
$(this).tabComplete(list);
$(this).tabComplete(list, options);
});
}
// Keep the list stored in the DOM via jQuery.data() variable.
self.data('list', list);
var match = [];
self.on('keydown', function(e) {
var key = e.which;
@ -25,12 +33,20 @@
return;
}
var text = self.val().split(' ');
var text = self.val().trim().split(' ');
var last = text.splice(-1)[0];
if (!match.length) {
match = $.grep(list, function(w) {
return last != '' && w.indexOf(last) !== -1;
match = $.grep(self.data('list'), function(w) {
var l = last;
if (l == '') {
return;
}
if (!settings.caseSensitive) {
l = l.toLowerCase();
w = w.toLowerCase();
}
return w.indexOf(l) !== -1;
});
}
@ -44,7 +60,7 @@
}
text.push(last);
self.val(text.join(' '));
self.val(text.join(' ') + (settings.appendSpace ? ' ' : ''));
return false;
});
};