]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/baseuiwindow.js
iframes should not have borders -- breaks page layout
[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 = new QHash();
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 var message = $defined(line) ? line["m"] : null;
107
108 /* https://dl.dropboxusercontent.com/u/180911/notify.png */
109 if(type.match(/^OUR/)) {
110 if(type.match(/NOTICE$/)) {
111 /* default */
112 } else {
113 hilight = qwebirc.ui.HILIGHT_SPEECH;
114 }
115 } else if(this.client.hilightController.match(message)) {
116 hilight = qwebirc.ui.HILIGHT_US;
117 lhilight = true;
118 } else if(type.match(/NOTICE$/)) {
119 /* default */
120 } else if(this.type == qwebirc.ui.WINDOW_QUERY || this.type == qwebirc.ui.WINDOW_MESSAGES) {
121 hilight = qwebirc.ui.HILIGHT_US;
122 } else {
123 hilight = qwebirc.ui.HILIGHT_SPEECH;
124 }
125
126 if(hilight == qwebirc.ui.HILIGHT_US) {
127 var title = this.parentObject.theme.message("NOTIFY" + type + "TITLE", line, false);
128 var body = this.parentObject.theme.message("NOTIFY" + type + "BODY", line, false);
129 var selectMe = function() { this.parentObject.selectWindow(this); }.bind(this);
130
131 this.parentObject.notify(title, body, selectMe);
132 }
133 }
134 }
135
136 if(!this.active && (hilight != qwebirc.ui.HILIGHT_NONE))
137 this.setHilighted(hilight);
138
139 if(type)
140 line = this.parentObject.theme.message(type, line, lhilight);
141
142 var tsE = document.createElement("span");
143 tsE.className = "timestamp";
144 tsE.appendChild(document.createTextNode(qwebirc.irc.IRCTimestamp(new Date()) + " "));
145 element.appendChild(tsE);
146
147 qwebirc.ui.Colourise(line, element, this.client.exec, this.parentObject.urlDispatcher.bind(this.parentObject), this);
148 this.scrollAdd(element);
149 },
150 errorMessage: function(message) {
151 this.addLine("", message, "warncolour");
152 },
153 infoMessage: function(type, message) {
154 if(message === undefined) {
155 this.addLine("", type, "infocolour");
156 } else {
157 this.addLine(type, message, "infocolour");
158 }
159 },
160 setHilighted: function(state) {
161 if(state == qwebirc.ui.HILIGHT_NONE || state >= this.hilighted)
162 this.hilighted = state;
163 },
164 scrolledDown: function() {
165 if(this.scrolltimer)
166 return true;
167
168 var parent = this.lines;
169
170 var scrollPos = parent.getScroll().y;
171 var linesHeight = parent.getScrollSize().y;
172 var windowHeight = parent.clientHeight;
173
174 /*
175 * fixes an IE bug: the scrollheight is less than the actual height
176 * when the div isn't full
177 */
178 if(linesHeight < windowHeight)
179 linesHeight = windowHeight;
180
181 return scrollPos + windowHeight >= linesHeight - 3; /* window of error */
182 },
183 getScrollParent: function() {
184 var scrollparent = this.lines;
185
186 if($defined(this.scroller))
187 scrollparent = this.scroller;
188 return scrollparent;
189 },
190 scrollToBottom: function() {
191 if(this.type == qwebirc.ui.WINDOW_CUSTOM || this.type == qwebirc.ui.WINDOW_CONNECT)
192 return;
193
194 var parent = this.lines;
195 var scrollparent = this.getScrollParent();
196
197 scrollparent.scrollTo(parent.getScroll().x, parent.getScrollSize().y);
198 },
199 scrollAdd: function(element) {
200 var parent = this.lines;
201
202 /* scroll in bursts, else the browser gets really slow */
203 if($defined(element)) {
204 var sd = this.scrolledDown();
205 parent.appendChild(element);
206 if(parent.childNodes.length > qwebirc.ui.MAXIMUM_LINES_PER_WINDOW)
207 parent.removeChild(parent.firstChild);
208
209 if(sd && !this.scrollTimer)
210 this.scrolltimer = this.scrollAdd.delay(50, this, [null]);
211 } else {
212 this.scrollToBottom();
213 this.scrolltimer = null;
214 }
215 },
216 updateNickList: function(nicks) {
217 var nickHash = new QHash(), present = new QSet();
218 var added = [];
219 var lnh = this.lastNickHash;
220
221 for(var i=0;i<nicks.length;i++)
222 present.add(nicks[i]);
223
224 lnh.each(function(k, v) {
225 if(!present.contains(k))
226 this.nickListRemove(k, v);
227 }, this);
228
229 for(var i=0;i<nicks.length;i++) {
230 var n = nicks[i];
231 var l = lnh.get(n);
232 if(!l) {
233 l = this.nickListAdd(n, i);
234 if(!l)
235 l = 1;
236 }
237 nickHash.put(n, l);
238 }
239
240 this.lastNickHash = nickHash;
241 },
242 nickListAdd: function(position, nick) {
243 },
244 nickListRemove: function(nick, stored) {
245 },
246 historyExec: function(line) {
247 this.commandhistory.addLine(line);
248 this.client.exec(line);
249 },
250 focusChange: function(newValue) {
251 if(newValue == true || !(this.type & qwebirc.ui.WINDOW_LASTLINE))
252 return;
253
254 this.replaceLastPositionLine();
255 },
256 replaceLastPositionLine: function() {
257 if(this.parentObject.uiOptions.LASTPOS_LINE) {
258 if(!this.scrolledDown())
259 return;
260
261 if(!this.lastPositionLineInserted) {
262 this.scrollAdd(this.lastPositionLine);
263 } else if(this.lines.lastChild != this.lastPositionLine) {
264 try {
265 this.lines.removeChild(this.lastPositionLine);
266 } catch(e) {
267 /* IGNORE, /clear removes lastPositionLine from the dom without resetting it. */
268 }
269 this.scrollAdd(this.lastPositionLine);
270 }
271 } else {
272 if(this.lastPositionLineInserted)
273 this.lines.removeChild(this.lastPositionLine);
274 }
275
276 this.lastPositionLineInserted = this.parentObject.uiOptions.LASTPOS_LINE;
277 },
278 rename: function(name) {
279 }
280 });