* Made the iconbar room list somewhat usable (collapse/expand). It is not yet
[citadel.git] / webcit / static / wclib.js
1 //
2 // $Id: wclib.js,v 625.2 2005/09/18 04:04:32 ajc Exp $
3 //
4 // JavaScript function library for WebCit.
5 //
6 //
7
8
9 var browserType;
10
11 if (document.layers) {browserType = "nn4"}
12 if (document.all) {browserType = "ie"}
13 if (window.navigator.userAgent.toLowerCase().match("gecko")) {
14         browserType= "gecko"
15 }
16
17 var ns6=document.getElementById&&!document.all;
18
19
20 //
21 // This code handles the popups for instant messages.
22 //
23
24
25 function hide_page_popup() {
26         if (browserType == "gecko" )
27                 document.poppedLayer = eval('document.getElementById(\'page_popup\')');
28         else if (browserType == "ie")
29                 document.poppedLayer = eval('document.all[\'page_popup\']');
30         else
31                 document.poppedLayer = eval('document.layers[\'`page_popup\']');
32
33         document.poppedLayer.style.visibility = "hidden";
34 }
35
36 function hide_imsg_popup() {
37         if (browserType == "gecko" )
38                 document.poppedLayer = eval('document.getElementById(\'important_message\')');
39         else if (browserType == "ie")
40                 document.poppedLayer = eval('document.all[\'important_message\']');
41         else
42                 document.poppedLayer = eval('document.layers[\'`important_message\']');
43
44         document.poppedLayer.style.visibility = "hidden";
45 }
46
47 // This function activates the ajax-powered recipient autocompleters on the message entry screen.
48 function activate_entmsg_autocompleters() {
49         new Ajax.Autocompleter('cc_id', 'cc_name_choices', 'cc_autocomplete', {} );
50         new Ajax.Autocompleter('bcc_id', 'bcc_name_choices', 'bcc_autocomplete', {} );
51         new Ajax.Autocompleter('recp_id', 'recp_name_choices', 'recp_autocomplete', {} );
52 }
53
54
55 // Static variables for mailbox view...
56 //
57 var CtdlNumMsgsSelected = 0;
58 var CtdlMsgsSelected = new Array();
59
60 // This gets called when you single click on a message in the mailbox view.
61 // We know that the element id of the table row will be the letter 'm' plus the message number.
62 //
63 function CtdlSingleClickMsg(evt, msgnum) {
64
65         // Clear the preview pane until we load the new message
66         $('preview_pane').innerHTML = '';
67
68         // De-select any messages that were already selected, *unless* the Ctrl key
69         // is being pressed, in which case the user wants multi select.
70         if (!evt.ctrlKey) {
71                 if (CtdlNumMsgsSelected > 0) {
72                         for (i=0; i<CtdlNumMsgsSelected; ++i) {
73                                 $('m'+CtdlMsgsSelected[i]).style.backgroundColor = '#fff';
74                                 $('m'+CtdlMsgsSelected[i]).style.color = '#000';
75                         }
76                         CtdlNumMsgsSelected = 0;
77                 }
78         }
79
80         // For multi select ... is the message being clicked already selected?
81         already_selected = 0;
82         if ( (evt.ctrlKey) && (CtdlNumMsgsSelected > 0) ) {
83                 for (i=0; i<CtdlNumMsgsSelected; ++i) {
84                         if (CtdlMsgsSelected[i] == msgnum) {
85                                 already_selected = 1;
86                         }
87                 }
88         }
89
90         // Now select (or de-select) the message
91         if ( (evt.ctrlKey) && (already_selected == 1) ) {
92                 $('m'+msgnum).style.backgroundColor = '#fff';
93                 $('m'+msgnum).style.color = '#000';
94         }
95         else {
96                 $('m'+msgnum).style.backgroundColor='#69aaff';
97                 $('m'+msgnum).style.color='#fff';
98                 CtdlNumMsgsSelected = CtdlNumMsgsSelected + 1;
99                 CtdlMsgsSelected[CtdlNumMsgsSelected-1] = msgnum;
100         }
101
102         // Update the preview pane
103         new Ajax.Updater('preview_pane', 'msg/'+msgnum, { method: 'get' } );
104
105         // Mark the message as read
106         new Ajax.Request(
107                 'ajax_servcmd', {
108                         method: 'post',
109                         parameters: 'g_cmd=SEEN '+msgnum+'|1',
110                         onComplete: CtdlRemoveTheUnseenBold(msgnum)
111                 }
112         );
113
114         return false;           // try to defeat the default click behavior
115 }
116
117 // Delete selected messages.
118 function CtdlDeleteSelectedMessages(evt) {
119         
120         if (CtdlNumMsgsSelected < 1) {
121                 // Nothing to delete, so exit silently.
122                 return false;
123         }
124         for (i=0; i<CtdlNumMsgsSelected; ++i) {
125                 new Ajax.Request(
126                         'ajax_servcmd', {
127                                 method: 'post',
128                                 parameters: 'g_cmd=MOVE ' + CtdlMsgsSelected[i] + '|_TRASH_|0',
129                                 onComplete: CtdlClearDeletedMsg(CtdlMsgsSelected[i])
130                         }
131                 );
132         }
133         CtdlNumMsgsSelected = 0;
134
135         // Clear the preview pane too.
136         $('preview_pane').innerHTML = '';
137 }
138
139 // This gets called when the user touches the keyboard after selecting messages...
140 function CtdlMsgListKeyPress(evt) {
141         if(document.all) {                              // aIEeee
142                 var whichKey = window.event.keyCode;
143         }
144         else {                                          // non-sux0r browsers
145                 var whichKey = evt.which;
146         }
147         if (whichKey == 46) {                           // DELETE key
148                 CtdlDeleteSelectedMessages(evt);
149         }
150         return true;
151 }
152
153 // Take the boldface away from a message to indicate that it has been seen.
154 function CtdlRemoveTheUnseenBold(msgnum) {
155         $('m'+msgnum).style.fontWeight='normal';
156 }
157
158 // A message has been deleted, so yank it from the list.
159 // (IE barfs on m9999.innerHTML='' so we use a script.aculo.us effect instead.)
160 function CtdlClearDeletedMsg(msgnum) {
161         new Effect.Squish('m'+msgnum);
162 }
163
164
165 // These functions called when the user down-clicks on the message list resizer bar
166
167 var saved_x = 0;
168 var saved_y = 0;
169
170 function CtdlResizeMsgListMouseUp(evt) {
171         document.onmouseup = null;
172         document.onmousemove = null;
173         if (document.layers) {
174                 document.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
175         }
176         return true;
177 }
178
179 function CtdlResizeMsgListMouseMove(evt) {
180         y = (ns6 ? evt.clientY : event.clientY);
181         increment = y - saved_y;
182
183         // First move the bottom of the message list...
184         d = $('message_list');
185         if (d.offsetHeight){
186                 divHeight = d.offsetHeight;
187         }
188         else if (d.style.pixelHeight) {
189                 divHeight = d.style.pixelHeight;
190         }
191         d.style.height = (divHeight + increment) + 'px';
192
193         // Then move the top of the preview pane...
194         d = $('preview_pane');
195         if (d.offsetTop){
196                 divTop = d.offsetTop;
197         }
198         else if (d.style.pixelTop) {
199                 divTop = d.style.pixelTop;
200         }
201         d.style.top = (divTop + increment) + 'px';
202
203         // Resize the bottom of the preview pane...
204         d = $('preview_pane');
205         if (d.offsetHeight){
206                 divHeight = d.offsetHeight;
207         }
208         else if (d.style.pixelHeight) {
209                 divHeight = d.style.pixelHeight;
210         }
211         d.style.height = (divHeight - increment) + 'px';
212
213         // Then move the top of the slider bar.
214         d = $('resize_msglist');
215         if (d.offsetTop){
216                 divTop = d.offsetTop;
217         }
218         else if (d.style.pixelTop) {
219                 divTop = d.style.pixelTop;
220         }
221         d.style.top = (divTop + increment) + 'px';
222
223         saved_y = y;
224         return true;
225 }
226
227 function CtdlResizeMsgListMouseDown(evt) {
228         saved_y = (ns6 ? evt.clientY : event.clientY);
229         document.onmouseup = CtdlResizeMsgListMouseUp;
230         document.onmousemove = CtdlResizeMsgListMouseMove;
231         if (document.layers) {
232                 document.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
233         }
234         return false;           // disable the default action
235 }
236
237
238
239 // These functions handle drag and drop message moving
240
241 var mm_div = null;
242
243 function CtdlMoveMsgMouseDown(evt, msgnum) {
244
245         // do the highlight first
246         CtdlSingleClickMsg(evt, msgnum);
247
248         // Now handle the possibility of dragging
249         saved_x = (ns6 ? evt.clientX : event.clientX);
250         saved_y = (ns6 ? evt.clientY : event.clientY);
251         document.onmouseup = CtdlMoveMsgMouseUp;
252         document.onmousemove = CtdlMoveMsgMouseMove;
253         if (document.layers) {
254                 document.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
255         }
256
257         return false;
258 }
259
260 function CtdlMoveMsgMouseMove(evt) {
261         x = (ns6 ? evt.clientX : event.clientX);
262         y = (ns6 ? evt.clientY : event.clientY);
263
264         if ( (x == saved_x) && (y == saved_y) ) {
265                 return true;
266         }
267
268         if (CtdlNumMsgsSelected < 1) { 
269                 return true;
270         }
271
272         if (!mm_div) {
273
274
275                 drag_o_text = "<div style=\"overflow:none; background-color:#fff; color:#000; border: 1px solid black; filter:alpha(opacity=75); -moz-opacity:.75; opacity:.75;\"><tr><td>";
276                 for (i=0; i<CtdlNumMsgsSelected; ++i) {
277                         drag_o_text = drag_o_text + 
278                                 ctdl_ts_getInnerText(
279                                         $('m'+CtdlMsgsSelected[i]).cells[0]
280                                 ) + '<br>';
281                 }
282                 drag_o_text = drag_o_text + "<div>";
283
284                 mm_div = document.createElement("DIV");
285                 mm_div.style.position='absolute';
286                 mm_div.style.top = y + 'px';
287                 mm_div.style.left = x + 'px';
288                 mm_div.style.pixelHeight = '300';
289                 mm_div.style.pixelWidth = '300';
290                 mm_div.innerHTML = drag_o_text;
291                 document.body.appendChild(mm_div);
292         }
293         else {
294                 mm_div.style.top = y + 'px';
295                 mm_div.style.left = x + 'px';
296         }
297
298         return false;   // prevent the default mouse action from happening?
299 }
300
301 function CtdlMoveMsgMouseUp(evt) {
302         document.onmouseup = null;
303         document.onmousemove = null;
304         if (document.layers) {
305                 document.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
306         }
307
308         if (mm_div) {
309                 document.body.removeChild(mm_div);      
310                 mm_div = null;
311         }
312
313         // Did we release the mouse button while hovering over a drop target?
314         // NOTE: this only works cross-browser because the iconbar div is always
315         //      positioned at 0,0.  Browsers differ in whether the 'offset'
316         //      functions return pos relative to the document or parent.
317
318         x = (ns6 ? evt.clientX : event.clientX);
319         y = (ns6 ? evt.clientY : event.clientY);
320
321         l = parseInt($('dropstuff').offsetLeft);
322         t = parseInt($('dropstuff').offsetTop);
323         r = parseInt($('dropstuff').offsetLeft) + parseInt($('dropstuff').offsetWidth);
324         b = parseInt($('dropstuff').offsetTop) + parseInt($('dropstuff').offsetHeight);
325
326         // alert('Offsets are: ' + l + ' ' + t + ' ' + r + ' ' + b + '.');
327
328         if ( (x >= l) && (x <= r) && (y >= t) && (y <= b) ) {
329                 // Yes, we dropped it on a hotspot.  Just delete for now... FIXME
330                 CtdlDeleteSelectedMessages(evt);
331         }
332
333         return true;
334 }
335
336
337 function ctdl_ts_getInnerText(el) {
338         if (typeof el == "string") return el;
339         if (typeof el == "undefined") { return el };
340         if (el.innerText) return el.innerText;  //Not needed but it is faster
341         var str = "";
342         
343         var cs = el.childNodes;
344         var l = cs.length;
345         for (var i = 0; i < l; i++) {
346                 switch (cs[i].nodeType) {
347                         case 1: //ELEMENT_NODE
348                                 str += ts_getInnerText(cs[i]);
349                                 break;
350                         case 3: //TEXT_NODE
351                                 str += cs[i].nodeValue;
352                                 break;
353                 }
354         }
355         return str;
356 }
357
358
359 // icon bar toggler tabs...
360
361 var which_div_expanded = null;
362
363 function switch_to_room_list() {
364         new Ajax.Updater('iconbar', 'iconbar_ajax_rooms', { method: 'get' } );
365 }
366
367 function expand_floor(floor_div) {
368         if (which_div_expanded != null) {
369                 $(which_div_expanded).style.display = 'none' ;
370         }
371         $(floor_div).style.display = 'block';
372         which_div_expanded = floor_div;
373 }
374
375 function switch_to_menu_buttons() {
376         new Ajax.Updater('iconbar', 'iconbar_ajax_menu', { method: 'get' } );
377 }