+ currentMailbox = "/" + accounts[0] + "/INBOX";
+ initMailboxTree();
+ }
+}
+
+function initMailboxTree() {
+ mailboxTree = new dTree("mailboxTree");
+ mailboxTree.config.folderLinks = true;
+ mailboxTree.config.hideRoot = true;
+
+ mailboxTree.icon.root = ResourcesURL + "/tbtv_account_17x17.gif";
+ mailboxTree.icon.folder = ResourcesURL + "/tbtv_leaf_corner_17x17.gif";
+ mailboxTree.icon.folderOpen = ResourcesURL + "/tbtv_leaf_corner_17x17.gif";
+ mailboxTree.icon.node = ResourcesURL + "/tbtv_leaf_corner_17x17.gif";
+ mailboxTree.icon.line = ResourcesURL + "/tbtv_line_17x17.gif";
+ mailboxTree.icon.join = ResourcesURL + "/tbtv_junction_17x17.gif";
+ mailboxTree.icon.joinBottom = ResourcesURL + "/tbtv_corner_17x17.gif";
+ mailboxTree.icon.plus = ResourcesURL + "/tbtv_plus_17x17.gif";
+ mailboxTree.icon.plusBottom = ResourcesURL + "/tbtv_corner_plus_17x17.gif";
+ mailboxTree.icon.minus = ResourcesURL + "/tbtv_minus_17x17.gif";
+ mailboxTree.icon.minusBottom = ResourcesURL + "/tbtv_corner_minus_17x17.gif";
+ mailboxTree.icon.nlPlus = ResourcesURL + "/tbtv_corner_plus_17x17.gif";
+ mailboxTree.icon.nlMinus = ResourcesURL + "/tbtv_corner_minus_17x17.gif";
+ mailboxTree.icon.empty = ResourcesURL + "/empty.gif";
+
+ mailboxTree.add(0, -1, '');
+
+ mailboxTree.pendingRequests = mailAccounts.length;
+ for (var i = 0; i < mailAccounts.length; i++) {
+ var url = ApplicationBaseURL + "/" + mailAccounts[i] + "/mailboxes";
+ triggerAjaxRequest(url, onLoadMailboxesCallback, mailAccounts[i]);
+ }
+}
+
+function updateMailboxTreeInPage() {
+ $("folderTreeContent").innerHTML = mailboxTree;
+
+ var inboxFound = false;
+ var tree = $("mailboxTree");
+ var nodes = document.getElementsByClassName("node", tree);
+ for (i = 0; i < nodes.length; i++) {
+ Event.observe(nodes[i], "click", onMailboxTreeItemClick.bindAsEventListener(nodes[i]));
+ Event.observe(nodes[i], "contextmenu", onFolderMenuClick.bindAsEventListener(nodes[i]));
+ if (!inboxFound
+ && nodes[i].parentNode.getAttribute("datatype") == "inbox") {
+ openInbox(nodes[i]);
+ inboxFound = true;
+ }
+ }
+}
+
+function mailboxMenuNode(type, name) {
+ var newNode = document.createElement("li");
+ var icon = MailerUIdTreeExtension.folderIcons[type];
+ if (!icon)
+ icon = "tbtv_leaf_corner_17x17.gif";
+ var image = document.createElement("img");
+ image.src = ResourcesURL + "/" + icon;
+ newNode.appendChild(image);
+ newNode.appendChild(document.createTextNode(" " + name));
+
+ return newNode;
+}
+
+function generateMenuForMailbox(mailbox, prefix, callback) {
+ var menuDIV = document.createElement("div");
+ menuDIV.addClassName("menu");
+ menuDIV.setAttribute("id", prefix + "Submenu");
+ var menu = document.createElement("ul");
+ menuDIV.appendChild(menu);
+
+ var callbacks = new Array();
+ if (mailbox.type != "account") {
+ var newNode = document.createElement("li");
+ newNode.mailbox = mailbox;
+ newNode.appendChild(document.createTextNode("coucou"));
+ menu.appendChild(newNode);
+ menu.appendChild(document.createElement("li"));
+ callbacks.push(callback);
+ callbacks.push("-");
+ }
+
+ var submenuCount = 0;
+ for (var i = 0; i < mailbox.children.length; i++) {
+ var child = mailbox.children[i];
+ var newNode = mailboxMenuNode(child.type, child.name);
+ menu.appendChild(newNode);
+ if (child.children.length > 0) {
+ var newPrefix = prefix + submenuCount;
+ var newSubmenu = generateMenuForMailbox(child,
+ newPrefix,
+ callback);
+ document.body.appendChild(newSubmenu);
+ callbacks.push(newPrefix + "Submenu");
+ submenuCount++;
+ }
+ else {
+ newNode.mailbox = child;
+ callbacks.push(callback);
+ }
+ }
+ initMenu(menuDIV, callbacks);
+
+ return menuDIV;
+}
+
+function updateMailboxMenus() {
+ var mailboxActions = { move: onMailboxMenuMove,
+ copy: onMailboxMenuCopy };
+
+ for (key in mailboxActions) {
+ var menuId = key + "MailboxMenu";
+ var menuDIV = $(menuId);
+ if (menuDIV)
+ menuDIV.parentNode.removeChild(menuDIV);
+ menuDIV = document.createElement("div");
+ menuDIV.addClassName("menu");
+ menuDIV.setAttribute("id", menuId);
+ var menu = document.createElement("ul");
+ menuDIV.appendChild(menu);
+
+ var submenuIds = new Array();
+ for (var i = 0; i < mailAccounts.length; i++) {
+ var menuEntry = mailboxMenuNode("account", mailAccounts[i]);
+ menu.appendChild(menuEntry);
+ var mailbox = accounts[mailAccounts[i]];
+ var newSubmenu = generateMenuForMailbox(mailbox,
+ key, mailboxActions[key]);
+ document.body.appendChild(newSubmenu);
+ submenuIds.push(newSubmenu.getAttribute("id"));
+ }
+ initMenu(menuDIV, submenuIds);
+
+ document.body.appendChild(menuDIV);
+ }
+}
+
+function onLoadMailboxesCallback(http) {
+ if (http.readyState == 4
+ && http.status == 200) {
+ var newAccount = buildMailboxes(http.callbackData,
+ http.responseText);
+ accounts[http.callbackData] = newAccount;
+ mailboxTree.addMailAccount(newAccount);
+ mailboxTree.pendingRequests--;
+ if (!mailboxTree.pendingRequests) {
+ updateMailboxTreeInPage();
+ updateMailboxMenus();
+ }
+ }
+
+// var tree = $("mailboxTree");
+// var treeNodes = document.getElementsByClassName("dTreeNode", tree);
+// var i = 0;
+// while (i < treeNodes.length
+// && treeNodes[i].getAttribute("dataname") != currentMailbox)
+// i++;
+// if (i < treeNodes.length) {
+// // log("found mailbox");
+// var links = document.getElementsByClassName("node", treeNodes[i]);
+// if (tree.selectedEntry)
+// tree.selectedEntry.deselect();
+// links[0].select();
+// tree.selectedEntry = links[0];
+// expandUpperTree(links[0]);
+// }
+}
+
+function buildMailboxes(accountName, encoded) {
+ var account = new Mailbox("account", accountName);
+ var data = encoded.evalJSON(true);
+ for (var i = 0; i < data.length; i++) {
+ var currentNode = account;
+ var names = data[i].path.split("/");
+ for (var j = 1; j < (names.length - 1); j++) {
+ var node = currentNode.findMailboxByName(names[j]);
+ if (!node) {
+ node = new Mailbox("additional", names[j]);
+ currentNode.addMailbox(node);