]>
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
) {
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_CHANNEL
) {
33 /* "slug[TAB]" == "slug: " */
35 if((postword
!= "") && postword
.charAt(0) == " ") {
36 postword
= ":" + postword
;
38 postword
= ": " + postword
;
42 obj
= qwebirc
.ui
.ChannelUsersTabCompleter
;
50 this.obj
= new obj(preword
, word
, postword
, w
);
51 if(!$defined(this.obj
))
55 var r
= this.obj
.get();
60 qwebirc
.util
.setCaretPos(textBox
, r
[0] + this.incr
);
67 qwebirc
.ui
.TabIterator
= new Class({
68 initialize: function(prefix
, list
) {
70 if(!$defined(list
) || list
.length
== 0) {
75 var prefixl
= prefix
.toIRCCompletion();
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
++) {
80 var l2
= list
[i
].toIRCCompletion();
82 if(l2
.startsWith(prefixl
))
92 * ideally next would do the list gubbins recursively, but no JS engine currently
93 * support tail recursion :(
95 if(!$defined(this.list
))
98 this.pos
= this.pos
+ 1;
99 if(this.pos
>= this.list
.length
)
102 return this.list
[this.pos
];
106 qwebirc
.ui
.BaseTabCompleter
= new Class({
107 initialize: function(prefix
, existingNick
, suffix
, list
) {
108 this.existingNick
= existingNick
;
109 this.prefix
= prefix
;
110 this.suffix
= suffix
;
111 this.iterator
= new qwebirc
.ui
.TabIterator(existingNick
, list
);
114 var n
= this.iterator
.next();
118 var p
= this.prefix
+ n
;
119 return [p
.length
, p
+ this.suffix
];
123 qwebirc
.ui
.QueryTabCompleter
= new Class({
124 Extends: qwebirc
.ui
.BaseTabCompleter
,
125 initialize: function(prefix
, existingNick
, suffix
, window
) {
126 this.parent(prefix
, existingNick
, suffix
, window
.client
.lastNicks
);
130 qwebirc
.ui
.ChannelNameTabCompleter
= new Class({
131 Extends: qwebirc
.ui
.BaseTabCompleter
,
132 initialize: function(prefix
, existingText
, suffix
, window
) {
136 var wa
= window
.parentObject
.windows
[window
.client
];
138 for(var c
in window
.client
.channels
) {
148 l
.sort(function(a
, b
) {
153 for(var i
=0;i
<l
.length
;i
++)
155 this.parent(prefix
, existingText
, suffix
, l2
);
159 qwebirc
.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
);
164 this.parent(prefix
, existingText
, suffix
, nc
);