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 //document.observe("dom:loaded", setupPrefEngine);
31 document.observe("dom:loaded", setupIconBar);
32 function CtdlRandomString() {
33 return((Math.random()+'').substr(3));
36 function emptyElement(element) {
37 childNodes = element.childNodes;
38 for(var i=0; i<childNodes.length; i++) {
39 element.removeChild(childNodes[i]);
42 /** Implements superior internet explorer 'extract all child text from element' feature'. Falls back on buggy, patent violating standardized method */
43 function getTextContent(element) {
44 if (element.textContent == undefined) {
45 return element.innerText;
47 return element.textContent;
49 /** Same reasons as above */
50 function setTextContent(element, textContent) {
51 if(element.textContent == undefined) {
52 element.innerText = textContent;
54 element.textContent = textContent;
58 // We love string tokenizers.
59 function extract_token(source_string, token_num, delimiter) {
61 var extracted_string = source_string;
64 for (i=0; i<token_num; ++i) {
65 var j = extracted_string.indexOf(delimiter);
67 extracted_string = extracted_string.substr(j+1);
72 j = extracted_string.indexOf(delimiter);
74 extracted_string = extracted_string.substr(0, j);
77 return extracted_string;
80 function CtdlSpawnContextMenu(event, source) {
81 // remove any existing menus
82 disintergrateContextMenus(null);
83 var x = event.clientX-10; // cut a few pixels out so our mouseout works right
84 var y = event.clientY-10;
85 var contextDIV = document.createElement("div");
86 contextDIV.setAttribute("id", "ctdlContextMenu");
87 document.body.appendChild(contextDIV);
88 var sourceChildren = source.childNodes;
89 for(var j=0; j<sourceChildren.length; j++) {
90 contextDIV.appendChild(sourceChildren[j].cloneNode(true));
92 var leftRule = "left: "+x+"px;";
93 contextDIV.setAttribute("style", leftRule);
94 contextDIV.setAttribute("actual", leftRule);
95 contextDIV.style.top = y+"px";
96 contextDIV.style.display = "block";
97 $(contextDIV).observe('mouseout',disintergrateContextMenus);
99 function disintergrateContextMenus(event) {
100 var contextMenu = document.getElementById("ctdlContextMenu");
102 contextMenu.parentNode.removeChild(contextMenu);
104 Event.stopObserving(document,'click',disintergrateContextMenus);
106 // This code handles the popups for important-messages.
107 function hide_imsg_popup() {
108 if (browserType == "gecko") {
109 document.poppedLayer = eval('document.getElementById(\'important_message\')');
111 else if (browserType == "ie") {
112 document.poppedLayer = eval('document.all[\'important_message\']');
115 document.poppedLayer = eval('document.layers[\'`important_message\']');
118 document.poppedLayer.style.visibility = "hidden";
122 // This function activates the ajax-powered recipient autocompleters on the message entry screen.
123 function activate_entmsg_autocompleters() {
124 new Ajax.Autocompleter('cc_id', 'cc_name_choices', 'cc_autocomplete', {} );
125 new Ajax.Autocompleter('bcc_id', 'bcc_name_choices', 'bcc_autocomplete', {} );
126 new Ajax.Autocompleter('recp_id', 'recp_name_choices', 'recp_autocomplete', {} );
129 function setupIconBar() {
130 if (!document.getElementById("switch")) {
133 var switchSpan = document.getElementById("switch").firstChild;
134 if (switchSpan != null) {
135 setTextContent(switchSpan, _switchToRoomList);
136 $(switchSpan).observe('click', changeIconBarEvent);
137 var currentView = ctdlLocalPrefs.readPref("iconbar_view");
138 if (currentView != null) {
139 switchSpan.ctdlSwitchIconBarTo = currentView;
140 changeIconBar(switchSpan);
142 switchSpan.ctdlSwitchIconBarTo = "rooms";
145 var online_users = document.getElementById("online_users");
146 var ou_displayAs = online_users.style.display;
147 if (ou_displayAs != "none") {
148 new Ajax.PeriodicalUpdater('online_users', 'do_template?template=wholist_section', {method: 'get', frequency: 30});
151 function changeIconBarEvent(event) {
152 changeIconBar(event.target);
154 function changeIconBar(target) {
155 var switchTo = target.ctdlSwitchIconBarTo;
156 if (!!window.console) {
157 console.log("Changing to: " + switchTo);
159 ctdlLocalPrefs.setPref("iconbar_view", target.ctdlSwitchIconBarTo);
160 if (switchTo == "rooms") {
161 switch_to_room_list();
162 setTextContent(target, _switchToMenu);
163 target.ctdlSwitchIconBarTo = "menu";
165 switch_to_menu_buttons();
166 setTextContent(target, _switchToRoomList);
167 target.ctdlSwitchIconBarTo = "rooms";
170 function switch_to_room_list() {
171 var roomlist = document.getElementById("roomlist");
172 var summary = document.getElementById("iconbar_menu");
173 if (!rooms || !floors || !roomlist) {
174 FillRooms(IconBarRoomList);
176 roomlist.className = roomlist.className.replace("hidden","");
177 summary.className += " hidden";
180 function switch_to_menu_buttons() {
181 if (roomlist != null) {
182 roomlist.className += "hidden";
184 var iconbar = document.getElementById("iconbar_menu");
185 iconbar.className = iconbar.className.replace("hidden","");
186 var roomlist = document.getElementById("roomlist");
187 roomlist.className += " hidden";
189 function IconBarRoomList() {
190 var currentExpanded = ctdlLocalPrefs.readPref("rooms_expanded");
191 currentDropTargets = new Array();
192 var iconbar = document.getElementById("iconbar");
193 roomlist = document.getElementById("roomlist");
194 var ul = document.createElement("ul");
195 roomlist.appendChild(ul);
196 // Add mailbox, because they are special
197 var mailboxLI = document.createElement("li");
198 ul.appendChild(mailboxLI);
199 var mailboxSPAN = document.createElement("span");
200 mailboxSPAN.appendChild(document.createTextNode("Mailbox"));
201 $(mailboxSPAN).observe('click', expandFloorEvent);
202 mailboxLI.appendChild(mailboxSPAN);
203 mailboxLI.setAttribute("class", "floor");
204 var mailboxUL = document.createElement("ul");
205 mailboxLI.appendChild(mailboxUL);
206 var mailboxRooms = GetMailboxRooms();
207 for(var i=0; i<mailboxRooms.length; i++) {
208 var room = mailboxRooms[i];
209 currentDropTargets.push(addRoomToList(mailboxUL, room));
211 if (currentExpanded != null && currentExpanded == "Mailbox") {
212 expandFloor(mailboxSPAN);
214 for(var a=0; a<floors.length; a++) {
215 var floor = floors[a];
216 var floornum = floor[0];
218 var floorLI = document.createElement("li");
219 ul.appendChild(floorLI);
220 var floorSPAN = document.createElement("span");
221 floorSPAN.appendChild(document.createTextNode(name));
222 $(floorSPAN).observe('click', expandFloorEvent);
223 floorLI.appendChild(floorSPAN);
224 floorLI.setAttribute("class", "floor");
225 var floorUL = document.createElement("ul");
226 floorLI.appendChild(floorUL);
227 var roomsForFloor = GetRoomsByFloorNum(floornum);
228 for(var b=0; b<roomsForFloor.length; b++) {
229 var room = roomsForFloor[b];
230 currentDropTargets.push(addRoomToList(floorUL, room));
232 if (currentExpanded != null && currentExpanded == name) {
233 expandFloor(floorSPAN);
238 function addRoomToList(floorUL,room) {
239 var roomName = room[RN_ROOM_NAME];
240 var flag = room[RN_ROOM_FLAG];
241 var curView = room[RN_CUR_VIEW];
242 var view = room[RN_DEF_VIEW];
243 var isMailBox = ((flag & QR_MAILBOX) == QR_MAILBOX);
244 var hasNewMsgs = ((curView & UA_HASNEWMSGS) == UA_HASNEWMSGS);
245 var roomLI = document.createElement("li");
246 var roomA = document.createElement("a");
247 roomA.setAttribute("href","dotgoto?room="+roomName);
248 roomA.appendChild(document.createTextNode(roomName));
249 roomLI.appendChild(roomA);
250 floorUL.appendChild(roomLI);
251 var className = "room ";
252 if (view == VIEW_MAILBOX) {
253 className += "room-private"
254 } else if (view == VIEW_ADDRESSBOOK) {
255 className += "room-addr";
256 } else if (view == VIEW_CALENDAR || view == VIEW_CALBRIEF) {
257 className += "room-cal";
258 } else if (view == VIEW_TASKS) {
259 className += "room-tasks";
260 } else if (view == VIEW_NOTES) {
261 className += "room-notes";
263 className += "room-chat";
266 className += " room-newmsgs";
268 roomLI.setAttribute("class", className);
269 roomA.dropTarget = true;
270 roomA.dropHandler = roomListDropHandler;
274 function roomListDropHandler(target, dropped) {
275 if (dropped.ctdlMsgId) {
276 var room = getTextContent(target);
278 for(msgId in currentlyMarkedRows) { //defined in summaryview.js
281 var mvCommand = "g_cmd=MOVE " + msgIds + "|"+room+"|0";
282 new Ajax.Request('ajax_servcmd', {
284 parameters: mvCommand,
285 onComplete: deleteAllMarkedRows()});
288 function expandFloorEvent(event) {
289 expandFloor(event.target);
291 function expandFloor(target) {
292 if (target.nodeName.toLowerCase() != "span") {
293 return; // ignore clicks on child UL
295 ctdlLocalPrefs.setPref("rooms_expanded", target.firstChild.nodeValue);
296 var parentUL = target.parentNode;
297 if (currentlyExpandedFloor != null) {
298 currentlyExpandedFloor.className = currentlyExpandedFloor.className.replace("floor-expanded","");
300 parentUL.className = parentUL.className + " floor-expanded";
301 currentlyExpandedFloor = parentUL;
304 // These functions handle moving sticky notes around the screen by dragging them
306 var uid_of_note_being_dragged = 0;
307 var saved_cursor_style = 'default';
308 var note_was_dragged = 0;
310 function NotesDragMouseUp(evt) {
311 document.onmouseup = null;
312 document.onmousemove = null;
313 if (document.layers) {
314 document.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
317 d = $('note-' + uid_of_note_being_dragged);
318 d.style.cursor = saved_cursor_style;
320 // If any motion actually occurred, submit an ajax http call to record it to the server
321 if (note_was_dragged > 0) {
322 p = 'note_uid=' + uid_of_note_being_dragged
323 + '&left=' + d.style.left
324 + '&top=' + d.style.top
325 + '&r=' + CtdlRandomString();
335 uid_of_note_being_dragged = '';
339 function NotesDragMouseMove(evt) {
340 x = (ns6 ? evt.clientX : event.clientX);
341 x_increment = x - saved_x;
342 y = (ns6 ? evt.clientY : event.clientY);
343 y_increment = y - saved_y;
346 d = $('note-' + uid_of_note_being_dragged);
348 divTop = parseInt(d.style.top);
349 divLeft = parseInt(d.style.left);
351 d.style.top = (divTop + y_increment) + 'px';
352 d.style.left = (divLeft + x_increment) + 'px';
356 note_was_dragged = 1;
361 function NotesDragMouseDown(evt, uid) {
362 saved_x = (ns6 ? evt.clientX : event.clientX);
363 saved_y = (ns6 ? evt.clientY : event.clientY);
364 document.onmouseup = NotesDragMouseUp;
365 document.onmousemove = NotesDragMouseMove;
366 if (document.layers) {
367 document.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
369 uid_of_note_being_dragged = uid;
370 d = $('note-' + uid_of_note_being_dragged);
371 saved_cursor_style = d.style.cursor;
372 d.style.cursor = 'move';
373 return false; // disable the default action
377 // Called when the user clicks on the palette icon of a sticky note to change its color.
378 // It toggles the color selector visible or invisible.
380 function NotesClickPalette(evt, uid) {
381 uid_of_note_being_colored = uid;
382 d = $('palette-' + uid_of_note_being_colored);
384 if (d.style.display) {
385 if (d.style.display == 'none') {
386 d.style.display = 'block';
389 d.style.display = 'none';
393 d.style.display = 'block';
400 // Called when the user clicks on one of the colors in an open color selector.
401 // Sets the desired color and then closes the color selector.
403 function NotesClickColor(evt, uid, red, green, blue, notecolor, titlecolor) {
404 uid_of_note_being_colored = uid;
405 palette_button = $('palette-' + uid_of_note_being_colored);
406 note_div = $('note-' + uid_of_note_being_colored);
407 titlebar_div = $('titlebar-' + uid_of_note_being_colored);
409 // alert('FIXME red=' + red + ' green=' + green + ' blue=' + blue);
411 note_div.style.backgroundColor = notecolor;
412 titlebar_div.style.backgroundColor = titlecolor;
413 palette_button.style.display = 'none';
415 // submit an ajax http call to record it to the server
416 p = 'note_uid=' + uid_of_note_being_colored
420 + '&r=' + CtdlRandomString();
433 // These functions handle resizing sticky notes by dragging the resize handle
435 var uid_of_note_being_resized = 0;
436 var saved_cursor_style = 'default';
437 var note_was_resized = 0;
439 function NotesResizeMouseUp(evt) {
440 document.onmouseup = null;
441 document.onmousemove = null;
442 if (document.layers) {
443 document.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
446 d = $('note-' + uid_of_note_being_resized);
447 d.style.cursor = saved_cursor_style;
449 // If any motion actually occurred, submit an ajax http call to record it to the server
450 if (note_was_resized > 0) {
451 p = 'note_uid=' + uid_of_note_being_resized
452 + '&width=' + d.style.width
453 + '&height=' + d.style.height
454 + '&r=' + CtdlRandomString();
464 uid_of_note_being_resized = '';
465 return false; // disable the default action
468 function NotesResizeMouseMove(evt) {
469 x = (ns6 ? evt.clientX : event.clientX);
470 x_increment = x - saved_x;
471 y = (ns6 ? evt.clientY : event.clientY);
472 y_increment = y - saved_y;
475 d = $('note-' + uid_of_note_being_resized);
477 divTop = parseInt(d.style.height);
478 divLeft = parseInt(d.style.width);
480 d.style.height = (divTop + y_increment) + 'px';
481 d.style.width = (divLeft + x_increment) + 'px';
485 note_was_resized = 1;
486 return false; // disable the default action
490 function NotesResizeMouseDown(evt, uid) {
491 saved_x = (ns6 ? evt.clientX : event.clientX);
492 saved_y = (ns6 ? evt.clientY : event.clientY);
493 document.onmouseup = NotesResizeMouseUp;
494 document.onmousemove = NotesResizeMouseMove;
495 if (document.layers) {
496 document.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
498 uid_of_note_being_resized = uid;
499 d = $('note-' + uid_of_note_being_resized);
500 saved_cursor_style = d.style.cursor;
501 d.style.cursor = 'move';
502 return false; // disable the default action
506 function DeleteStickyNote(evt, uid, confirmation_prompt) {
507 uid_of_note_being_deleted = uid;
508 d = $('note-' + uid_of_note_being_deleted);
510 if (confirm(confirmation_prompt)) {
513 // submit an ajax http call to delete it on the server
514 p = 'note_uid=' + uid_of_note_being_deleted
516 + '&r=' + CtdlRandomString();
527 function ctdl_ts_getInnerText(el) {
528 if (typeof el == "string") return el;
529 if (typeof el == "undefined") { return el };
530 if (el.innerText) return el.innerText; //Not needed but it is faster
533 var cs = el.childNodes;
535 for (var i = 0; i < l; i++) {
536 switch (cs[i].nodeType) {
537 case 1: //ELEMENT_NODE
538 str += ts_getInnerText(cs[i]);
541 str += cs[i].nodeValue;
549 // Place a gradient loadscreen on an element, e.g to use before Ajax.updater
550 function CtdlLoadScreen(elementid) {
551 var elem = document.getElementById(elementid);
552 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>";
557 // Pop open the address book (target_input is the INPUT field to populate)
559 function PopOpenAddressBook(target_input) {
560 $('address_book_popup').style.display = 'block';
561 p = 'target_input=' + target_input + '&r=' + CtdlRandomString();
563 'address_book_popup_middle_div',
564 'display_address_book_middle_div',
573 function PopulateAddressBookInnerDiv(which_addr_book, target_input) {
574 $('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>";
575 p = 'which_addr_book=' + which_addr_book
576 + '&target_input=' + target_input
577 + '&r=' + CtdlRandomString();
579 'address_book_inner_div',
580 'display_address_book_inner_div',
588 // What happens when a contact is selected from the address book popup
589 // (populate the specified target)
591 function AddContactsToTarget(target, whichaddr) {
592 while (whichaddr.selectedIndex != -1) {
593 if (target.value.length > 0) {
594 target.value = target.value + ', ';
596 target.value = target.value + whichaddr.value;
597 whichaddr.options[whichaddr.selectedIndex].selected = false;
601 // Respond to a meeting invitation
602 function RespondToInvitation(question_divname, title_divname, msgnum, cal_partnum, sc) {
603 p = 'msgnum=' + msgnum + '&cal_partnum=' + cal_partnum + '&sc=' + sc ;
604 new Ajax.Updater(title_divname, 'respond_to_request', { method: 'post', parameters: p } );
605 Effect.Fade(question_divname, { duration: 0.5 });
608 // Handle a received RSVP
609 function HandleRSVP(question_divname, title_divname, msgnum, cal_partnum, sc) {
610 p = 'msgnum=' + msgnum + '&cal_partnum=' + cal_partnum + '&sc=' + sc ;
611 new Ajax.Updater(title_divname, 'handle_rsvp', { method: 'post', parameters: p } );
612 Effect.Fade(question_divname, { duration: 0.5 });
614 /* var fakeMouse = document.createEvent("MouseEvents");
615 fakeMouse.initMouseEvent("click", true, true, window,
616 0,0,0,0,0, false, false, false, false, 0, null); */
617 // TODO: Collapse into one function
618 function toggleTaskDtStart(event) {
619 var checkBox = $('nodtstart');
620 dtStart = document.getElementById("dtstart");
621 if (checkBox.checked) {
622 dtStart.disabled = true;
623 dtStart.style.textDecoration = "line-through";
625 dtStart.disabled = false;
626 dtStart.style.textDecoration = "";
627 if (dtStart.value.length == 0)
628 dtStart.dpck._initCurrentDate();
631 function toggleTaskDue(event) {
632 var checkBox = $('nodue');
633 dueField = document.getElementById("due");
634 if (checkBox.checked) {
635 dueField.disabled = true;
636 dueField.style.textDecoration = "line-through";
638 dueField.disabled = false;
639 dueField.style.textDecoration = "";
640 if (dueField.value.length == 0)
641 dueField.dpck._initCurrentDate();
644 function ToggleTaskDateOrNoDateActivate(event) {
645 var dtstart = document.getElementById("nodtstart");
646 if (dtstart != null) {
647 toggleTaskDtStart(null);
649 $('nodtstart').observe('click', toggleTaskDtStart);
650 $('nodue').observe('click', toggleTaskDue);
653 function TaskViewGatherCategoriesFromTable() {
654 var table = $('taskview');
657 function attachDatePicker(relative) {
658 var dpck = new DatePicker({
660 language: 'en', // fix please
661 disableFutureDate: false
663 document.getElementById(relative).dpck = dpck; // attach a ref to it
665 function eventEditAllDay() {
666 var allDayCheck = document.getElementById("alldayevent");
667 var dtend= document.getElementById("dtendcell");
668 if(allDayCheck.checked) {
669 //dtend.disabled = true;
670 dtend.style.textDecoration = "line-through";
672 //dtend_day.disabled = false;
673 dtend.style.textDecoration = "";
680 // Functions which handle show/hide of various elements in the recurrence editor
682 function RecurrenceShowHide() {
684 if ($('is_recur').checked) {
685 $('rrule_div').style.display = 'block';
688 $('rrule_div').style.display = 'none';
691 if ($('freq_selector').selectedIndex == 4) {
692 $('weekday_selector').style.display = 'block';
695 $('weekday_selector').style.display = 'none';
698 if ($('freq_selector').selectedIndex == 5) {
699 $('monthday_selector').style.display = 'block';
702 $('monthday_selector').style.display = 'none';
705 if ($('rrend_count').checked) {
706 $('rrcount').disabled = false;
709 $('rrcount').disabled = true;
712 if ($('rrend_until').checked) {
713 $('rruntil').disabled = false;
716 $('rruntil').disabled = true;
719 if ($('rrmonthtype_mday').checked) {
720 $('rrmday').disabled = false;
723 $('rrmday').disabled = true;
726 if ($('rrmonthtype_wday').checked) {
727 $('rrmweek').disabled = false;
728 $('rrmweekday').disabled = false;
731 $('rrmweek').disabled = true;
732 $('rrmweekday').disabled = true;
735 if ($('freq_selector').selectedIndex == 6) {
736 $('yearday_selector').style.display = 'block';
739 $('yearday_selector').style.display = 'none';
742 $('ymday').innerHTML = 'XXXX-' + $('dtstart').value.substr(5);
743 $('rrmday').innerHTML = $('dtstart').value.substr(8);
745 if ($('rryeartype_ywday').checked) {
746 $('rrymweek').disabled = false;
747 $('rrymweekday').disabled = false;
748 $('rrymonth').disabled = false;
751 $('rrymweek').disabled = true;
752 $('rrymweekday').disabled = true;
753 $('rrymonth').disabled = true;