]>
jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/tabcompleter.js
1 qwebirc
.ui
.TabCompleterFactory
= new Class({
2 initialize: function(ui
) {
6 tabComplete: function(textBox
, backwards
) {
7 var text
= textBox
.value
;
9 if(!$defined(this.obj
)) {
12 var w
= this.ui
.getActiveWindow();
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
);
24 var ltext
= text
.toLowerCase();
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: " */
37 if((postword
!= "") && postword
.charAt(0) == " ") {
38 postword
= ":" + postword
;
40 postword
= ": " + postword
;
44 obj
= qwebirc
.ui
.ChannelUsersTabCompleter
;
52 this.obj
= new obj(preword
, word
, postword
, w
);
53 if(!$defined(this.obj
))
57 var r
= this.obj
.get(backwards
);
62 qwebirc
.util
.setCaretPos(textBox
, r
[0] + this.incr
);
69 qwebirc
.ui
.TabIterator
= new Class({
70 initialize: function(client
, prefix
, list
) {
72 if(!$defined(list
) || list
.length
== 0) {
77 var prefixl
= qwebirc
.irc
.toIRCCompletion(client
, prefix
);
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
]);
84 if(l2
.startsWith(prefixl
))
94 * ideally next would do the list gubbins recursively, but no JS engine currently
95 * support tail recursion :(
97 if(!$defined(this.list
) || this.list
.length
== 0)
100 this.pos
= this.pos
+ 1;
101 if(this.pos
>= this.list
.length
)
104 return this.list
[this.pos
];
107 if(!$defined(this.list
) || this.list
.length
== 0)
110 this.pos
= this.pos
- 1;
112 this.pos
= this.list
.length
- 1;
114 return this.list
[this.pos
];
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
);
125 get: function(backwards
) {
126 var n
= backwards
? this.iterator
.prev() : this.iterator
.next();
130 var p
= this.prefix
+ n
;
131 return [p
.length
, p
+ this.suffix
];
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
);
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
]);
150 qwebirc
.ui
.ChannelNameTabCompleter
= new Class({
151 Extends: qwebirc
.ui
.BaseTabCompleter
,
152 initialize: function(prefix
, existingText
, suffix
, window
) {
154 var wa
= window
.parentObject
.windows
.get(window
.parentObject
.getClientId(window
.client
));
155 var l
= window
.client
.channels
.map(function(c
) {
165 l
.sort(function(a
, b
) {
170 for(var i
=0;i
<l
.length
;i
++)
172 this.parent(window
.client
, prefix
, existingText
, suffix
, l2
);
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
);
181 this.parent(window
.client
, prefix
, existingText
, suffix
, nc
);