4 // JavaScript function library for WebCit.
10 var room_is_trash = 0;
12 var currentlyExpandedFloor = null;
15 var _switchToRoomList = "switch to room list";
16 var _switchToMenu = "switch to menu";
18 var currentDropTarget = null;
20 var supportsAddEventListener = (!!document.addEventListener);
21 var today = new Date();
23 if (document.all) {browserType = "ie"}
24 if (window.navigator.userAgent.toLowerCase().match("gecko")) {
27 var ns6=document.getElementById&&!document.all;
28 Event.observe(window, 'load', ToggleTaskDateOrNoDateActivate);
29 Event.observe(window, 'load', taskViewActivate);
30 Event.observe(window, 'load', fixbanner);
31 //document.observe("dom:loaded", setupPrefEngine);
32 document.observe("dom:loaded", setupIconBar);
33 document.observe('dom:loaded', function() { if (!!document.getElementById("ib_chat_launch")) { $('ib_chat_launch').observe('click', launchChat); } });
34 function CtdlRandomString() {
35 return((Math.random()+'').substr(3));
38 function emptyElement(element) {
39 childNodes = element.childNodes;
40 for(var i=0; i<childNodes.length; i++) {
41 element.removeChild(childNodes[i]);
44 /** Implements superior internet explorer 'extract all child text from element' feature'. Falls back on buggy, patent violating standardized method */
45 function getTextContent(element) {
46 if (element.textContent == undefined) {
47 return element.innerText;
49 return element.textContent;
51 /** Same reasons as above */
52 function setTextContent(element, textContent) {
53 if(element.textContent == undefined) {
54 element.innerText = textContent;
56 element.textContent = textContent;
60 // We love string tokenizers.
61 function extract_token(source_string, token_num, delimiter) {
63 var extracted_string = source_string;
66 for (i=0; i<token_num; ++i) {
67 var j = extracted_string.indexOf(delimiter);
69 extracted_string = extracted_string.substr(j+1);
74 j = extracted_string.indexOf(delimiter);
76 extracted_string = extracted_string.substr(0, j);
79 return extracted_string;
82 function CtdlSpawnContextMenu(event, source) {
83 // remove any existing menus
84 disintergrateContextMenus(null);
85 var x = event.clientX-10; // cut a few pixels out so our mouseout works right
86 var y = event.clientY-10;
87 var contextDIV = document.createElement("div");
88 contextDIV.setAttribute("id", "ctdlContextMenu");
89 document.body.appendChild(contextDIV);
90 var sourceChildren = source.childNodes;
91 for(var j=0; j<sourceChildren.length; j++) {
92 contextDIV.appendChild(sourceChildren[j].cloneNode(true));
94 var leftRule = "left: "+x+"px;";
95 contextDIV.setAttribute("style", leftRule);
96 contextDIV.setAttribute("actual", leftRule);
97 contextDIV.style.top = y+"px";
98 contextDIV.style.display = "block";
99 $(contextDIV).observe('mouseout',disintergrateContextMenus);
101 function disintergrateContextMenus(event) {
102 var contextMenu = document.getElementById("ctdlContextMenu");
104 contextMenu.parentNode.removeChild(contextMenu);
106 Event.stopObserving(document,'click',disintergrateContextMenus);
108 // This code handles the popups for important-messages.
109 function hide_imsg_popup() {
110 if (browserType == "gecko") {
111 document.poppedLayer = eval('document.getElementById(\'important_message\')');
113 else if (browserType == "ie") {
114 document.poppedLayer = eval('document.all[\'important_message\']');
117 document.poppedLayer = eval('document.layers[\'`important_message\']');
120 document.poppedLayer.style.visibility = "hidden";
124 // This function activates the ajax-powered recipient autocompleters on the message entry screen.
125 function activate_entmsg_autocompleters() {
126 new Ajax.Autocompleter('cc_id', 'cc_name_choices', 'cc_autocomplete', {} );
127 new Ajax.Autocompleter('bcc_id', 'bcc_name_choices', 'bcc_autocomplete', {} );
128 new Ajax.Autocompleter('recp_id', 'recp_name_choices', 'recp_autocomplete', {} );
131 function setupIconBar() {
132 if (!document.getElementById("switch")) {
135 _switchToRoomList = getTextContent(document.getElementById("rmlist_template"));
136 _switchToMenu = getTextContent(document.getElementById("mnlist_template"));
137 var switchSpan = document.getElementById("switch").firstChild;
138 if (switchSpan != null) {
139 setTextContent(switchSpan, _switchToRoomList);
140 $(switchSpan).observe('click', changeIconBarEvent);
141 var currentView = ctdlLocalPrefs.readPref("iconbar_view");
142 if (currentView != null) {
143 switchSpan.ctdlSwitchIconBarTo = currentView;
144 changeIconBar(switchSpan);
146 switchSpan.ctdlSwitchIconBarTo = "rooms";
149 var online_users = document.getElementById("online_users");
150 if (online_users.offsetParent != null) {
151 new Ajax.PeriodicalUpdater('online_users', 'do_template?template=wholist_section', {method: 'get', frequency: 30});
154 function changeIconBarEvent(event) {
155 changeIconBar(event.target);
157 function changeIconBar(target) {
158 var switchTo = target.ctdlSwitchIconBarTo;
159 WCLog("Changing to: " + switchTo);
160 ctdlLocalPrefs.setPref("iconbar_view", target.ctdlSwitchIconBarTo);
161 if (switchTo == "rooms") {
162 switch_to_room_list();
163 setTextContent(target, _switchToMenu);
164 target.ctdlSwitchIconBarTo = "menu";
166 switch_to_menu_buttons();
167 setTextContent(target, _switchToRoomList);
168 target.ctdlSwitchIconBarTo = "rooms";
171 function switch_to_room_list() {
172 var roomlist = document.getElementById("roomlist");
173 var summary = document.getElementById("iconbar_menu");
174 if (!rooms || !floors || !roomlist) {
175 FillRooms(IconBarRoomList);
177 roomlist.className = roomlist.className.replace("hidden","");
178 summary.className += " hidden";
181 function switch_to_menu_buttons() {
182 if (roomlist != null) {
183 roomlist.className += "hidden";
185 var iconbar = document.getElementById("iconbar_menu");
186 iconbar.className = iconbar.className.replace("hidden","");
187 var roomlist = document.getElementById("roomlist");
188 roomlist.className += " hidden";
190 function IconBarRoomList() {
191 var currentExpanded = ctdlLocalPrefs.readPref("rooms_expanded");
192 currentDropTargets = new Array();
193 var iconbar = document.getElementById("iconbar");
194 roomlist = document.getElementById("roomlist");
195 var ul = document.createElement("ul");
196 roomlist.appendChild(ul);
197 // Add mailbox, because they are special
198 var mailboxLI = document.createElement("li");
199 ul.appendChild(mailboxLI);
200 var mailboxSPAN = document.createElement("span");
201 var _mailbox = getTextContent(document.getElementById("mbox_template"));
202 mailboxSPAN.appendChild(document.createTextNode(_mailbox));
203 $(mailboxSPAN).observe('click', expandFloorEvent);
204 mailboxLI.appendChild(mailboxSPAN);
205 mailboxLI.setAttribute("class", "floor");
206 var mailboxUL = document.createElement("ul");
207 mailboxLI.appendChild(mailboxUL);
208 var mailboxRooms = GetMailboxRooms();
209 for(var i=0; i<mailboxRooms.length; i++) {
210 var room = mailboxRooms[i];
211 currentDropTargets.push(addRoomToList(mailboxUL, room));
213 if (currentExpanded != null && currentExpanded == "Mailbox") {
214 expandFloor(mailboxSPAN);
216 for(var a=0; a<floors.length; a++) {
217 var floor = floors[a];
218 var floornum = floor[0];
220 var floorLI = document.createElement("li");
221 ul.appendChild(floorLI);
222 var floorSPAN = document.createElement("span");
223 floorSPAN.appendChild(document.createTextNode(name));
224 $(floorSPAN).observe('click', expandFloorEvent);
225 floorLI.appendChild(floorSPAN);
226 floorLI.setAttribute("class", "floor");
227 var floorUL = document.createElement("ul");
228 floorLI.appendChild(floorUL);
229 var roomsForFloor = GetRoomsByFloorNum(floornum);
230 for(var b=0; b<roomsForFloor.length; b++) {
231 var room = roomsForFloor[b];
232 currentDropTargets.push(addRoomToList(floorUL, room));
234 if (currentExpanded != null && currentExpanded == name) {
235 expandFloor(floorSPAN);
240 function addRoomToList(floorUL,room) {
241 var roomName = room[RN_ROOM_NAME];
242 var flag = room[RN_ROOM_FLAG];
243 var curView = room[RN_CUR_VIEW];
244 var view = room[RN_DEF_VIEW];
245 var isMailBox = ((flag & QR_MAILBOX) == QR_MAILBOX);
246 var hasNewMsgs = ((curView & UA_HASNEWMSGS) == UA_HASNEWMSGS);
247 var roomLI = document.createElement("li");
248 var roomA = document.createElement("a");
249 roomA.setAttribute("href","dotgoto?room="+roomName);
250 roomA.appendChild(document.createTextNode(roomName));
251 roomLI.appendChild(roomA);
252 floorUL.appendChild(roomLI);
253 var className = "room ";
254 if (view == VIEW_MAILBOX) {
255 className += "room-private"
256 } else if (view == VIEW_ADDRESSBOOK) {
257 className += "room-addr";
258 } else if (view == VIEW_CALENDAR || view == VIEW_CALBRIEF) {
259 className += "room-cal";
260 } else if (view == VIEW_TASKS) {
261 className += "room-tasks";
262 } else if (view == VIEW_NOTES) {
263 className += "room-notes";
265 className += "room-chat";
268 className += " room-newmsgs";
270 roomLI.setAttribute("class", className);
271 roomA.dropTarget = true;
272 roomA.dropHandler = roomListDropHandler;
276 function roomListDropHandler(target, dropped) {
277 if (dropped.ctdlMsgId) {
278 var room = getTextContent(target);
280 for(msgId in currentlyMarkedRows) { //defined in summaryview.js
283 var mvCommand = "g_cmd=MOVE " + msgIds + "|"+room+"|0";
284 new Ajax.Request('ajax_servcmd', {
286 parameters: mvCommand,
287 onComplete: deleteAllMarkedRows()});
290 function expandFloorEvent(event) {
291 expandFloor(event.target);
293 function expandFloor(target) {
294 if (target.nodeName.toLowerCase() != "span") {
295 return; // ignore clicks on child UL
297 ctdlLocalPrefs.setPref("rooms_expanded", target.firstChild.nodeValue);
298 var parentUL = target.parentNode;
299 if (currentlyExpandedFloor != null) {
300 currentlyExpandedFloor.className = currentlyExpandedFloor.className.replace("floor-expanded","");
302 parentUL.className = parentUL.className + " floor-expanded";
303 currentlyExpandedFloor = parentUL;
306 // These functions handle moving sticky notes around the screen by dragging them
308 var uid_of_note_being_dragged = 0;
309 var saved_cursor_style = 'default';
310 var note_was_dragged = 0;
312 function NotesDragMouseUp(evt) {
313 document.onmouseup = null;
314 document.onmousemove = null;
315 if (document.layers) {
316 document.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
319 d = $('note-' + uid_of_note_being_dragged);
320 d.style.cursor = saved_cursor_style;
322 // If any motion actually occurred, submit an ajax http call to record it to the server
323 if (note_was_dragged > 0) {
324 p = 'note_uid=' + uid_of_note_being_dragged
325 + '&left=' + d.style.left
326 + '&top=' + d.style.top
327 + '&r=' + CtdlRandomString();
337 uid_of_note_being_dragged = '';
341 function NotesDragMouseMove(evt) {
342 x = (ns6 ? evt.clientX : event.clientX);
343 x_increment = x - saved_x;
344 y = (ns6 ? evt.clientY : event.clientY);
345 y_increment = y - saved_y;
348 d = $('note-' + uid_of_note_being_dragged);
350 divTop = parseInt(d.style.top);
351 divLeft = parseInt(d.style.left);
353 d.style.top = (divTop + y_increment) + 'px';
354 d.style.left = (divLeft + x_increment) + 'px';
358 note_was_dragged = 1;
363 function NotesDragMouseDown(evt, uid) {
364 saved_x = (ns6 ? evt.clientX : event.clientX);
365 saved_y = (ns6 ? evt.clientY : event.clientY);
366 document.onmouseup = NotesDragMouseUp;
367 document.onmousemove = NotesDragMouseMove;
368 if (document.layers) {
369 document.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
371 uid_of_note_being_dragged = uid;
372 d = $('note-' + uid_of_note_being_dragged);
373 saved_cursor_style = d.style.cursor;
374 d.style.cursor = 'move';
375 return false; // disable the default action
379 // Called when the user clicks on the palette icon of a sticky note to change its color.
380 // It toggles the color selector visible or invisible.
382 function NotesClickPalette(evt, uid) {
383 uid_of_note_being_colored = uid;
384 d = $('palette-' + uid_of_note_being_colored);
386 if (d.style.display) {
387 if (d.style.display == 'none') {
388 d.style.display = 'block';
391 d.style.display = 'none';
395 d.style.display = 'block';
402 // Called when the user clicks on one of the colors in an open color selector.
403 // Sets the desired color and then closes the color selector.
405 function NotesClickColor(evt, uid, red, green, blue, notecolor, titlecolor) {
406 uid_of_note_being_colored = uid;
407 palette_button = $('palette-' + uid_of_note_being_colored);
408 note_div = $('note-' + uid_of_note_being_colored);
409 titlebar_div = $('titlebar-' + uid_of_note_being_colored);
411 // alert('FIXME red=' + red + ' green=' + green + ' blue=' + blue);
413 note_div.style.backgroundColor = notecolor;
414 titlebar_div.style.backgroundColor = titlecolor;
415 palette_button.style.display = 'none';
417 // submit an ajax http call to record it to the server
418 p = 'note_uid=' + uid_of_note_being_colored
422 + '&r=' + CtdlRandomString();
435 // These functions handle resizing sticky notes by dragging the resize handle
437 var uid_of_note_being_resized = 0;
438 var saved_cursor_style = 'default';
439 var note_was_resized = 0;
441 function NotesResizeMouseUp(evt) {
442 document.onmouseup = null;
443 document.onmousemove = null;
444 if (document.layers) {
445 document.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
448 d = $('note-' + uid_of_note_being_resized);
449 d.style.cursor = saved_cursor_style;
451 // If any motion actually occurred, submit an ajax http call to record it to the server
452 if (note_was_resized > 0) {
453 p = 'note_uid=' + uid_of_note_being_resized
454 + '&width=' + d.style.width
455 + '&height=' + d.style.height
456 + '&r=' + CtdlRandomString();
466 uid_of_note_being_resized = '';
467 return false; // disable the default action
470 function NotesResizeMouseMove(evt) {
471 x = (ns6 ? evt.clientX : event.clientX);
472 x_increment = x - saved_x;
473 y = (ns6 ? evt.clientY : event.clientY);
474 y_increment = y - saved_y;
477 d = $('note-' + uid_of_note_being_resized);
479 divTop = parseInt(d.style.height);
480 divLeft = parseInt(d.style.width);
482 d.style.height = (divTop + y_increment) + 'px';
483 d.style.width = (divLeft + x_increment) + 'px';
487 note_was_resized = 1;
488 return false; // disable the default action
492 function NotesResizeMouseDown(evt, uid) {
493 saved_x = (ns6 ? evt.clientX : event.clientX);
494 saved_y = (ns6 ? evt.clientY : event.clientY);
495 document.onmouseup = NotesResizeMouseUp;
496 document.onmousemove = NotesResizeMouseMove;
497 if (document.layers) {
498 document.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
500 uid_of_note_being_resized = uid;
501 d = $('note-' + uid_of_note_being_resized);
502 saved_cursor_style = d.style.cursor;
503 d.style.cursor = 'move';
504 return false; // disable the default action
508 function DeleteStickyNote(evt, uid, confirmation_prompt) {
509 uid_of_note_being_deleted = uid;
510 d = $('note-' + uid_of_note_being_deleted);
512 if (confirm(confirmation_prompt)) {
515 // submit an ajax http call to delete it on the server
516 p = 'note_uid=' + uid_of_note_being_deleted
518 + '&r=' + CtdlRandomString();
529 function ctdl_ts_getInnerText(el) {
530 if (typeof el == "string") return el;
531 if (typeof el == "undefined") { return el };
532 if (el.innerText) return el.innerText; //Not needed but it is faster
535 var cs = el.childNodes;
537 for (var i = 0; i < l; i++) {
538 switch (cs[i].nodeType) {
539 case 1: //ELEMENT_NODE
540 str += ts_getInnerText(cs[i]);
543 str += cs[i].nodeValue;
551 // Place a gradient loadscreen on an element, e.g to use before Ajax.updater
552 function CtdlLoadScreen(elementid) {
553 var elem = document.getElementById(elementid);
554 elem.innerHTML = "<div align=center><br><table border=0 cellpadding=10 bgcolor=\"#ffffff\"><tr><td><img src=\"static/throbber.gif\" /><font color=\"#AAAAAA\"> Loading....</font></td></tr></table><br /></div>";
559 // Pop open the address book (target_input is the INPUT field to populate)
561 function PopOpenAddressBook(target_input) {
562 $('address_book_popup').style.display = 'block';
563 p = 'target_input=' + target_input + '&r=' + CtdlRandomString();
565 'address_book_popup_middle_div',
566 'display_address_book_middle_div',
575 function PopulateAddressBookInnerDiv(which_addr_book, target_input) {
576 $('address_book_inner_div').innerHTML = "<div align=center><br><table border=0 cellpadding=10 bgcolor=\"#ffffff\"><tr><td><img src=\"static/throbber.gif\" /><font color=\"#AAAAAA\"> Loading....</font></td></tr></table><br /></div>";
577 p = 'which_addr_book=' + which_addr_book
578 + '&target_input=' + target_input
579 + '&r=' + CtdlRandomString();
581 'address_book_inner_div',
582 'display_address_book_inner_div',
590 // What happens when a contact is selected from the address book popup
591 // (populate the specified target)
593 function AddContactsToTarget(target, whichaddr) {
594 while (whichaddr.selectedIndex != -1) {
595 if (target.value.length > 0) {
596 target.value = target.value + ', ';
598 target.value = target.value + whichaddr.value;
599 whichaddr.options[whichaddr.selectedIndex].selected = false;
603 // Respond to a meeting invitation
604 function RespondToInvitation(question_divname, title_divname, msgnum, cal_partnum, sc) {
605 p = 'msgnum=' + msgnum + '&cal_partnum=' + cal_partnum + '&sc=' + sc ;
606 new Ajax.Updater(title_divname, 'respond_to_request', { method: 'post', parameters: p } );
607 Effect.Fade(question_divname, { duration: 0.5 });
610 // Handle a received RSVP
611 function HandleRSVP(question_divname, title_divname, msgnum, cal_partnum, sc) {
612 p = 'msgnum=' + msgnum + '&cal_partnum=' + cal_partnum + '&sc=' + sc ;
613 new Ajax.Updater(title_divname, 'handle_rsvp', { method: 'post', parameters: p } );
614 Effect.Fade(question_divname, { duration: 0.5 });
616 /* var fakeMouse = document.createEvent("MouseEvents");
617 fakeMouse.initMouseEvent("click", true, true, window,
618 0,0,0,0,0, false, false, false, false, 0, null); */
619 // TODO: Collapse into one function
620 function toggleTaskDtStart(event) {
621 var checkBox = $('nodtstart');
622 dtStart = document.getElementById("dtstart");
623 if (checkBox.checked) {
624 dtStart.disabled = true;
625 dtStart.style.textDecoration = "line-through";
627 dtStart.disabled = false;
628 dtStart.style.textDecoration = "";
629 if (dtStart.value.length == 0)
630 dtStart.dpck._initCurrentDate();
633 function toggleTaskDue(event) {
634 var checkBox = $('nodue');
635 dueField = document.getElementById("due");
636 if (checkBox.checked) {
637 dueField.disabled = true;
638 dueField.style.textDecoration = "line-through";
640 dueField.disabled = false;
641 dueField.style.textDecoration = "";
642 if (dueField.value.length == 0)
643 dueField.dpck._initCurrentDate();
646 function ToggleTaskDateOrNoDateActivate(event) {
647 var dtstart = document.getElementById("nodtstart");
648 if (dtstart != null) {
649 toggleTaskDtStart(null);
651 $('nodtstart').observe('click', toggleTaskDtStart);
652 $('nodue').observe('click', toggleTaskDue);
655 function TaskViewGatherCategoriesFromTable() {
656 var table = $('taskview');
659 function attachDatePicker(relative) {
660 var dpck = new DatePicker({
662 language: 'en', //wclang.substr(0,2),
663 disableFutureDate: false,
664 dateFormat: [ ["yyyy", "mm", "dd"], "-"],
667 document.getElementById(relative).dpck = dpck; // attach a ref to it
669 function eventEditAllDay() {
670 var allDayCheck = document.getElementById("alldayevent");
671 var dtend= document.getElementById("dtendcell");
672 if(allDayCheck.checked) {
673 //dtend.disabled = true;
674 dtend.style.textDecoration = "line-through";
676 //dtend_day.disabled = false;
677 dtend.style.textDecoration = "";
681 // Functions which handle show/hide of various elements in the recurrence editor
683 function RecurrenceShowHide() {
685 if ($('is_recur').checked) {
686 $('rrule_div').style.display = 'block';
689 $('rrule_div').style.display = 'none';
692 if ($('freq_selector').selectedIndex == 4) {
693 $('weekday_selector').style.display = 'block';
696 $('weekday_selector').style.display = 'none';
699 if ($('freq_selector').selectedIndex == 5) {
700 $('monthday_selector').style.display = 'block';
703 $('monthday_selector').style.display = 'none';
706 if ($('rrend_count').checked) {
707 $('rrcount').disabled = false;
710 $('rrcount').disabled = true;
713 if ($('rrend_until').checked) {
714 $('rruntil').disabled = false;
717 $('rruntil').disabled = true;
720 if ($('rrmonthtype_mday').checked) {
721 $('rrmday').disabled = false;
724 $('rrmday').disabled = true;
727 if ($('rrmonthtype_wday').checked) {
728 $('rrmweek').disabled = false;
729 $('rrmweekday').disabled = false;
732 $('rrmweek').disabled = true;
733 $('rrmweekday').disabled = true;
736 if ($('freq_selector').selectedIndex == 6) {
737 $('yearday_selector').style.display = 'block';
740 $('yearday_selector').style.display = 'none';
743 $('ymday').innerHTML = 'XXXX-' + $('dtstart').value.substr(5);
744 $('rrmday').innerHTML = $('dtstart').value.substr(8);
746 if ($('rryeartype_ywday').checked) {
747 $('rrymweek').disabled = false;
748 $('rrymweekday').disabled = false;
749 $('rrymonth').disabled = false;
752 $('rrymweek').disabled = true;
753 $('rrymweekday').disabled = true;
754 $('rrymonth').disabled = true;
758 function launchChat(event) {
759 window.open('chat', 'ctdl_chat_window', 'toolbar=no,location=no,directories=no,copyhistory=no,status=no,scrollbars=yes,resizable=yes');
762 function WCLog(msg) {
763 if (!!window.console && !!console.log) {
765 } else if (!!window.opera && !!opera.postError) {
766 opera.postError(msg);
770 function fixMissingCSSTable(elems) {
771 if (elems[0] == null || elems[1] == null) {
774 if (elems[0].getStyle("display") != "table-cell") {
775 var parentNode = elems[0].parentNode;
776 var table = document.createElement("table");
777 table.style.width = "100%";
778 var tbody = document.createElement("tbody");
779 table.appendChild(tbody);
780 var tr = document.createElement("tr");
781 tbody.appendChild(tr);
782 parentNode.appendChild(table);
783 for(var i=0; i<elems.length; i++) {
784 parentNode.removeChild(elems[i]);
785 var td = document.createElement("td");
786 td.appendChild(elems[i]);
791 function fixbanner() {
792 var elems = [$('room_banner'),$('actiondiv')];
793 fixMissingCSSTable(elems);