]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/ui/baseuiwindow.js
Refactor window blur/focus code in order to allow windows to receive notification...
[irc/quakenet/qwebirc.git] / js / ui / baseuiwindow.js
CommitLineData
96f28062
CP
1qwebirc.ui.HILIGHT_NONE = 0;
2qwebirc.ui.HILIGHT_ACTIVITY = 1;
3qwebirc.ui.HILIGHT_SPEECH = 2;
4qwebirc.ui.HILIGHT_US = 3;
5
5c0b5a6e
CP
6qwebirc.ui.WINDOW_LASTLINE = qwebirc.ui.WINDOW_QUERY | qwebirc.ui.WINDOW_MESSAGES | qwebirc.ui.WINDOW_CHANNEL | qwebirc.ui.WINDOW_STATUS;
7
e20e5a6b 8qwebirc.ui.Window = new Class({
94b18192
CP
9 Implements: [Events],
10 initialize: function(parentObject, client, type, name, identifier) {
11 this.parentObject = parentObject;
12 this.type = type;
13 this.name = name;
14 this.active = false;
15 this.client = client;
16 this.identifier = identifier;
96f28062 17 this.hilighted = qwebirc.ui.HILIGHT_NONE;
381fddfd 18 this.scrolltimer = null;
9b63b053 19 this.commandhistory = this.parentObject.commandhistory;
25be5960 20 this.scrolleddown = true;
9d538d73 21 this.scrollpos = null;
52090a1f 22 this.lastNickHash = {};
3236ca77 23 this.lastSelected = null;
17f40fd9 24 this.subWindow = null;
f84bf379 25 this.closed = false;
5c0b5a6e
CP
26
27 if(this.type & qwebirc.ui.WINDOW_LASTLINE) {
28 this.lastPositionLine = new Element("hr");
29 this.lastPositionLine.addClass("lastpos");
30 this.lastPositionLineInserted = false;
31 }
94b18192 32 },
1f06a70a 33 updateTopic: function(topic, element) {
2cd9e32d 34 qwebirc.ui.Colourise("[" + topic + "]", element, this.client.exec, this.parentObject.urlDispatcher.bind(this.parentObject), this);
94b18192
CP
35 },
36 close: function() {
f84bf379
CP
37 this.closed = true;
38
381fddfd
CP
39 if($defined(this.scrolltimer)) {
40 $clear(this.scrolltimer);
41 this.scrolltimer = null;
42 }
43
94b18192
CP
44 this.parentObject.__closed(this);
45 this.fireEvent("close", this);
46 },
17f40fd9
CP
47 subEvent: function(event) {
48 if($defined(this.subWindow))
49 this.subWindow.fireEvent(event);
50 },
51 setSubWindow: function(window) {
52 this.subWindow = window;
53 },
94b18192 54 select: function() {
5c0b5a6e
CP
55 if(this.lastPositionLineInserted && !this.parentObject.uiOptions.LASTPOS_LINE) {
56 this.lines.removeChild(this.lastPositionLine);
57 this.lastPositionLineInserted = false;
58 }
59
94b18192
CP
60 this.active = true;
61 this.parentObject.__setActiveWindow(this);
381fddfd 62 if(this.hilighted)
96f28062 63 this.setHilighted(qwebirc.ui.HILIGHT_NONE);
17f40fd9
CP
64
65 this.subEvent("select");
9d538d73 66 this.resetScrollPos();
3236ca77 67 this.lastSelected = new Date();
94b18192
CP
68 },
69 deselect: function() {
17f40fd9
CP
70 this.subEvent("deselect");
71
9d538d73 72 this.setScrollPos();
7c633700
CP
73 if($defined(this.scrolltimer)) {
74 $clear(this.scrolltimer);
75 this.scrolltimer = null;
76 }
77
5c0b5a6e
CP
78 if(this.type & qwebirc.ui.WINDOW_LASTLINE) {
79 if(this.lastPositionLineInserted)
80 this.lines.removeChild(this.lastPositionLine);
81
82 if(this.parentObject.uiOptions.LASTPOS_LINE)
83 this.lines.appendChild(this.lastPositionLine);
84
85 this.lastPositionLineInserted = this.parentObject.uiOptions.LASTPOS_LINE;
86 }
87
94b18192
CP
88 this.active = false;
89 },
9d538d73
CP
90 resetScrollPos: function() {
91 if(this.scrolleddown) {
92 this.scrollToBottom();
93 } else if($defined(this.scrollpos)) {
94 this.getScrollParent().scrollTo(this.scrollpos.x, this.scrollpos.y);
95 }
96 },
97 setScrollPos: function() {
98 if(!this.parentObject.singleWindow) {
99 this.scrolleddown = this.scrolledDown();
100 this.scrollpos = this.lines.getScroll();
101 }
102 },
30bd2620 103 addLine: function(type, line, colour, element) {
96f28062 104 var hilight = qwebirc.ui.HILIGHT_NONE;
b2e77cf9
CP
105 var lhilight = false;
106
96f28062
CP
107 if(type) {
108 hilight = qwebirc.ui.HILIGHT_ACTIVITY;
109
110 if(type.match(/(NOTICE|ACTION|MSG)$/)) {
f74802c5 111 if(this.type == qwebirc.ui.WINDOW_QUERY || this.type == qwebirc.ui.WINDOW_MESSAGES) {
7af2d0c1 112 if(type.match(/^OUR/) || type.match(/NOTICE$/)) {
f74802c5
CP
113 hilight = qwebirc.ui.HILIGHT_ACTIVITY;
114 } else {
115 hilight = qwebirc.ui.HILIGHT_US;
aeb8c784 116 this.parentObject.beep();
326478c2 117 this.parentObject.flash();
f74802c5 118 }
b2e77cf9
CP
119 }
120 if(!type.match(/^OUR/) && this.client.hilightController.match(line["m"])) {
121 lhilight = true;
122 hilight = qwebirc.ui.HILIGHT_US;
aeb8c784 123 this.parentObject.beep();
326478c2 124 this.parentObject.flash();
b2e77cf9 125 } else if(hilight != qwebirc.ui.HILIGHT_US) {
96f28062
CP
126 hilight = qwebirc.ui.HILIGHT_SPEECH;
127 }
128 }
129 }
130
131 if(!this.active && (hilight != qwebirc.ui.HILIGHT_NONE))
132 this.setHilighted(hilight);
133
381fddfd 134 if(type)
b2e77cf9 135 line = this.parentObject.theme.message(type, line, lhilight);
381fddfd 136
2cd9e32d 137 qwebirc.ui.Colourise(qwebirc.irc.IRCTimestamp(new Date()) + " " + line, element, this.client.exec, this.parentObject.urlDispatcher.bind(this.parentObject), this);
381fddfd 138 this.scrollAdd(element);
94b18192
CP
139 },
140 errorMessage: function(message) {
5fc104ff 141 this.addLine("", message, "warncolour");
381fddfd 142 },
0acc3c54 143 infoMessage: function(message) {
5fc104ff 144 this.addLine("", message, "infocolour");
0acc3c54 145 },
381fddfd 146 setHilighted: function(state) {
96f28062
CP
147 if(state == qwebirc.ui.HILIGHT_NONE || state >= this.hilighted)
148 this.hilighted = state;
381fddfd 149 },
25be5960 150 scrolledDown: function() {
7c633700
CP
151 if(this.scrolltimer)
152 return true;
153
381fddfd 154 var parent = this.lines;
381fddfd 155
381fddfd
CP
156 var prev = parent.getScroll();
157 var prevbottom = parent.getScrollSize().y;
158 var prevsize = parent.getSize();
25be5960 159
fccb1dac
CP
160 /* fixes an IE bug */
161 if(prevbottom < prevsize.y)
162 prevbottom = prevsize.y;
163
7c633700
CP
164 return prev.y + prevsize.y == prevbottom;
165 },
9d538d73
CP
166 getScrollParent: function() {
167 var scrollparent = this.lines;
7c633700
CP
168
169 if($defined(this.scroller))
170 scrollparent = this.scroller;
9d538d73
CP
171 return scrollparent;
172 },
173 scrollToBottom: function() {
73a06774
CP
174 if(this.type == qwebirc.ui.WINDOW_CUSTOM || this.type == qwebirc.ui.WINDOW_CONNECT)
175 return;
176
9d538d73
CP
177 var parent = this.lines;
178 var scrollparent = this.getScrollParent();
7c633700
CP
179
180 scrollparent.scrollTo(parent.getScroll().x, parent.getScrollSize().y);
181 },
182 scrollAdd: function(element) {
183 var parent = this.lines;
381fddfd
CP
184
185 /* scroll in bursts, else the browser gets really slow */
186 if($defined(element)) {
25be5960 187 var sd = this.scrolledDown();
381fddfd 188 parent.appendChild(element);
7c633700 189 if(sd) {
381fddfd
CP
190 if(this.scrolltimer)
191 $clear(this.scrolltimer);
7c633700 192 this.scrolltimer = this.scrollAdd.delay(50, this, [null]);
381fddfd
CP
193 }
194 } else {
25be5960 195 this.scrollToBottom();
381fddfd
CP
196 this.scrolltimer = null;
197 }
9b63b053 198 },
52090a1f 199 updateNickList: function(nicks) {
833f14ce 200 var nickHash = {}, present = {};
52090a1f
CP
201 var added = [];
202 var lnh = this.lastNickHash;
203
833f14ce
CP
204 for(var i=0;i<nicks.length;i++)
205 present[nicks[i]] = 1;
206
207 for(var k in lnh)
208 if(!present[k])
209 this.nickListRemove(k, lnh[k]);
210
52090a1f
CP
211 for(var i=0;i<nicks.length;i++) {
212 var n = nicks[i];
213 var l = lnh[n];
214 if(!l) {
215 l = this.nickListAdd(n, i);
216 if(!l)
217 l = 1;
218 }
219 nickHash[n] = l;
220 }
221
52090a1f
CP
222 this.lastNickHash = nickHash;
223 },
224 nickListAdd: function(position, nick) {
225 },
226 nickListRemove: function(nick, stored) {
227 },
9b63b053
CP
228 historyExec: function(line) {
229 this.commandhistory.addLine(line);
230 this.client.exec(line);
eb271d86
CP
231 },
232 focusChange: function(newValue) {
233 console.log("focus change: " + newValue);
234 if(newValue == true || !(this.type & qwebirc.ui.WINDOW_LASTLINE))
235 return;
236
237 if(this.lastPositionLineInserted)
238 this.lines.removeChild(this.lastPositionLine);
239
240 if(this.parentObject.uiOptions.LASTPOS_LINE)
241 this.lines.appendChild(this.lastPositionLine);
242 }
94b18192 243});