]> err.no Git - scalable-opengroupware.org/blobdiff - UI/WebServerResources/generic.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1267 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / WebServerResources / generic.js
index 68a5d49148bd2ba0a3af1be48b526c32f61443cb..ce7a12ed45171a4c42d9c14348d16ee1f19d6cc7 100644 (file)
@@ -27,12 +27,11 @@ var logWindow = null;
 
 var queryParameters;
 
-var activeAjaxRequests = 0;
 var menus = new Array();
 var search = {};
 var sorting = {};
 
-var lastClickedRow = null;
+var lastClickedRow = -1;
 
 var weekStartIsMonday = true;
 
@@ -42,6 +41,10 @@ var allDocumentElements = null;
 var userDefaults = null;
 var userSettings = null;
 
+// Ajax requests counts
+var activeAjaxRequests = 0;
+var removeFolderRequestCount = 0;
+
 /* a W3C compliant document.all */
 function getAllScopeElements(scope) {
   var elements = new Array();
@@ -147,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];
   }
@@ -215,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();
@@ -225,12 +234,22 @@ function openContactWindow(url, wId) {
 }
 
 function openMailComposeWindow(url, wId) {
-  if (!wId)
-    wId = "" + (new Date().getTime());
-  var w = window.open(url, wId,
+  var parentWindow = this;
+
+  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"
                       + ",copyhistory=0");
+
   w.focus();
 
   return w;
@@ -238,9 +257,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 */
 }
@@ -274,7 +294,7 @@ function triggerAjaxRequest(url, callback, userdata) {
   var http = createHTTPClient();
 
   activeAjaxRequests += 1;
-  document.animTimer = setTimeout("checkAjaxRequestsState();", 200);
+  document.animTimer = setTimeout("checkAjaxRequestsState();", 50);
   //url = appendDifferentiator(url);
 
   if (http) {
@@ -282,12 +302,13 @@ 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) {
          if (!http.aborted) {
-           http.callbackData = userdata;
+           if (userdata)
+             http.callbackData = userdata;
            callback(http);
          }
          activeAjaxRequests -= 1;
@@ -434,44 +455,6 @@ function generateQueryString(queryDict) {
   return s;
 }
 
-function getQueryParaArray(s) {
-  if (s.charAt(0) == "?") s = s.substr(1, s.length - 1);
-  return s.split("&");
-}
-
-function getQueryParaValue(s, name) {
-  var t;
-  
-  t = getQueryParaArray(s);
-  for (var i = 0; i < t.length; i++) {
-    var s = t[i];
-    
-    if (s.indexOf(name) != 0)
-      continue;
-    
-    s = s.substr(name.length, s.length - name.length);
-    return decodeURIComponent(s);
-  }
-  return null;
-}
-
-/* opener callback */
-
-function triggerOpenerCallback() {
-  /* this code has some issue if the folder has no proper trailing slash! */
-  if (window.opener && !window.opener.closed) {
-    var t, cburl;
-    
-    t = getQueryParaValue(window.location.search, "openerurl=");
-    cburl = window.opener.location.href;
-    if (cburl[cburl.length - 1] != "/") {
-      cburl = cburl.substr(0, cburl.lastIndexOf("/") + 1);
-    }
-    cburl = cburl + t;
-    window.opener.location.href = cburl;
-  }
-}
-
 /* selection mechanism */
 
 function deselectAll(parent) {
@@ -522,7 +505,7 @@ function onRowClick(event) {
 
   var initialSelection = $(node.parentNode).getSelectedNodes();
   if ((event.shiftKey == 1 || event.ctrlKey == 1)
-      && lastClickedRow
+      && (lastClickedRow >= 0)
       && (acceptMultiSelect(node.parentNode)
          || acceptMultiSelect(node.parentNode.parentNode))) {
     if (event.shiftKey)
@@ -543,18 +526,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;
@@ -573,8 +545,20 @@ function popupMenu(event, menuId, target) {
     hideMenu(document.currentPopupMenu);
 
   var popup = $(menuId);
-  var menuTop = Event.pointerY(event);
-  var menuLeft = Event.pointerX(event);
+
+  var deltaX = 0;
+  var deltaY = 0;
+
+  var pageContent = $("pageContent");
+  if (popup.parentNode.tagName != "BODY") {
+    var offset = pageContent.cascadeLeftOffset();
+    deltaX = -($(popup.parentNode).cascadeLeftOffset() - offset);
+    offset = pageContent.cascadeTopOffset();
+    deltaY = -($(popup.parentNode).cascadeTopOffset() - offset);
+  }
+
+  var menuTop = Event.pointerY(event) + deltaY;
+  var menuLeft = Event.pointerX(event) + deltaX;
   var heightDiff = (window.height()
                    - (menuTop + popup.offsetHeight));
   if (heightDiff < 0)
@@ -587,13 +571,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);
 }
@@ -616,11 +601,11 @@ function getParentMenu(node) {
 }
 
 function onBodyClickMenuHandler(event) {
-  document.menuTarget = null;
   hideMenu(document.currentPopupMenu);
-  Event.stopObserving(document.body, "click", onBodyClickMenuHandler);
+  document.body.stopObserving("click", onBodyClickMenuHandler);
 
-  preventDefault(event);
+  if (event)
+    preventDefault(event);
 }
 
 function hideMenu(menuNode) {
@@ -632,15 +617,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;
@@ -649,18 +631,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) {
@@ -690,8 +661,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);
   }
 }
@@ -793,15 +764,11 @@ function popupSubmenu(event) {
       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",
@@ -846,7 +813,7 @@ function popupSearchMenu(event) {
          visibility: "visible" });
   
     document.currentPopupMenu = popup;
-    Event.observe(document.body, "click", onBodyClickMenuHandler);
+    $(document.body).observe("click", onBodyClickMenuHandler);
   }
 }
 
@@ -882,17 +849,12 @@ function configureSearchField() {
 
   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
@@ -992,7 +954,7 @@ function popupToolbarMenu(node, menuId) {
        visibility: "visible" });
 
   document.currentPopupMenu = popup;
-  Event.observe(document.body, "click", onBodyClickMenuHandler);
+  $(document.body).observe("click", onBodyClickMenuHandler);
 }
 
 /* contact selector */
@@ -1029,19 +991,18 @@ function subscribeToFolder(refreshCallback, refreshCallbackData) {
                                                          rfCbData);
   }
   else
-    refreshCallbackData["window"].alert(clabels["You cannot subscribe to a folder that you own!"]
-                                       );
+    refreshCallbackData["window"].alert(clabels["You cannot subscribe to a folder that you own!"]);
 }
 
 function folderUnsubscriptionCallback(http) {
   if (http.readyState == 4) {
+    removeFolderRequestCount--;
     if (isHttpStatus204(http.status)) {
       if (http.callbackData)
        http.callbackData["method"](http.callbackData["data"]);
     }
     else
       window.alert(clabels["Unable to unsubscribe from that folder!"]);
-    document.unsubscriptionAjaxRequest = null;
   }
 }
 
@@ -1051,19 +1012,16 @@ function unsubscribeFromFolder(folder, refreshCallback, refreshCallbackData) {
                                        refreshCallbackData);
   }
   else {
-    var folderData = folder.split("+");
+    var folderData = folder.split("_");
     var username = folderData[0];
     var folderPath = folderData[1];
+    if (username.startsWith('/'))
+      username = username.substring(1);
     if (username != UserLogin) {
       var url = (ApplicationBaseURL + folder + "/unsubscribe");
-      if (document.unsubscriptionAjaxRequest) {
-       document.unsubscriptionAjaxRequest.aborted = true;
-       document.unsubscriptionAjaxRequest.abort();
-      }
+      removeFolderRequestCount++;
       var rfCbData = { method: refreshCallback, data: refreshCallbackData };
-      document.unsubscriptionAjaxRequest
-       = triggerAjaxRequest(url, folderUnsubscriptionCallback,
-                            rfCbData);
+      triggerAjaxRequest(url, folderUnsubscriptionCallback, rfCbData);
     }
     else
       window.alert(clabels["You cannot unsubscribe from a folder that you own!"]);
@@ -1084,6 +1042,17 @@ function accessToSubscribedFolder(serverFolder) {
   return folder;
 }
 
+function getSubscribedFolderOwner(serverFolder) {
+  var owner;
+  
+  var parts = serverFolder.split(":");
+  if (parts.length > 1) {
+    owner = parts[0];
+  }
+  
+  return owner;
+}
+
 function listRowMouseDownHandler(event) {
   preventDefault(event);
   //Event.stop(event); 
@@ -1109,10 +1078,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();
       }
     }
@@ -1142,9 +1109,7 @@ function initMenu(menuDIV, callbacks) {
   var lis = $(menuDIV.childNodesWithTag("ul")[0]).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") {
@@ -1153,13 +1118,13 @@ 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",
-                     $(callback).bindAsEventListener(node));
+      else {
+       node.observe("mouseup", onBodyClickMenuHandler);
+       node.observe("click", callback);
+      }
     }
     else
       node.addClassName("disabled");
@@ -1322,14 +1287,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) {
@@ -1348,7 +1321,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) {
@@ -1359,8 +1332,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() {
@@ -1383,13 +1356,13 @@ function configureLinkBanner() {
   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);
+      $(anchors[i]).observe("mousedown", listRowMouseDownHandler);
+      $(anchors[i]).observe("click", onLinkBannerClick);
     }
-    Event.observe(anchors[4], "mousedown", listRowMouseDownHandler);
-    Event.observe(anchors[4], "click", onPreferencesClick);
+    $(anchors[4]).observe("mousedown", listRowMouseDownHandler);
+    $(anchors[4]).observe("click", onPreferencesClick);
     if (anchors.length > 5)
-      Event.observe(anchors[5], "click", toggleLogConsole);
+      $(anchors[5]).observe("click", toggleLogConsole);
   }
 }
 
@@ -1414,7 +1387,7 @@ function createFolderCallback(http) {
     var data = http.callbackData;
     if (http.status == 201) {
       if (data.okCB)
-       data.okCB(data.name, "/" + http.responseText);
+       data.okCB(data.name, "/" + http.responseText, UserLogin);
     }
     else {
       if (data.notOkCB)
@@ -1425,7 +1398,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);