Nick autocompletion
authorDarren <darren@darrenwhitlen.com>
Tue, 4 Sep 2012 22:59:25 +0000 (23:59 +0100)
committerDarren <darren@darrenwhitlen.com>
Tue, 4 Sep 2012 22:59:25 +0000 (23:59 +0100)
client_backbone/dev/view.js

index 7340b0c2f4b70870ff8f86411cd11108b39cc7fe..ded9124db26bdf1da55fc80bf3c6ec860f06794c 100644 (file)
@@ -425,6 +425,9 @@ kiwi.view.ControlBox = Backbone.View.extend({
     buffer: [],  // Stores previously run commands\r
     buffer_pos: 0,  // The current position in the buffer\r
 \r
+    // Hold tab autocomplete data\r
+    tabcomplete: {active: false, data: [], prefix: ''},\r
+\r
     events: {\r
         'keydown input': 'process'\r
     },\r
@@ -438,7 +441,8 @@ kiwi.view.ControlBox = Backbone.View.extend({
     },\r
 \r
     process: function (ev) {\r
-        var inp = $(ev.currentTarget),\r
+        var that = this,\r
+            inp = $(ev.currentTarget),\r
             inp_val = inp.val(),\r
             meta;\r
 \r
@@ -447,6 +451,13 @@ kiwi.view.ControlBox = Backbone.View.extend({
         } else {\r
             meta = ev.altKey;\r
         }\r
+\r
+        // If not a tab key, reset the tabcomplete data\r
+        if (this.tabcomplete.active && ev.keyCode !== 9) {\r
+            this.tabcomplete.active = false;\r
+            this.tabcomplete.data = [];\r
+            this.tabcomplete.prefix = '';\r
+        }\r
         \r
         switch (true) {\r
         case (ev.keyCode === 13):              // return\r
@@ -480,12 +491,66 @@ kiwi.view.ControlBox = Backbone.View.extend({
         case (ev.keyCode === 37 && meta):            // left\r
             kiwi.app.panels.view.prev();\r
             return false;\r
-            break;\r
 \r
         case (ev.keyCode === 39 && meta):            // right\r
             kiwi.app.panels.view.next();\r
             return false;\r
-            break;\r
+\r
+        case (ev.keyCode === 9):                     // tab\r
+            this.tabcomplete.active = true;\r
+            if (_.isEqual(this.tabcomplete.data, [])) {\r
+                // Get possible autocompletions\r
+                var ac_data = [];\r
+                $.each(kiwi.app.panels.active.get('members').models, function (i, member) {\r
+                    if (!member) return;\r
+                    ac_data.push(member.get('nick'));\r
+                });\r
+                ac_data = _.sortBy(ac_data, function (nick) {\r
+                    return nick;\r
+                });\r
+                this.tabcomplete.data = ac_data;\r
+            }\r
+\r
+            if (inp_val[inp[0].selectionStart - 1] === ' ') {\r
+                return false;\r
+            }\r
+            \r
+            (function () {\r
+                var tokens = inp_val.substring(0, inp[0].selectionStart).split(' '),\r
+                    val,\r
+                    p1,\r
+                    newnick,\r
+                    range,\r
+                    nick = tokens[tokens.length - 1];\r
+                if (this.tabcomplete.prefix === '') {\r
+                    this.tabcomplete.prefix = nick;\r
+                }\r
+\r
+                this.tabcomplete.data = _.select(this.tabcomplete.data, function (n) {\r
+                    return (n.toLowerCase().indexOf(that.tabcomplete.prefix.toLowerCase()) === 0);\r
+                });\r
+\r
+                if (this.tabcomplete.data.length > 0) {\r
+                    p1 = inp[0].selectionStart - (nick.length);\r
+                    val = inp_val.substr(0, p1);\r
+                    newnick = this.tabcomplete.data.shift();\r
+                    this.tabcomplete.data.push(newnick);\r
+                    val += newnick;\r
+                    val += inp_val.substr(inp[0].selectionStart);\r
+                    inp.val(val);\r
+\r
+                    if (inp[0].setSelectionRange) {\r
+                        inp[0].setSelectionRange(p1 + newnick.length, p1 + newnick.length);\r
+                    } else if (inp[0].createTextRange) { // not sure if this bit is actually needed....\r
+                        range = inp[0].createTextRange();\r
+                        range.collapse(true);\r
+                        range.moveEnd('character', p1 + newnick.length);\r
+                        range.moveStart('character', p1 + newnick.length);\r
+                        range.select();\r
+                    }\r
+                }\r
+            }).apply(this);\r
+            return false;\r
         }\r
     },\r
 \r