]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/ui/tabcompleter.js
Refactor RFC1459 case handling functions and their callers, as a consequence fixing...
[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;
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
67qwebirc.ui.TabIterator = new Class({
fd3734d4 68 initialize: function(client, prefix, list) {
3184781b
CP
69 this.prefix = prefix;
70 if(!$defined(list) || list.length == 0) {
71 this.list = null;
72 } else {
73 var l = [];
74
fd3734d4 75 var prefixl = qwebirc.irc.toIRCCompletion(client, prefix);
3184781b
CP
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++) {
fd3734d4 80 var l2 = qwebirc.irc.toIRCCompletion(client, list[i]);
3184781b
CP
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
106qwebirc.ui.BaseTabCompleter = new Class({
fd3734d4 107 initialize: function(client, prefix, existingNick, suffix, list) {
3184781b
CP
108 this.existingNick = existingNick;
109 this.prefix = prefix;
110 this.suffix = suffix;
fd3734d4 111 this.iterator = new qwebirc.ui.TabIterator(client, existingNick, list);
3184781b
CP
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
123qwebirc.ui.QueryTabCompleter = new Class({
124 Extends: qwebirc.ui.BaseTabCompleter,
125 initialize: function(prefix, existingNick, suffix, window) {
fd3734d4 126 this.parent(window.client, prefix, existingNick, suffix, window.client.lastNicks);
3184781b
CP
127 }
128});
129
130qwebirc.ui.ChannelNameTabCompleter = new Class({
131 Extends: qwebirc.ui.BaseTabCompleter,
132 initialize: function(prefix, existingText, suffix, window) {
133
134 /* WTB map */
3236ca77 135 var l = [];
fd3734d4 136 var wa = window.parentObject.windows[window.parentObject.getClientId(window.client)];
3236ca77
CP
137
138 for(var c in window.client.channels) {
139 var w = wa[c];
3184781b 140
3236ca77
CP
141 /* redundant? */
142 if($defined(w))
143 w = w.lastSelected;
144
145 l.push([w, c]);
146 }
147
148 l.sort(function(a, b) {
149 return b[0] - a[0];
150 });
151
152 var l2 = [];
153 for(var i=0;i<l.length;i++)
154 l2.push(l[i][1]);
fd3734d4 155 this.parent(window.client, prefix, existingText, suffix, l2);
3184781b
CP
156 }
157});
158
159qwebirc.ui.ChannelUsersTabCompleter = new Class({
160 Extends: qwebirc.ui.BaseTabCompleter,
161 initialize: function(prefix, existingText, suffix, window) {
162 var nc = window.client.tracker.getSortedByLastSpoke(window.name);
163
fd3734d4 164 this.parent(window.client, prefix, existingText, suffix, nc);
3184781b
CP
165 }
166});