]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/baseuiwindow.js
Merge (drop connect.html change as too many forks rely on our existing crappy version...)
[irc/quakenet/qwebirc.git] / js / ui / baseuiwindow.js
1 qwebirc.ui.HILIGHT_NONE = 0;
2 qwebirc.ui.HILIGHT_ACTIVITY = 1;
3 qwebirc.ui.HILIGHT_SPEECH = 2;
4 qwebirc.ui.HILIGHT_US = 3;
5
6 qwebirc.ui.MAXIMUM_LINES_PER_WINDOW = 1000;
7
8 qwebirc.ui.WINDOW_LASTLINE = qwebirc.ui.WINDOW_QUERY | qwebirc.ui.WINDOW_MESSAGES | qwebirc.ui.WINDOW_CHANNEL | qwebirc.ui.WINDOW_STATUS;
9
10 qwebirc.ui.Window = new Class({
11 Implements: [Events],
12 initialize: function(parentObject, client, type, name, identifier) {
13 this.parentObject = parentObject;
14 this.type = type;
15 this.name = name;
16 this.active = false;
17 this.client = client;
18 this.identifier = identifier;
19 this.hilighted = qwebirc.ui.HILIGHT_NONE;
20 this.scrolltimer = null;
21 this.commandhistory = this.parentObject.commandhistory;
22 this.scrolleddown = true;
23 this.scrollpos = null;
24 this.lastNickHash = {};
25 this.lastSelected = null;
26 this.subWindow = null;
27 this.closed = false;
28
29 if(this.type & qwebirc.ui.WINDOW_LASTLINE) {
30 this.lastPositionLine = new Element("hr");
31 this.lastPositionLine.addClass("lastpos");
32 this.lastPositionLineInserted = false;
33 }
34 },
35 updateTopic: function(topic, element) {
36 qwebirc.ui.Colourise("[" + topic + "]", element, this.client.exec, this.parentObject.urlDispatcher.bind(this.parentObject), this);
37 },
38 close: function() {
39 this.closed = true;
40
41 if($defined(this.scrolltimer)) {
42 $clear(this.scrolltimer);
43 this.scrolltimer = null;
44 }
45
46 this.parentObject.__closed(this);
47 this.fireEvent("close", this);
48 },
49 subEvent: function(event) {
50 if($defined(this.subWindow))
51 this.subWindow.fireEvent(event);
52 },
53 setSubWindow: function(window) {
54 this.subWindow = window;
55 },
56 select: function() {
57 if(this.lastPositionLineInserted && !this.parentObject.uiOptions.LASTPOS_LINE) {
58 this.lines.removeChild(this.lastPositionLine);
59 this.lastPositionLineInserted = false;
60 }
61
62 this.active = true;
63 this.parentObject.__setActiveWindow(this);
64 if(this.hilighted)
65 this.setHilighted(qwebirc.ui.HILIGHT_NONE);
66
67 this.subEvent("select");
68 this.resetScrollPos();
69 this.lastSelected = new Date();
70 },
71 deselect: function() {
72 this.subEvent("deselect");
73
74 this.setScrollPos();
75 if($defined(this.scrolltimer)) {
76 $clear(this.scrolltimer);
77 this.scrolltimer = null;
78 }
79
80 if(this.type & qwebirc.ui.WINDOW_LASTLINE)
81 this.replaceLastPositionLine();
82
83 this.active = false;
84 },
85 resetScrollPos: function() {
86 if(this.scrolleddown) {
87 this.scrollToBottom();
88 } else if($defined(this.scrollpos)) {
89 this.getScrollParent().scrollTo(this.scrollpos.x, this.scrollpos.y);
90 }
91 },
92 setScrollPos: function() {
93 if(!this.parentObject.singleWindow) {
94 this.scrolleddown = this.scrolledDown();
95 this.scrollpos = this.lines.getScroll();
96 }
97 },
98 addLine: function(type, line, colour, element) {
99 var hilight = qwebirc.ui.HILIGHT_NONE;
100 var lhilight = false;
101
102 if(type) {
103 hilight = qwebirc.ui.HILIGHT_ACTIVITY;
104
105 if(type.match(/(NOTICE|ACTION|MSG)$/)) {
106 if(this.type == qwebirc.ui.WINDOW_QUERY || this.type == qwebirc.ui.WINDOW_MESSAGES) {
107 if(type.match(/^OUR/) || type.match(/NOTICE$/)) {
108 hilight = qwebirc.ui.HILIGHT_ACTIVITY;
109 } else {
110 hilight = qwebirc.ui.HILIGHT_US;
111 this.parentObject.beep();
112 this.parentObject.flash();
113 }
114 }
115 if(!type.match(/^OUR/) && this.client.hilightController.match(line["m"])) {
116 lhilight = true;
117 hilight = qwebirc.ui.HILIGHT_US;
118 this.parentObject.beep();
119 this.parentObject.flash();
120 } else if(hilight != qwebirc.ui.HILIGHT_US) {
121 hilight = qwebirc.ui.HILIGHT_SPEECH;
122 }
123 }
124 }
125
126 if(!this.active && (hilight != qwebirc.ui.HILIGHT_NONE))
127 this.setHilighted(hilight);
128
129 if(type)
130 line = this.parentObject.theme.message(type, line, lhilight);
131
132 var tsE = document.createElement("span");
133 tsE.className = "timestamp";
134 tsE.appendChild(document.createTextNode(qwebirc.irc.IRCTimestamp(new Date()) + " "));
135 element.appendChild(tsE);
136
137 qwebirc.ui.Colourise(line, element, this.client.exec, this.parentObject.urlDispatcher.bind(this.parentObject), this);
138 this.scrollAdd(element);
139 },
140 errorMessage: function(message) {
141 this.addLine("", message, "warncolour");
142 },
143 infoMessage: function(message) {
144 this.addLine("", message, "infocolour");
145 },
146 setHilighted: function(state) {
147 if(state == qwebirc.ui.HILIGHT_NONE || state >= this.hilighted)
148 this.hilighted = state;
149 },
150 scrolledDown: function() {
151 if(this.scrolltimer)
152 return true;
153
154 var parent = this.lines;
155
156 var prev = parent.getScroll();
157 var prevbottom = parent.getScrollSize().y;
158 var prevheight = parent.clientHeight;
159
160 /*
161 * fixes an IE bug: the scrollheight is less than the actual height
162 * when the div isn't full
163 */
164 if(prevbottom < prevheight)
165 prevbottom = prevheight;
166
167 return prev.y + prevheight == prevbottom;
168 },
169 getScrollParent: function() {
170 var scrollparent = this.lines;
171
172 if($defined(this.scroller))
173 scrollparent = this.scroller;
174 return scrollparent;
175 },
176 scrollToBottom: function() {
177 if(this.type == qwebirc.ui.WINDOW_CUSTOM || this.type == qwebirc.ui.WINDOW_CONNECT)
178 return;
179
180 var parent = this.lines;
181 var scrollparent = this.getScrollParent();
182
183 scrollparent.scrollTo(parent.getScroll().x, parent.getScrollSize().y);
184 },
185 scrollAdd: function(element) {
186 var parent = this.lines;
187
188 /* scroll in bursts, else the browser gets really slow */
189 if($defined(element)) {
190 var sd = this.scrolledDown();
191 parent.appendChild(element);
192 if(parent.childNodes.length > qwebirc.ui.MAXIMUM_LINES_PER_WINDOW)
193 parent.removeChild(parent.firstChild);
194 if(sd) {
195 if(this.scrolltimer)
196 $clear(this.scrolltimer);
197 this.scrolltimer = this.scrollAdd.delay(50, this, [null]);
198 } else {
199 this.scrollToBottom();
200 this.scrolltimer = null;
201 }
202 } else {
203 this.scrollToBottom();
204 this.scrolltimer = null;
205 }
206 },
207 updateNickList: function(nicks) {
208 var nickHash = {}, present = {};
209 var added = [];
210 var lnh = this.lastNickHash;
211
212 for(var i=0;i<nicks.length;i++)
213 present[nicks[i]] = 1;
214
215 for(var k in lnh)
216 if(!present[k])
217 this.nickListRemove(k, lnh[k]);
218
219 for(var i=0;i<nicks.length;i++) {
220 var n = nicks[i];
221 var l = lnh[n];
222 if(!l) {
223 l = this.nickListAdd(n, i);
224 if(!l)
225 l = 1;
226 }
227 nickHash[n] = l;
228 }
229
230 this.lastNickHash = nickHash;
231 },
232 nickListAdd: function(position, nick) {
233 },
234 nickListRemove: function(nick, stored) {
235 },
236 historyExec: function(line) {
237 this.commandhistory.addLine(line);
238 this.client.exec(line);
239 },
240 focusChange: function(newValue) {
241 if(newValue == true || !(this.type & qwebirc.ui.WINDOW_LASTLINE))
242 return;
243
244 this.replaceLastPositionLine();
245 },
246 replaceLastPositionLine: function() {
247 if(this.parentObject.uiOptions.LASTPOS_LINE) {
248 if(!this.lastPositionLineInserted) {
249 this.scrollAdd(this.lastPositionLine);
250 } else if(this.lines.lastChild != this.lastPositionLine) {
251 try {
252 this.lines.removeChild(this.lastPositionLine);
253 } catch(e) {
254 /* IGNORE, /clear removes lastPositionLine from the dom without resetting it. */
255 }
256 this.scrollAdd(this.lastPositionLine);
257 }
258 } else {
259 if(this.lastPositionLineInserted)
260 this.lines.removeChild(this.lastPositionLine);
261 }
262
263 this.lastPositionLineInserted = this.parentObject.uiOptions.LASTPOS_LINE;
264 },
265 rename: function(name) {
266 }
267 });