]> err.no Git - scalable-opengroupware.org/blobdiff - UI/WebServerResources/generic.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1294 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / WebServerResources / generic.js
index 5498edf5ad4313b113cb9c1cded84e88da9577ee..054005e0a257c051322baf7b47dfe37d0b1884c6 100644 (file)
@@ -31,7 +31,7 @@ var menus = new Array();
 var search = {};
 var sorting = {};
 
-var lastClickedRow = null;
+var lastClickedRow = -1;
 
 var weekStartIsMonday = true;
 
@@ -150,7 +150,8 @@ function URLForFolderID(folderID) {
   var url;
   if (folderInfos.length > 1) {
     url = UserFolderURL + "../" + folderInfos[0];
-    if (!folderInfos[1].startsWith('/'))
+    if (!(folderInfos[0].endsWith('/')
+         || folderInfos[1].startsWith('/')))
       url += '/';
     url += folderInfos[1];
   }
@@ -218,8 +219,13 @@ function openUserFolderSelector(callback, type) {
 }
 
 function openContactWindow(url, wId) {
-  if (!wId)
-    wId = "" + (new Date().getTime());
+  if (typeof wId == "undefined")
+    wId = "_blank";
+  else {
+    var r = new RegExp("[\.\/-]", "g");
+    wId = wId.replace(r, "_");
+  }
+
   var w = window.open(url, wId,
                      "width=450,height=600,resizable=0,location=0");
   w.focus();
@@ -230,12 +236,16 @@ function openContactWindow(url, wId) {
 function openMailComposeWindow(url, wId) {
   var parentWindow = this;
 
-  if (!wId)
-    wId = "" + (new Date().getTime());
-  else
-    if (document.body.hasClassName("popup"))
-      parentWindow = window.opener;
+  if (typeof wId == "undefined")
+    wId = "_blank";
+  else {
+    var r = new RegExp("[\.\/-]", "g");
+    wId = wId.replace(r, "_");
+  }
   
+  if (document.body.hasClassName("popup"))
+    parentWindow = window.opener;
+
   var w = parentWindow.open(url, wId,
                       "width=680,height=520,resizable=1,scrollbars=1,toolbar=0,"
                       + "location=0,directories=0,status=0,menubar=0"
@@ -248,9 +258,10 @@ function openMailComposeWindow(url, wId) {
 
 function openMailTo(senderMailTo) {
   var mailto = sanitizeMailTo(senderMailTo);
+
   if (mailto.length > 0)
     openMailComposeWindow(ApplicationBaseURL
-                         + "/../Mail/compose?mailto=" + mailto);
+                         + "../Mail/compose?mailto=" + mailto);
 
   return false; /* stop following the link */
 }
@@ -280,11 +291,11 @@ function appendDifferentiator(url) {
   return url_nocache;
 }
 
-function triggerAjaxRequest(url, callback, userdata) {
+function triggerAjaxRequest(url, callback, userdata, content, headers) {
   var http = createHTTPClient();
 
   activeAjaxRequests += 1;
-  document.animTimer = setTimeout("checkAjaxRequestsState();", 200);
+  document.animTimer = setTimeout("checkAjaxRequestsState();", 50);
   //url = appendDifferentiator(url);
 
   if (http) {
@@ -292,7 +303,7 @@ function triggerAjaxRequest(url, callback, userdata) {
     http.url = url;
     http.onreadystatechange
       = function() {
-      //log ("state changed (" + http.readyState + "): " + url);
+//       log ("state changed (" + http.readyState + "): " + url);
       try {
        if (http.readyState == 4
            && activeAjaxRequests > 0) {
@@ -313,7 +324,11 @@ function triggerAjaxRequest(url, callback, userdata) {
        log(backtrace());
       }
     };
-    http.send(null);
+    if (headers) {
+      for (var i in headers)
+       http.setRequestHeader(i, headers[i]);
+    }
+    http.send(content);
   }
   else {
     log("triggerAjaxRequest: error creating HTTP Client!");
@@ -494,8 +509,14 @@ function onRowClick(event) {
   }
 
   var initialSelection = $(node.parentNode).getSelectedNodes();
+  if (initialSelection.length > 0 
+      && initialSelection.indexOf(node) >= 0
+      && !Event.isLeftClick(event))
+    // Ignore non primary-click (ie right-click) inside current selection
+    return true;
+  
   if ((event.shiftKey == 1 || event.ctrlKey == 1)
-      && lastClickedRow
+      && (lastClickedRow >= 0)
       && (acceptMultiSelect(node.parentNode)
          || acceptMultiSelect(node.parentNode.parentNode))) {
     if (event.shiftKey)
@@ -516,18 +537,7 @@ function onRowClick(event) {
       var parentNode = node.parentNode;
       if (parentNode.tagName == 'TBODY')
        parentNode = parentNode.parentNode;
-      if (document.createEvent) {
-       var onSelectionChangeEvent;
-       if (isSafari())
-         onSelectionChangeEvent = document.createEvent("UIEvents");
-       else
-         onSelectionChangeEvent = document.createEvent("Events");
-       onSelectionChangeEvent.initEvent("mousedown", true, true);
-       parentNode.dispatchEvent(onSelectionChangeEvent);
-      }
-      else if (document.createEventObject) {
-       parentNode.fireEvent("onmousedown");
-      }
+      parentNode.fire("mousedown");
     }
   }
   lastClickedRow = rowIndex;
@@ -572,13 +582,14 @@ function popupMenu(event, menuId, target) {
 
   if (popup.prepareVisibility)
     popup.prepareVisibility();
+  
   popup.setStyle({ top: menuTop + "px",
                   left: menuLeft + "px",
                   visibility: "visible" });
-
+  
   document.currentPopupMenu = popup;
 
-  Event.observe(document.body, "click", onBodyClickMenuHandler);
+  $(document.body).observe("click", onBodyClickMenuHandler);
 
   preventDefault(event);
 }
@@ -602,7 +613,7 @@ function getParentMenu(node) {
 
 function onBodyClickMenuHandler(event) {
   hideMenu(document.currentPopupMenu);
-  Event.stopObserving(document.body, "click", onBodyClickMenuHandler);
+  document.body.stopObserving("click", onBodyClickMenuHandler);
 
   if (event)
     preventDefault(event);
@@ -617,15 +628,12 @@ function hideMenu(menuNode) {
   }
 
   menuNode.setStyle({ visibility: "hidden" });
-  //   menuNode.hide();
   if (menuNode.parentMenuItem) {
-    Event.stopObserving(menuNode.parentMenuItem, "mouseover",
-                       onMouseEnteredSubmenu);
-    Event.stopObserving(menuNode, "mouseover", onMouseEnteredSubmenu);
-    Event.stopObserving(menuNode.parentMenuItem, "mouseout", onMouseLeftSubmenu);
-    Event.stopObserving(menuNode, "mouseout", onMouseLeftSubmenu);
-    Event.stopObserving(menuNode.parentMenu, "mouseover",
-                       onMouseEnteredParentMenu);
+    menuNode.parentMenuItem.stopObserving("mouseover",onMouseEnteredSubmenu);
+    menuNode.stopObserving("mouseover", onMouseEnteredSubmenu);
+    menuNode.parentMenuItem.stopObserving("mouseout", onMouseLeftSubmenu);
+    menuNode.stopObserving("mouseout", onMouseLeftSubmenu);
+    menuNode.parentMenu.stopObserving("mouseover", onMouseEnteredParentMenu);
     $(menuNode.parentMenuItem).removeClassName("submenu-selected");
     menuNode.parentMenuItem.mouseInside = false;
     menuNode.parentMenuItem = null;
@@ -634,18 +642,7 @@ function hideMenu(menuNode) {
     menuNode.parentMenu = null;
   }
 
-  if (document.createEvent) { // Safari & Mozilla
-    var onhideEvent;
-    if (isSafari())
-      onhideEvent = document.createEvent("UIEvents");
-    else
-      onhideEvent = document.createEvent("Events");
-    onhideEvent.initEvent("mousedown", false, true);
-    menuNode.dispatchEvent(onhideEvent);
-  }
-  else if (document.createEventObject) { // IE
-    menuNode.fireEvent("onmousedown");
-  }
+  $(menuNode).fire("mousedown");
 }
 
 function onMenuEntryClick(event) {
@@ -675,8 +672,8 @@ function initLogConsole() {
   var logConsole = $("logConsole");
   if (logConsole) {
     logConsole.highlighted = false;
-    Event.observe(logConsole, "dblclick", onLogDblClick, false);
-    logConsole.innerHTML = "";
+    logConsole.observe("dblclick", onLogDblClick, false);
+    logConsole.update();
     Event.observe(window, "keydown", onBodyKeyDown);
   }
 }
@@ -768,25 +765,25 @@ function popupSubmenu(event) {
 
     var menuTop = (parentNode.offsetTop - 1
                   + this.offsetTop);
+
     if (window.height()
-       < (menuTop + submenuNode.offsetHeight)
-       && submenuNode.offsetHeight < window.height())
-      menuTop -= submenuNode.offsetHeight - this.offsetHeight - 4;
+       < (menuTop + submenuNode.offsetHeight))
+      if (submenuNode.offsetHeight < window.height())
+       menuTop = window.height() - submenuNode.offsetHeight;
+      else
+       menuTop = 0;
+
     var menuLeft = (parentNode.offsetLeft + parentNode.offsetWidth - 3);
     if (window.width()
        < (menuLeft + submenuNode.offsetWidth))
       menuLeft = parentNode.offsetLeft - submenuNode.offsetWidth + 3;
 
     this.mouseInside = true;
-    Event.observe(this, "mouseover",
-                 onMouseEnteredSubmenu.bindAsEventListener(this));
-    Event.observe(submenuNode, "mouseover",
-                 onMouseEnteredSubmenu.bindAsEventListener(submenuNode));
-    Event.observe(this, "mouseout", onMouseLeftSubmenu.bindAsEventListener(this));
-    Event.observe(submenuNode, "mouseout",
-                 onMouseLeftSubmenu.bindAsEventListener(submenuNode));
-    Event.observe(parentNode, "mouseover",
-                 onMouseEnteredParentMenu.bindAsEventListener(parentNode));
+    this.observe("mouseover", onMouseEnteredSubmenu);
+    submenuNode.observe("mouseover", onMouseEnteredSubmenu);
+    this.observe("mouseout", onMouseLeftSubmenu);
+    submenuNode.observe("mouseout", onMouseLeftSubmenu);
+    parentNode.observe("mouseover", onMouseEnteredParentMenu);
     $(this).addClassName("submenu-selected");
     submenuNode.setStyle({ top: menuTop + "px",
                           left: menuLeft + "px",
@@ -831,7 +828,7 @@ function popupSearchMenu(event) {
          visibility: "visible" });
   
     document.currentPopupMenu = popup;
-    Event.observe(document.body, "click", onBodyClickMenuHandler);
+    $(document.body).observe("click", onBodyClickMenuHandler);
   }
 }
 
@@ -839,7 +836,7 @@ function setSearchCriteria(event) {
   var searchValue = $("searchValue");
   var searchCriteria = $("searchCriteria");
 
-  searchValue.setAttribute("ghost-phrase", this.innerHTML);
+  searchValue.ghostPhrase = this.innerHTML;
   searchCriteria.value = this.getAttribute('id');
   
   if (this.parentNode.chosenNode)
@@ -849,35 +846,24 @@ function setSearchCriteria(event) {
 }
 
 function checkSearchValue(event) {
-  var form = event.target;
   var searchValue = $("searchValue");
-  var ghostPhrase = searchValue.getAttribute('ghost-phrase');
 
-  if (searchValue.value == ghostPhrase)
+  if (searchValue.value == searchValue.ghostPhrase)
     searchValue.value = "";
 }
 
-function onSearchChange() {
-  log ("onSearchChange()...");
-}
-
 function configureSearchField() {
   var searchValue = $("searchValue");
   var searchOptions = $("searchOptions");
 
   if (!searchValue) return;
 
-  Event.observe(searchValue, "mousedown",
-               onSearchMouseDown.bindAsEventListener(searchValue));
-  Event.observe(searchValue, "click",
-               popupSearchMenu.bindAsEventListener(searchValue));
-  Event.observe(searchValue, "blur",
-               onSearchBlur.bindAsEventListener(searchValue));
-  Event.observe(searchValue, "focus",
-               onSearchFocus.bindAsEventListener(searchValue));
-  Event.observe(searchValue, "keydown",
-               onSearchKeyDown.bindAsEventListener(searchValue));
-
+  searchValue.observe("click", popupSearchMenu);
+  searchValue.observe("blur", onSearchBlur);
+  searchValue.observe("focus", onSearchFocus);
+  searchValue.observe("keydown", onSearchKeyDown);
+  searchValue.observe("mousedown", onSearchMouseDown);
+  
   if (!searchOptions) return;
    
   // Set the checkmark to the first option
@@ -898,7 +884,7 @@ function onSearchMouseDown(event) {
 }
 
 function onSearchFocus() {
-  ghostPhrase = this.getAttribute("ghost-phrase");
+  ghostPhrase = this.ghostPhrase;
   if (this.value == ghostPhrase) {
     this.value = "";
     this.setAttribute("modified", "");
@@ -910,14 +896,16 @@ function onSearchFocus() {
 }
 
 function onSearchBlur(event) {
-  var ghostPhrase = this.getAttribute("ghost-phrase");
-
   if (!this.value) {
     this.setAttribute("modified", "");
     this.setStyle({ color: "#aaa" });
-    this.value = ghostPhrase;
-    refreshCurrentFolder();
-  } else if (this.value == ghostPhrase) {
+    this.value = this.ghostPhrase;
+    search["value"] = "";
+    if (searchValue.lastSearch != "") {
+      searchValue.lastSearch = "";
+      refreshCurrentFolder();
+    }
+  } else if (this.value == this.ghostPhrase) {
     this.setAttribute("modified", "");
     this.setStyle({ color: "#aaa" });
   } else {
@@ -930,37 +918,43 @@ function onSearchKeyDown(event) {
   if (this.timer)
     clearTimeout(this.timer);
 
-  this.timer = setTimeout("onSearchFormSubmit()", 1000);
+  if (event.keyCode == 13) {
+    onSearchFormSubmit();
+    preventDefault(event);
+  }
+  else
+    this.timer = setTimeout("onSearchFormSubmit()", 1000);
 }
 
 function onSearchFormSubmit(event) {
   var searchValue = $("searchValue");
   var searchCriteria = $("searchCriteria");
-  var ghostPhrase = searchValue.getAttribute('ghost-phrase');
    
-  if (searchValue.value == ghostPhrase) return;
-
-  search["criteria"] = searchCriteria.value;
-  search["value"] = searchValue.value;
-
-  refreshCurrentFolder();
+  if (searchValue.value != searchValue.ghostPhrase
+      && searchValue.value != searchValue.lastSearch) {
+    search["criteria"] = searchCriteria.value;
+    search["value"] = searchValue.value;
+    searchValue.lastSearch = searchValue.value;
+    refreshCurrentFolder();
+  }
 }
 
 function initCriteria() {
   var searchCriteria = $("searchCriteria");
   var searchValue = $("searchValue");
  
-  if (!searchValue) return;
-
-  var searchOptions = $("searchOptions").childNodesWithTag("li");
-  if (searchOptions.length > 0) {
-    var firstChild = searchOptions[0];
-    searchCriteria.value = $(firstChild).getAttribute('id');
-    searchValue.setAttribute('ghost-phrase', firstChild.innerHTML);
-    if (searchValue.value == '') {
-      searchValue.value = firstChild.innerHTML;
-      searchValue.setAttribute("modified", "");
-      searchValue.setStyle({ color: "#aaa" });
+  if (searchValue) {
+    var searchOptions = $("searchOptions").childNodesWithTag("li");
+    if (searchOptions.length > 0) {
+      var firstChild = searchOptions[0];
+      searchCriteria.value = $(firstChild).getAttribute('id');
+      searchValue.ghostPhrase = firstChild.innerHTML;
+      searchValue.lastSearch = "";
+      if (searchValue.value == '') {
+       searchValue.value = firstChild.innerHTML;
+       searchValue.setAttribute("modified", "");
+       searchValue.setStyle({ color: "#aaa" });
+      }
     }
   }
 }
@@ -977,7 +971,7 @@ function popupToolbarMenu(node, menuId) {
        visibility: "visible" });
 
   document.currentPopupMenu = popup;
-  Event.observe(document.body, "click", onBodyClickMenuHandler);
+  $(document.body).observe("click", onBodyClickMenuHandler);
 }
 
 /* contact selector */
@@ -1076,6 +1070,28 @@ function getSubscribedFolderOwner(serverFolder) {
   return owner;
 }
 
+function getListIndexForFolder(items, owner, folderName) {
+  var i;
+  var previousOwner = null;
+  
+  for (var i = 0; i < items.length; i++) {
+    var currentFolderName = items[i].lastChild.nodeValue.strip();
+    var currentOwner = items[i].readAttribute('owner');
+    if (currentOwner == owner) {
+      previousOwner = currentOwner;
+      if (currentFolderName > folderName)
+       break;
+    }
+    else if (previousOwner || 
+            (currentOwner != UserLogin && currentOwner > owner))
+      break;
+    else if (currentOwner == "nobody")
+      break;
+  }
+  
+  return i;
+}
+
 function listRowMouseDownHandler(event) {
   preventDefault(event);
   //Event.stop(event); 
@@ -1101,10 +1117,8 @@ function initTabs() {
       if (currentNode.tagName == 'LI') {
        if (!firstTab)
          firstTab = i;
-       Event.observe(currentNode, "mousedown",
-                     onTabMouseDown.bindAsEventListener(currentNode));
-       Event.observe(currentNode, "click",
-                     onTabClick.bindAsEventListener(currentNode));
+       $(currentNode).observe("mousedown", onTabMouseDown);
+       $(currentNode).observe("click", onTabClick);
        //$(currentNode.getAttribute("target")).hide();
       }
     }
@@ -1131,12 +1145,10 @@ function initMenus() {
 }
 
 function initMenu(menuDIV, callbacks) {
-  var lis = $(menuDIV.childNodesWithTag("ul")[0]).childNodesWithTag("li");
+  var lis = $(menuDIV.down("ul")).childNodesWithTag("li");
   for (var j = 0; j < lis.length; j++) {
     var node = $(lis[j]);
-    Event.observe(node, "mousedown",
-                 listRowMouseDownHandler.bindAsEventListener(node),
-                 false);
+    node.observe("mousedown", listRowMouseDownHandler, false);
     var callback = callbacks[j];
     if (callback) {
       if (typeof(callback) == "string") {
@@ -1145,15 +1157,12 @@ function initMenu(menuDIV, callbacks) {
        else {
          node.submenu = callback;
          node.addClassName("submenu");
-         Event.observe(node, "mouseover",
-                       popupSubmenu.bindAsEventListener(node));
+         node.observe("mouseover", popupSubmenu);
        }
       }
       else {
-       Event.observe(node, "mouseup",
-                     onBodyClickMenuHandler);
-       Event.observe(node, "click",
-                     $(callback).bindAsEventListener(node));
+       node.observe("mouseup", onBodyClickMenuHandler);
+       node.observe("click", callback);
       }
     }
     else
@@ -1317,14 +1326,22 @@ function loadPreferences() {
   var http = createHTTPClient();
   http.open("GET", url, false);
   http.send("");
-  if (http.status == 200)
-    userDefaults = http.responseText.evalJSON(true);
+  if (http.status == 200) {
+    if (http.responseText.length > 0)
+      userDefaults = http.responseText.evalJSON(true);
+    else
+      userDefaults = {};
+  }
 
   url = UserFolderURL + "jsonSettings";
   http.open("GET", url, false);
   http.send("");
-  if (http.status == 200)
-    userSettings = http.responseText.evalJSON(true);
+  if (http.status == 200) {
+    if (http.responseText.length > 0)
+      userSettings = http.responseText.evalJSON(true);
+    else
+      userSettings = {};
+  }
 }
 
 function onLoadHandler(event) {
@@ -1343,7 +1360,7 @@ function onLoadHandler(event) {
   var progressImage = $("progressIndicator");
   if (progressImage)
     progressImage.parentNode.removeChild(progressImage);
-  Event.observe(document.body, "contextmenu", onBodyClickContextMenu);
+  $(document.body).observe("contextmenu", onBodyClickContextMenu);
 }
 
 function onBodyClickContextMenu(event) {
@@ -1354,8 +1371,8 @@ function configureSortableTableHeaders(table) {
   var headers = $(table).getElementsByClassName("sortableTableHeader");
   for (var i = 0; i < headers.length; i++) {
     var header = headers[i];
-    Event.observe(header, "click", onHeaderClick.bindAsEventListener(header))
-      }
+    $(header).observe("click", onHeaderClick);
+  }
 }
 
 function onLinkBannerClick() {
@@ -1376,15 +1393,24 @@ function onPreferencesClick(event) {
 function configureLinkBanner() {
   var linkBanner = $("linkBanner");
   if (linkBanner) {
-    var anchors = linkBanner.childNodesWithTag("a");
-    for (var i = 1; i < 3; i++) {
-      Event.observe(anchors[i], "mousedown", listRowMouseDownHandler);
-      Event.observe(anchors[i], "click", onLinkBannerClick);
+    var moduleLinks = [ "calendar", "contacts", "mail" ];
+    for (var i = 0; i < moduleLinks.length; i++) {
+      var link = $(moduleLinks[i] + "BannerLink");
+      if (link) {
+       link.observe("mousedown", listRowMouseDownHandler);
+       link.observe("click", onLinkBannerClick);
+      }
+    }
+    link = $("preferencesBannerLink");
+    if (link) {
+      link.observe("mousedown", listRowMouseDownHandler);
+      link.observe("click", onPreferencesClick);
+    }
+    link = $("consoleBannerLink");
+    if (link) {
+      link.observe("mousedown", listRowMouseDownHandler);
+      link.observe("click", toggleLogConsole);
     }
-    Event.observe(anchors[4], "mousedown", listRowMouseDownHandler);
-    Event.observe(anchors[4], "click", onPreferencesClick);
-    if (anchors.length > 5)
-      Event.observe(anchors[5], "click", toggleLogConsole);
   }
 }
 
@@ -1420,7 +1446,13 @@ function createFolderCallback(http) {
   }
 }
 
-addEvent(window, 'load', onLoadHandler);
+function onFinalLoadHandler(event) {
+  var safetyNet = $("javascriptSafetyNet");
+  if (safetyNet)
+    safetyNet.parentNode.removeChild(safetyNet);
+}
+
+FastInit.addOnLoad(onLoadHandler);
 
 function parent$(element) {
   return this.opener.document.getElementById(element);