* Applied matt's patch to have it show a loading graphic for ajax operations
[citadel.git] / webcit / static / wclib.js
index 23ef4f8ddd712a3a5f4f261fe4e36009d1517976..fbe50fde5a673aa3f427d4940b7ebee76a730c04 100644 (file)
@@ -7,6 +7,7 @@
 
 
 var browserType;
+var room_is_trash = 0;
 
 if (document.layers) {browserType = "nn4"}
 if (document.all) {browserType = "ie"}
@@ -17,22 +18,32 @@ if (window.navigator.userAgent.toLowerCase().match("gecko")) {
 var ns6=document.getElementById&&!document.all;
 
 
-//
-// This code handles the popups for instant messages.
-//
 
+// We love string tokenizers.
+function extract_token(source_string, token_num, delimiter) {
+       var i = 0;
+       var extracted_string = source_string;
 
-function hide_page_popup() {
-       if (browserType == "gecko" )
-               document.poppedLayer = eval('document.getElementById(\'page_popup\')');
-       else if (browserType == "ie")
-               document.poppedLayer = eval('document.all[\'page_popup\']');
-       else
-               document.poppedLayer = eval('document.layers[\'`page_popup\']');
+       if (token_num > 0) {
+               for (i=0; i<token_num; ++i) {
+                       var j = extracted_string.indexOf(delimiter);
+                       if (j >= 0) {
+                               extracted_string = extracted_string.substr(j+1);
+                       }
+               }
+       }
 
-       document.poppedLayer.style.visibility = "hidden";
+       j = extracted_string.indexOf(delimiter);
+       if (j >= 0) {
+               extracted_string = extracted_string.substr(0, j);
+       }
+
+       return extracted_string;
 }
 
+
+
+// This code handles the popups for important-messages.
 function hide_imsg_popup() {
        if (browserType == "gecko" )
                document.poppedLayer = eval('document.getElementById(\'important_message\')');
@@ -44,6 +55,7 @@ function hide_imsg_popup() {
        document.poppedLayer.style.visibility = "hidden";
 }
 
+
 // This function activates the ajax-powered recipient autocompleters on the message entry screen.
 function activate_entmsg_autocompleters() {
        new Ajax.Autocompleter('cc_id', 'cc_name_choices', 'cc_autocomplete', {} );
@@ -52,10 +64,64 @@ function activate_entmsg_autocompleters() {
 }
 
 
+
+// Toggle the icon bar between menu/roomlist...
+var which_div_expanded = null;
+var num_drop_targets = 0;
+var drop_targets_elements = new Array();
+var drop_targets_roomnames = new Array();
+
+function switch_to_room_list() {
+       $('iconbar').innerHTML = $('iconbar').innerHTML.substr(0, $('iconbar').innerHTML.indexOf('switch'));
+       CtdlLoadScreen('iconbar');
+       new Ajax.Updater('iconbar', 'iconbar_ajax_rooms', { method: 'get' } );
+}
+
+function expand_floor(floor_div) {
+       if (which_div_expanded != null) {
+               if ($(which_div_expanded) != null) {
+                       $(which_div_expanded).style.display = 'none' ;
+               }
+       }
+
+       // clicking on the already-expanded floor causes the whole list to collapse
+       if (which_div_expanded == floor_div) {
+               which_div_expanded = null;
+
+               // notify the server that no floors are expanded
+               new Ajax.Request(
+                       'set_floordiv_expanded/-1', {
+                               method: 'post'
+                       }
+               );
+               return true;
+       }
+
+       // expand the requested floor
+       $(floor_div).style.display = 'block';
+       which_div_expanded = floor_div;
+
+       // notify the server of which floor is expanded
+       new Ajax.Request(
+               'set_floordiv_expanded/'+floor_div, {
+                       method: 'post'
+               }
+       );
+}
+
+function switch_to_menu_buttons() {
+       which_div_expanded = null;
+       num_drop_targets = 0;
+       CtdlLoadScreen('iconbar');
+       new Ajax.Updater('iconbar', 'iconbar_ajax_menu', { method: 'get' } );
+}
+
+
 // Static variables for mailbox view...
 //
 var CtdlNumMsgsSelected = 0;
-var CtdlMsgsSelected = new Array(65536);       // arbitrary
+var CtdlMsgsSelected = new Array();
+var CtdlLastMsgnumSelected = 0;
 
 // This gets called when you single click on a message in the mailbox view.
 // We know that the element id of the table row will be the letter 'm' plus the message number.
@@ -65,9 +131,10 @@ function CtdlSingleClickMsg(evt, msgnum) {
        // Clear the preview pane until we load the new message
        $('preview_pane').innerHTML = '';
 
-       // De-select any messages that were already selected, *unless* the Ctrl key
-       // is being pressed, in which case the user wants multi select.
-       if (!evt.ctrlKey) {
+       // De-select any messages that were already selected, *unless* the Ctrl or
+       // Shift key is being pressed, in which case the user wants multi select
+       // or group select.
+       if ( (!evt.ctrlKey) && (!evt.shiftKey) ) {
                if (CtdlNumMsgsSelected > 0) {
                        for (i=0; i<CtdlNumMsgsSelected; ++i) {
                                $('m'+CtdlMsgsSelected[i]).style.backgroundColor = '#fff';
@@ -83,33 +150,92 @@ function CtdlSingleClickMsg(evt, msgnum) {
                for (i=0; i<CtdlNumMsgsSelected; ++i) {
                        if (CtdlMsgsSelected[i] == msgnum) {
                                already_selected = 1;
+                               already_selected_pos = i;
                        }
                }
        }
 
        // Now select (or de-select) the message
        if ( (evt.ctrlKey) && (already_selected == 1) ) {
+
+               // Deselect: first un-highlight it...
                $('m'+msgnum).style.backgroundColor = '#fff';
                $('m'+msgnum).style.color = '#000';
+
+               // Then remove it from the selected messages list.
+               for (i=already_selected_pos; i<(CtdlNumMsgsSelected-1); ++i) {
+                       CtdlMsgsSelected[i] = CtdlMsgsSelected[i+1];
+               }
+               CtdlNumMsgsSelected = CtdlNumMsgsSelected - 1;
+               
+       }
+
+       else if (evt.shiftKey) {
+               
+               // Group select: first clear everything out...
+               if (CtdlNumMsgsSelected > 0) {
+                       for (i=0; i<CtdlNumMsgsSelected; ++i) {
+                               $('m'+CtdlMsgsSelected[i]).style.backgroundColor = '#fff';
+                               $('m'+CtdlMsgsSelected[i]).style.color = '#000';
+                       }
+               }
+               CtdlNumMsgsSelected = 0;
+
+               // Then highlight and select the group.
+               // Traverse the table looking for a row whose ID contains the desired msgnum
+
+               var in_the_group = 0;
+               var is_edge = 0;
+               var table = $('summary_headers');
+               if (table) {
+                       for (var r = 0; r < table.rows.length; r++) {
+                               var thename = table.rows[r].id;
+                               if ( (thename.substr(1) == msgnum) || (thename.substr(1) == CtdlLastMsgnumSelected) ) {
+                                       in_the_group = 1 - in_the_group;
+                                       is_edge = 1;
+                               }
+                               else {
+                                       is_edge = 0;
+                               }
+                               if ( (in_the_group == 1) || (is_edge == 1) ) {
+                                       // Highlight it...
+                                       table.rows[r].style.backgroundColor='#69aaff';
+                                       table.rows[r].style.color='#fff';
+
+                                       // And add it to the selected messages list.
+                                       CtdlNumMsgsSelected = CtdlNumMsgsSelected + 1;
+                                       CtdlMsgsSelected[CtdlNumMsgsSelected-1] = thename.substr(1);
+                               }
+                       }
+               }
        }
+
        else {
+               // Select: first highlight it...
                $('m'+msgnum).style.backgroundColor='#69aaff';
                $('m'+msgnum).style.color='#fff';
+
+               // Then add it to the selected messages list.
                CtdlNumMsgsSelected = CtdlNumMsgsSelected + 1;
                CtdlMsgsSelected[CtdlNumMsgsSelected-1] = msgnum;
-       }
-
-       // Update the preview pane
-       new Ajax.Updater('preview_pane', 'msg/'+msgnum, { method: 'get' } );
 
-       // Mark the message as read
-       new Ajax.Request(
-               'ajax_servcmd', {
-                       method: 'post',
-                       parameters: 'g_cmd=SEEN '+msgnum+'|1',
-                       onComplete: CtdlRemoveTheUnseenBold(msgnum)
-               }
-       );
+               // Gradient
+               CtdlLoadScreen('preview_pane');
+               // Update the preview pane
+               new Ajax.Updater('preview_pane', 'msg/'+msgnum, { method: 'get' } );
+       
+               // Mark the message as read
+               new Ajax.Request(
+                       'ajax_servcmd', {
+                               method: 'post',
+                               parameters: 'g_cmd=SEEN '+msgnum+'|1',
+                               onComplete: CtdlRemoveTheUnseenBold(msgnum)
+                       }
+               );
+       }
+       
+       // Save the selected position in case the user does a group select next time.
+       CtdlLastMsgnumSelected = msgnum;
 
        return false;           // try to defeat the default click behavior
 }
@@ -117,6 +243,40 @@ function CtdlSingleClickMsg(evt, msgnum) {
 // Delete selected messages.
 function CtdlDeleteSelectedMessages(evt) {
        
+       if (CtdlNumMsgsSelected < 1) {
+               // Nothing to delete, so exit silently.
+               return false;
+       }
+       for (i=0; i<CtdlNumMsgsSelected; ++i) {
+               if (parseInt(room_is_trash) > 0) {
+                       new Ajax.Request(
+                               'ajax_servcmd', {
+                                       method: 'post',
+                                       parameters: 'g_cmd=DELE ' + CtdlMsgsSelected[i],
+                                       onComplete: CtdlClearDeletedMsg(CtdlMsgsSelected[i])
+                               }
+                       );
+               }
+               else {
+                       new Ajax.Request(
+                               'ajax_servcmd', {
+                                       method: 'post',
+                                       parameters: 'g_cmd=MOVE ' + CtdlMsgsSelected[i] + '|_TRASH_|0',
+                                       onComplete: CtdlClearDeletedMsg(CtdlMsgsSelected[i])
+                               }
+                       );
+               }
+       }
+       CtdlNumMsgsSelected = 0;
+
+       // Clear the preview pane too.
+       $('preview_pane').innerHTML = '';
+}
+
+
+// Move selected messages.
+function CtdlMoveSelectedMessages(evt, target_roomname) {
+       
        if (CtdlNumMsgsSelected < 1) {
                // Nothing to delete, so exit silently.
                return false;
@@ -124,9 +284,9 @@ function CtdlDeleteSelectedMessages(evt) {
        for (i=0; i<CtdlNumMsgsSelected; ++i) {
                new Ajax.Request(
                        'ajax_servcmd', {
-                               method: 'post',
-                               parameters: 'g_cmd=MOVE ' + CtdlMsgsSelected[i] + '|_TRASH_|0',
-                               onComplete: CtdlClearDeletedMsg(CtdlMsgsSelected[i])
+                               method:'post',
+                               parameters:'g_cmd=MOVE ' + CtdlMsgsSelected[i] + '|' + target_roomname + '|0',
+                               onComplete:CtdlClearDeletedMsg(CtdlMsgsSelected[i])
                        }
                );
        }
@@ -136,6 +296,8 @@ function CtdlDeleteSelectedMessages(evt) {
        $('preview_pane').innerHTML = '';
 }
 
+
+
 // This gets called when the user touches the keyboard after selecting messages...
 function CtdlMsgListKeyPress(evt) {
        if(document.all) {                              // aIEeee
@@ -156,12 +318,31 @@ function CtdlRemoveTheUnseenBold(msgnum) {
 }
 
 // A message has been deleted, so yank it from the list.
-// (IE barfs on m9999.innerHTML='' so we use a script.aculo.us effect instead.)
 function CtdlClearDeletedMsg(msgnum) {
-       new Effect.Squish('m'+msgnum);
-}
 
 
+       // Traverse the table looking for a row whose ID contains the desired msgnum
+       var table = $('summary_headers');
+       if (table) {
+               for (var r = 0; r < table.rows.length; r++) {
+                       var thename = table.rows[r].id;
+                       if (thename.substr(1) == msgnum) {
+                               try {
+                                       table.deleteRow(r);
+                               }
+                               catch(e) {
+                                       alert('error: browser failed to clear row ' + r);
+                               }
+                       }
+               }
+       }
+       else {                                          // if we can't delete it,
+               new Effect.Squish('m'+msgnum);          // just hide it.
+       }
+
+
+}
+
 // These functions called when the user down-clicks on the message list resizer bar
 
 var saved_x = 0;
@@ -231,7 +412,7 @@ function CtdlResizeMsgListMouseDown(evt) {
        if (document.layers) {
                document.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
        }
-       return true;
+       return false;           // disable the default action
 }
 
 
@@ -309,6 +490,37 @@ function CtdlMoveMsgMouseUp(evt) {
                document.body.removeChild(mm_div);      
                mm_div = null;
        }
+
+       if (num_drop_targets < 1) {     // nowhere to drop
+               return true;
+       }
+
+       // Did we release the mouse button while hovering over a drop target?
+       // NOTE: this only works cross-browser because the iconbar div is always
+       //      positioned at 0,0.  Browsers differ in whether the 'offset'
+       //      functions return pos relative to the document or parent.
+
+       for (i=0; i<num_drop_targets; ++i) {
+
+               x = (ns6 ? evt.clientX : event.clientX);
+               y = (ns6 ? evt.clientY : event.clientY);
+
+               l = parseInt(drop_targets_elements[i].offsetLeft);
+               t = parseInt(drop_targets_elements[i].offsetTop);
+               r = parseInt(drop_targets_elements[i].offsetLeft)
+                 + parseInt(drop_targets_elements[i].offsetWidth);
+               b = parseInt(drop_targets_elements[i].offsetTop)
+                 + parseInt(drop_targets_elements[i].offsetHeight);
+
+               /* alert('Offsets are: ' + l + ' ' + t + ' ' + r + ' ' + b + '.'); */
+       
+               if ( (x >= l) && (x <= r) && (y >= t) && (y <= b) ) {
+                       // Yes, we dropped it on a hotspot.
+                       CtdlMoveSelectedMessages(evt, drop_targets_roomnames[i]);
+                       return true;
+               }
+       }
+
        return true;
 }
 
@@ -333,3 +545,109 @@ function ctdl_ts_getInnerText(el) {
        }
        return str;
 }
+
+
+
+// This function handles the creation of new notes in the "Notes" view.
+//
+function add_new_note() {
+
+       new_eid = Math.random() + '';
+       new_eid = new_eid.substr(3);
+
+       $('new_notes_here').innerHTML = $('new_notes_here').innerHTML
+               + '<IMG ALIGN=MIDDLE src=\"static/storenotes_48x.gif\">'
+               + '<span id=\"note' + new_eid + '\">' + Date() + '</span><br />'
+       ;
+
+       new Ajax.InPlaceEditor('note' + new_eid,
+               'updatenote?eid=' + new_eid , {rows:5,cols:72});
+}
+
+function CtdlShowRaw(msgnum) {
+var customnav = document.createElement("span");
+var mode_citadel = document.createElement("a");
+mode_citadel.appendChild(document.createTextNode("Citadel Source"));
+var mode_rfc822 = document.createElement("a");
+mode_rfc822.appendChild(document.createTextNode(" RFC822 Source"));
+mode_citadel.setAttribute("href","#");
+mode_rfc822.setAttribute("href","#");
+mode_rfc822.setAttribute("onclick","rawSwitch822('" + msgnum + "');");
+mode_citadel.setAttribute("onclick","rawSwitchCitadel('" + msgnum + "');");
+customnav.appendChild(mode_citadel);
+customnav.appendChild(mode_rfc822);
+customnav.setAttribute("class","floatcustomnav");
+floatwindow("headerscreen","pre",customnav);
+rawSwitch822(msgnum);
+}
+function rawSwitch822(msgnum) {
+CtdlLoadScreen("headerscreen");
+new Ajax.Updater("headerscreen", 
+'ajax_servcmd_esc',
+ { method: 'post',parameters: 'g_cmd=MSG2 ' +msgnum  } );
+
+}
+function rawSwitchCitadel(msgnum) {
+CtdlLoadScreen("headerscreen");
+new Ajax.Updater("headerscreen", 
+'ajax_servcmd_esc',
+ { method: 'post',parameters: 'g_cmd=MSG0 ' +msgnum  } );
+
+}
+function floatwindow(newdivid,contentelementtype,customnav) {
+var windiv = document.createElement("div");
+windiv.setAttribute("class","floatwindow");
+var winid = newdivid+"_window";
+windiv.setAttribute("id",winid);
+var nav = document.createElement("div");
+if (customnav != null) {
+nav.appendChild(customnav);
+}
+var minimizeA = document.createElement("a");
+var minimizeButton = document.createTextNode("Close");
+minimizeA.appendChild(minimizeButton);
+minimizeA.setAttribute("onclick","killFloatWindow(this);");
+minimizeA.setAttribute("href","#");
+nav.appendChild(minimizeA);
+nav.setAttribute("class","floatnav");
+windiv.appendChild(nav);
+var contentarea = document.createElement("pre");
+contentarea.setAttribute("class","floatcontent");
+contentarea.setAttribute("id",newdivid);
+windiv.appendChild(contentarea);
+document.body.appendChild(windiv);
+}
+function killFloatWindow(caller) {
+var span = caller.parentNode;
+var fwindow = span.parentNode;
+fwindow.parentNode.removeChild(fwindow);
+}
+// Place a gradient loadscreen on an element, e.g to use before Ajax.updater
+function CtdlLoadScreen(elementid) {
+var elem = document.getElementById(elementid);
+elem.innerHTML = "<b>Loading....</b> <img src=\"/static/gradientanim.gif\"/>";
+}
+// Show info for a user, basically replaces showuser()
+// matt@comalies is to blame for this poorly coded masterpiece. 
+function CtdlShowUserInfoPopup(Element) {
+try {
+// hopefully no one needs to use the class attribute... could be better done 
+// with xmlns though..
+var user = Element.getAttribute("class");
+var updname = "biospace_"+user;
+if (document.getElementById(updname) == null) {
+// insert a space for the bio
+var pNode = Element.parentNode;
+var newdiv = document.createElement("div");
+newdiv.id = updname;
+newdiv.innerHTML = "Getting user info....";
+pNode.appendChild(newdiv);
+CtdlLoadScreen(updname);
+new Ajax.Updater(updname, 'showuser_ajax?who='+user, { method: 'get' } );
+}
+}
+catch(err){
+return true;
+}
+return false;
+}