]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/ui/tabcompleter.js
use home-made string hashset/map instead of js default
[irc/quakenet/qwebirc.git] / js / ui / tabcompleter.js
CommitLineData
3184781b
CP
1qwebirc.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;
89e80348
S
32 } else if(w.type == qwebirc.ui.WINDOW_QUERY) {
33 obj = qwebirc.ui.QueryNickTabCompleter;
3184781b
CP
34 } else if(w.type == qwebirc.ui.WINDOW_CHANNEL) {
35 /* "slug[TAB]" == "slug: " */
36 if(preword == "") {
37 if((postword != "") && postword.charAt(0) == " ") {
38 postword = ":" + postword;
39 } else {
40 postword = ": " + postword;
41 }
42 this.incr++;
43 }
44 obj = qwebirc.ui.ChannelUsersTabCompleter;
45 } else {
46 return;
47 }
48
49 if(postword == "")
50 postword = " ";
51
52 this.obj = new obj(preword, word, postword, w);
53 if(!$defined(this.obj))
54 return;
55 }
56
57 var r = this.obj.get();
58 if(!$defined(r))
59 return;
60
61 textBox.value = r[1];
62 qwebirc.util.setCaretPos(textBox, r[0] + this.incr);
63 },
64 reset: function() {
65 this.obj = null;
66 }
67});
68
69qwebirc.ui.TabIterator = new Class({
fd3734d4 70 initialize: function(client, prefix, list) {
3184781b
CP
71 this.prefix = prefix;
72 if(!$defined(list) || list.length == 0) {
73 this.list = null;
74 } else {
75 var l = [];
76
fd3734d4 77 var prefixl = qwebirc.irc.toIRCCompletion(client, prefix);
3184781b
CP
78
79 /* convert the nick list to IRC lower case, stripping all non letters
80 * before comparisions */
81 for(var i=0;i<list.length;i++) {
fd3734d4 82 var l2 = qwebirc.irc.toIRCCompletion(client, list[i]);
3184781b
CP
83
84 if(l2.startsWith(prefixl))
85 l.push(list[i]);
86 }
87 this.list = l;
88 }
89
90 this.pos = -1;
91 },
92 next: function() {
93 /*
94 * ideally next would do the list gubbins recursively, but no JS engine currently
95 * support tail recursion :(
96 */
97 if(!$defined(this.list))
98 return null;
99
100 this.pos = this.pos + 1;
101 if(this.pos >= this.list.length)
102 this.pos = 0;
103
104 return this.list[this.pos];
105 }
106});
107
108qwebirc.ui.BaseTabCompleter = new Class({
fd3734d4 109 initialize: function(client, prefix, existingNick, suffix, list) {
3184781b
CP
110 this.existingNick = existingNick;
111 this.prefix = prefix;
112 this.suffix = suffix;
fd3734d4 113 this.iterator = new qwebirc.ui.TabIterator(client, existingNick, list);
3184781b
CP
114 },
115 get: function() {
116 var n = this.iterator.next();
117 if(!$defined(n))
118 return null;
119
120 var p = this.prefix + n;
121 return [p.length, p + this.suffix];
122 }
123});
124
125qwebirc.ui.QueryTabCompleter = new Class({
126 Extends: qwebirc.ui.BaseTabCompleter,
127 initialize: function(prefix, existingNick, suffix, window) {
fd3734d4 128 this.parent(window.client, prefix, existingNick, suffix, window.client.lastNicks);
3184781b
CP
129 }
130});
131
89e80348
S
132qwebirc.ui.QueryNickTabCompleter = new Class({
133 Extends: qwebirc.ui.BaseTabCompleter,
134 initialize: function(prefix, existingText, suffix, window) {
0aecbefd 135 var nick = window.name
89e80348
S
136 this.parent(window.client, prefix, existingText, suffix, [nick]);
137 }
138});
139
3184781b
CP
140qwebirc.ui.ChannelNameTabCompleter = new Class({
141 Extends: qwebirc.ui.BaseTabCompleter,
142 initialize: function(prefix, existingText, suffix, window) {
143
ea29e3d7
CP
144 var wa = window.parentObject.windows.get(window.parentObject.getClientId(window.client));
145 var l = window.client.channels.map(function(c) {
146 var w = wa.get(c);
3184781b 147
3236ca77
CP
148 /* redundant? */
149 if($defined(w))
150 w = w.lastSelected;
151
ea29e3d7
CP
152 return [w, c];
153 });
3236ca77
CP
154
155 l.sort(function(a, b) {
156 return b[0] - a[0];
157 });
158
159 var l2 = [];
160 for(var i=0;i<l.length;i++)
161 l2.push(l[i][1]);
fd3734d4 162 this.parent(window.client, prefix, existingText, suffix, l2);
3184781b
CP
163 }
164});
165
166qwebirc.ui.ChannelUsersTabCompleter = new Class({
167 Extends: qwebirc.ui.BaseTabCompleter,
168 initialize: function(prefix, existingText, suffix, window) {
169 var nc = window.client.tracker.getSortedByLastSpoke(window.name);
170
fd3734d4 171 this.parent(window.client, prefix, existingText, suffix, nc);
3184781b
CP
172 }
173});