1 /* JavaScript for SOGoContacts */
3 var cachedContacts = new Array();
4 var currentContactFolder = null;
6 var usersRightsWindowHeight = 200;
7 var usersRightsWindowWidth = 450;
9 function openContactWindow(sender, url) {
10 var msgWin = window.open(url, null, "width=450,height=600,resizable=0");
14 function validateEditorInput(sender) {
18 field = document.pageform.subject;
19 if (field.value == "")
20 errortext = errortext + labels.error_missingsubject + "\n";
22 if (!UIxRecipientSelectorHasRecipients())
23 errortext = errortext + labels.error_missingrecipients + "\n";
25 if (errortext.length > 0) {
26 alert(labels.error_validationfailed.decodeEntities() + ":\n"
27 + errortext.decodeEntities());
33 function openContactsFolder(contactsFolder, reload, idx) {
34 if ((currentContactFolder && contactsFolder != currentContactFolder)
36 currentContactFolder = contactsFolder; log("openContactsFolder " + contactsFolder);
37 var url = URLForFolderID(currentContactFolder) +
40 var searchValue = search["value"];
41 if (searchValue && searchValue.length > 0)
42 url += ("&search=" + search["criteria"]
43 + "&value=" + searchValue);
44 var sortAttribute = sorting["attribute"];
45 if (sortAttribute && sortAttribute.length > 0)
46 url += ("&sort=" + sorting["attribute"]
47 + "&asc=" + sorting["ascending"]);
50 if (contactsFolder == currentContactFolder) {
51 var contactsList = $("contactsList");
53 selection = contactsList.getSelectedRowsId();
55 window.alert("no contactsList");
60 if (document.contactsListAjaxRequest) {
61 document.contactsListAjaxRequest.aborted = true;
62 document.contactsListAjaxRequest.abort();
64 document.contactsListAjaxRequest
65 = triggerAjaxRequest(url, contactsListCallback, selection);
69 function openContactsFolderAtIndex(element) {
70 var idx = element.getAttribute("idx");
71 var url = URLForFolderID(currentContactFolder) + "/view?noframe=1&idx=" + idx;
73 if (document.contactsListAjaxRequest) {
74 document.contactsListAjaxRequest.aborted = true;
75 document.contactsListAjaxRequest.abort();
77 document.contactsListAjaxRequest
78 = triggerAjaxRequest(url, contactsListCallback);
81 function configureContactsListHeaders(cells) {
82 for (var i = 0; i < cells.length; i++) {
83 var currentCell = $(cells[i]);
84 Event.observe(currentCell, "click",
85 onHeaderClick.bindAsEventListener(currentCell));
86 Event.observe(currentCell, "mousedown", listRowMouseDownHandler);
90 function contactsListCallback(http) {
91 var div = $("contactsListContent");
93 if (http.readyState == 4
94 && http.status == 200) {
95 document.contactsListAjaxRequest = null;
96 div.innerHTML = http.responseText;
98 var table = $("contactsList");
100 configureContactsListHeaders(table.tBodies[0].rows[0].cells);
102 if (sorting["attribute"] && sorting["attribute"].length > 0) {
104 if (sorting["attribute"] == "cn")
105 sortHeader = $("nameHeader");
106 else if (sorting["attribute"] == "mail")
107 sortHeader = $("mailHeader");
108 else if (sorting["attribute"] == "screenname")
109 sortHeader = $("screenNameHeader");
110 else if (sorting["attribute"] == "o")
111 sortHeader = $("orgHeader");
112 else if (sorting["attribute"] == "telephonenumber")
113 sortHeader = $("phoneHeader");
118 var sortImage = createElement("img", "messageSortImage", "sortImage");
119 sortHeader.insertBefore(sortImage, sortHeader.firstChild);
120 if (sorting["ascending"])
121 sortImage.src = ResourcesURL + "/title_sortdown_12x12.png";
123 sortImage.src = ResourcesURL + "/title_sortup_12x12.png";
127 var selected = http.callbackData;
129 for (var i = 0; i < selected.length; i++) {
130 var row = $(selected[i]);
137 log ("ajax problem 1: status = " + http.status);
140 function onContactFoldersContextMenu(event) {
141 var menu = $("contactFoldersMenu");
142 Event.observe(menu, "hideMenu", onContactFoldersContextMenuHide, false);
143 popupMenu(event, "contactFoldersMenu", this);
145 var topNode = $("contactFolders");
146 var selectedNodes = topNode.getSelectedRows();
147 topNode.menuSelectedRows = selectedNodes;
148 for (var i = 0; i < selectedNodes.length; i++)
149 selectedNodes[i].deselect();
150 topNode.menuSelectedEntry = this;
154 function onContactContextMenu(event, element) {
155 var menu = $("contactMenu");
156 Event.observe(menu, "hideMenu", onContactContextMenuHide, false);
157 popupMenu(event, "contactMenu", element);
159 var topNode = $("contactsList");
160 var selectedNodes = topNode.getSelectedRows();
161 topNode.menuSelectedRows = selectedNodes;
162 for (var i = 0; i < selectedNodes.length; i++)
163 selectedNodes[i].deselect();
164 topNode.menuSelectedEntry = element;
168 function onContactContextMenuHide(event) {
169 var topNode = $("contactsList");
171 if (topNode.menuSelectedEntry) {
172 topNode.menuSelectedEntry.deselect();
173 topNode.menuSelectedEntry = null;
175 if (topNode.menuSelectedRows) {
176 var nodes = topNode.menuSelectedRows;
177 for (var i = 0; i < nodes.length; i++)
179 topNode.menuSelectedRows = null;
183 function onContactFoldersContextMenuHide(event) {
184 var topNode = $("contactFolders");
186 if (topNode.menuSelectedEntry) {
187 topNode.menuSelectedEntry.deselect();
188 topNode.menuSelectedEntry = null;
190 if (topNode.menuSelectedRows) {
191 var nodes = topNode.menuSelectedRows;
192 for (var i = 0; i < nodes.length; i++)
194 topNode.menuSelectedRows = null;
198 function onFolderMenuHide(event) {
199 var topNode = $('d');
201 if (topNode.menuSelectedEntry) {
202 topNode.menuSelectedEntry.deselect();
203 topNode.menuSelectedEntry = null;
205 if (topNode.selectedEntry)
206 topNode.selectedEntry.select();
209 function loadContact(idx) {
210 if (document.contactAjaxRequest) {
211 document.contactAjaxRequest.aborted = true;
212 document.contactAjaxRequest.abort();
215 if (cachedContacts[currentContactFolder + "/" + idx]) {
216 var div = $('contactView');
217 div.innerHTML = cachedContacts[currentContactFolder + "/" + idx];
220 var url = (URLForFolderID(currentContactFolder)
221 + "/" + idx + "/view?noframe=1");
222 document.contactAjaxRequest
223 = triggerAjaxRequest(url, contactLoadCallback, idx);
227 function contactLoadCallback(http) {
228 var div = $('contactView');
230 if (http.readyState == 4
231 && http.status == 200) {
232 document.contactAjaxRequest = null;
233 var content = http.responseText;
234 cachedContacts[currentContactFolder + "/" + http.callbackData] = content;
235 div.innerHTML = content;
238 log ("ajax problem 2: " + http.status);
241 var rowSelectionCount = 0;
245 function showElement(e, shouldShow) {
246 e.style.display = shouldShow ? "" : "none";
249 function enableElement(e, shouldEnable) {
253 if(e.hasAttribute("disabled"))
254 e.removeAttribute("disabled");
257 e.setAttribute("disabled", "1");
261 function validateControls() {
263 this.enableElement(e, rowSelectionCount > 0);
266 function moveTo(uri) {
267 alert("MoveTo: " + uri);
270 /* contact menu entries */
271 function onContactRowClick(event, node) {
272 loadContact(node.getAttribute('id'));
274 return onRowClick(event);
277 function onContactRowDblClick(event, node) {
278 var contactId = node.getAttribute('id');
280 openContactWindow(null,
281 URLForFolderID(currentContactFolder)
282 + "/" + contactId + "/edit");
287 function onMenuEditContact(event) {
288 var contactId = document.menuTarget.getAttribute('id');
290 openContactWindow(null,
291 URLForFolderID(currentContactFolder)
292 + "/" + contactId + "/edit");
295 function onMenuWriteToContact(event) {
296 var contactId = document.menuTarget.getAttribute('id');
298 openMailComposeWindow(ApplicationBaseURL + currentContactFolder
299 + "/" + contactId + "/write");
302 function onMenuDeleteContact(event) {
303 uixDeleteSelectedContacts(this);
306 function onToolbarEditSelectedContacts(event) {
307 var contactsList = $('contactsList');
308 var rows = contactsList.getSelectedRowsId();
310 for (var i = 0; i < rows.length; i++) {
311 openContactWindow(null,
312 URLForFolderID(currentContactFolder)
313 + "/" + rows[i] + "/edit");
319 function onToolbarWriteToSelectedContacts(event) {
320 var contactsList = $('contactsList');
321 var rows = contactsList.getSelectedRowsId();
323 for (var i = 0; i < rows.length; i++)
324 openMailComposeWindow(ApplicationBaseURL + currentContactFolder
325 + "/" + rows[i] + "/write");
330 function uixDeleteSelectedContacts(sender) {
332 var contactsList = $('contactsList');
333 var rows = contactsList.getSelectedRowsId();
335 var contactView = $('contactView');
336 contactView.innerHTML = '';
338 for (var i = 0; i < rows.length; i++) {
339 var url, http, rowElem;
341 /* send AJAX request (synchronously) */
343 url = (URLForFolderID(currentContactFolder) + "/"
344 + rows[i] + "/delete");
345 http = createHTTPClient();
346 http.open("POST", url, false /* not async */);
348 if (http.status != 200) { /* request failed */
355 /* remove from page */
357 /* line-through would be nicer, but hiding is OK too */
358 rowElem = $(rows[i]);
359 rowElem.parentNode.removeChild(rowElem);
363 alert("Could not delete the selected contacts!");
368 function newEmailTo(sender) {
369 var mailto = sanitizeMailTo(sender.parentNode.parentNode.menuTarget.innerHTML);
371 if (mailto.length > 0)
373 w = window.open("compose?mailto=" + mailto,
375 "width=680,height=520,resizable=1,scrollbars=1,toolbar=0," +
376 "location=0,directories=0,status=0,menubar=0,copyhistory=0");
380 return false; /* stop following the link */
383 function onHeaderClick(event) {
384 var headerId = this.getAttribute("id");
385 var newSortAttribute;
386 if (headerId == "nameHeader")
387 newSortAttribute = "cn";
388 else if (headerId == "mailHeader")
389 newSortAttribute = "mail";
390 else if (headerId == "screenNameHeader")
391 newSortAttribute = "screenname";
392 else if (headerId == "orgHeader")
393 newSortAttribute = "o";
394 else if (headerId == "phoneHeader")
395 newSortAttribute = "telephonenumber";
397 log("header: " + headerId);
399 if (sorting["attribute"] == newSortAttribute)
400 sorting["ascending"] = !sorting["ascending"];
402 sorting["attribute"] = newSortAttribute;
403 sorting["ascending"] = true;
406 refreshCurrentFolder();
408 preventDefault(event);
411 function registerDraggableMessageNodes() {
412 log ("can we drag...");
415 function newContact(sender) {
416 openContactWindow(sender,
417 URLForFolderID(currentContactFolder) + "/new");
419 return false; /* stop following the link */
422 function onFolderSelectionChange() {
423 var folderList = $("contactFolders");
424 var nodes = folderList.getSelectedNodes();
425 $("contactView").innerHTML = '';
427 if (nodes[0].hasClassName("denied")) {
428 var div = $("contactsListContent");
434 $("searchValue").value = "";
436 openContactsFolder(nodes[0].getAttribute("id"));
440 function refreshCurrentFolder() { log("refreshCurrentFolder");
441 openContactsFolder(currentContactFolder, true);
444 function onConfirmContactSelection(event) {
445 var tag = this.getAttribute("name");
446 var folderLi = $(currentContactFolder);
447 var currentContactFolderName = folderLi.innerHTML;
448 var selectorList = null;
449 var initialValues = null;
452 var selectorId = selector.getAttribute("id");
453 selectorList = opener.window.document.getElementById('uixselector-'
456 initialValues = selectorList.value;
459 var contactsList = $("contactsList");
460 var rows = contactsList.getSelectedRows();
461 for (i = 0; i < rows.length; i++) {
462 var cid = rows[i].getAttribute("contactid");
463 var cname = '' + rows[i].getAttribute("contactname");
464 var email = '' + rows[i].cells[1].innerHTML;
465 opener.window.addContact(tag, currentContactFolderName + '/' + cname,
469 if (selector && selector.changeNotification
470 && selectorList.value != initialValues)
471 selector.changeNotification("addition");
473 preventDefault(event);
476 function onContactMailTo(node) {
477 return openMailTo(node.innerHTML);
480 function refreshContacts(contactId) {
481 refreshCurrentFolder();
482 cachedContacts[currentContactFolder + "/" + contactId] = null;
483 loadContact(contactId);
488 function onAddressBookNew(event) {
489 var name = window.prompt(labels["Name of the Address Book"].decodeEntities());
491 if (document.newAbAjaxRequest) {
492 document.newAbAjaxRequest.aborted = true;
493 document.newAbAjaxRequest.abort();
495 var url = ApplicationBaseURL + "/newAb?name=" + name;
496 document.newAbAjaxRequest
497 = triggerAjaxRequest(url, newAbCallback, name);
499 preventDefault(event);
502 function appendAddressBook(name, folder) {
503 var li = document.createElement("li");
504 $("contactFolders").appendChild(li);
505 li.setAttribute("id", folder);
506 li.appendChild(document.createTextNode(name));
507 setEventsOnContactFolder(li);
510 function newAbCallback(http) {
511 if (http.readyState == 4
512 && http.status == 201) {
513 var name = http.callbackData;
514 appendAddressBook(name, "/" + name);
517 log ("ajax problem 4:" + http.status);
520 function newUserFolderCallback(folderData) {
521 var folder = $(folderData["folder"]);
523 appendAddressBook(folderData["folderName"], folderData["folder"]);
526 function onAddressBookAdd(event) {
527 openUserFolderSelector(newUserFolderCallback, "contact");
529 preventDefault(event);
532 function onFolderUnsubscribeCB(folderId) {
533 var node = $(folderId);
534 node.parentNode.removeChild(node);
535 var personal = $("/personal");
537 onFolderSelectionChange();
540 function onAddressBookRemove(event) {
541 var selector = $("contactFolders");
542 var nodes = selector.getSelectedNodes();
543 if (nodes.length > 0) {
545 var folderId = nodes[0].getAttribute("id");
546 var folderIdElements = folderId.split(":");
547 if (folderIdElements.length > 1)
548 unsubscribeFromFolder(folderId, onFolderUnsubscribeCB, folderId);
550 var abId = folderIdElements[0].substr(1);
551 deletePersonalAddressBook(abId);
552 var personal = $("/personal");
554 onFolderSelectionChange();
558 preventDefault(event);
561 function deletePersonalAddressBook(folderId) {
563 = labels["Are you sure you want to delete the selected address book?"];
564 if (window.confirm(label.decodeEntities())) {
565 if (document.deletePersonalABAjaxRequest) {
566 document.deletePersonalABAjaxRequest.aborted = true;
567 document.deletePersonalABAjaxRequest.abort();
569 var url = ApplicationBaseURL + "/" + folderId + "/delete";
570 document.deletePersonalABAjaxRequest
571 = triggerAjaxRequest(url, deletePersonalAddressBookCallback,
576 function deletePersonalAddressBookCallback(http) {
577 if (http.readyState == 4) {
578 if (http.status == 200) {
579 var ul = $("contactFolders");
581 var children = ul.childNodesWithTag("li");
584 while (!done && i < children.length) {
585 var currentFolderId = children[i].getAttribute("id").substr(1);
586 if (currentFolderId == http.callbackData) {
587 ul.removeChild(children[i]);
594 document.deletePersonalABAjaxRequest = null;
597 log ("ajax problem 5: " + http.status);
600 function configureDragHandles() {
601 var handle = $("dragHandle");
603 handle.addInterface(SOGoDragHandlesInterface);
604 handle.leftBlock=$("contactFoldersList");
605 handle.rightBlock=$("rightPanel");
608 handle = $("rightDragHandle");
610 handle.addInterface(SOGoDragHandlesInterface);
611 handle.upperBlock=$("contactsListContent");
612 handle.lowerBlock=$("contactView");
616 function lookupDeniedFolders() {
617 var list = $("contactFolders").childNodesWithTag("li");
618 for (var i = 0; i < list.length; i++) {
619 var folderID = list[i].getAttribute("id");
620 var url = URLForFolderID(folderID) + "/canAccessContent";
622 triggerAjaxRequest(url, deniedFoldersLookupCallback, folderID);
626 function deniedFoldersLookupCallback(http) {
627 if (http.readyState == 4) {
628 var denied = ! isHttpStatus204(http.status);
629 var entry = $(http.callbackData);
631 entry.addClassName("denied");
633 entry.removeClassName("denied");
637 function configureAbToolbar() {
638 var toolbar = $("abToolbar");
639 var links = toolbar.childNodesWithTag("a");
640 Event.observe(links[0], "click", onAddressBookNew, false);
641 Event.observe(links[1], "click", onAddressBookAdd, false);
642 Event.observe(links[2], "click", onAddressBookRemove, false);
645 function configureContactFolders() {
646 var contactFolders = $("contactFolders");
647 if (contactFolders) {
648 Event.observe(contactFolders, "mousedown", listRowMouseDownHandler);
649 Event.observe(contactFolders, "click", onFolderSelectionChange);
650 var lis = contactFolders.childNodesWithTag("li");
651 for (var i = 0; i < lis.length; i++)
652 setEventsOnContactFolder(lis[i]);
654 lookupDeniedFolders();
656 var personalFolder = $("/personal");
657 personalFolder.select();
658 openContactsFolder("/personal");
662 function setEventsOnContactFolder(node) {
663 Event.observe(node, "mousedown", listRowMouseDownHandler, false);
664 Event.observe(node, "click", onRowClick, false);
665 Event.observe(node, "contextmenu",
666 onContactFoldersContextMenu.bindAsEventListener(node), false);
669 function onMenuSharing(event) {
670 var folders = $("contactFolders");
671 var selected = folders.getSelectedNodes()[0];
672 var title = this.innerHTML;
673 var url = URLForFolderID(selected.getAttribute("id"));
675 openAclWindow(url + "/acls", title);
678 function getMenus() {
680 menus["contactFoldersMenu"] = new Array(null, "-", null,
681 null, "-", null, "-",
683 menus["contactMenu"] = new Array(onMenuEditContact, "-",
684 onMenuWriteToContact, null, "-",
685 onMenuDeleteContact);
686 menus["searchMenu"] = new Array(setSearchCriteria);
691 function configureSelectionButtons() {
692 var container = $("contactSelectionButtons");
694 var buttons = container.childNodesWithTag("input");
695 for (var i = 0; i < buttons.length; i++)
696 Event.observe(buttons[i], "click",
697 onConfirmContactSelection.bindAsEventListener(buttons[i]));
701 function initContacts(event) {
702 if (!document.body.hasClassName("popup")) {
703 configureAbToolbar();
706 configureSelectionButtons();
707 configureContactFolders();
711 addEvent(window, 'load', initContacts);