]> err.no Git - scalable-opengroupware.org/blobdiff - UI/WebServerResources/generic.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1163 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / WebServerResources / generic.js
index 0ff9cfa81c5a7250105891b378b4873a5b463c08..608947f61cb78ce1e820adfce37f1e5a6d76d4d0 100644 (file)
@@ -29,12 +29,17 @@ var queryParameters;
 
 var activeAjaxRequests = 0;
 var menus = new Array();
+var search = {};
+var sorting = {};
 
 var weekStartIsMonday = true;
 
 // logArea = null;
 var allDocumentElements = null;
 
+var userDefaults = null;
+var userSettings = null;
+
 /* a W3C compliant document.all */
 function getAllScopeElements(scope) {
   var elements = new Array();
@@ -99,6 +104,30 @@ function getElementsByClassName2(_tag, _class, _scope) {
   }
 }
 
+function createElement(tagName, id, classes, attributes, htmlAttributes,
+                      parentNode) {
+   var newElement = $(document.createElement(tagName));
+   if (id)
+      newElement.setAttribute("id", id);
+   if (classes) {
+      if (typeof(classes) == "string")
+        newElement.addClassName(classes);
+      else
+        for (var i = 0; i < classes.length; i++)
+           newElement.addClassName(classes[i]);
+   }
+   if (attributes)
+      for (var i in attributes)
+        newElement[i] = attributes[i];
+   if (htmlAttributes)
+      for (var i in htmlAttributes)
+        newElement.setAttribute(i, htmlAttributes[i]);
+   if (parentNode)
+      parentNode.appendChild(newElement);
+
+   return $(newElement);
+}
+
 function ml_stripActionInURL(url) {
   if (url[url.length - 1] != '/') {
     var i;
@@ -116,7 +145,7 @@ function URLForFolderID(folderID) {
    var url;
    if (folderInfos.length > 1) {
       url = UserFolderURL + "../" + folderInfos[0];
-      if (folderInfos[1][0] != '/')
+      if (!folderInfos[1].startsWith('/'))
         url += '/';
       url += folderInfos[1];
    }
@@ -172,19 +201,31 @@ function sanitizeMailTo(dirtyMailTo) {
 
 function openUserFolderSelector(callback, type) {
    var urlstr = ApplicationBaseURL;
-   if (urlstr[urlstr.length-1] != '/')
+   if (! urlstr.endsWith('/'))
       urlstr += '/';
    urlstr += ("../../" + UserLogin + "/Contacts/userFolders");
-   var w = window.open(urlstr, "User Selector",
+   var w = window.open(urlstr, "_blank",
                       "width=322,height=250,resizable=1,scrollbars=0");
    w.opener = window;
-   w.userFolderCallback = callback;
-   w.userFolderType = type;
+   window.userFolderCallback = callback;
+   window.userFolderType = type;
    w.focus();
 }
 
-function openMailComposeWindow(url) {
-  var w = window.open(url, null,
+function openContactWindow(url, wId) {
+  if (!wId)
+    wId = "" + (new Date().getTime());
+  var w = window.open(url, wId,
+                     "width=450,height=600,resizable=0");
+  w.focus();
+
+  return w;
+}
+
+function openMailComposeWindow(url, wId) {
+  if (!wId)
+    wId = "" + (new Date().getTime());
+  var w = window.open(url, wId,
                       "width=680,height=520,resizable=1,scrollbars=1,toolbar=0,"
                       + "location=0,directories=0,status=0,menubar=0"
                       + ",copyhistory=0");
@@ -206,7 +247,7 @@ function createHTTPClient() {
   // http://developer.apple.com/internet/webcontent/xmlhttpreq.html
   if (typeof XMLHttpRequest != "undefined")
     return new XMLHttpRequest();
-  
+
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } 
   catch (e) { }
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } 
@@ -268,19 +309,27 @@ function triggerAjaxRequest(url, callback, userdata) {
   return http;
 }
 
+function startAnimation(parent, nextNode) {
+  var anim = document.createElement("img");
+  anim = $(anim);
+  anim.id = "progressIndicator";
+  anim.src = ResourcesURL + "/busy.gif";
+  anim.setStyle({ visibility: "hidden" });
+  if (nextNode)
+    parent.insertBefore(anim, nextNode);
+  else
+    parent.appendChild(anim);
+  anim.setStyle({ visibility: "visible" });
+
+  return anim;
+}
+
 function checkAjaxRequestsState() {
   var toolbar = document.getElementById("toolbar");
   if (toolbar) {
     if (activeAjaxRequests > 0
         && !document.busyAnim) {
-      var anim = document.createElement("img");
-      anim = $(anim);
-      document.busyAnim = anim;
-      anim.id = "progressIndicator";
-      anim.src = ResourcesURL + "/busy.gif";
-      anim.setStyle({ visibility: "hidden" });
-      toolbar.appendChild(anim);
-      anim.setStyle({ visibility: "visible" });
+      document.busyAnim = startAnimation(toolbar);
     }
     else if (activeAjaxRequests == 0
             && document.busyAnim
@@ -492,11 +541,11 @@ function popupMenu(event, menuId, target) {
    document.menuTarget = target;
 
    if (document.currentPopupMenu)
-       hideMenu(event, document.currentPopupMenu);
+      hideMenu(document.currentPopupMenu);
 
    var popup = $(menuId);
-   var menuTop = event.pageY;
-   var menuLeft = event.pageX;
+   var menuTop =  Event.pointerY(event);
+   var menuLeft = Event.pointerX(event);
    var heightDiff = (window.innerHeight
                     - (menuTop + popup.offsetHeight));
    if (heightDiff < 0)
@@ -512,6 +561,7 @@ function popupMenu(event, menuId, target) {
                    visibility: "visible" });
 
    document.currentPopupMenu = popup;
+
    Event.observe(document.body, "click", onBodyClickMenuHandler);
 
    preventDefault(event);
@@ -536,18 +586,17 @@ function getParentMenu(node) {
 
 function onBodyClickMenuHandler(event) {
    document.body.menuTarget = null;
-   hideMenu(event, document.currentPopupMenu);
+   hideMenu(document.currentPopupMenu);
    Event.stopObserving(document.body, "click", onBodyClickMenuHandler);
 
    preventDefault(event);
 }
 
-function hideMenu(event, menuNode) {
+function hideMenu(menuNode) { //log ("hideMenu");
   var onHide;
 
-//   log('hiding menu "' + menuNode.getAttribute('id') + '"');
   if (menuNode.submenu) {
-    hideMenu(event, menuNode.submenu);
+    hideMenu(menuNode.submenu);
     menuNode.submenu = null;
   }
 
@@ -562,13 +611,17 @@ function hideMenu(event, menuNode) {
     menuNode.parentMenu = null;
   }
 
-  if (document.initEvent) {
-    var onhideEvent = document.createEvent("UIEvents");
-    onhideEvent.initEvent("hideMenu", false, true);
+  if (document.createEvent) {
+    var onhideEvent;
+    if (isSafari())
+      onhideEvent = document.createEvent("UIEvents");
+    else // Mozilla
+      onhideEvent = document.createEvent("Events");
+    onhideEvent.initEvent("mousedown", false, true);
     menuNode.dispatchEvent(onhideEvent);
   }
-  else if (document.createEventObject) {
-    // TODO: add support for IE
+  else if (document.createEventObject) { // IE
+    menuNode.fireEvent("onmousedown");
   }
 }
 
@@ -680,7 +733,7 @@ function dropDownSubmenu(event) {
       var submenuNode = $(this.submenu);
       var parentNode = getParentMenu(node);
       if (parentNode.submenu)
-        hideMenu(event, parentNode.submenu);
+        hideMenu(parentNode.submenu);
       submenuNode.parentMenuItem = node;
       submenuNode.parentMenu = parentNode;
       parentNode.submenuItem = node;
@@ -720,7 +773,7 @@ function checkDropDown(event) {
         && menuX < itemX + submenuItem.offsetWidth
         && (menuY < itemY
             || menuY > (itemY + submenuItem.offsetHeight))) {
-      hideMenu(event, parentMenu.submenu);
+      hideMenu(parentMenu.submenu);
       parentMenu.submenu = null;
       parentMenu.submenuItem = null;
       parentMenu.setAttribute('onmousemove', null);
@@ -730,11 +783,11 @@ function checkDropDown(event) {
 
 /* search field */
 function popupSearchMenu(event) {
-  var node = getTarget(event);
-
   var menuId = this.getAttribute("menuid");
-  relX = event.pageX - node.cascadeLeftOffset();
-  relY = event.pageY - node.cascadeTopOffset();
+  var offset = Position.cumulativeOffset(this);
+
+  relX = Event.pointerX(event) - offset[0];
+  relY = Event.pointerY(event) - offset[1];
 
   if (event.button == 0
       && relX < 24) {
@@ -742,11 +795,12 @@ function popupSearchMenu(event) {
     event.returnValue = false;
 
     if (document.currentPopupMenu)
-      hideMenu(event, document.currentPopupMenu);
+      hideMenu(document.currentPopupMenu);
 
-    var popup = document.getElementById(menuId);
-    popup.setStyle({ top: node.offsetHeight + "px",
-                    left: (node.offsetLeft + 3) + "px",
+    var popup = $(menuId);
+    offset = Position.positionedOffset(this);
+    popup.setStyle({ top: this.offsetHeight + "px",
+                   left: (offset[0] + 3) + "px",
                            visibility: "visible" });
   
     document.currentPopupMenu = popup;
@@ -755,11 +809,11 @@ function popupSearchMenu(event) {
 }
 
 function setSearchCriteria(event) {
-  searchValue = $("searchValue");
-  searchCriteria = $("searchCriteria");
+  var searchValue = $("searchValue");
+  var searchCriteria = $("searchCriteria");
 
   searchValue.setAttribute("ghost-phrase", this.innerHTML);
-//   searchCriteria = this.getAttribute('id');
+  searchCriteria.value = this.getAttribute('id');
 }
 
 function checkSearchValue(event) {
@@ -775,10 +829,27 @@ function onSearchChange() {
   log ("onSearchChange()...");
 }
 
+function configureSearchField() {
+   var searchValue = $("searchValue");
+
+   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));
+}
+
 function onSearchMouseDown(event) {
    var superNode = this.parentNode.parentNode.parentNode;
-   relX = (event.pageX - superNode.offsetLeft - this.offsetLeft);
-   relY = (event.pageY - superNode.offsetTop - this.offsetTop);
+   relX = (Event.pointerX(event) - superNode.offsetLeft - this.offsetLeft);
+   relY = (Event.pointerY(event) - superNode.offsetTop - this.offsetTop);
 
    if (relY < 24) {
       event.cancelBubble = true;
@@ -799,12 +870,13 @@ function onSearchFocus() {
 }
 
 function onSearchBlur(event) {
-  var ghostPhrase = this.getAttribute("ghost-phrase");
-//   log ("search blur: '" + this.value + "'");
-  if (!this.value) {
+   var ghostPhrase = this.getAttribute("ghost-phrase");
+   //log ("search blur: '" + this.value + "'");
+   if (!this.value) {
     this.setAttribute("modified", "");
     this.setStyle({ color: "#aaa" });
     this.value = ghostPhrase;
+    refreshCurrentFolder();
   } else if (this.value == ghostPhrase) {
     this.setAttribute("modified", "");
     this.setStyle({ color: "#aaa" });
@@ -821,23 +893,32 @@ function onSearchKeyDown(event) {
   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();
+}
+
 function initCriteria() {
   var searchCriteria = $("searchCriteria");
   var searchValue = $("searchValue");
-  var firstOption;
  
-  var searchOptions = $("searchOptions");
-  if (searchOptions) {
-    var firstOption;
-    $A(searchOptions.childNodes).each(function (item) {
-      if (item.tagName == 'LI') {
-       firstOption = item;
-      }
-    });
-    searchCriteria.value = firstOption.getAttribute('id');
-    searchValue.setAttribute('ghost-phrase', firstOption.innerHTML);
+  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 = firstOption.innerHTML;
+      searchValue.value = firstChild.innerHTML;
       searchValue.setAttribute("modified", "");
       searchValue.setStyle({ color: "#aaa" });
     }
@@ -845,45 +926,34 @@ function initCriteria() {
 }
 
 /* toolbar buttons */
-function popupToolbarMenu(event, menuId) {
-   var toolbar = $("toolbar");
-   var node = getTarget(event);
-   if (node.tagName != 'A')
-      node = node.getParentWithTagName("a");
-   node = node.childNodesWithTag("span")[0];
-
-   if (event.button == 0) {
-      event.cancelBubble = true;
-      event.returnValue = false;
-      
-      if (document.currentPopupMenu)
-        hideMenu(event, document.currentPopupMenu);
-      
-      var popup = document.getElementById(menuId);
-      var top = node.offsetTop + node.offsetHeight - 2;
-      popup.setStyle({ top: top + "px",
-                      left: node.cascadeLeftOffset() + "px",
-                      visibility: "visible" });
-
-      document.currentPopupMenu = popup;
-      Event.observe(document.body, "click", onBodyClickMenuHandler);
-   }
+function popupToolbarMenu(node, menuId) {
+   if (document.currentPopupMenu)
+      hideMenu(document.currentPopupMenu);
+
+   var popup = $(menuId);
+   var top = ($(node).getStyle('top') || 0) + node.offsetHeight - 2;
+   popup.setStyle({ top: top + "px",
+                   left: $(node).cascadeLeftOffset() + "px",
+                   visibility: "visible" });
+
+   document.currentPopupMenu = popup;
+   Event.observe(document.body, "click", onBodyClickMenuHandler);
 }
 
 /* contact selector */
 
 function folderSubscriptionCallback(http) {
    if (http.readyState == 4) {
-      if (http.status == 204) {
+      if (isHttpStatus204(http.status)) {
         if (http.callbackData)
            http.callbackData["method"](http.callbackData["data"]);
       }
       else
-        window.alert(labels["Unable to subscribe to that folder!"].decodeEntities());
+        window.alert(clabels["Unable to subscribe to that folder!"].decodeEntities());
       document.subscriptionAjaxRequest = null;
    }
    else
-      log ("ajax fuckage");
+      log ("folderSubscriptionCallback Ajax error");
 }
 
 function subscribeToFolder(refreshCallback, refreshCallbackData) {
@@ -903,18 +973,18 @@ function subscribeToFolder(refreshCallback, refreshCallbackData) {
                                                            rfCbData);
    }
    else
-      window.alert(labels["You cannot subscribe to a folder that you own!"]
+      refreshCallbackData["window"].alert(clabels["You cannot subscribe to a folder that you own!"]
                   .decodeEntities());
 }
 
 function folderUnsubscriptionCallback(http) {
    if (http.readyState == 4) {
-      if (http.status == 204) {
+      if (isHttpStatus204(http.status)) {
         if (http.callbackData)
            http.callbackData["method"](http.callbackData["data"]);
       }
       else
-        window.alert(labels["Unable to unsubscribe from that folder!"].decodeEntities());
+        window.alert(clabels["Unable to unsubscribe from that folder!"].decodeEntities());
       document.unsubscriptionAjaxRequest = null;
    }
 }
@@ -941,12 +1011,12 @@ function unsubscribeFromFolder(folder, refreshCallback, refreshCallbackData) {
                                 rfCbData);
       }
       else
-        window.alert(labels["You cannot unsubscribe from a folder that you own!"].decodeEntities());
+        window.alert(clabels["You cannot unsubscribe from a folder that you own!"].decodeEntities());
    }
 }
 
 function listRowMouseDownHandler(event) {
-  preventDefault(event);
+   preventDefault(event);
 }
 
 /* tabs */
@@ -1001,7 +1071,7 @@ function initMenus() {
 function initMenu(menuDIV, callbacks) {
    var lis = $(menuDIV.childNodesWithTag("ul")[0]).childNodesWithTag("li");
    for (var j = 0; j < lis.length; j++) {
-      var node = lis[j];
+      var node = $(lis[j]);
       Event.observe(node, "mousedown", listRowMouseDownHandler, false);
       var callback = callbacks[j];
       if (callback) {
@@ -1174,36 +1244,55 @@ function indexColor(number) {
   return color;
 }
 
+function loadPreferences() {
+   var url = UserFolderURL + "jsonDefaults";
+   var http = createHTTPClient();
+   http.open("GET", url, false);
+   http.send("");
+   if (http.status == 200)
+      userDefaults = http.responseText.evalJSON(true);
+
+   url = UserFolderURL + "jsonSettings";
+   http.open("GET", url, false);
+   http.send("");
+   if (http.status == 200)
+      userSettings = http.responseText.evalJSON(true);
+}
+
 function onLoadHandler(event) {
-    queryParameters = parseQueryParameters('' + window.location);
-    if (!$(document.body).hasClassName("popup")) {
-       initLogConsole();
-       initCriteria();
-    }
-    initMenus();
-    initTabs();
-    configureDragHandles();
-    configureSortableTableHeaders();
-    configureLinkBanner();
-    var progressImage = $("progressIndicator");
-    if (progressImage)
-       progressImage.parentNode.removeChild(progressImage);
-    Event.observe(document.body, "contextmenu", onBodyClickContextMenu);
+   if (!document.body.hasClassName("loginPage")) {
+     loadPreferences();
+   }
+   queryParameters = parseQueryParameters('' + window.location);
+   if (!$(document.body).hasClassName("popup")) {
+      initLogConsole();
+   }
+   initCriteria();
+   configureSearchField();
+   initMenus();
+   initTabs();
+   configureDragHandles();
+   configureSortableTableHeaders();
+   configureLinkBanner();
+   var progressImage = $("progressIndicator");
+   if (progressImage)
+      progressImage.parentNode.removeChild(progressImage);
+   Event.observe(document.body, "contextmenu", onBodyClickContextMenu);
 }
 
 function onBodyClickContextMenu(event) {
-  preventDefault(event);
+   preventDefault(event);
 }
 
 function configureSortableTableHeaders() {
-  var headers = document.getElementsByClassName("sortableTableHeader");
-  for (var i = 0; i < headers.length; i++) {
-     var header = headers[i];
-     var anchor = $(header).childNodesWithTag("a")[0];
-     if (anchor)
-       Event.observe(anchor, "click",
-                     onHeaderClick.bindAsEventListener(anchor));
-  }
+   var headers = document.getElementsByClassName("sortableTableHeader");
+   for (var i = 0; i < headers.length; i++) {
+      var header = headers[i];
+      var anchor = $(header).childNodesWithTag("a")[0];
+      if (anchor)
+        Event.observe(anchor, "click",
+                      onHeaderClick.bindAsEventListener(anchor));
+   }
 }
 
 function onLinkBannerClick() {
@@ -1218,27 +1307,34 @@ function onPreferencesClick(event) {
    w.opener = window;
    w.focus();
 
-   event.preventDefault();
+   preventDefault(event);
 }
 
 function configureLinkBanner() {
   var linkBanner = $("linkBanner");
   if (linkBanner) {
     var anchors = linkBanner.childNodesWithTag("a");
-    for (var i = 0; i < 2; i++) {
+    for (var i = 1; i < 3; i++) {
        Event.observe(anchors[i], "mousedown", listRowMouseDownHandler);
        Event.observe(anchors[i], "click", onLinkBannerClick);
     }
-    Event.observe(anchors[3], "mousedown", listRowMouseDownHandler);
-    Event.observe(anchors[3], "click", onPreferencesClick);
-    if (anchors.length > 4)
-       Event.observe(anchors[4], "click", toggleLogConsole);
+    Event.observe(anchors[4], "mousedown", listRowMouseDownHandler);
+    Event.observe(anchors[4], "click", onPreferencesClick);
+    if (anchors.length > 5)
+       Event.observe(anchors[5], "click", toggleLogConsole);
   }
 }
 
 addEvent(window, 'load', onLoadHandler);
 
+function parent$(element) {
+   return this.opener.document.getElementById(element);
+}
+
 /* stubs */
+function refreshCurrentFolder() {
+}
+
 function configureDragHandles() {
 }
 
@@ -1248,7 +1344,3 @@ function getMenus() {
 function onHeaderClick(event) {
    window.alert("generic headerClick");
 }
-
-function parent$(element) {
-   return this.opener.document.getElementById(element);
-}