]>
Commit | Line | Data |
---|---|---|
3184781b CP |
1 | qwebirc.ui.TabCompleterFactory = new Class({ |
2 | initialize: function(ui) { | |
3 | this.ui = ui; | |
4 | this.reset(); | |
5 | }, | |
710fd882 | 6 | tabComplete: function(textBox, backwards) { |
3184781b CP |
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 | ||
710fd882 | 57 | var r = this.obj.get(backwards); |
3184781b CP |
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 | ||
69 | qwebirc.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 | */ | |
710fd882 | 97 | if(!$defined(this.list) || this.list.length == 0) |
3184781b CP |
98 | return null; |
99 | ||
100 | this.pos = this.pos + 1; | |
101 | if(this.pos >= this.list.length) | |
102 | this.pos = 0; | |
103 | ||
710fd882 CP |
104 | return this.list[this.pos]; |
105 | }, | |
106 | prev: function() { | |
107 | if(!$defined(this.list) || this.list.length == 0) | |
108 | return null; | |
109 | ||
110 | this.pos = this.pos - 1; | |
111 | if(this.pos < 0) | |
112 | this.pos = this.list.length - 1; | |
113 | ||
3184781b CP |
114 | return this.list[this.pos]; |
115 | } | |
116 | }); | |
117 | ||
118 | qwebirc.ui.BaseTabCompleter = new Class({ | |
fd3734d4 | 119 | initialize: function(client, prefix, existingNick, suffix, list) { |
3184781b CP |
120 | this.existingNick = existingNick; |
121 | this.prefix = prefix; | |
122 | this.suffix = suffix; | |
fd3734d4 | 123 | this.iterator = new qwebirc.ui.TabIterator(client, existingNick, list); |
3184781b | 124 | }, |
710fd882 CP |
125 | get: function(backwards) { |
126 | var n = backwards ? this.iterator.prev() : this.iterator.next(); | |
3184781b CP |
127 | if(!$defined(n)) |
128 | return null; | |
129 | ||
130 | var p = this.prefix + n; | |
131 | return [p.length, p + this.suffix]; | |
132 | } | |
133 | }); | |
134 | ||
135 | qwebirc.ui.QueryTabCompleter = new Class({ | |
136 | Extends: qwebirc.ui.BaseTabCompleter, | |
137 | initialize: function(prefix, existingNick, suffix, window) { | |
fd3734d4 | 138 | this.parent(window.client, prefix, existingNick, suffix, window.client.lastNicks); |
3184781b CP |
139 | } |
140 | }); | |
141 | ||
89e80348 S |
142 | qwebirc.ui.QueryNickTabCompleter = new Class({ |
143 | Extends: qwebirc.ui.BaseTabCompleter, | |
144 | initialize: function(prefix, existingText, suffix, window) { | |
0aecbefd | 145 | var nick = window.name |
89e80348 S |
146 | this.parent(window.client, prefix, existingText, suffix, [nick]); |
147 | } | |
148 | }); | |
149 | ||
3184781b CP |
150 | qwebirc.ui.ChannelNameTabCompleter = new Class({ |
151 | Extends: qwebirc.ui.BaseTabCompleter, | |
152 | initialize: function(prefix, existingText, suffix, window) { | |
153 | ||
ea29e3d7 CP |
154 | var wa = window.parentObject.windows.get(window.parentObject.getClientId(window.client)); |
155 | var l = window.client.channels.map(function(c) { | |
156 | var w = wa.get(c); | |
3184781b | 157 | |
3236ca77 CP |
158 | /* redundant? */ |
159 | if($defined(w)) | |
160 | w = w.lastSelected; | |
161 | ||
ea29e3d7 CP |
162 | return [w, c]; |
163 | }); | |
3236ca77 CP |
164 | |
165 | l.sort(function(a, b) { | |
166 | return b[0] - a[0]; | |
167 | }); | |
168 | ||
169 | var l2 = []; | |
170 | for(var i=0;i<l.length;i++) | |
171 | l2.push(l[i][1]); | |
fd3734d4 | 172 | this.parent(window.client, prefix, existingText, suffix, l2); |
3184781b CP |
173 | } |
174 | }); | |
175 | ||
176 | qwebirc.ui.ChannelUsersTabCompleter = new Class({ | |
177 | Extends: qwebirc.ui.BaseTabCompleter, | |
178 | initialize: function(prefix, existingText, suffix, window) { | |
f9a07b87 | 179 | var nc = window.client.tracker.getSortedByLastSpokePrefix(window.name); |
3184781b | 180 | |
fd3734d4 | 181 | this.parent(window.client, prefix, existingText, suffix, nc); |
3184781b CP |
182 | } |
183 | }); |