]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/tabcompleter.js
add spinner to connect dialog
[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, backwards) {
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_QUERY) {
33 obj = qwebirc.ui.QueryNickTabCompleter;
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(backwards);
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({
70 initialize: function(client, prefix, list) {
71 this.prefix = prefix;
72 if(!$defined(list) || list.length == 0) {
73 this.list = null;
74 } else {
75 var l = [];
76
77 var prefixl = qwebirc.irc.toIRCCompletion(client, prefix);
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++) {
82 var l2 = qwebirc.irc.toIRCCompletion(client, list[i]);
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) || this.list.length == 0)
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 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
114 return this.list[this.pos];
115 }
116 });
117
118 qwebirc.ui.BaseTabCompleter = new Class({
119 initialize: function(client, prefix, existingNick, suffix, list) {
120 this.existingNick = existingNick;
121 this.prefix = prefix;
122 this.suffix = suffix;
123 this.iterator = new qwebirc.ui.TabIterator(client, existingNick, list);
124 },
125 get: function(backwards) {
126 var n = backwards ? this.iterator.prev() : this.iterator.next();
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) {
138 this.parent(window.client, prefix, existingNick, suffix, window.client.lastNicks);
139 }
140 });
141
142 qwebirc.ui.QueryNickTabCompleter = new Class({
143 Extends: qwebirc.ui.BaseTabCompleter,
144 initialize: function(prefix, existingText, suffix, window) {
145 var nick = window.name
146 this.parent(window.client, prefix, existingText, suffix, [nick]);
147 }
148 });
149
150 qwebirc.ui.ChannelNameTabCompleter = new Class({
151 Extends: qwebirc.ui.BaseTabCompleter,
152 initialize: function(prefix, existingText, suffix, window) {
153
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);
157
158 /* redundant? */
159 if($defined(w))
160 w = w.lastSelected;
161
162 return [w, c];
163 });
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]);
172 this.parent(window.client, prefix, existingText, suffix, l2);
173 }
174 });
175
176 qwebirc.ui.ChannelUsersTabCompleter = new Class({
177 Extends: qwebirc.ui.BaseTabCompleter,
178 initialize: function(prefix, existingText, suffix, window) {
179 var nc = window.client.tracker.getSortedByLastSpokePrefix(window.name);
180
181 this.parent(window.client, prefix, existingText, suffix, nc);
182 }
183 });