]>
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_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();
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
))
100 this.pos
= this.pos
+ 1;
101 if(this.pos
>= this.list
.length
)
104 return this.list
[this.pos
];
108 qwebirc
.ui
.BaseTabCompleter
= new Class({
109 initialize: function(client
, prefix
, existingNick
, suffix
, list
) {
110 this.existingNick
= existingNick
;
111 this.prefix
= prefix
;
112 this.suffix
= suffix
;
113 this.iterator
= new qwebirc
.ui
.TabIterator(client
, existingNick
, list
);
116 var n
= this.iterator
.next();
120 var p
= this.prefix
+ n
;
121 return [p
.length
, p
+ this.suffix
];
125 qwebirc
.ui
.QueryTabCompleter
= new Class({
126 Extends: qwebirc
.ui
.BaseTabCompleter
,
127 initialize: function(prefix
, existingNick
, suffix
, window
) {
128 this.parent(window
.client
, prefix
, existingNick
, suffix
, window
.client
.lastNicks
);
132 qwebirc
.ui
.QueryNickTabCompleter
= new Class({
133 Extends: qwebirc
.ui
.BaseTabCompleter
,
134 initialize: function(prefix
, existingText
, suffix
, window
) {
135 var nick
= window
.name
136 this.parent(window
.client
, prefix
, existingText
, suffix
, [nick
]);
140 qwebirc
.ui
.ChannelNameTabCompleter
= new Class({
141 Extends: qwebirc
.ui
.BaseTabCompleter
,
142 initialize: function(prefix
, existingText
, suffix
, window
) {
144 var wa
= window
.parentObject
.windows
.get(window
.parentObject
.getClientId(window
.client
));
145 var l
= window
.client
.channels
.map(function(c
) {
155 l
.sort(function(a
, b
) {
160 for(var i
=0;i
<l
.length
;i
++)
162 this.parent(window
.client
, prefix
, existingText
, suffix
, l2
);
166 qwebirc
.ui
.ChannelUsersTabCompleter
= new Class({
167 Extends: qwebirc
.ui
.BaseTabCompleter
,
168 initialize: function(prefix
, existingText
, suffix
, window
) {
169 var nc
= window
.client
.tracker
.getSortedByLastSpoke(window
.name
);
171 this.parent(window
.client
, prefix
, existingText
, suffix
, nc
);