Room links in iconbar roomlist now properly escaped with encodeURIcomponent()
[citadel.git] / webcit / static / wclib.js
index 7a84ec05a3a7b7ba8afad0c35bafba227fbd5d76..e8c0c4ee72c2aa1cd2df3f9e8132c02c284d9552 100644 (file)
@@ -1,10 +1,21 @@
 /*
- * $Id$
- * Copyright 2005 - 2009 The Citadel Team
- * Licensed under the GPL V3
- *
  * JavaScript function library for WebCit.
  *
+ * Copyright (c) 2005-2011 by the citadel.org team
+ *
+ * This program is open source software.  You can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
 
@@ -13,17 +24,12 @@ var room_is_trash = 0;
 
 var currentlyExpandedFloor = null;
 var roomlist = null;
-
-var _switchToRoomList = "switch to room list";
-var _switchToMenu = "switch to menu";
-
 var currentDropTarget = null;
 
 var supportsAddEventListener = (!!document.addEventListener);
 var today = new Date();
 
 var wc_log = "";
-var is_ie6 = false;
 if (document.all) {browserType = "ie"}
 if (window.navigator.userAgent.toLowerCase().match("gecko")) {
        browserType= "gecko";
@@ -31,12 +37,8 @@ if (window.navigator.userAgent.toLowerCase().match("gecko")) {
 var ns6=document.getElementById&&!document.all;
 Event.observe(window, 'load', ToggleTaskDateOrNoDateActivate);
 Event.observe(window, 'load', taskViewActivate);
-Event.observe(window, 'load', fixbanner);
-Event.observe(window, 'load', resizeViewport);
-Event.observe(window, 'resize', resizeViewport);
 //document.observe("dom:loaded", setupPrefEngine);
 document.observe("dom:loaded", setupIconBar);
-document.observe('dom:loaded', function() { if (!!document.getElementById("ib_chat_launch")) { $('ib_chat_launch').observe('click', launchChat); } });
 function CtdlRandomString()  {
        return((Math.random()+'').substr(3));
 }
@@ -54,6 +56,19 @@ function strcmp ( str1, str2 ) {
     return ( ( str1 == str2 ) ? 0 : ( ( str1 > str2 ) ? 1 : -1 ) );
 }
 
+
+
+function ToggleVisibility ($Which)
+{
+       if (document.getElementById)
+       {
+               if (document.getElementById($Which).style.display  == "none")
+                       document.getElementById($Which).style.display  = "inline";
+               else
+                       document.getElementById($Which).style.display  = "none";
+       }
+}
+
 function emptyElement(element) {
   childNodes = element.childNodes;
   for(var i=0; i<childNodes.length; i++) {
@@ -151,64 +166,32 @@ function activate_entmsg_autocompleters() {
        new Ajax.Autocompleter('recp_id', 'recp_name_choices', 'recp_autocomplete', {} );
 }
 
-function setupIconBar() {
-  if (!document.getElementById("switch")) {
-      return;
-    }
-  _switchToRoomList = getTextContent(document.getElementById("rmlist_template"));
-  _switchToMenu = getTextContent(document.getElementById("mnlist_template"));
-  var switchSpan = document.getElementById("switch").firstChild;
-  if (switchSpan != null) {
-    setTextContent(switchSpan, _switchToRoomList);
-    $(switchSpan).observe('click', changeIconBarEvent);
-    var currentView = ctdlLocalPrefs.readPref("iconbar_view");
-    if (currentView != null) {
-      switchSpan.ctdlSwitchIconBarTo = currentView;
-      changeIconBar(switchSpan);
-    } else {
-      switchSpan.ctdlSwitchIconBarTo = "rooms";
-    }
-  }
-  var online_users = document.getElementById("online_users");
-  if (online_users.offsetParent != null && online_users.offsetTop > 0) {
-    new Ajax.PeriodicalUpdater('online_users', 'do_template?template=wholist_section', {method: 'get', frequency: 30});
-  }
-}
-function changeIconBarEvent(event) {
-  changeIconBar(event.target);
-}
-function changeIconBar(target) {
-  var switchTo = target.ctdlSwitchIconBarTo;
-  WCLog("Changing to: " + switchTo);
-  ctdlLocalPrefs.setPref("iconbar_view", target.ctdlSwitchIconBarTo);  
-  if (switchTo == "rooms") {
-    switch_to_room_list();
-    setTextContent(target, _switchToMenu);
-    target.ctdlSwitchIconBarTo = "menu";
-  } else {
-    switch_to_menu_buttons();
-    setTextContent(target, _switchToRoomList);
-    target.ctdlSwitchIconBarTo = "rooms";
-  }
-}
-function switch_to_room_list() {
-  var roomlist = document.getElementById("roomlist");
-  var summary = document.getElementById("iconbar_menu");
-  if (!rooms || !floors || !roomlist) {
-    FillRooms(IconBarRoomList);
-  }
-  roomlist.className = roomlist.className.replace("hidden","");
-  summary.className += " hidden";
+function activate_iconbar_wholist_populat0r() 
+{
+       new Ajax.PeriodicalUpdater('online_users', 'do_template?template=who_iconbar', {method: 'get', frequency: 30});
 }
 
-function switch_to_menu_buttons() {
-  if (roomlist != null) {
-    roomlist.className += "hidden";
-  }
-  var iconbar = document.getElementById("iconbar_menu");
-  iconbar.className = iconbar.className.replace("hidden","");
-  var roomlist = document.getElementById("roomlist");
-  roomlist.className += " hidden";
+function setupIconBar() {
+
+       /* WARNING: VILE, SLEAZY HACK.  We determine the state of the box based on the image loaded. */
+       if ( $('expand_roomlist').src.substring($('expand_roomlist').src.length - 12) == "collapse.gif" ) {
+               $('roomlist').style.display = 'block';
+               $('roomlist').innerHTML = '';
+               FillRooms(IconBarRoomList);
+       }
+       else {
+               $('roomlist').style.display = 'none';
+       }
+
+       /* WARNING: VILE, SLEAZY HACK.  We determine the state of the box based on the image loaded. */
+       if ( $('expand_wholist').src.substring($('expand_wholist').src.length - 12) == "collapse.gif" ) {
+               $('online_users').style.display = 'block';
+               activate_iconbar_wholist_populat0r();
+       }
+       else {
+               $('online_users').style.display = 'none';
+       }
+
 }
 
 function GenericTreeRoomList(roomlist) {
@@ -282,11 +265,12 @@ function addRoomToList(floorUL,room, roomToEmphasize) {
   var flag = room[RN_ROOM_FLAG];
   var curView = room[RN_CUR_VIEW];
   var view = room[RN_DEF_VIEW];
+  var raflags = room[RN_RAFLAGS];
   var isMailBox = ((flag & QR_MAILBOX) == QR_MAILBOX);
-  var hasNewMsgs = ((curView & UA_HASNEWMSGS) == UA_HASNEWMSGS);
+  var hasNewMsgs = ((raflags & UA_HASNEWMSGS) == UA_HASNEWMSGS);
   var roomLI = document.createElement("li");
   var roomA = document.createElement("a");
-  roomA.setAttribute("href","dotgoto?room="+roomName);
+  roomA.setAttribute("href","dotgoto?room="+encodeURIComponent(roomName));
   roomA.appendChild(document.createTextNode(roomName));
   roomLI.appendChild(roomA);
   floorUL.appendChild(roomLI);
@@ -318,17 +302,26 @@ function addRoomToList(floorUL,room, roomToEmphasize) {
 
 function roomListDropHandler(target, dropped) {
   if (dropped.getAttribute("citadel:msgid")) {
-    var room = getTextContent(target);
-    var msgIds = "";
-    for(msgId in currentlyMarkedRows) { //defined in summaryview.js
-      msgIds += ","+msgId;
-    }
-    var mvCommand = "g_cmd=MOVE " + msgIds + "|"+room+"|0";
-    new Ajax.Request('ajax_servcmd', {
-      method: 'post',
+      var room = getTextContent(target);
+      var msgIds = "";
+      for(msgId in currentlyMarkedRows) { //defined in summaryview.js
+         msgIds += ","+msgId;
+         if (msgIds.length > 800) {
+             var mvCommand = encodeURI("g_cmd=MOVE " + msgIds + "|"+room+"|0");
+             new Ajax.Request("ajax_servcmd", {
+                 parameters: mvCommand,
+                 method: 'post',
+             });
+             msgIds = "";
+         }
+
+      }
+      var mvCommand = encodeURI("g_cmd=MOVE " + msgIds + "|"+room+"|0");
+      new Ajax.Request('ajax_servcmd', {
+         method: 'post',
          parameters: mvCommand,
          onComplete: deleteAllMarkedRows()});
-    } 
+  }
 }
 function expandFloorEvent(event) {
   expandFloor(event.target);
@@ -600,7 +593,7 @@ function ctdl_ts_getInnerText(el) {
 // Place a gradient loadscreen on an element, e.g to use before Ajax.updater
 function CtdlLoadScreen(elementid) {
 var elem = document.getElementById(elementid);
-elem.innerHTML = "<div align=center><br><table border=0 cellpadding=10 bgcolor=\"#ffffff\"><tr><td><img src=\"static/throbber.gif\" /><font color=\"#AAAAAA\">&nbsp;&nbsp;Loading....</font></td></tr></table><br /></div>";
+elem.innerHTML = "<div align=center><br><table border=0 cellpadding=10 bgcolor=\"#ffffff\"><tr><td><img src=\"static/throbber.gif\" /><font color=\"#AAAAAA\">&nbsp;&nbsp;Loading....</font></td></tr></table><br></div>";
 }
 
 
@@ -622,7 +615,7 @@ function PopOpenAddressBook(target_input) {
 }
 
 function PopulateAddressBookInnerDiv(which_addr_book, target_input) {
-       $('address_book_inner_div').innerHTML = "<div align=center><br><table border=0 cellpadding=10 bgcolor=\"#ffffff\"><tr><td><img src=\"static/throbber.gif\" /><font color=\"#AAAAAA\">&nbsp;&nbsp;Loading....</font></td></tr></table><br /></div>";
+       $('address_book_inner_div').innerHTML = "<div align=center><br><table border=0 cellpadding=10 bgcolor=\"#ffffff\"><tr><td><img src=\"static/throbber.gif\" /><font color=\"#AAAAAA\">&nbsp;&nbsp;Loading....</font></td></tr></table><br></div>";
        p = 'which_addr_book=' + which_addr_book
          + '&target_input=' + target_input
          + '&r=' + CtdlRandomString();
@@ -852,92 +845,6 @@ function WCLog(msg) {
   }
 }
 
-function fixMissingCSSTable(elems) {
- if (elems[0] == null || elems[1] == null) {
-    return;
-  }
-  if (elems[0].getStyle("display") != "table-cell") {
-    var parentNode = elems[0].parentNode;
-    var table = document.createElement("table");
-    table.style.width = "100%";
-    var tbody = document.createElement("tbody");
-    table.appendChild(tbody);
-    var tr = document.createElement("tr");
-    tbody.appendChild(tr);
-    parentNode.appendChild(table);
-    for(var i=0; i<elems.length; i++) {
-      parentNode.removeChild(elems[i]);
-      var td = document.createElement("td");
-      td.appendChild(elems[i]);
-      tr.appendChild(td);
-    }
-  }
-}
-function fixbanner() {
-  // Use prototype api methods here
-  var elems = [$('room_banner'),$('actiondiv')];
-  fixMissingCSSTable(elems);
-  if (!is_ie6) {
-    Event.observe(window, 'resize', makeContentScrollable);
-    makeContentScrollable();
-  }
-}
-function makeContentScrollable() {
-if (document.getElementById("banner")
-      && document.getElementById("content") 
-      && !document.getElementById("summary_view")) {
-  WCLog("makeContentScrollable");
-    document.body.style.overflowY="hidden";
-    var global = $("global");
-    global.className += "scrollable";
-    var content = document.getElementById("content");
-    var banner = document.getElementById("banner");
-    var bannerHeight = banner.offsetHeight;
-    banner.style.width="100%";
-    var bannerPercent = (bannerHeight/document.viewport.getHeight())*100;
-    //banner.style.height=bannerPercent+"%";
-    content.style.overflowY="scroll";
-    //content.style.top=bannerPercent+"%";
-    content.style.height=(100-bannerPercent)+"%";
-    content.style.right="0px";
-  }
-}
-function fixOffsetBanner() {
-  var banner = document.getElementById("banner");
-  if (banner.offsetLeft > 0) {
-    var viewportWidth = document.viewport.getWidth();
-    var iconbarWidth = document.getElementById("iconbar").offsetWidth;
-    var contentDiv = document.getElementById("content");
-    var newContentWidth = viewportWidth-iconbarWidth;
-    contentDiv.style.width = newContentWidth+"px";
-  }
-}
-/** Attempt to stop overflowing in x-axis in IE6 */
-function resizeViewport() {
-  var documentWidth = 0;
-  var viewportWidth = document.viewport.getWidth();
-  var iconbar = $('iconbar');
-  var global = $('global');
-  if (iconbar == null || global == null || document.documentElement == null) {
-    return;
-  }
-  if (typeof window.offsetWidth != 'undefined') {
-    documentWidth = window.offsetWidth;
-  } else {
-    documentWidth = document.documentElement.offsetWidth;
-  }
-  if (documentWidth > viewportWidth) {
-    WCLog("resizeViewport");
-    document.documentElement.style.width = viewportWidth+"px";
-    document.documentElement.style.overflowX = "hidden";
-    //viewportWidth = 0.98 * viewportWidth;
-    var newIconBarSize = 0.16 * viewportWidth;
-    var newContentSize = viewportWidth - newIconBarSize;
-    iconbar.style.width = newIconBarSize+"px";
-    global.style.width = newContentSize+"px";
-  }
-}
-
 function RefreshSMTPqueueDisplay() {
        new Ajax.Updater('smtpqueue_inner_div',
        'display_smtpqueue_inner_div', { method: 'get',
@@ -945,11 +852,85 @@ function RefreshSMTPqueueDisplay() {
 }
 
 function DeleteSMTPqueueMsg(msgnum1, msgnum2) {
+       var p = encodeURI('g_cmd=DELE ' + msgnum1 + ',' + msgnum2);
        new Ajax.Request(
                'ajax_servcmd', {
                        method: 'post',
-                       parameters: 'g_cmd=DELE ' + msgnum1 + ',' + msgnum2,
+                       parameters: p,
                        onComplete: RefreshSMTPqueueDisplay()
                }
        );
 }
+
+
+function ConfirmLogoff() {
+       new Ajax.Updater(
+               'md-content',
+               'do_template?template=confirmlogoff',
+               {
+                       method: 'get',
+                       evalScripts: true,
+                       onSuccess: function(cl_success) {
+                               toggleModal(1);
+                       }
+               }
+       );
+}
+
+
+function switch_to_lang(new_lang) {
+       p = 'push?url=' + encodeURI(window.location);
+       new Ajax.Request(p, { method: 'get' } );
+       window.location = 'switch_language?lang=' + new_lang ;
+}
+
+
+function toggle_roomlist() 
+{
+       /* WARNING: VILE, SLEAZY HACK.  We determine the state of the box based on the image loaded. */
+       if ( $('expand_roomlist').src.substring($('expand_roomlist').src.length - 12) == "collapse.gif" ) {
+               $('roomlist').style.display = 'none';
+               $('expand_roomlist').src = 'static/webcit_icons/expand.gif';
+               wstate=0;
+       }
+
+       else {
+               $('roomlist').style.display = 'block';
+               $('expand_roomlist').src = 'static/webcit_icons/collapse.gif';
+               $('roomlist').innerHTML = '';
+               FillRooms(IconBarRoomList);
+               wstate=1;
+       }
+
+       // tell the server what I did
+       p = 'toggle_roomlist_expanded_state?wstate=' + wstate + '?rand=' + Math.random() ;
+       new Ajax.Request(p, { method: 'get' } );
+
+       return false;   /* this prevents the click from registering as a roomlist button press */
+}
+
+
+function toggle_wholist() 
+{
+       /* WARNING: VILE, SLEAZY HACK.  We determine the state of the box based on the image loaded. */
+       if ( $('expand_wholist').src.substring($('expand_wholist').src.length - 12) == "collapse.gif" ) {
+               $('online_users').style.display = 'none';
+               $('expand_wholist').src = 'static/webcit_icons/expand.gif';
+               wstate=0;
+       }
+
+       else {
+               $('online_users').style.display = 'block';
+               $('expand_wholist').src = 'static/webcit_icons/collapse.gif';
+               activate_iconbar_wholist_populat0r();
+               wstate=1;
+       }
+
+       // tell the server what I did
+       p = 'toggle_wholist_expanded_state?wstate=' + wstate + '?rand=' + Math.random() ;
+       new Ajax.Request(p, { method: 'get' } );
+
+       return false;   /* this prevents the click from registering as a wholist button press */
+}
+
+