1 /* JavaScript for SOGoContacts */
3 var cachedContacts = new Array();
4 var currentContactFolder = '/personal';
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, params) {
34 if (contactsFolder != currentContactFolder || params) {
35 if (contactsFolder == currentContactFolder) {
36 var contactsList = $("contactsList");
38 selection = contactsList.getSelectedRowsId();
40 window.alert("no contactsList");
45 currentContactFolder = contactsFolder;
46 var url = URLForFolderID(currentContactFolder) +
47 "/view?noframe=1&sort=cn&desc=0";
52 if (document.contactsListAjaxRequest) {
53 document.contactsListAjaxRequest.aborted = true;
54 document.contactsListAjaxRequest.abort();
56 document.contactsListAjaxRequest
57 = triggerAjaxRequest(url, contactsListCallback, selection);
61 function openContactsFolderAtIndex(element) {
62 var idx = element.getAttribute("idx");
63 var url = URLForFolderID(currentContactFolder) + "/view?noframe=1&idx=" + idx;
65 if (document.contactsListAjaxRequest) {
66 document.contactsListAjaxRequest.aborted = true;
67 document.contactsListAjaxRequest.abort();
69 document.contactsListAjaxRequest
70 = triggerAjaxRequest(url, contactsListCallback);
73 function contactsListCallback(http) {
74 var div = $("contactsListContent");
76 if (http.readyState == 4
77 && http.status == 200) {
78 document.contactsListAjaxRequest = null;
79 div.innerHTML = http.responseText;
80 var selected = http.callbackData;
82 for (var i = 0; i < selected.length; i++)
83 $(selected[i]).select();
85 configureSortableTableHeaders();
88 log ("ajax fuckage 1");
91 function onContactFoldersContextMenu(event) {
92 var menu = $("contactFoldersMenu");
93 Event.observe(menu, "hideMenu", onContactFoldersContextMenuHide, false);
94 popupMenu(event, "contactFoldersMenu", this);
96 var topNode = $("contactFolders");
97 var selectedNodes = topNode.getSelectedRows();
98 topNode.menuSelectedRows = selectedNodes;
99 for (var i = 0; i < selectedNodes.length; i++)
100 selectedNodes[i].deselect();
101 topNode.menuSelectedEntry = this;
105 function onContactContextMenu(event, element) {
106 var menu = $("contactMenu");
107 Event.observe(menu, "hideMenu", onContactContextMenuHide, false);
108 popupMenu(event, "contactMenu", element);
110 var topNode = $("contactsList");
111 var selectedNodes = topNode.getSelectedRows();
112 topNode.menuSelectedRows = selectedNodes;
113 for (var i = 0; i < selectedNodes.length; i++)
114 selectedNodes[i].deselect();
115 topNode.menuSelectedEntry = element;
119 function onContactContextMenuHide(event) {
120 var topNode = $("contactsList");
122 if (topNode.menuSelectedEntry) {
123 topNode.menuSelectedEntry.deselect();
124 topNode.menuSelectedEntry = null;
126 if (topNode.menuSelectedRows) {
127 var nodes = topNode.menuSelectedRows;
128 for (var i = 0; i < nodes.length; i++)
130 topNode.menuSelectedRows = null;
134 function onContactFoldersContextMenuHide(event) {
135 var topNode = $("contactFolders");
137 if (topNode.menuSelectedEntry) {
138 topNode.menuSelectedEntry.deselect();
139 topNode.menuSelectedEntry = null;
141 if (topNode.menuSelectedRows) {
142 var nodes = topNode.menuSelectedRows;
143 for (var i = 0; i < nodes.length; i++)
145 topNode.menuSelectedRows = null;
149 function onFolderMenuHide(event) {
150 var topNode = $('d');
152 if (topNode.menuSelectedEntry) {
153 topNode.menuSelectedEntry.deselect();
154 topNode.menuSelectedEntry = null;
156 if (topNode.selectedEntry)
157 topNode.selectedEntry.select();
160 function loadContact(idx) {
161 if (document.contactAjaxRequest) {
162 document.contactAjaxRequest.aborted = true;
163 document.contactAjaxRequest.abort();
166 if (cachedContacts[currentContactFolder + "/" + idx]) {
167 var div = $('contactView');
168 div.innerHTML = cachedContacts[currentContactFolder + "/" + idx];
171 var url = (URLForFolderID(currentContactFolder)
172 + "/" + idx + "/view?noframe=1");
173 document.contactAjaxRequest
174 = triggerAjaxRequest(url, contactLoadCallback, idx);
178 function contactLoadCallback(http) {
179 var div = $('contactView');
181 if (http.readyState == 4
182 && http.status == 200) {
183 document.contactAjaxRequest = null;
184 var content = http.responseText;
185 cachedContacts[currentContactFolder + "/" + http.callbackData] = content;
186 div.innerHTML = content;
189 log ("ajax fuckage 2: " + http.status);
192 var rowSelectionCount = 0;
196 function showElement(e, shouldShow) {
197 e.style.display = shouldShow ? "" : "none";
200 function enableElement(e, shouldEnable) {
204 if(e.hasAttribute("disabled"))
205 e.removeAttribute("disabled");
208 e.setAttribute("disabled", "1");
212 function validateControls() {
214 this.enableElement(e, rowSelectionCount > 0);
217 function moveTo(uri) {
218 alert("MoveTo: " + uri);
221 /* contact menu entries */
222 function onContactRowClick(event, node) {
223 loadContact(node.getAttribute('id'));
225 return onRowClick(event);
228 function onContactRowDblClick(event, node) {
229 var contactId = node.getAttribute('id');
231 openContactWindow(null,
232 URLForFolderID(currentContactFolder)
233 + "/" + contactId + "/edit");
238 function onMenuEditContact(event) {
239 var contactId = document.menuTarget.getAttribute('id');
241 openContactWindow(null,
242 URLForFolderID(currentContactFolder)
243 + "/" + contactId + "/edit");
246 function onMenuWriteToContact(event) {
247 var contactId = document.menuTarget.getAttribute('id');
249 openMailComposeWindow(ApplicationBaseURL + currentContactFolder
250 + "/" + contactId + "/write");
253 function onMenuDeleteContact(event) {
254 uixDeleteSelectedContacts(this);
257 function onToolbarEditSelectedContacts(event) {
258 var contactsList = $('contactsList');
259 var rows = contactsList.getSelectedRowsId();
261 for (var i = 0; i < rows.length; i++) {
262 openContactWindow(null,
263 URLForFolderID(currentContactFolder)
264 + "/" + rows[i] + "/edit");
270 function onToolbarWriteToSelectedContacts(event) {
271 var contactsList = $('contactsList');
272 var rows = contactsList.getSelectedRowsId();
274 for (var i = 0; i < rows.length; i++)
275 openMailComposeWindow(ApplicationBaseURL + currentContactFolder
276 + "/" + rows[i] + "/write");
281 function uixDeleteSelectedContacts(sender) {
283 var contactsList = $('contactsList');
284 var rows = contactsList.getSelectedRowsId();
286 var contactView = $('contactView');
287 contactView.innerHTML = '';
289 for (var i = 0; i < rows.length; i++) {
290 var url, http, rowElem;
292 /* send AJAX request (synchronously) */
294 url = (URLForFolderID(currentContactFolder) + "/"
295 + rows[i] + "/delete");
296 http = createHTTPClient();
297 http.open("POST", url, false /* not async */);
299 if (http.status != 200) { /* request failed */
306 /* remove from page */
308 /* line-through would be nicer, but hiding is OK too */
309 rowElem = $(rows[i]);
310 rowElem.parentNode.removeChild(rowElem);
314 alert("Could not delete the selected contacts!");
319 function newEmailTo(sender) {
320 var mailto = sanitizeMailTo(sender.parentNode.parentNode.menuTarget.innerHTML);
322 if (mailto.length > 0)
324 w = window.open("compose?mailto=" + mailto,
326 "width=680,height=520,resizable=1,scrollbars=1,toolbar=0," +
327 "location=0,directories=0,status=0,menubar=0,copyhistory=0");
331 return false; /* stop following the link */
334 function onHeaderClick(event) {
335 if (document.contactsListAjaxRequest) {
336 document.contactsListAjaxRequest.aborted = true;
337 document.contactsListAjaxRequest.abort();
339 url = URLForFolderID(currentContactFolder) + "/" + this.link;
340 if (!this.link.match(/noframe=/))
342 document.contactsListAjaxRequest
343 = triggerAjaxRequest(url, contactsListCallback);
345 event.preventDefault();
348 function registerDraggableMessageNodes() {
349 log ("can we drag...");
352 function newContact(sender) {
353 openContactWindow(sender,
354 URLForFolderID(currentContactFolder) + "/new");
356 return false; /* stop following the link */
359 function onFolderSelectionChange() {
360 var folderList = $("contactFolders");
361 var nodes = folderList.getSelectedNodes();
362 $("contactView").innerHTML = '';
364 if (nodes[0].hasClassName("denied")) {
365 var div = $("contactsListContent");
369 openContactsFolder(nodes[0].getAttribute("id"), null);
372 function onSearchFormSubmit() {
373 var searchValue = $("searchValue");
375 openContactsFolder(currentContactFolder,
376 "search=" + searchValue.value);
381 function onConfirmContactSelection(event) {
382 var tag = this.getAttribute("name");
383 var folderLi = $(currentContactFolder);
384 var currentContactFolderName = folderLi.innerHTML;
385 var selectorList = null;
386 var initialValues = null;
389 var selectorId = selector.getAttribute("id");
390 selectorList = opener.window.document.getElementById('uixselector-'
393 initialValues = selectorList.value;
396 var contactsList = $("contactsList");
397 var rows = contactsList.getSelectedRows();
398 for (i = 0; i < rows.length; i++) {
399 var cid = rows[i].getAttribute("contactid");
400 var cname = '' + rows[i].getAttribute("contactname");
401 var email = '' + rows[i].cells[1].innerHTML;
402 opener.window.addContact(tag, currentContactFolderName + '/' + cname,
406 if (selector && selector.changeNotification
407 && selectorList.value != initialValues)
408 selector.changeNotification("addition");
410 event.preventDefault();
413 function onContactMailTo(node) {
414 return openMailTo(node.innerHTML);
417 function refreshContacts(contactId) {
418 openContactsFolder(currentContactFolder, "reload=true");
419 cachedContacts[currentContactFolder + "/" + contactId] = null;
420 loadContact(contactId);
425 function onAddressBookNew(event) {
426 var name = window.prompt(labels["Name of the Address Book"].decodeEntities());
428 if (document.newAbAjaxRequest) {
429 document.newAbAjaxRequest.aborted = true;
430 document.newAbAjaxRequest.abort();
432 var url = ApplicationBaseURL + "/newAb?name=" + name;
433 document.newAbAjaxRequest
434 = triggerAjaxRequest(url, newAbCallback, name);
436 event.preventDefault();
439 function appendAddressBook(name, folder) {
440 var li = document.createElement("li");
441 li.setAttribute("id", folder);
442 li.appendChild(document.createTextNode(name));
443 setEventsOnContactFolder(li);
444 $("contactFolders").appendChild(li);
447 function newAbCallback(http) {
448 if (http.readyState == 4
449 && http.status == 201) {
450 var name = http.callbackData;
451 appendAddressBook(name, "/" + name);
454 log ("ajax fuckage 4:" + http.status);
457 function newUserFolderCallback(folderData) {
458 var folder = $(folderData["folder"]);
460 appendAddressBook(folderData["folderName"], folderData["folder"]);
463 function onAddressBookAdd(event) {
464 openUserFolderSelector(newUserFolderCallback, "contact");
466 event.preventDefault();
469 function onFolderUnsubscribeCB(folderId) {
470 var node = $(folderId);
471 node.parentNode.removeChild(node);
472 var personal = $("/personal");
474 onFolderSelectionChange();
477 function onAddressBookRemove(event) {
478 var selector = $("contactFolders");
479 var nodes = selector.getSelectedNodes();
480 if (nodes.length > 0) {
482 var folderId = nodes[0].getAttribute("id");
483 var folderIdElements = folderId.split(":");
484 if (folderIdElements.length > 1)
485 unsubscribeFromFolder(folderId, onFolderUnsubscribeCB, folderId);
487 var abId = folderIdElements[0].substr(1);
488 deletePersonalAddressBook(abId);
489 var personal = $("/personal");
491 onFolderSelectionChange();
495 event.preventDefault();
498 function deletePersonalAddressBook(folderId) {
500 = labels["Are you sure you want to delete the selected address book?"];
501 if (window.confirm(label.decodeEntities())) {
502 if (document.deletePersonalABAjaxRequest) {
503 document.deletePersonalABAjaxRequest.aborted = true;
504 document.deletePersonalABAjaxRequest.abort();
506 var url = ApplicationBaseURL + "/" + folderId + "/delete";
507 document.deletePersonalABAjaxRequest
508 = triggerAjaxRequest(url, deletePersonalAddressBookCallback,
513 function deletePersonalAddressBookCallback(http) {
514 if (http.readyState == 4) {
515 if (http.status == 200) {
516 var ul = $("contactFolders");
518 var children = ul.childNodesWithTag("li");
521 while (!done && i < children.length) {
522 var currentFolderId = children[i].getAttribute("id").substr(1);
523 if (currentFolderId == http.callbackData) {
524 ul.removeChild(children[i]);
531 document.deletePersonalABAjaxRequest = null;
534 log ("ajax fuckage");
537 function configureDragHandles() {
538 var handle = $("dragHandle");
540 handle.addInterface(SOGoDragHandlesInterface);
541 handle.leftBlock=$("contactFoldersList");
542 handle.rightBlock=$("rightPanel");
545 handle = $("rightDragHandle");
547 handle.addInterface(SOGoDragHandlesInterface);
548 handle.upperBlock=$("contactsListContent");
549 handle.lowerBlock=$("contactView");
553 function lookupDeniedFolders() {
554 var list = $("contactFolders").childNodesWithTag("li");
555 for (var i = 0; i < list.length; i++) {
556 var folderID = list[i].getAttribute("id");
557 var url = URLForFolderID(folderID) + "/canAccessContent";
558 triggerAjaxRequest(url, deniedFoldersLookupCallback, folderID);
562 function deniedFoldersLookupCallback(http) {
563 if (http.readyState == 4) {
564 var denied = (http.status != 204)
565 var entry = $(http.callbackData);
567 entry.addClassName("denied");
569 entry.removeClassName("denied");
573 function configureAbToolbar() {
574 var toolbar = $("abToolbar");
575 var links = toolbar.childNodesWithTag("a");
576 Event.observe(links[0], "click", onAddressBookNew, false);
577 Event.observe(links[1], "click", onAddressBookAdd, false);
578 Event.observe(links[2], "click", onAddressBookRemove, false);
581 function configureContactFolders() {
582 var contactFolders = $("contactFolders");
583 if (contactFolders) {
584 Event.observe(contactFolders, "selectionchange", onFolderSelectionChange, false);
585 var lis = contactFolders.childNodesWithTag("li");
586 for (var i = 0; i < lis.length; i++)
587 setEventsOnContactFolder(lis[i]);
589 lookupDeniedFolders();
590 contactFolders.setStyle({ visibility: 'visible' });
592 var personalFolder = $("/personal");
593 personalFolder.select();
597 function setEventsOnContactFolder(node) {
598 Event.observe(node, "mousedown", listRowMouseDownHandler, false);
599 Event.observe(node, "click", onRowClick, false);
600 Event.observe(node, "contextmenu", onContactFoldersContextMenu, false);
603 function onMenuSharing(event) {
604 var folders = $("contactFolders");
605 var selected = folders.getSelectedNodes()[0];
606 var title = this.innerHTML;
607 var url = URLForFolderID(selected.getAttribute("id"));
609 openAclWindow(url + "/acls", title);
612 function getMenus() {
614 menus["contactFoldersMenu"] = new Array(null, "-", null,
615 null, "-", null, "-",
617 menus["contactMenu"] = new Array(onMenuEditContact, "-",
618 onMenuWriteToContact, null, "-",
619 onMenuDeleteContact);
620 menus["searchMenu"] = new Array(setSearchCriteria);
625 function configureSearchField() {
626 var searchValue = $("searchValue");
628 Event.observe(searchValue, "mousedown", onSearchMouseDown.bindAsEventListener(searchValue), false);
629 Event.observe(searchValue, "click", popupSearchMenu.bindAsEventListener(searchValue), false);
630 Event.observe(searchValue, "blur", onSearchBlur.bindAsEventListener(searchValue), false);
631 Event.observe(searchValue, "focus", onSearchFocus.bindAsEventListener(searchValue), false);
632 Event.observe(searchValue, "keydown", onSearchKeyDown.bindAsEventListener(searchValue), false);
635 function configureSelectionButtons() {
636 var container = $("contactSelectionButtons");
638 var buttons = container.childNodesWithTag("input");
639 for (var i = 0; i < buttons.length; i++)
640 Event.observe(buttons[i], "click", onConfirmContactSelection.bindAsEventListener(buttons[i]), false);
645 handleEvent: function (event) {
646 if (!document.body.hasClassName("popup")) {
647 configureAbToolbar();
648 configureSearchField();
651 configureSelectionButtons();
652 configureContactFolders();
657 //window.addEventListener("load", initContacts, false);
658 Event.observe(window, "load", initContacts, false);