]> code.citadel.org Git - citadel.git/blob - webcit/static/wclib.js
f328bdf81076cf4a88c22c23afe4492558afb62e
[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 var room_is_trash = 0;
11
12 if (document.layers) {browserType = "nn4"}
13 if (document.all) {browserType = "ie"}
14 if (window.navigator.userAgent.toLowerCase().match("gecko")) {
15         browserType= "gecko"
16 }
17
18 var ns6=document.getElementById&&!document.all;
19
20
21
22 // We love string tokenizers.
23 function extract_token(source_string, token_num, delimiter) {
24         var i = 0;
25         var extracted_string = source_string;
26
27         if (token_num > 0) {
28                 for (i=0; i<token_num; ++i) {
29                         var j = extracted_string.indexOf(delimiter);
30                         if (j >= 0) {
31                                 extracted_string = extracted_string.substr(j+1);
32                         }
33                 }
34         }
35
36         j = extracted_string.indexOf(delimiter);
37         if (j >= 0) {
38                 extracted_string = extracted_string.substr(0, j);
39         }
40
41         return extracted_string;
42 }
43
44
45
46 // This code handles the popups for important-messages.
47 function hide_imsg_popup() {
48         if (browserType == "gecko" )
49                 document.poppedLayer = eval('document.getElementById(\'important_message\')');
50         else if (browserType == "ie")
51                 document.poppedLayer = eval('document.all[\'important_message\']');
52         else
53                 document.poppedLayer = eval('document.layers[\'`important_message\']');
54
55         document.poppedLayer.style.visibility = "hidden";
56 }
57
58
59 // This function activates the ajax-powered recipient autocompleters on the message entry screen.
60 function activate_entmsg_autocompleters() {
61         new Ajax.Autocompleter('cc_id', 'cc_name_choices', 'cc_autocomplete', {} );
62         new Ajax.Autocompleter('bcc_id', 'bcc_name_choices', 'bcc_autocomplete', {} );
63         new Ajax.Autocompleter('recp_id', 'recp_name_choices', 'recp_autocomplete', {} );
64 }
65
66
67
68 // Toggle the icon bar between menu/roomlist...
69 var which_div_expanded = null;
70 var num_drop_targets = 0;
71 var drop_targets_elements = new Array();
72 var drop_targets_roomnames = new Array();
73
74 function switch_to_room_list() {
75         $('iconbar').innerHTML = $('iconbar').innerHTML.substr(0, $('iconbar').innerHTML.indexOf('switch'));
76         new Ajax.Updater('iconbar', 'iconbar_ajax_rooms', { method: 'get' } );
77 }
78
79 function expand_floor(floor_div) {
80         if (which_div_expanded != null) {
81                 if ($(which_div_expanded) != null) {
82                         $(which_div_expanded).style.display = 'none' ;
83                 }
84         }
85
86         // clicking on the already-expanded floor causes the whole list to collapse
87         if (which_div_expanded == floor_div) {
88                 which_div_expanded = null;
89
90                 // notify the server that no floors are expanded
91                 new Ajax.Request(
92                         'set_floordiv_expanded/-1', {
93                                 method: 'post'
94                         }
95                 );
96                 return true;
97         }
98
99         // expand the requested floor
100         $(floor_div).style.display = 'block';
101         which_div_expanded = floor_div;
102
103         // notify the server of which floor is expanded
104         new Ajax.Request(
105                 'set_floordiv_expanded/'+floor_div, {
106                         method: 'post'
107                 }
108         );
109 }
110
111 function switch_to_menu_buttons() {
112         which_div_expanded = null;
113         num_drop_targets = 0;
114         new Ajax.Updater('iconbar', 'iconbar_ajax_menu', { method: 'get' } );
115 }
116
117
118 // Static variables for mailbox view...
119 //
120 var CtdlNumMsgsSelected = 0;
121 var CtdlMsgsSelected = new Array();
122
123 // This gets called when you single click on a message in the mailbox view.
124 // We know that the element id of the table row will be the letter 'm' plus the message number.
125 //
126 function CtdlSingleClickMsg(evt, msgnum) {
127
128         // Clear the preview pane until we load the new message
129         $('preview_pane').innerHTML = '';
130
131         // De-select any messages that were already selected, *unless* the Ctrl key
132         // is being pressed, in which case the user wants multi select.
133         if (!evt.ctrlKey) {
134                 if (CtdlNumMsgsSelected > 0) {
135                         for (i=0; i<CtdlNumMsgsSelected; ++i) {
136                                 $('m'+CtdlMsgsSelected[i]).style.backgroundColor = '#fff';
137                                 $('m'+CtdlMsgsSelected[i]).style.color = '#000';
138                         }
139                         CtdlNumMsgsSelected = 0;
140                 }
141         }
142
143         // For multi select ... is the message being clicked already selected?
144         already_selected = 0;
145         if ( (evt.ctrlKey) && (CtdlNumMsgsSelected > 0) ) {
146                 for (i=0; i<CtdlNumMsgsSelected; ++i) {
147                         if (CtdlMsgsSelected[i] == msgnum) {
148                                 already_selected = 1;
149                                 already_selected_pos = i;
150                         }
151                 }
152         }
153
154         // Now select (or de-select) the message
155         if ( (evt.ctrlKey) && (already_selected == 1) ) {
156
157                 // Deselect: first un-highlight it...
158                 $('m'+msgnum).style.backgroundColor = '#fff';
159                 $('m'+msgnum).style.color = '#000';
160
161                 // Then remove it from the selected messages list.
162                 for (i=already_selected_pos; i<(CtdlNumMsgsSelected-1); ++i) {
163                         CtdlMsgsSelected[i] = CtdlMsgsSelected[i+1];
164                 }
165                 CtdlNumMsgsSelected = CtdlNumMsgsSelected - 1;
166                 
167         }
168         else {
169                 // Select: first highlight it...
170                 $('m'+msgnum).style.backgroundColor='#69aaff';
171                 $('m'+msgnum).style.color='#fff';
172
173                 // Then add it to the selected messages list.
174                 CtdlNumMsgsSelected = CtdlNumMsgsSelected + 1;
175                 CtdlMsgsSelected[CtdlNumMsgsSelected-1] = msgnum;
176         }
177
178         // Update the preview pane
179         new Ajax.Updater('preview_pane', 'msg/'+msgnum, { method: 'get' } );
180
181         // Mark the message as read
182         new Ajax.Request(
183                 'ajax_servcmd', {
184                         method: 'post',
185                         parameters: 'g_cmd=SEEN '+msgnum+'|1',
186                         onComplete: CtdlRemoveTheUnseenBold(msgnum)
187                 }
188         );
189
190         return false;           // try to defeat the default click behavior
191 }
192
193 // Delete selected messages.
194 function CtdlDeleteSelectedMessages(evt) {
195         
196         if (CtdlNumMsgsSelected < 1) {
197                 // Nothing to delete, so exit silently.
198                 return false;
199         }
200         for (i=0; i<CtdlNumMsgsSelected; ++i) {
201                 if (parseInt(room_is_trash) > 0) {
202                         new Ajax.Request(
203                                 'ajax_servcmd', {
204                                         method: 'post',
205                                         parameters: 'g_cmd=DELE ' + CtdlMsgsSelected[i],
206                                         onComplete: CtdlClearDeletedMsg(CtdlMsgsSelected[i])
207                                 }
208                         );
209                 }
210                 else {
211                         new Ajax.Request(
212                                 'ajax_servcmd', {
213                                         method: 'post',
214                                         parameters: 'g_cmd=MOVE ' + CtdlMsgsSelected[i] + '|_TRASH_|0',
215                                         onComplete: CtdlClearDeletedMsg(CtdlMsgsSelected[i])
216                                 }
217                         );
218                 }
219         }
220         CtdlNumMsgsSelected = 0;
221
222         // Clear the preview pane too.
223         $('preview_pane').innerHTML = '';
224 }
225
226
227 // Move selected messages.
228 function CtdlMoveSelectedMessages(evt, target_roomname) {
229         
230         if (CtdlNumMsgsSelected < 1) {
231                 // Nothing to delete, so exit silently.
232                 return false;
233         }
234         for (i=0; i<CtdlNumMsgsSelected; ++i) {
235                 new Ajax.Request(
236                         'ajax_servcmd', {
237                                 method:'post',
238                                 parameters:'g_cmd=MOVE ' + CtdlMsgsSelected[i] + '|' + target_roomname + '|0',
239                                 onComplete:CtdlClearDeletedMsg(CtdlMsgsSelected[i])
240                         }
241                 );
242         }
243         CtdlNumMsgsSelected = 0;
244
245         // Clear the preview pane too.
246         $('preview_pane').innerHTML = '';
247 }
248
249
250
251 // This gets called when the user touches the keyboard after selecting messages...
252 function CtdlMsgListKeyPress(evt) {
253         if(document.all) {                              // aIEeee
254                 var whichKey = window.event.keyCode;
255         }
256         else {                                          // non-sux0r browsers
257                 var whichKey = evt.which;
258         }
259         if (whichKey == 46) {                           // DELETE key
260                 CtdlDeleteSelectedMessages(evt);
261         }
262         return true;
263 }
264
265 // Take the boldface away from a message to indicate that it has been seen.
266 function CtdlRemoveTheUnseenBold(msgnum) {
267         $('m'+msgnum).style.fontWeight='normal';
268 }
269
270 // A message has been deleted, so yank it from the list.
271 function CtdlClearDeletedMsg(msgnum) {
272
273
274         // Traverse the table looking for a row whose ID contains the desired msgnum
275         var table = $('summary_headers');
276         if (table) {
277                 for (var r = 0; r < table.rows.length; r++) {
278                         var thename = table.rows[r].id;
279                         if (thename.substr(1) == msgnum) {
280                                 try {
281                                         table.deleteRow(r);
282                                 }
283                                 catch(e) {
284                                         alert('error: browser failed to clear row ' + r);
285                                 }
286                         }
287                 }
288         }
289         else {                                          // if we can't delete it,
290                 new Effect.Squish('m'+msgnum);          // just hide it.
291         }
292
293
294 }
295
296 // These functions called when the user down-clicks on the message list resizer bar
297
298 var saved_x = 0;
299 var saved_y = 0;
300
301 function CtdlResizeMsgListMouseUp(evt) {
302         document.onmouseup = null;
303         document.onmousemove = null;
304         if (document.layers) {
305                 document.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
306         }
307         return true;
308 }
309
310 function CtdlResizeMsgListMouseMove(evt) {
311         y = (ns6 ? evt.clientY : event.clientY);
312         increment = y - saved_y;
313
314         // First move the bottom of the message list...
315         d = $('message_list');
316         if (d.offsetHeight){
317                 divHeight = d.offsetHeight;
318         }
319         else if (d.style.pixelHeight) {
320                 divHeight = d.style.pixelHeight;
321         }
322         d.style.height = (divHeight + increment) + 'px';
323
324         // Then move the top of the preview pane...
325         d = $('preview_pane');
326         if (d.offsetTop){
327                 divTop = d.offsetTop;
328         }
329         else if (d.style.pixelTop) {
330                 divTop = d.style.pixelTop;
331         }
332         d.style.top = (divTop + increment) + 'px';
333
334         // Resize the bottom of the preview pane...
335         d = $('preview_pane');
336         if (d.offsetHeight){
337                 divHeight = d.offsetHeight;
338         }
339         else if (d.style.pixelHeight) {
340                 divHeight = d.style.pixelHeight;
341         }
342         d.style.height = (divHeight - increment) + 'px';
343
344         // Then move the top of the slider bar.
345         d = $('resize_msglist');
346         if (d.offsetTop){
347                 divTop = d.offsetTop;
348         }
349         else if (d.style.pixelTop) {
350                 divTop = d.style.pixelTop;
351         }
352         d.style.top = (divTop + increment) + 'px';
353
354         saved_y = y;
355         return true;
356 }
357
358 function CtdlResizeMsgListMouseDown(evt) {
359         saved_y = (ns6 ? evt.clientY : event.clientY);
360         document.onmouseup = CtdlResizeMsgListMouseUp;
361         document.onmousemove = CtdlResizeMsgListMouseMove;
362         if (document.layers) {
363                 document.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
364         }
365         return false;           // disable the default action
366 }
367
368
369
370 // These functions handle drag and drop message moving
371
372 var mm_div = null;
373
374 function CtdlMoveMsgMouseDown(evt, msgnum) {
375
376         // do the highlight first
377         CtdlSingleClickMsg(evt, msgnum);
378
379         // Now handle the possibility of dragging
380         saved_x = (ns6 ? evt.clientX : event.clientX);
381         saved_y = (ns6 ? evt.clientY : event.clientY);
382         document.onmouseup = CtdlMoveMsgMouseUp;
383         document.onmousemove = CtdlMoveMsgMouseMove;
384         if (document.layers) {
385                 document.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
386         }
387
388         return false;
389 }
390
391 function CtdlMoveMsgMouseMove(evt) {
392         x = (ns6 ? evt.clientX : event.clientX);
393         y = (ns6 ? evt.clientY : event.clientY);
394
395         if ( (x == saved_x) && (y == saved_y) ) {
396                 return true;
397         }
398
399         if (CtdlNumMsgsSelected < 1) { 
400                 return true;
401         }
402
403         if (!mm_div) {
404
405
406                 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>";
407                 for (i=0; i<CtdlNumMsgsSelected; ++i) {
408                         drag_o_text = drag_o_text + 
409                                 ctdl_ts_getInnerText(
410                                         $('m'+CtdlMsgsSelected[i]).cells[0]
411                                 ) + '<br>';
412                 }
413                 drag_o_text = drag_o_text + "<div>";
414
415                 mm_div = document.createElement("DIV");
416                 mm_div.style.position='absolute';
417                 mm_div.style.top = y + 'px';
418                 mm_div.style.left = x + 'px';
419                 mm_div.style.pixelHeight = '300';
420                 mm_div.style.pixelWidth = '300';
421                 mm_div.innerHTML = drag_o_text;
422                 document.body.appendChild(mm_div);
423         }
424         else {
425                 mm_div.style.top = y + 'px';
426                 mm_div.style.left = x + 'px';
427         }
428
429         return false;   // prevent the default mouse action from happening?
430 }
431
432 function CtdlMoveMsgMouseUp(evt) {
433         document.onmouseup = null;
434         document.onmousemove = null;
435         if (document.layers) {
436                 document.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
437         }
438
439         if (mm_div) {
440                 document.body.removeChild(mm_div);      
441                 mm_div = null;
442         }
443
444         if (num_drop_targets < 1) {     // nowhere to drop
445                 return true;
446         }
447
448         // Did we release the mouse button while hovering over a drop target?
449         // NOTE: this only works cross-browser because the iconbar div is always
450         //      positioned at 0,0.  Browsers differ in whether the 'offset'
451         //      functions return pos relative to the document or parent.
452
453         for (i=0; i<num_drop_targets; ++i) {
454
455                 x = (ns6 ? evt.clientX : event.clientX);
456                 y = (ns6 ? evt.clientY : event.clientY);
457
458                 l = parseInt(drop_targets_elements[i].offsetLeft);
459                 t = parseInt(drop_targets_elements[i].offsetTop);
460                 r = parseInt(drop_targets_elements[i].offsetLeft)
461                   + parseInt(drop_targets_elements[i].offsetWidth);
462                 b = parseInt(drop_targets_elements[i].offsetTop)
463                   + parseInt(drop_targets_elements[i].offsetHeight);
464
465                 /* alert('Offsets are: ' + l + ' ' + t + ' ' + r + ' ' + b + '.'); */
466         
467                 if ( (x >= l) && (x <= r) && (y >= t) && (y <= b) ) {
468                         // Yes, we dropped it on a hotspot.
469                         CtdlMoveSelectedMessages(evt, drop_targets_roomnames[i]);
470                         return true;
471                 }
472         }
473
474         return true;
475 }
476
477
478 function ctdl_ts_getInnerText(el) {
479         if (typeof el == "string") return el;
480         if (typeof el == "undefined") { return el };
481         if (el.innerText) return el.innerText;  //Not needed but it is faster
482         var str = "";
483         
484         var cs = el.childNodes;
485         var l = cs.length;
486         for (var i = 0; i < l; i++) {
487                 switch (cs[i].nodeType) {
488                         case 1: //ELEMENT_NODE
489                                 str += ts_getInnerText(cs[i]);
490                                 break;
491                         case 3: //TEXT_NODE
492                                 str += cs[i].nodeValue;
493                                 break;
494                 }
495         }
496         return str;
497 }
498
499
500
501 // This function handles the creation of new notes in the "Notes" view.
502 //
503 function add_new_note() {
504
505         new_eid = Math.random() + '';
506         new_eid = new_eid.substr(3);
507
508         $('new_notes_here').innerHTML = $('new_notes_here').innerHTML
509                 + '<IMG ALIGN=MIDDLE src=\"static/storenotes_48x.gif\">'
510                 + '<span id=\"note' + new_eid + '\">' + Date() + '</span><br />'
511         ;
512
513         new Ajax.InPlaceEditor('note' + new_eid,
514                 'updatenote?eid=' + new_eid , {rows:5,cols:72});
515 }