c2572b8c74a55698e779b086f5f121a5fbcbe59d
[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 //
10 // This code handles the popups for instant messages.
11 //
12
13 var browserType;
14
15 if (document.layers) {browserType = "nn4"}
16 if (document.all) {browserType = "ie"}
17 if (window.navigator.userAgent.toLowerCase().match("gecko")) {
18         browserType= "gecko"
19 }
20
21 function hide_page_popup() {
22         if (browserType == "gecko" )
23                 document.poppedLayer = eval('document.getElementById(\'page_popup\')');
24         else if (browserType == "ie")
25                 document.poppedLayer = eval('document.all[\'page_popup\']');
26         else
27                 document.poppedLayer = eval('document.layers[\'`page_popup\']');
28
29         document.poppedLayer.style.visibility = "hidden";
30 }
31
32 function hide_imsg_popup() {
33         if (browserType == "gecko" )
34                 document.poppedLayer = eval('document.getElementById(\'important_message\')');
35         else if (browserType == "ie")
36                 document.poppedLayer = eval('document.all[\'important_message\']');
37         else
38                 document.poppedLayer = eval('document.layers[\'`important_message\']');
39
40         document.poppedLayer.style.visibility = "hidden";
41 }
42
43 // This function activates the ajax-powered recipient autocompleters on the message entry screen.
44 function activate_entmsg_autocompleters() {
45         new Ajax.Autocompleter('cc_id', 'cc_name_choices', 'cc_autocomplete', {} );
46         new Ajax.Autocompleter('bcc_id', 'bcc_name_choices', 'bcc_autocomplete', {} );
47         new Ajax.Autocompleter('recp_id', 'recp_name_choices', 'recp_autocomplete', {} );
48 }
49
50
51 // Static variables for mailbox view...
52 //
53 var CtdlNumMsgsSelected = 0;
54 var CtdlMsgsSelected = new Array(65536);        // arbitrary
55
56 // This gets called when you single click on a message in the mailbox view.
57 // We know that the element id of the table row will be the letter 'm' plus the message number.
58 //
59 function CtdlSingleClickMsg(evt, msgnum) {
60
61         // Clear the preview pane until we load the new message
62         $('preview_pane').innerHTML = '';
63
64         // De-select any messages that were already selected, *unless* the Ctrl key
65         // is being pressed, in which case the user wants multi select.
66         if (!evt.ctrlKey) {
67                 if (CtdlNumMsgsSelected > 0) {
68                         for (i=0; i<CtdlNumMsgsSelected; ++i) {
69                                 $('m'+CtdlMsgsSelected[i]).style.backgroundColor = '#fff';
70                                 $('m'+CtdlMsgsSelected[i]).style.color = '#000';
71                         }
72                         CtdlNumMsgsSelected = 0;
73                 }
74         }
75
76         // For multi select ... is the message being clicked already selected?
77         already_selected = 0;
78         if ( (evt.ctrlKey) && (CtdlNumMsgsSelected > 0) ) {
79                 for (i=0; i<CtdlNumMsgsSelected; ++i) {
80                         if (CtdlMsgsSelected[i] == msgnum) {
81                                 already_selected = 1;
82                         }
83                 }
84         }
85
86         // Now select (or de-select) the message
87         if ( (evt.ctrlKey) && (already_selected == 1) ) {
88                 $('m'+msgnum).style.backgroundColor = '#fff';
89                 $('m'+msgnum).style.color = '#000';
90         }
91         else {
92                 $('m'+msgnum).style.backgroundColor='#69aaff';
93                 $('m'+msgnum).style.color='#fff';
94                 CtdlNumMsgsSelected = CtdlNumMsgsSelected + 1;
95                 CtdlMsgsSelected[CtdlNumMsgsSelected-1] = msgnum;
96         }
97
98         // Update the preview pane
99         new Ajax.Updater('preview_pane', 'msg/'+msgnum, { method: 'get' } );
100
101         // Mark the message as read
102         new Ajax.Request(
103                 'ajax_servcmd', {
104                         method: 'post',
105                         parameters: 'g_cmd=SEEN '+msgnum+'|1',
106                         onComplete: CtdlRemoveTheUnseenBold(msgnum)
107                 }
108         );
109
110         return false;           // try to defeat the default click behavior
111 }
112
113 // Delete selected messages.
114 function CtdlDeleteSelectedMessages(evt) {
115         
116         if (CtdlNumMsgsSelected < 1) {
117                 // Nothing to delete, so exit silently.
118                 return false;
119         }
120         for (i=0; i<CtdlNumMsgsSelected; ++i) {
121                 new Ajax.Request(
122                         'ajax_servcmd', {
123                                 method: 'post',
124                                 parameters: 'g_cmd=MOVE ' + CtdlMsgsSelected[i] + '|_TRASH_|0',
125                                 onComplete: CtdlClearDeletedMsg(CtdlMsgsSelected[i])
126                         }
127                 );
128         }
129         CtdlNumMsgsSelected = 0;
130
131         // Clear the preview pane too.
132         $('preview_pane').innerHTML = '';
133 }
134
135 // This gets called when the user touches the keyboard after selecting messages...
136 function CtdlMsgListKeyPress(evt) {
137         if(document.all) {                              // aIEeee
138                 var whichKey = window.event.keyCode;
139         }
140         else {                                          // non-sux0r browsers
141                 var whichKey = evt.which;
142         }
143         if (whichKey == 46) {                           // DELETE key
144                 CtdlDeleteSelectedMessages(evt);
145         }
146         return true;
147 }
148
149 // Take the boldface away from a message to indicate that it has been seen.
150 function CtdlRemoveTheUnseenBold(msgnum) {
151         $('m'+msgnum).style.fontWeight='normal';
152 }
153
154 // A message has been deleted, so yank it from the list.
155 // (IE barfs on m9999.innerHTML='' so we use a script.aculo.us effect instead.)
156 function CtdlClearDeletedMsg(msgnum) {
157         new Effect.Squish('m'+msgnum);
158 }
159