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

View file

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