]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/tabcompleter.js
Add tab completion for nicknames in channels (by last spoke order), queries (again...
[irc/quakenet/qwebirc.git] / js / ui / tabcompleter.js
1 qwebirc.ui.TabCompleterFactory = new Class({
2 initialize: function(ui) {
3 this.ui = ui;
4 this.reset();
5 },
6 tabComplete: function(textBox) {
7 var text = textBox.value;
8
9 if(!$defined(this.obj)) {
10 this.incr = 1;
11
12 var w = this.ui.getActiveWindow();
13 if(!w)
14 return;
15
16 var startingWord = qwebirc.util.getEnclosedWord(text, qwebirc.util.getCaretPos(textBox));
17 var preword = "", word = "", postword = "";
18 if($defined(startingWord)) {
19 var preword = text.substring(0, startingWord[0]);
20 var word = startingWord[1];
21 var postword = text.substring(startingWord[0] + word.length);
22 }
23
24 var ltext = text.toLowerCase();
25 if(text == "") {
26 preword = "/msg ";
27 obj = qwebirc.ui.QueryTabCompleter;
28 } else if(w.client.isChannel(word)) {
29 obj = qwebirc.ui.ChannelNameTabCompleter;
30 } else if(ltext.match(/^\/(q|query|msg) /i)) {
31 obj = qwebirc.ui.QueryTabCompleter;
32 } else if(w.type == qwebirc.ui.WINDOW_CHANNEL) {
33 /* "slug[TAB]" == "slug: " */
34 if(preword == "") {
35 if((postword != "") && postword.charAt(0) == " ") {
36 postword = ":" + postword;
37 } else {
38 postword = ": " + postword;
39 }
40 this.incr++;
41 }
42 obj = qwebirc.ui.ChannelUsersTabCompleter;
43 } else {
44 return;
45 }
46
47 if(postword == "")
48 postword = " ";
49
50 this.obj = new obj(preword, word, postword, w);
51 if(!$defined(this.obj))
52 return;
53 }
54
55 var r = this.obj.get();
56 if(!$defined(r))
57 return;
58
59 textBox.value = r[1];
60 qwebirc.util.setCaretPos(textBox, r[0] + this.incr);
61 },
62 reset: function() {
63 this.obj = null;
64 }
65 });
66
67 qwebirc.ui.TabIterator = new Class({
68 initialize: function(prefix, list) {
69 this.prefix = prefix;
70 if(!$defined(list) || list.length == 0) {
71 this.list = null;
72 } else {
73 var l = [];
74
75 var prefixl = prefix.toIRCCompletion();
76
77 /* convert the nick list to IRC lower case, stripping all non letters
78 * before comparisions */
79 for(var i=0;i<list.length;i++) {
80 var l2 = list[i].toIRCCompletion();
81
82 if(l2.startsWith(prefixl))
83 l.push(list[i]);
84 }
85 this.list = l;
86 }
87
88 this.pos = -1;
89 },
90 next: function() {
91 /*
92 * ideally next would do the list gubbins recursively, but no JS engine currently
93 * support tail recursion :(
94 */
95 if(!$defined(this.list))
96 return null;
97
98 this.pos = this.pos + 1;
99 if(this.pos >= this.list.length)
100 this.pos = 0;
101
102 return this.list[this.pos];
103 }
104 });
105
106 qwebirc.ui.BaseTabCompleter = new Class({
107 initialize: function(prefix, existingNick, suffix, list) {
108 this.existingNick = existingNick;
109 this.prefix = prefix;
110 this.suffix = suffix;
111 this.iterator = new qwebirc.ui.TabIterator(existingNick, list);
112 },
113 get: function() {
114 var n = this.iterator.next();
115 if(!$defined(n))
116 return null;
117
118 var p = this.prefix + n;
119 return [p.length, p + this.suffix];
120 }
121 });
122
123 qwebirc.ui.QueryTabCompleter = new Class({
124 Extends: qwebirc.ui.BaseTabCompleter,
125 initialize: function(prefix, existingNick, suffix, window) {
126 this.parent(prefix, existingNick, suffix, window.client.lastNicks);
127 }
128 });
129
130 qwebirc.ui.ChannelNameTabCompleter = new Class({
131 Extends: qwebirc.ui.BaseTabCompleter,
132 initialize: function(prefix, existingText, suffix, window) {
133
134 /* WTB map */
135 var l = [];
136 for(var c in window.client.channels)
137 l.push(c);
138
139 this.parent(prefix, existingText, suffix, l);
140 }
141 });
142
143 qwebirc.ui.ChannelUsersTabCompleter = new Class({
144 Extends: qwebirc.ui.BaseTabCompleter,
145 initialize: function(prefix, existingText, suffix, window) {
146 var nc = window.client.tracker.getSortedByLastSpoke(window.name);
147
148 this.parent(prefix, existingText, suffix, nc);
149 }
150 });