diff --git a/client/js/chat.js b/client/js/chat.js index c3e4dfc..2a31543 100644 --- a/client/js/chat.js +++ b/client/js/chat.js @@ -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") diff --git a/client/js/lib/jquery.tabComplete.js b/client/js/lib/jquery.tabComplete.js index f4b299b..c8bde39 100644 --- a/client/js/lib/jquery.tabComplete.js +++ b/client/js/lib/jquery.tabComplete.js @@ -5,18 +5,26 @@ * Copyright (c) 2014 Mattias Erming * 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; }); };