From 6498f6431bc3efb08e4416ff445e15a7d5f08532 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Fri, 31 Jan 2014 11:01:51 -0600 Subject: [PATCH] Fix tab completion of names --- www/assets/js/ui.js | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/www/assets/js/ui.js b/www/assets/js/ui.js index a3b9f1b0..04cb5eba 100644 --- a/www/assets/js/ui.js +++ b/www/assets/js/ui.js @@ -110,22 +110,50 @@ function chatTabComplete() { return; } - console.log($("#userlist").children().map); - - var users = $("#userlist").children().map(function (_, elem) { + var __slice = Array.prototype.slice; + var users = __slice.call($("#userlist").children()).map(function (elem) { return elem.children[1].innerHTML; - }).filter(function (_, name) { + }).filter(function (name) { return name.toLowerCase().indexOf(current) === 0; }); // users now contains a list of names that start with current word + if (users.length === 0) { return; } - var min = Math.min.apply(Math, users.map(function (_, name) { + // trim possible names to the shortest possible completion + var min = Math.min.apply(Math, users.map(function (name) { return name.length; })); + users = users.map(function (name) { + return name.substring(0, min); + }); + + // continually trim off letters until all prefixes are the same + var changed = true; + var iter = 21; + while (changed) { + changed = false; + var first = users[0]; + for (var i = 1; i < users.length; i++) { + if (users[i] !== first) { + users[i] = users[i].substring(0, users[i].length - 1); + changed = true; + } + } + + if (changed) { + users[0] = users[0].substring(0, users[0].length - 1); + } + + // In the event something above doesn't generate a break condition, limit + // the maximum number of repetitions + if (--iter < 0) { + break; + } + } current = users[0].substring(0, min); if (users.length === 1) {