2 +-----------------------------------------------------------------------+
3 | RoundCube Webmail Client Script |
5 | This file is part of the RoundCube Webmail client |
6 | Copyright (C) 2005-2007, RoundCube Dev, - Switzerland |
7 | Licensed under the GNU GPL |
9 +-----------------------------------------------------------------------+
10 | Authors: Thomas Bruederli <roundcube@gmail.com> |
11 | Charles McNulty <charles@charlesmcnulty.com> |
12 +-----------------------------------------------------------------------+
13 | Requires: common.js, list.js |
14 +-----------------------------------------------------------------------+
16 $Id: app.js 888 2007-10-19 14:04:43Z robin $
20 var rcube_webmail_client;
22 function rcube_webmail()
24 this.env = new Object();
25 this.labels = new Object();
26 this.buttons = new Object();
27 this.gui_objects = new Object();
28 this.commands = new Object();
29 this.onloads = new Array();
31 // create protected reference to myself
32 rcube_webmail_client = this;
33 this.ref = 'rcube_webmail_client';
36 // webmail client settings
37 this.dblclick_time = 500;
38 this.message_time = 3000;
40 this.identifier_expr = new RegExp('[^0-9a-z\-_]', 'gi');
42 // mimetypes supported by the browser (default settings)
43 this.mimetypes = new Array('text/plain', 'text/html', 'text/xml',
44 'image/jpeg', 'image/gif', 'image/png',
45 'application/x-javascript', 'application/pdf',
46 'application/x-shockwave-flash');
48 // default environment vars
49 this.env.keep_alive = 60; // seconds
50 this.env.request_timeout = 180; // seconds
51 this.env.draft_autosave = 0; // seconds
52 this.env.comm_path = './';
53 this.env.bin_path = './bin/';
54 this.env.blankpage = 'program/blank.gif';
57 // set environment variable(s)
58 this.set_env = function(p, value)
60 if (p != null && typeof(p) == 'object' && !value)
68 // add a localized label to the client environment
69 this.add_label = function(key, value)
71 this.labels[key] = value;
75 // add a button to the button list
76 this.register_button = function(command, id, type, act, sel, over)
78 if (!this.buttons[command])
79 this.buttons[command] = new Array();
81 var button_prop = {id:id, type:type};
82 if (act) button_prop.act = act;
83 if (sel) button_prop.sel = sel;
84 if (over) button_prop.over = over;
86 this.buttons[command][this.buttons[command].length] = button_prop;
89 // register a specific gui object
90 this.gui_object = function(name, id)
92 this.gui_objects[name] = id;
95 // execute the given script on load
96 this.add_onload = function(f)
98 this.onloads[this.onloads.length] = f;
101 // initialize webmail client
102 this.init = function()
105 this.task = this.env.task;
108 if (!bw.dom || !bw.xmlhttp_test())
110 this.goto_url('error', '_code=0x199');
114 // find all registered gui objects
115 for (var n in this.gui_objects)
116 this.gui_objects[n] = rcube_find_object(this.gui_objects[n]);
118 // tell parent window that this frame is loaded
119 if (this.env.framed && parent.rcmail && parent.rcmail.set_busy)
120 parent.rcmail.set_busy(false);
122 // enable general commands
123 this.enable_command('logout', 'mail', 'addressbook', 'settings', true);
128 if (this.gui_objects.messagelist)
130 this.message_list = new rcube_list_widget(this.gui_objects.messagelist, {multiselect:true, draggable:true, keyboard:true, dblclick_time:this.dblclick_time});
131 this.message_list.row_init = function(o){ p.init_message_row(o); };
132 this.message_list.addEventListener('dblclick', function(o){ p.msglist_dbl_click(o); });
133 this.message_list.addEventListener('keypress', function(o){ p.msglist_keypress(o); });
134 this.message_list.addEventListener('select', function(o){ p.msglist_select(o); });
135 this.message_list.addEventListener('dragstart', function(o){ p.drag_active = true; });
136 this.message_list.addEventListener('dragend', function(o){ p.drag_active = false; });
138 this.message_list.init();
139 this.enable_command('toggle_status', true);
141 if (this.gui_objects.mailcontframe)
143 this.gui_objects.mailcontframe.onmousedown = function(e){ return p.click_on_list(e); };
144 document.onmouseup = function(e){ return p.doc_mouse_up(e); };
147 this.message_list.focus();
150 // enable mail commands
151 this.enable_command('list', 'checkmail', 'compose', 'add-contact', 'search', 'reset-search', true);
153 if (this.env.action=='show' || this.env.action=='preview')
155 this.enable_command('show', 'reply', 'reply-all', 'forward', 'moveto', 'delete', 'viewsource', 'print', 'load-attachment', true);
156 if (this.env.next_uid)
158 this.enable_command('nextmessage', true);
159 this.enable_command('lastmessage', true);
161 if (this.env.prev_uid)
163 this.enable_command('previousmessage', true);
164 this.enable_command('firstmessage', true);
168 if (this.env.trash_mailbox && this.env.mailbox != this.env.trash_mailbox)
169 this.set_alttext('delete', 'movemessagetotrash');
171 // make preview/message frame visible
172 if (this.env.action == 'preview' && this.env.framed && parent.rcmail)
174 this.enable_command('compose', 'add-contact', false);
175 parent.rcmail.show_contentframe(true);
176 parent.rcmail.mark_message('read', this.env.uid);
179 if ((this.env.action=='show' || this.env.action=='preview') && this.env.blockedobjects)
181 if (this.gui_objects.remoteobjectsmsg)
182 this.gui_objects.remoteobjectsmsg.style.display = 'block';
183 this.enable_command('load-images', true);
186 if (this.env.action=='compose')
188 this.enable_command('add-attachment', 'send-attachment', 'remove-attachment', 'send', true);
189 if (this.env.spellcheck)
191 this.env.spellcheck.spelling_state_observer = function(s){ ref.set_spellcheck_state(s); };
192 this.set_spellcheck_state('ready');
194 if (this.env.drafts_mailbox)
195 this.enable_command('savedraft', true);
198 if (this.env.messagecount)
199 this.enable_command('select-all', 'select-none', 'sort', 'expunge', true);
201 if (this.env.messagecount && (this.env.mailbox==this.env.trash_mailbox || this.env.mailbox==this.env.junk_mailbox))
202 this.enable_command('purge', true);
204 this.set_page_buttons();
207 if (this.env.framed && window.parent)
208 window.parent.focus();
212 // init message compose form
213 if (this.env.action=='compose')
214 this.init_messageform();
216 // show printing dialog
217 if (this.env.action=='print')
220 // get unread count for each mailbox
221 if (this.gui_objects.mailboxlist)
223 this.gui_objects.folderlist = this.gui_objects.mailboxlist;
224 this.http_request('getunread', '');
231 if (this.gui_objects.contactslist)
233 this.contact_list = new rcube_list_widget(this.gui_objects.contactslist, {multiselect:true, draggable:true, keyboard:true});
234 this.contact_list.addEventListener('keypress', function(o){ p.contactlist_keypress(o); });
235 this.contact_list.addEventListener('select', function(o){ p.contactlist_select(o); });
236 this.contact_list.addEventListener('dragstart', function(o){ p.drag_active = true; });
237 this.contact_list.addEventListener('dragend', function(o){ p.drag_active = false; });
238 this.contact_list.init();
241 this.contact_list.highlight_row(this.env.cid);
243 if (this.gui_objects.contactslist.parentNode)
245 this.gui_objects.contactslist.parentNode.onmousedown = function(e){ return p.click_on_list(e); };
246 document.onmouseup = function(e){ return p.doc_mouse_up(e); };
249 this.contact_list.focus();
252 this.set_page_buttons();
254 if (this.env.address_sources && !this.env.address_sources[this.env.source].readonly)
255 this.enable_command('add', true);
258 this.enable_command('show', 'edit', true);
260 if ((this.env.action=='add' || this.env.action=='edit') && this.gui_objects.editform)
261 this.enable_command('save', true);
263 this.enable_command('search', 'reset-search', 'moveto', true);
265 this.enable_command('list', true);
270 this.enable_command('preferences', 'identities', 'save', 'folders', true);
272 if (this.env.action=='identities' || this.env.action=='edit-identity' || this.env.action=='add-identity')
273 this.enable_command('edit', 'add', 'delete', true);
275 if (this.env.action=='edit-identity' || this.env.action=='add-identity')
276 this.enable_command('save', true);
278 if (this.env.action=='folders')
279 this.enable_command('subscribe', 'unsubscribe', 'create-folder', 'rename-folder', 'delete-folder', true);
281 if (this.gui_objects.identitieslist)
283 this.identity_list = new rcube_list_widget(this.gui_objects.identitieslist, {multiselect:false, draggable:false, keyboard:false});
284 this.identity_list.addEventListener('select', function(o){ p.identity_select(o); });
285 this.identity_list.init();
286 this.identity_list.focus();
289 this.identity_list.highlight_row(this.env.iid);
292 if (this.gui_objects.subscriptionlist)
293 this.init_subscription_list();
298 var input_user = rcube_find_object('_user');
299 var input_pass = rcube_find_object('_pass');
301 input_user.onkeypress = function(e){ return rcmail.login_user_keypress(e); };
302 if (input_user && input_user.value=='')
307 this.enable_command('login', true);
315 // enable basic commands
316 this.enable_command('logout', true);
318 // flag object as complete
322 if (this.pending_message)
323 this.display_message(this.pending_message[0], this.pending_message[1]);
325 // start keep-alive interval
326 this.start_keepalive();
329 // execute all foreign onload scripts
330 for (var i=0; i<this.onloads.length; i++)
332 if (typeof(this.onloads[i]) == 'string')
333 eval(this.onloads[i]);
334 else if (typeof(this.onloads[i]) == 'function')
340 // start interval for keep-alive/recent_check signal
341 this.start_keepalive = function()
343 if (this.env.keep_alive && !this.env.framed && this.task=='mail' && this.gui_objects.messagelist)
344 this._int = setInterval(function(){ ref.check_for_recent(); }, this.env.keep_alive * 1000);
345 else if (this.env.keep_alive && !this.env.framed && this.task!='login')
346 this._int = setInterval(function(){ ref.send_keep_alive(); }, this.env.keep_alive * 1000);
350 this.init_message_row = function(row)
353 if (uid && this.env.messages[uid])
355 row.deleted = this.env.messages[uid].deleted ? true : false;
356 row.unread = this.env.messages[uid].unread ? true : false;
357 row.replied = this.env.messages[uid].replied ? true : false;
360 // set eventhandler to message icon
361 if ((row.icon = row.obj.cells[0].childNodes[0]) && row.icon.nodeName=='IMG')
364 row.icon.id = 'msgicn_'+row.uid;
365 row.icon._row = row.obj;
366 row.icon.onmousedown = function(e) { p.command('toggle_status', this); };
371 // init message compose form: set focus and eventhandlers
372 this.init_messageform = function()
374 if (!this.gui_objects.messageform)
377 //this.messageform = this.gui_objects.messageform;
378 var input_from = rcube_find_object('_from');
379 var input_to = rcube_find_object('_to');
380 var input_cc = rcube_find_object('_cc');
381 var input_bcc = rcube_find_object('_bcc');
382 var input_replyto = rcube_find_object('_replyto');
383 var input_subject = rcube_find_object('_subject');
384 var input_message = rcube_find_object('_message');
386 // init live search events
388 this.init_address_input_events(input_to);
390 this.init_address_input_events(input_cc);
392 this.init_address_input_events(input_bcc);
394 // add signature according to selected identity
395 if (input_from && input_from.type=='select-one')
396 this.change_identity(input_from);
398 if (input_to && input_to.value=='')
400 else if (input_subject && input_subject.value=='')
401 input_subject.focus();
402 else if (input_message)
403 this.set_caret2start(input_message);
405 // get summary of all field values
406 this.compose_field_hash(true);
408 // start the auto-save timer
409 this.auto_save_start();
412 this.init_address_input_events = function(obj)
414 var handler = function(e){ return ref.ksearch_keypress(e,this); };
415 var handler2 = function(e){ return ref.ksearch_blur(e,this); };
417 if (obj.addEventListener)
419 obj.addEventListener(bw.safari ? 'keydown' : 'keypress', handler, false);
420 obj.addEventListener('blur', handler2, false);
424 obj.onkeydown = handler;
425 obj.onblur = handler2;
428 obj.setAttribute('autocomplete', 'off');
433 /*********************************************************/
434 /********* client command interface *********/
435 /*********************************************************/
438 // execute a specific command on the web client
439 this.command = function(command, props, obj)
447 // command not supported or allowed
448 if (!this.commands[command])
450 // pass command to parent window
451 if (this.env.framed && parent.rcmail && parent.rcmail.command)
452 parent.rcmail.command(command, props);
458 // check input before leaving compose step
459 if (this.task=='mail' && this.env.action=='compose' && (command=='list' || command=='mail' || command=='addressbook' || command=='settings'))
461 if (this.cmp_hash != this.compose_field_hash() && !confirm(this.get_label('notsentwarning')))
470 if (this.gui_objects.loginform)
471 this.gui_objects.loginform.submit();
475 this.goto_url('logout', true);
478 // commands to switch task
482 this.switch_task(command);
486 // misc list commands
488 if (this.task=='mail')
490 if (this.env.search_request<0 || (this.env.search_request && props != this.env.mailbox))
491 this.reset_qsearch();
493 this.list_mailbox(props);
495 if (this.env.trash_mailbox)
496 this.set_alttext('delete', this.env.mailbox != this.env.trash_mailbox ? 'movemessagetotrash' : 'deletemessage');
498 else if (this.task=='addressbook')
500 if (this.env.search_request<0 || (this.env.search_request && props != this.env.source))
501 this.reset_qsearch();
503 this.list_contacts(props);
504 this.enable_command('add', (this.env.address_sources && !this.env.address_sources[props].readonly));
510 // get the type of sorting
511 var a_sort = props.split('_');
512 var sort_col = a_sort[0];
513 var sort_order = a_sort[1] ? a_sort[1].toUpperCase() : null;
516 // no sort order specified: toggle
517 if (sort_order==null)
519 if (this.env.sort_col==sort_col)
520 sort_order = this.env.sort_order=='ASC' ? 'DESC' : 'ASC';
522 sort_order = this.env.sort_order;
525 if (this.env.sort_col==sort_col && this.env.sort_order==sort_order)
528 // set table header class
529 if (header = document.getElementById('rcmHead'+this.env.sort_col))
530 this.set_classname(header, 'sorted'+(this.env.sort_order.toUpperCase()), false);
531 if (header = document.getElementById('rcmHead'+sort_col))
532 this.set_classname(header, 'sorted'+sort_order, true);
534 // save new sort properties
535 this.env.sort_col = sort_col;
536 this.env.sort_order = sort_order;
538 // reload message list
539 this.list_mailbox('', '', sort_col+'_'+sort_order);
543 this.list_page('next');
547 this.list_page('last');
551 this.list_page('prev');
555 this.list_page('first');
559 if (this.env.messagecount)
560 this.expunge_mailbox(this.env.mailbox);
564 case 'empty-mailbox':
565 if (this.env.messagecount)
566 this.purge_mailbox(this.env.mailbox);
570 // common commands used in multiple tasks
572 if (this.task=='mail')
574 var uid = this.get_single_uid();
575 if (uid && (!this.env.uid || uid != this.env.uid))
577 if (this.env.mailbox == this.env.drafts_mailbox)
578 this.goto_url('compose', '_draft_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
580 this.show_message(uid);
583 else if (this.task=='addressbook')
585 var cid = props ? props : this.get_single_cid();
586 if (cid && !(this.env.action=='show' && cid==this.env.cid))
587 this.load_contact(cid, 'show');
592 if (this.task=='addressbook')
593 this.load_contact(0, 'add');
594 else if (this.task=='settings')
596 this.identity_list.clear_selection();
597 this.load_identity(0, 'add-identity');
603 if (this.task=='addressbook' && (cid = this.get_single_cid()))
604 this.load_contact(cid, 'edit');
605 else if (this.task=='settings' && props)
606 this.load_identity(props, 'edit-identity');
609 case 'save-identity':
611 if (this.gui_objects.editform)
613 var input_pagesize = rcube_find_object('_pagesize');
614 var input_name = rcube_find_object('_name');
615 var input_email = rcube_find_object('_email');
618 if (input_pagesize && isNaN(input_pagesize.value))
620 alert(this.get_label('nopagesizewarning'));
621 input_pagesize.focus();
624 // contacts/identities
627 if (input_name && input_name.value == '')
629 alert(this.get_label('nonamewarning'));
633 else if (input_email && !rcube_check_email(input_email.value))
635 alert(this.get_label('noemailwarning'));
641 this.gui_objects.editform.submit();
647 if (this.task=='mail')
648 this.delete_messages();
650 else if (this.task=='addressbook')
651 this.delete_contacts();
652 // user settings task
653 else if (this.task=='settings')
654 this.delete_identity();
658 // mail task commands
661 if (this.task == 'mail')
662 this.move_messages(props);
663 else if (this.task == 'addressbook' && this.drag_active)
664 this.copy_contact(null, props);
667 case 'toggle_status':
668 if (props && !props._row)
676 uid = props._row.uid;
677 this.message_list.dont_select = true;
678 // toggle read/unread
679 if (this.message_list.rows[uid].deleted) {
681 } else if (!this.message_list.rows[uid].unread)
685 this.mark_message(flag, uid);
690 this.show_message(this.env.uid, true, this.env.action=='preview');
693 case 'load-attachment':
694 var qstring = '_mbox='+this.env.mailbox+'&_uid='+this.env.uid+'&_part='+props.part;
696 // open attachment in frame if it's of a supported mimetype
697 if (this.env.uid && props.mimetype && find_in_array(props.mimetype, this.mimetypes)>=0)
699 if (props.mimetype == 'text/html')
700 qstring += '&_safe=1';
701 this.attachment_win = window.open(this.env.comm_path+'&_action=get&'+qstring+'&_frame=1', 'rcubemailattachment');
702 if (this.attachment_win)
704 setTimeout(function(){ ref.attachment_win.focus(); }, 10);
709 this.goto_url('get', qstring+'&_download=1', false);
713 this.message_list.select_all(props);
717 this.message_list.clear_selection();
721 if (this.env.next_uid)
722 this.show_message(this.env.next_uid, false, this.env.action=='preview');
726 if (this.env.last_uid)
727 this.show_message(this.env.last_uid);
730 case 'previousmessage':
731 if (this.env.prev_uid)
732 this.show_message(this.env.prev_uid, false, this.env.action=='preview');
736 if (this.env.first_uid)
737 this.show_message(this.env.first_uid);
741 this.check_for_recent();
745 var url = this.env.comm_path+'&_action=compose';
747 if (this.task=='mail' && this.env.mailbox==this.env.drafts_mailbox)
750 if (uid = this.get_single_uid())
751 url += '&_draft_uid='+uid+'&_mbox='+urlencode(this.env.mailbox);
753 // modify url if we're in addressbook
754 else if (this.task=='addressbook')
756 // switch to mail compose step directly
757 if (props && props.indexOf('@') > 0)
759 url = this.get_task_url('mail', url);
760 this.redirect(url + '&_to='+urlencode(props));
764 // use contact_id passed as command parameter
765 var a_cids = new Array();
767 a_cids[a_cids.length] = props;
768 // get selected contacts
769 else if (this.contact_list)
771 var selection = this.contact_list.get_selection();
772 for (var n=0; n<selection.length; n++)
773 a_cids[a_cids.length] = selection[n];
777 this.http_request('mailto', '_cid='+urlencode(a_cids.join(','))+'&_source='+urlencode(this.env.source), true);
782 url += '&_to='+urlencode(props);
784 // don't know if this is necessary...
785 url = url.replace(/&_framed=1/, "");
791 if (this.env.spellcheck && this.env.spellcheck.spellCheck && this.spellcheck_ready)
793 this.env.spellcheck.spellCheck(this.env.spellcheck.check_link);
794 this.set_spellcheck_state('checking');
799 // Reset the auto-save timer
800 self.clearTimeout(this.save_timer);
802 if (!this.gui_objects.messageform)
805 // if saving Drafts is disabled in main.inc.php
806 // or if compose form did not change
807 if (!this.env.drafts_mailbox || this.cmp_hash == this.compose_field_hash())
810 this.set_busy(true, 'savingmessage');
811 var form = this.gui_objects.messageform;
812 form.target = "savetarget";
813 form._draft.value = '1';
818 if (!this.gui_objects.messageform)
821 if (!this.check_compose_input())
824 // Reset the auto-save timer
825 self.clearTimeout(this.save_timer);
827 // all checks passed, send message
828 this.set_busy(true, 'sendingmessage');
829 var form = this.gui_objects.messageform;
830 form.target = "savetarget";
831 form._draft.value = '';
834 // clear timeout (sending could take longer)
835 clearTimeout(this.request_timer);
838 case 'add-attachment':
839 this.show_attachment_form(true);
841 case 'send-attachment':
842 // Reset the auto-save timer
843 self.clearTimeout(this.save_timer);
845 this.upload_file(props)
848 case 'remove-attachment':
849 this.remove_attachment(props);
855 if (uid = this.get_single_uid())
856 this.goto_url('compose', '_reply_uid='+uid+'&_mbox='+urlencode(this.env.mailbox)+(command=='reply-all' ? '&_all=1' : ''), true);
861 if (uid = this.get_single_uid())
862 this.goto_url('compose', '_forward_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
867 if (uid = this.get_single_uid())
869 ref.printwin = window.open(this.env.comm_path+'&_action=print&_uid='+uid+'&_mbox='+urlencode(this.env.mailbox)+(this.env.safemode ? '&_safe=1' : ''));
871 setTimeout(function(){ ref.printwin.focus(); }, 20);
877 if (uid = this.get_single_uid())
879 ref.sourcewin = window.open(this.env.comm_path+'&_action=viewsource&_uid='+this.env.uid+'&_mbox='+urlencode(this.env.mailbox));
881 setTimeout(function(){ ref.sourcewin.focus(); }, 20);
886 this.add_contact(props);
891 if (!props && this.gui_objects.qsearchbox)
892 props = this.gui_objects.qsearchbox.value;
901 var s = this.env.search_request;
902 this.reset_qsearch();
904 if (s && this.env.mailbox)
905 this.list_mailbox(this.env.mailbox);
906 else if (s && this.task == 'addressbook')
907 this.list_contacts(this.env.source);
911 // user settings commands
917 this.goto_url('identities');
920 case 'delete-identity':
921 this.delete_identity();
924 this.goto_url('folders');
928 this.subscribe_folder(props);
932 this.unsubscribe_folder(props);
935 case 'create-folder':
936 this.create_folder(props);
939 case 'rename-folder':
940 this.rename_folder(props);
943 case 'delete-folder':
944 this.delete_folder(props);
949 return obj ? false : true;
953 // set command enabled or disabled
954 this.enable_command = function()
956 var args = arguments;
957 if(!args.length) return -1;
960 var enable = args[args.length-1];
962 for(var n=0; n<args.length-1; n++)
965 this.commands[command] = enable;
966 this.set_button(command, (enable ? 'act' : 'pas'));
972 // lock/unlock interface
973 this.set_busy = function(a, message)
977 var msg = this.get_label(message);
981 this.display_message(msg, 'loading', true);
987 //document.body.style.cursor = a ? 'wait' : 'default';
989 if (this.gui_objects.editform)
990 this.lock_form(this.gui_objects.editform, a);
992 // clear pending timer
993 if (this.request_timer)
994 clearTimeout(this.request_timer);
996 // set timer for requests
997 if (a && this.env.request_timeout)
998 this.request_timer = setTimeout(function(){ ref.request_timed_out(); }, this.env.request_timeout * 1000);
1002 // return a localized string
1003 this.get_label = function(name)
1005 if (this.labels[name])
1006 return this.labels[name];
1012 // switch to another application task
1013 this.switch_task = function(task)
1015 if (this.task===task && task!='mail')
1018 var url = this.get_task_url(task);
1020 url += '&_mbox=INBOX';
1026 this.get_task_url = function(task, url)
1029 url = this.env.comm_path;
1031 return url.replace(/_task=[a-z]+/, '_task='+task);
1035 // called when a request timed out
1036 this.request_timed_out = function()
1038 this.set_busy(false);
1039 this.display_message('Request timed out!', 'error');
1043 /*********************************************************/
1044 /********* event handling methods *********/
1045 /*********************************************************/
1048 this.doc_mouse_up = function(e)
1050 if (this.message_list)
1051 this.message_list.blur();
1052 else if (this.contact_list)
1053 this.contact_list.blur();
1056 this.focus_folder = function(id)
1059 if (this.drag_active && this.check_droptarget(id) && (li = this.get_folder_li(id)))
1060 this.set_classname(li, 'droptarget', true);
1063 this.unfocus_folder = function(id)
1066 if (this.drag_active && (li = this.get_folder_li(id)))
1067 this.set_classname(li, 'droptarget', false);
1070 // onmouseup handler for folder list item
1071 this.folder_mouse_up = function(id)
1073 if (this.drag_active)
1075 this.unfocus_folder(id);
1076 this.command('moveto', id);
1079 // Hide message command buttons until a message is selected
1080 this.enable_command('reply', 'reply-all', 'forward', 'delete', 'print', false);
1084 this.click_on_list = function(e)
1086 if (this.message_list)
1087 this.message_list.focus();
1088 else if (this.contact_list)
1089 this.contact_list.focus();
1092 if (mbox_li = this.get_folder_li())
1093 this.set_classname(mbox_li, 'unfocused', true);
1095 rcube_event.cancel(e);
1099 this.msglist_select = function(list)
1101 if (this.preview_timer)
1102 clearTimeout(this.preview_timer);
1104 var selected = list.selection.length==1;
1106 // Hide certain command buttons when Drafts folder is selected
1107 if (this.env.mailbox == this.env.drafts_mailbox)
1109 this.enable_command('reply', 'reply-all', 'forward', false);
1110 this.enable_command('show', selected);
1111 this.enable_command('delete', 'moveto', (list.selection.length > 0 ? true : false));
1115 this.enable_command('show', 'reply', 'reply-all', 'forward', 'print', selected);
1116 this.enable_command('delete', 'moveto', (list.selection.length > 0 ? true : false));
1119 // start timer for message preview (wait for double click)
1120 if (selected && this.env.contentframe)
1121 this.preview_timer = setTimeout(function(){ ref.msglist_get_preview(); }, this.dblclick_time + 10);
1122 else if (this.env.contentframe)
1123 this.show_contentframe(false);
1127 this.msglist_dbl_click = function(list)
1129 if (this.preview_timer)
1130 clearTimeout(this.preview_timer);
1132 var uid = list.get_single_selection();
1133 if (uid && this.env.mailbox == this.env.drafts_mailbox)
1134 this.goto_url('compose', '_draft_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
1136 this.show_message(uid, false, false);
1140 this.msglist_keypress = function(list)
1142 if (list.key_pressed == list.ENTER_KEY)
1143 this.command('show');
1144 else if (list.key_pressed == list.DELETE_KEY)
1145 this.command('delete');
1149 this.msglist_get_preview = function()
1151 var uid = this.get_single_uid();
1152 if (uid && this.env.contentframe && !this.drag_active)
1153 this.show_message(uid, false, true);
1154 else if (this.env.contentframe)
1155 this.show_contentframe(false);
1159 this.check_droptarget = function(id)
1161 if (this.task == 'mail')
1162 return (id != this.env.mailbox);
1163 else if (this.task == 'addressbook')
1164 return (id != this.env.source && this.env.address_sources[id] && !this.env.address_sources[id].readonly);
1165 else if (this.task == 'settings')
1166 return (id != this.env.folder);
1170 /*********************************************************/
1171 /********* (message) list functionality *********/
1172 /*********************************************************/
1175 // when user doble-clicks on a row
1176 this.show_message = function(id, safe, preview)
1179 var action = preview ? 'preview': 'show';
1180 var target = window;
1181 if (preview && this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1183 target = window.frames[this.env.contentframe];
1184 add_url = '&_framed=1';
1188 add_url = '&_safe=1';
1192 var url = '&_action='+action+'&_uid='+id+'&_mbox='+urlencode(this.env.mailbox)+add_url;
1193 if (action == 'preview' && String(target.location.href).indexOf(url) >= 0)
1194 this.show_contentframe(true);
1197 this.set_busy(true, 'loading');
1198 target.location.href = this.env.comm_path+url;
1204 this.show_contentframe = function(show)
1207 if (this.env.contentframe && (frm = rcube_find_object(this.env.contentframe)))
1209 if (!show && window.frames[this.env.contentframe] && frames[this.env.contentframe].location.href.indexOf(this.env.blankpage)<0)
1210 frames[this.env.contentframe].location.href = this.env.blankpage;
1212 frm.style.display = show ? 'block' : 'none';
1215 if (!show && this.busy)
1216 this.set_busy(false);
1220 // list a specific page
1221 this.list_page = function(page)
1224 page = this.env.current_page+1;
1226 page = this.env.pagecount;
1227 if (page=='prev' && this.env.current_page>1)
1228 page = this.env.current_page-1;
1229 if (page=='first' && this.env.current_page>1)
1232 if (page > 0 && page <= this.env.pagecount)
1234 this.env.current_page = page;
1236 if (this.task=='mail')
1237 this.list_mailbox(this.env.mailbox, page);
1238 else if (this.task=='addressbook')
1239 this.list_contacts(this.env.source, page);
1244 // list messages of a specific mailbox
1245 this.list_mailbox = function(mbox, page, sort)
1247 this.last_selected = 0;
1249 var target = window;
1252 mbox = this.env.mailbox;
1254 // add sort to url if set
1256 add_url += '&_sort=' + sort;
1258 // also send search request to get the right messages
1259 if (this.env.search_request)
1260 add_url += '&_search='+this.env.search_request;
1262 // set page=1 if changeing to another mailbox
1263 if (!page && mbox != this.env.mailbox)
1266 this.env.current_page = page;
1267 if (this.message_list)
1268 this.message_list.clear_selection();
1269 this.show_contentframe(false);
1272 if (mbox != this.env.mailbox || (mbox == this.env.mailbox && !page && !sort))
1273 add_url += '&_refresh=1';
1275 this.select_folder(mbox, this.env.mailbox);
1276 this.env.mailbox = mbox;
1278 // load message list remotely
1279 if (this.gui_objects.messagelist)
1281 this.list_mailbox_remote(mbox, page, add_url);
1285 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1287 target = window.frames[this.env.contentframe];
1288 add_url += '&_framed=1';
1291 // load message list to target frame/window
1294 this.set_busy(true, 'loading');
1295 target.location.href = this.env.comm_path+'&_mbox='+urlencode(mbox)+(page ? '&_page='+page : '')+add_url;
1300 // send remote request to load message list
1301 this.list_mailbox_remote = function(mbox, page, add_url)
1303 // clear message list first
1304 this.message_list.clear();
1306 // send request to server
1307 var url = '_mbox='+urlencode(mbox)+(page ? '&_page='+page : '');
1308 this.set_busy(true, 'loading');
1309 this.http_request('list', url+add_url, true);
1313 this.expunge_mailbox = function(mbox)
1318 // lock interface if it's the active mailbox
1319 if (mbox == this.env.mailbox)
1322 this.set_busy(true, 'loading');
1323 add_url = '&_reload=1';
1326 // send request to server
1327 var url = '_mbox='+urlencode(mbox);
1328 this.http_post('expunge', url+add_url, lock);
1332 this.purge_mailbox = function(mbox)
1337 if (!confirm(this.get_label('purgefolderconfirm')))
1340 // lock interface if it's the active mailbox
1341 if (mbox == this.env.mailbox)
1344 this.set_busy(true, 'loading');
1345 add_url = '&_reload=1';
1348 // send request to server
1349 var url = '_mbox='+urlencode(mbox);
1350 this.http_post('purge', url+add_url, lock);
1355 // move selected messages to the specified mailbox
1356 this.move_messages = function(mbox)
1358 // exit if current or no mailbox specified or if selection is empty
1359 if (!mbox || !this.env.uid || mbox==this.env.mailbox)
1361 if (!this.message_list || !this.message_list.get_selection().length)
1366 var add_url = '&_target_mbox='+urlencode(mbox)+'&_from='+(this.env.action ? this.env.action : '');
1368 // show wait message
1369 if (this.env.action=='show')
1372 this.set_busy(true, 'movingmessage');
1375 this.show_contentframe(false);
1377 this._with_selected_messages('moveto', lock, add_url);
1380 // delete selected messages from the current mailbox
1381 this.delete_messages = function()
1383 var selection = this.message_list ? this.message_list.get_selection() : new Array();
1385 // exit if no mailbox specified or if selection is empty
1386 if (!this.env.uid && !selection.length)
1389 // if there is a trash mailbox defined and we're not currently in it:
1390 if (this.env.trash_mailbox && String(this.env.mailbox).toLowerCase() != String(this.env.trash_mailbox).toLowerCase())
1392 // if shift was pressed delete it immediately
1393 if (this.message_list && this.message_list.shiftkey)
1395 if (confirm(this.get_label('deletemessagesconfirm')))
1396 this.permanently_remove_messages();
1399 this.move_messages(this.env.trash_mailbox);
1401 // if there is a trash mailbox defined but we *are* in it:
1402 else if (this.env.trash_mailbox && String(this.env.mailbox).toLowerCase() == String(this.env.trash_mailbox).toLowerCase())
1403 this.permanently_remove_messages();
1404 // if there isn't a defined trash mailbox and the config is set to flag for deletion
1405 else if (!this.env.trash_mailbox && this.env.flag_for_deletion)
1407 this.mark_message('delete');
1408 if(this.env.action=="show")
1409 this.command('nextmessage','',this);
1410 else if (selection.length == 1)
1411 this.message_list.select_next();
1413 // if there isn't a defined trash mailbox and the config is set NOT to flag for deletion
1414 else if (!this.env.trash_mailbox)
1415 this.permanently_remove_messages();
1419 // delete the selected messages permanently
1420 this.permanently_remove_messages = function()
1422 // exit if no mailbox specified or if selection is empty
1423 if (!this.env.uid && (!this.message_list || !this.message_list.get_selection().length))
1426 this.show_contentframe(false);
1427 this._with_selected_messages('delete', false, '&_from='+(this.env.action ? this.env.action : ''));
1430 // Send a specifc request with UIDs of all selected messages
1432 this._with_selected_messages = function(action, lock, add_url)
1434 var a_uids = new Array();
1436 a_uids[a_uids.length] = this.env.uid;
1439 var selection = this.message_list.get_selection();
1441 for (var n=0; n<selection.length; n++)
1444 a_uids[a_uids.length] = id;
1445 this.message_list.remove_row(id, (n == selection.length-1));
1449 // also send search request to get the right messages
1450 if (this.env.search_request)
1451 add_url += '&_search='+this.env.search_request;
1453 // send request to server
1454 this.http_post(action, '_uid='+a_uids.join(',')+'&_mbox='+urlencode(this.env.mailbox)+add_url, lock);
1458 // set a specific flag to one or more messages
1459 this.mark_message = function(flag, uid)
1461 var a_uids = new Array();
1462 var selection = this.message_list ? this.message_list.get_selection() : new Array();
1466 else if (this.env.uid)
1467 a_uids[0] = this.env.uid;
1468 else if (this.message_list)
1470 for (var id, n=0; n<selection.length; n++)
1473 if ((flag=='read' && this.message_list.rows[id].unread) || (flag=='unread' && !this.message_list.rows[id].unread)
1474 || (flag=='delete' && !this.message_list.rows[id].deleted) || (flag=='undelete' && this.message_list.rows[id].deleted))
1475 a_uids[a_uids.length] = id;
1487 this.toggle_read_status(flag, a_uids);
1491 this.toggle_delete_status(a_uids);
1496 // set class to read/unread
1497 this.toggle_read_status = function(flag, a_uids)
1499 // mark all message rows as read/unread
1501 var rows = this.message_list.rows;
1502 for (var i=0; i<a_uids.length; i++)
1507 rows[uid].unread = (flag=='unread' ? true : false);
1509 if (rows[uid].classname.indexOf('unread')<0 && rows[uid].unread)
1511 rows[uid].classname += ' unread';
1512 this.set_classname(rows[uid].obj, 'unread', true);
1514 if (this.env.unreadicon)
1515 icn_src = this.env.unreadicon;
1517 else if (!rows[uid].unread)
1519 rows[uid].classname = rows[uid].classname.replace(/\s*unread/, '');
1520 this.set_classname(rows[uid].obj, 'unread', false);
1522 if (rows[uid].replied && this.env.repliedicon)
1523 icn_src = this.env.repliedicon;
1524 else if (this.env.messageicon)
1525 icn_src = this.env.messageicon;
1528 if (rows[uid].icon && icn_src)
1529 rows[uid].icon.src = icn_src;
1533 this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag='+flag);
1536 // mark all message rows as deleted/undeleted
1537 this.toggle_delete_status = function(a_uids)
1539 if (this.env.read_when_deleted)
1540 this.toggle_read_status('read',a_uids);
1542 // if deleting message from "view message" don't bother with delete icon
1543 if (this.env.action == "show")
1546 var rows = this.message_list.rows;
1547 if (a_uids.length==1)
1549 if (rows[a_uids[0]] && rows[a_uids[0]].classname.indexOf('deleted') < 0)
1550 this.flag_as_deleted(a_uids);
1552 this.flag_as_undeleted(a_uids);
1557 var all_deleted = true;
1558 for (var i=0; i<a_uids.length; i++)
1562 if (rows[uid].classname.indexOf('deleted')<0)
1564 all_deleted = false;
1571 this.flag_as_undeleted(a_uids);
1573 this.flag_as_deleted(a_uids);
1579 this.flag_as_undeleted = function(a_uids)
1581 // if deleting message from "view message" don't bother with delete icon
1582 if (this.env.action == "show")
1586 var rows = this.message_list.rows;
1588 for (var i=0; i<a_uids.length; i++)
1592 rows[uid].deleted = false;
1594 if (rows[uid].classname.indexOf('deleted') > 0)
1596 rows[uid].classname = rows[uid].classname.replace(/\s*deleted/, '');
1597 this.set_classname(rows[uid].obj, 'deleted', false);
1599 if (rows[uid].unread && this.env.unreadicon)
1600 icn_src = this.env.unreadicon;
1601 else if (rows[uid].replied && this.env.repliedicon)
1602 icn_src = this.env.repliedicon;
1603 else if (this.env.messageicon)
1604 icn_src = this.env.messageicon;
1605 if (rows[uid].icon && icn_src)
1606 rows[uid].icon.src = icn_src;
1610 this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag=undelete');
1615 this.flag_as_deleted = function(a_uids)
1617 // if deleting message from "view message" don't bother with delete icon
1618 if (this.env.action == "show")
1621 var rows = this.message_list.rows;
1622 for (var i=0; i<a_uids.length; i++)
1626 rows[uid].deleted = true;
1628 if (rows[uid].classname.indexOf('deleted')<0) {
1629 rows[uid].classname += ' deleted';
1630 this.set_classname(rows[uid].obj, 'deleted', true);
1632 if (rows[uid].icon && this.env.deletedicon)
1633 rows[uid].icon.src = this.env.deletedicon;
1637 this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag=delete');
1642 /*********************************************************/
1643 /********* login form methods *********/
1644 /*********************************************************/
1646 // handler for keyboard events on the _user field
1647 this.login_user_keypress = function(e)
1649 var key = rcube_event.get_keycode(e);
1653 if ((key==13) && (elm = rcube_find_object('_pass')))
1661 /*********************************************************/
1662 /********* message compose methods *********/
1663 /*********************************************************/
1666 // checks the input fields before sending a message
1667 this.check_compose_input = function()
1669 // check input fields
1670 var input_to = rcube_find_object('_to');
1671 var input_subject = rcube_find_object('_subject');
1672 var input_message = rcube_find_object('_message');
1674 // check for empty recipient
1675 if (input_to && !rcube_check_email(input_to.value, true))
1677 alert(this.get_label('norecipientwarning'));
1682 // display localized warning for missing subject
1683 if (input_subject && input_subject.value == '')
1685 var subject = prompt(this.get_label('nosubjectwarning'), this.get_label('nosubject'));
1687 // user hit cancel, so don't send
1688 if (!subject && subject !== '')
1690 input_subject.focus();
1695 input_subject.value = subject ? subject : this.get_label('nosubject');
1699 // check for empty body
1700 if ((input_message.value == '' && (!window.tinyMCE || tinyMCE.getContent() == '')) && !confirm(this.get_label('nobodywarning')))
1702 input_message.focus();
1710 this.set_spellcheck_state = function(s)
1712 this.spellcheck_ready = (s=='check_spelling' || s=='ready');
1713 this.enable_command('spellcheck', this.spellcheck_ready);
1717 this.set_draft_id = function(id)
1720 if (f = rcube_find_object('_draft_saveid'))
1724 this.auto_save_start = function()
1726 if (this.env.draft_autosave)
1727 this.save_timer = self.setTimeout(function(){ ref.command("savedraft"); }, this.env.draft_autosave * 1000);
1729 // Unlock interface now that saving is complete
1734 this.compose_field_hash = function(save)
1736 // check input fields
1737 var input_to = rcube_find_object('_to');
1738 var input_cc = rcube_find_object('_to');
1739 var input_bcc = rcube_find_object('_to');
1740 var input_subject = rcube_find_object('_subject');
1741 var input_message = rcube_find_object('_message');
1744 if (input_to && input_to.value)
1745 str += input_to.value+':';
1746 if (input_cc && input_cc.value)
1747 str += input_cc.value+':';
1748 if (input_bcc && input_bcc.value)
1749 str += input_bcc.value+':';
1750 if (input_subject && input_subject.value)
1751 str += input_subject.value+':';
1752 if (input_message && input_message.value)
1753 str += input_message.value;
1756 this.cmp_hash = str;
1762 this.change_identity = function(obj)
1764 if (!obj || !obj.options)
1767 var id = obj.options[obj.selectedIndex].value;
1768 var input_message = rcube_find_object('_message');
1769 var message = input_message ? input_message.value : '';
1770 var is_html = (rcube_find_object('_is_html').value == '1');
1773 if (!this.env.identity)
1774 this.env.identity = id
1778 // remove the 'old' signature
1779 if (this.env.identity && this.env.signatures && this.env.signatures[this.env.identity])
1781 sig = this.env.signatures[this.env.identity]['text'];
1782 if (sig.indexOf('-- ')!=0)
1785 p = message.lastIndexOf(sig);
1787 message = message.substring(0, p-1) + message.substring(p+sig.length, message.length);
1790 // add the new signature string
1791 if (this.env.signatures && this.env.signatures[id])
1793 sig = this.env.signatures[id]['text'];
1794 if (this.env.signatures[id]['is_html'])
1796 sig = this.env.signatures[id]['plain_text'];
1798 if (sig.indexOf('-- ')!=0)
1800 message += '\n'+sig;
1805 var eid = tinyMCE.getEditorId('_message');
1806 // editor is a TinyMCE_Control object
1807 var editor = tinyMCE.getInstanceById(eid);
1808 var msgDoc = editor.getDoc();
1809 var msgBody = msgDoc.body;
1811 if (this.env.signatures && this.env.signatures[id])
1813 // Append the signature as a span within the body
1814 var sigElem = msgDoc.getElementById("_rc_sig");
1817 sigElem = msgDoc.createElement("span");
1818 sigElem.setAttribute("id", "_rc_sig");
1819 msgBody.appendChild(sigElem);
1821 if (this.env.signatures[id]['is_html'])
1823 sigElem.innerHTML = this.env.signatures[id]['text'];
1827 sigElem.innerHTML = '<pre>' + this.env.signatures[id]['text'] + '</pre>';
1833 input_message.value = message;
1835 this.env.identity = id;
1840 this.show_attachment_form = function(a)
1842 if (!this.gui_objects.uploadbox)
1846 if (elm = this.gui_objects.uploadbox)
1848 if (a && (list = this.gui_objects.attachmentlist))
1850 var pos = rcube_get_object_pos(list);
1852 var top = pos.y + list.offsetHeight + 10;
1854 elm.style.top = top+'px';
1855 elm.style.left = left+'px';
1858 elm.style.visibility = a ? 'visible' : 'hidden';
1861 // clear upload form
1863 if (!a && this.gui_objects.attachmentform != this.gui_objects.messageform)
1864 this.gui_objects.attachmentform.reset();
1866 catch(e){} // ignore errors
1872 // upload attachment file
1873 this.upload_file = function(form)
1879 // get file input fields
1881 for (var n=0; n<form.elements.length; n++)
1882 if (form.elements[n].type=='file' && form.elements[n].value)
1888 // create hidden iframe and post upload form
1891 var ts = new Date().getTime();
1892 var frame_name = 'rcmupload'+ts;
1894 // have to do it this way for IE
1895 // otherwise the form will be posted to a new window
1896 if(document.all && !window.opera)
1898 var html = '<iframe name="'+frame_name+'" src="program/blank.gif" style="width:0;height:0;visibility:hidden;"></iframe>';
1899 document.body.insertAdjacentHTML('BeforeEnd',html);
1901 else // for standards-compilant browsers
1903 var frame = document.createElement('IFRAME');
1904 frame.name = frame_name;
1907 frame.style.visibility = 'hidden';
1908 document.body.appendChild(frame);
1911 form.target = frame_name;
1912 form.action = this.env.comm_path+'&_action=upload';
1913 form.setAttribute('enctype', 'multipart/form-data');
1917 // set reference to the form object
1918 this.gui_objects.attachmentform = form;
1923 // add file name to attachment list
1924 // called from upload page
1925 this.add2attachment_list = function(name, content)
1927 if (!this.gui_objects.attachmentlist)
1930 var li = document.createElement('LI');
1932 li.innerHTML = content;
1933 this.gui_objects.attachmentlist.appendChild(li);
1937 this.remove_from_attachment_list = function(name)
1939 if (!this.gui_objects.attachmentlist)
1942 var list = this.gui_objects.attachmentlist.getElementsByTagName("li");
1943 for (i=0;i<list.length;i++)
1944 if (list[i].id == name)
1945 this.gui_objects.attachmentlist.removeChild(list[i]);
1948 this.remove_attachment = function(name)
1951 this.http_post('remove-attachment', '_file='+urlencode(name));
1956 // send remote request to add a new contact
1957 this.add_contact = function(value)
1960 this.http_post('addcontact', '_address='+value);
1965 // send remote request to search mail or contacts
1966 this.qsearch = function(value)
1970 if (this.message_list)
1971 this.message_list.clear();
1972 else if (this.contact_list) {
1973 this.contact_list.clear(true);
1974 this.show_contentframe(false);
1978 this.env.current_page = 1;
1979 this.set_busy(true, 'searching');
1980 this.http_request('search', '_q='+urlencode(value)+(this.env.mailbox ? '&_mbox='+this.env.mailbox : '')+(this.env.source ? '&_source='+urlencode(this.env.source) : ''), true);
1985 // reset quick-search form
1986 this.reset_qsearch = function()
1988 if (this.gui_objects.qsearchbox)
1989 this.gui_objects.qsearchbox.value = '';
1991 this.env.search_request = null;
1996 this.sent_successfully = function(msg)
1998 this.list_mailbox();
1999 this.display_message(msg, 'confirmation', true);
2003 /*********************************************************/
2004 /********* keyboard live-search methods *********/
2005 /*********************************************************/
2008 // handler for keyboard events on address-fields
2009 this.ksearch_keypress = function(e, obj)
2011 if (typeof(this.env.contacts)!='object' || !this.env.contacts.length)
2014 if (this.ksearch_timer)
2015 clearTimeout(this.ksearch_timer);
2018 var key = rcube_event.get_keycode(e);
2019 var mod = rcube_event.get_modifier(e);
2024 case 40: // key down
2025 if (!this.ksearch_pane)
2028 var dir = key==38 ? 1 : 0;
2031 highlight = document.getElementById('rcmksearchSelected');
2033 highlight = this.ksearch_pane.ul.firstChild;
2035 if (highlight && (next = dir ? highlight.previousSibling : highlight.nextSibling))
2037 highlight.removeAttribute('id');
2038 this.set_classname(highlight, 'selected', false);
2043 next.setAttribute('id', 'rcmksearchSelected');
2044 this.set_classname(next, 'selected', true);
2045 this.ksearch_selected = next._rcm_id;
2048 return rcube_event.cancel(e);
2051 if(mod == SHIFT_KEY)
2055 if (this.ksearch_selected===null || !this.ksearch_input || !this.ksearch_value)
2058 // insert selected address and hide ksearch pane
2059 this.insert_recipient(this.ksearch_selected);
2060 this.ksearch_hide();
2062 return rcube_event.cancel(e);
2065 this.ksearch_hide();
2071 this.ksearch_timer = setTimeout(function(){ ref.ksearch_get_results(); }, 200);
2072 this.ksearch_input = obj;
2078 this.insert_recipient = function(id)
2080 if (!this.env.contacts[id] || !this.ksearch_input)
2084 var inp_value = this.ksearch_input.value.toLowerCase();
2085 var cpos = this.get_caret_pos(this.ksearch_input);
2086 var p = inp_value.lastIndexOf(this.ksearch_value, cpos);
2088 // replace search string with full address
2089 var pre = this.ksearch_input.value.substring(0, p);
2090 var end = this.ksearch_input.value.substring(p+this.ksearch_value.length, this.ksearch_input.value.length);
2091 var insert = this.env.contacts[id]+', ';
2092 this.ksearch_input.value = pre + insert + end;
2094 // set caret to insert pos
2095 cpos = p+insert.length;
2096 if (this.ksearch_input.setSelectionRange)
2097 this.ksearch_input.setSelectionRange(cpos, cpos);
2102 // address search processor
2103 this.ksearch_get_results = function()
2105 var inp_value = this.ksearch_input ? this.ksearch_input.value : null;
2106 if (inp_value===null)
2109 // get string from current cursor pos to last comma
2110 var cpos = this.get_caret_pos(this.ksearch_input);
2111 var p = inp_value.lastIndexOf(',', cpos-1);
2112 var q = inp_value.substring(p+1, cpos);
2114 // trim query string
2115 q = q.replace(/(^\s+|\s+$)/g, '').toLowerCase();
2117 if (!q.length || q==this.ksearch_value)
2119 if (!q.length && this.ksearch_pane && this.ksearch_pane.visible)
2120 this.ksearch_pane.show(0);
2125 this.ksearch_value = q;
2127 // start searching the contact list
2128 var a_results = new Array();
2129 var a_result_ids = new Array();
2131 for (var i=0; i<this.env.contacts.length; i++)
2133 if (this.env.contacts[i].toLowerCase().indexOf(q)>=0)
2135 a_results[c] = this.env.contacts[i];
2136 a_result_ids[c++] = i;
2138 if (c==15) // limit search results
2143 // display search results
2144 if (c && a_results.length)
2148 // create results pane if not present
2149 if (!this.ksearch_pane)
2151 ul = document.createElement('UL');
2152 this.ksearch_pane = new rcube_layer('rcmKSearchpane', {vis:0, zindex:30000});
2153 this.ksearch_pane.elm.appendChild(ul);
2154 this.ksearch_pane.ul = ul;
2157 ul = this.ksearch_pane.ul;
2159 // remove all search results
2162 // add each result line to list
2163 for (i=0; i<a_results.length; i++)
2165 li = document.createElement('LI');
2166 li.innerHTML = a_results[i].replace(/</, '<').replace(/>/, '>');
2167 li.onmousedown = function(e){ ref.insert_recipient(this._rcm_id); ref.ksearch_pane.show(0); return rcube_event.cancel(e); };
2168 li.style.cursor = 'pointer';
2169 li._rcm_id = a_result_ids[i];
2173 // check if last selected item is still in result list
2174 if (this.ksearch_selected!==null)
2176 p = find_in_array(this.ksearch_selected, a_result_ids);
2177 if (p>=0 && ul.childNodes)
2179 ul.childNodes[p].setAttribute('id', 'rcmksearchSelected');
2180 this.set_classname(ul.childNodes[p], 'selected', true);
2183 this.ksearch_selected = null;
2186 // if no item selected, select the first one
2187 if (this.ksearch_selected===null)
2189 ul.firstChild.setAttribute('id', 'rcmksearchSelected');
2190 this.set_classname(ul.firstChild, 'selected', true);
2191 this.ksearch_selected = a_result_ids[0];
2194 // move the results pane right under the input box and make it visible
2195 var pos = rcube_get_object_pos(this.ksearch_input);
2196 this.ksearch_pane.move(pos.x, pos.y+this.ksearch_input.offsetHeight);
2197 this.ksearch_pane.show(1);
2199 // hide results pane
2201 this.ksearch_hide();
2205 this.ksearch_blur = function(e, obj)
2207 if (this.ksearch_timer)
2208 clearTimeout(this.ksearch_timer);
2210 this.ksearch_value = '';
2211 this.ksearch_input = null;
2213 this.ksearch_hide();
2217 this.ksearch_hide = function()
2219 this.ksearch_selected = null;
2221 if (this.ksearch_pane)
2222 this.ksearch_pane.show(0);
2227 /*********************************************************/
2228 /********* address book methods *********/
2229 /*********************************************************/
2232 this.contactlist_keypress = function(list)
2234 if (list.key_pressed == list.DELETE_KEY)
2235 this.command('delete');
2239 this.contactlist_select = function(list)
2241 if (this.preview_timer)
2242 clearTimeout(this.preview_timer);
2244 var id, frame, ref = this;
2245 if (id = list.get_single_selection())
2246 this.preview_timer = setTimeout(function(){ ref.load_contact(id, 'show'); }, this.dblclick_time + 10);
2247 else if (this.env.contentframe)
2248 this.show_contentframe(false);
2250 this.enable_command('edit', id?true:false);
2251 this.enable_command('compose', list.selection.length > 0);
2252 this.enable_command('delete', list.selection.length && this.env.address_sources && !this.env.address_sources[this.env.source].readonly);
2258 this.list_contacts = function(src, page)
2261 var target = window;
2264 src = this.env.source;
2266 if (page && this.current_page==page && src == this.env.source)
2269 if (src != this.env.source)
2272 this.env.current_page = page;
2273 this.reset_qsearch();
2276 this.select_folder(src, this.env.source);
2277 this.env.source = src;
2279 // load contacts remotely
2280 if (this.gui_objects.contactslist)
2282 this.list_contacts_remote(src, page);
2286 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2288 target = window.frames[this.env.contentframe];
2289 add_url = '&_framed=1';
2292 // also send search request to get the correct listing
2293 if (this.env.search_request)
2294 add_url += '&_search='+this.env.search_request;
2296 this.set_busy(true, 'loading');
2297 target.location.href = this.env.comm_path+(src ? '&_source='+urlencode(src) : '')+(page ? '&_page='+page : '')+add_url;
2301 // send remote request to load contacts list
2302 this.list_contacts_remote = function(src, page)
2304 // clear message list first
2305 this.contact_list.clear(true);
2306 this.show_contentframe(false);
2307 this.enable_command('delete', 'compose', false);
2309 // send request to server
2310 var url = (src ? '_source='+urlencode(src) : '') + (page ? (src?'&':'') + '_page='+page : '');
2311 this.env.source = src;
2313 // also send search request to get the right messages
2314 if (this.env.search_request)
2315 url += '&_search='+this.env.search_request;
2317 this.set_busy(true, 'loading');
2318 this.http_request('list', url, true);
2322 // load contact record
2323 this.load_contact = function(cid, action, framed)
2326 var target = window;
2327 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2329 add_url = '&_framed=1';
2330 target = window.frames[this.env.contentframe];
2331 this.show_contentframe(true);
2336 if (action && (cid || action=='add') && !this.drag_active)
2338 this.set_busy(true);
2339 target.location.href = this.env.comm_path+'&_action='+action+'&_source='+urlencode(this.env.source)+'&_cid='+urlencode(cid) + add_url;
2344 // copy a contact to the specified target (group or directory)
2345 this.copy_contact = function(cid, to)
2348 cid = this.contact_list.get_selection().join(',');
2350 if (to != this.env.source && cid && this.env.address_sources[to] && !this.env.address_sources[to].readonly)
2351 this.http_post('copy', '_cid='+urlencode(cid)+'&_source='+urlencode(this.env.source)+'&_to='+urlencode(to));
2355 this.delete_contacts = function()
2357 // exit if no mailbox specified or if selection is empty
2358 var selection = this.contact_list.get_selection();
2359 if (!(selection.length || this.env.cid) || !confirm(this.get_label('deletecontactconfirm')))
2362 var a_cids = new Array();
2365 a_cids[a_cids.length] = this.env.cid;
2369 for (var n=0; n<selection.length; n++)
2372 a_cids[a_cids.length] = id;
2373 this.contact_list.remove_row(id, (n == selection.length-1));
2376 // hide content frame if we delete the currently displayed contact
2377 if (selection.length == 1)
2378 this.show_contentframe(false);
2381 // send request to server
2382 this.http_post('delete', '_cid='+urlencode(a_cids.join(','))+'&_from='+(this.env.action ? this.env.action : ''));
2387 // update a contact record in the list
2388 this.update_contact_row = function(cid, cols_arr)
2391 if (this.contact_list.rows[cid] && (row = this.contact_list.rows[cid].obj))
2393 for (var c=0; c<cols_arr.length; c++)
2395 row.cells[c].innerHTML = cols_arr[c];
2404 /*********************************************************/
2405 /********* user settings methods *********/
2406 /*********************************************************/
2408 this.init_subscription_list = function()
2411 this.subscription_list = new rcube_list_widget(this.gui_objects.subscriptionlist, {multiselect:false, draggable:true, keyboard:false, toggleselect:true});
2412 this.subscription_list.addEventListener('select', function(o){ p.subscription_select(o); });
2413 this.subscription_list.addEventListener('dragstart', function(o){ p.drag_active = true; });
2414 this.subscription_list.addEventListener('dragend', function(o){ p.subscription_move_folder(o); });
2415 this.subscription_list.row_init = function (row)
2417 var anchors = row.obj.getElementsByTagName('A');
2419 anchors[0].onclick = function() { p.rename_folder(row.id); };
2421 anchors[1].onclick = function() { p.delete_folder(row.id); };
2422 row.obj.onmouseover = function() { p.focus_subscription(row.id); };
2423 row.obj.onmouseout = function() { p.unfocus_subscription(row.id); };
2425 this.subscription_list.init();
2428 this.identity_select = function(list)
2431 if (id = list.get_single_selection())
2432 this.load_identity(id, 'edit-identity');
2435 // load contact record
2436 this.load_identity = function(id, action)
2438 if (action=='edit-identity' && (!id || id==this.env.iid))
2442 var target = window;
2443 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2445 add_url = '&_framed=1';
2446 target = window.frames[this.env.contentframe];
2447 document.getElementById(this.env.contentframe).style.visibility = 'inherit';
2450 if (action && (id || action=='add-identity'))
2452 this.set_busy(true);
2453 target.location.href = this.env.comm_path+'&_action='+action+'&_iid='+id+add_url;
2459 this.delete_identity = function(id)
2461 // exit if no mailbox specified or if selection is empty
2462 var selection = this.identity_list.get_selection();
2463 if (!(selection.length || this.env.iid))
2467 id = this.env.iid ? this.env.iid : selection[0];
2469 // if (this.env.framed && id)
2470 this.goto_url('delete-identity', '_iid='+id, true);
2475 this.focus_subscription = function(id)
2478 var reg = RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
2480 if (this.drag_active && (row = document.getElementById(id)))
2481 if (this.env.subscriptionrows[id] &&
2482 (folder = this.env.subscriptionrows[id][0]))
2484 if (this.check_droptarget(folder) &&
2485 (folder != this.env.folder.replace(reg, '')) &&
2486 (!folder.match(new RegExp('^'+RegExp.escape(this.env.folder+this.env.delimiter)))))
2488 this.set_env('dstfolder', folder);
2489 this.set_classname(row, 'droptarget', true);
2492 else if (this.env.folder.match(new RegExp(RegExp.escape(this.env.delimiter))))
2494 this.set_env('dstfolder', this.env.delimiter);
2495 this.set_classname(this.subscription_list.frame, 'droptarget', true);
2500 this.unfocus_subscription = function(id)
2503 this.set_env('dstfolder', null);
2504 if (this.env.subscriptionrows[id] &&
2505 (row = document.getElementById(id)))
2506 this.set_classname(row, 'droptarget', false);
2508 this.set_classname(this.subscription_list.frame, 'droptarget', false);
2512 this.subscription_select = function(list)
2515 if ((id = list.get_single_selection()) &&
2516 this.env.subscriptionrows['rcmrow'+id] &&
2517 (folder = this.env.subscriptionrows['rcmrow'+id][0]) &&
2518 (find_in_array(this.env.defaultfolders, folder)!=0))
2519 this.set_env('folder', folder);
2521 this.set_env('folder', null);
2525 this.subscription_move_folder = function(list)
2527 var reg = RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
2528 if (this.env.folder && this.env.dstfolder && (this.env.dstfolder != this.env.folder) &&
2529 (this.env.dstfolder != this.env.folder.replace(reg, '')))
2531 var reg = new RegExp('[^'+RegExp.escape(this.env.delimiter)+']*['+RegExp.escape(this.env.delimiter)+']', 'g');
2532 var basename = this.env.folder.replace(reg, '');
2533 var newname = this.env.dstfolder==this.env.delimiter ? basename : this.env.dstfolder+this.env.delimiter+basename;
2534 this.http_post('rename-folder', '_folder_oldname='+urlencode(this.env.folder)+'&_folder_newname='+urlencode(newname));
2536 this.drag_active = false;
2537 this.unfocus_subscription(this.get_folder_row_id(this.env.dstfolder));
2541 // tell server to create and subscribe a new mailbox
2542 this.create_folder = function(name)
2544 if (this.edit_folder)
2545 this.reset_folder_rename();
2548 if ((form = this.gui_objects.editform) && form.elements['_folder_name'])
2549 name = form.elements['_folder_name'].value;
2550 if (this.env.folder)
2551 name = this.env.folder+this.env.delimiter+name;
2554 this.http_post('create-folder', '_name='+urlencode(name), true);
2555 else if (form.elements['_folder_name'])
2556 form.elements['_folder_name'].focus();
2560 // start renaming the mailbox name.
2561 // this will replace the name string with an input field
2562 this.rename_folder = function(id)
2564 var temp, row, form;
2565 var folder = this.env.subscriptionrows[id][0];
2567 // reset current renaming
2568 if (temp = this.edit_folder)
2570 this.reset_folder_rename();
2575 if (id && (row = document.getElementById(id)))
2577 var reg = new RegExp('.*['+RegExp.escape(this.env.delimiter)+']');
2578 this.name_input = document.createElement('INPUT');
2579 this.name_input.value = folder.replace(reg, '');
2580 this.name_input.style.width = '100%';
2581 reg = new RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
2582 this.name_input.setAttribute('parent', folder.replace(reg, ''));
2583 this.name_input.onkeypress = function(e){ rcmail.name_input_keypress(e); };
2585 row.cells[0].replaceChild(this.name_input, row.cells[0].firstChild);
2586 this.edit_folder = id;
2587 this.name_input.select();
2589 if (form = this.gui_objects.editform)
2590 form.onsubmit = function(){ return false; };
2595 // remove the input field and write the current mailbox name to the table cell
2596 this.reset_folder_rename = function()
2598 var cell = this.name_input ? this.name_input.parentNode : null;
2599 if (cell && this.edit_folder && this.env.subscriptionrows[this.edit_folder])
2601 var reg = new RegExp('[^'+RegExp.escape(this.env.delimiter)+']*['+RegExp.escape(this.env.delimiter)+']', 'g');
2602 cell.innerHTML = this.env.subscriptionrows[this.edit_folder][1].replace(reg, ' ');
2605 this.edit_folder = null;
2609 // handler for keyboard events on the input field
2610 this.name_input_keypress = function(e)
2612 var key = rcube_event.get_keycode(e);
2617 var newname = this.name_input ? this.name_input.value : null;
2618 if (this.edit_folder && newname)
2620 if (this.name_input.getAttribute('parent') && this.name_input.getAttribute('parent')!='')
2621 newname = this.name_input.getAttribute('parent')+this.env.delimiter+newname;
2622 this.http_post('rename-folder', '_folder_oldname='+urlencode(this.env.subscriptionrows[this.edit_folder][0])+'&_folder_newname='+urlencode(newname));
2627 this.reset_folder_rename();
2631 // delete a specific mailbox with all its messages
2632 this.delete_folder = function(id)
2634 var folder = this.env.subscriptionrows[id][0];
2636 if (this.edit_folder)
2637 this.reset_folder_rename();
2639 if (folder && confirm(this.get_label('deletefolderconfirm')))
2641 this.http_post('delete-folder', '_mboxes='+urlencode(folder));
2642 this.set_env('folder', null);
2647 // add a new folder to the subscription list by cloning a folder row
2648 this.add_folder_row = function(name, display_name, replace)
2650 name = name.replace('\\',"");
2651 if (!this.gui_objects.subscriptionlist)
2654 for (var refid in this.env.subscriptionrows)
2655 if (this.env.subscriptionrows[refid]!=null)
2659 var tbody = this.gui_objects.subscriptionlist.tBodies[0];
2660 var id = replace && replace.id ? replace.id : 'rcmrow'+(tbody.childNodes.length+1);
2661 var selection = this.subscription_list.get_single_selection();
2663 if (!id || !(refrow = document.getElementById(refid)))
2665 // Refresh page if we don't have a table row to clone
2666 this.goto_url('folders');
2670 // clone a table row if there are existing rows
2671 var row = this.clone_table_row(refrow);
2674 tbody.replaceChild(row, replace);
2676 tbody.appendChild(row);
2679 // add to folder/row-ID map
2680 this.env.subscriptionrows[row.id] = [name, display_name];
2683 row.cells[0].innerHTML = display_name;
2684 if (row.cells[1] && row.cells[1].firstChild.tagName=='INPUT')
2686 row.cells[1].firstChild.value = name;
2687 row.cells[1].firstChild.checked = true;
2690 // add new folder to rename-folder list and clear input field
2691 if (!replace && (form = this.gui_objects.editform))
2693 if (form.elements['_folder_oldname'])
2694 form.elements['_folder_oldname'].options[form.elements['_folder_oldname'].options.length] = new Option(name,name);
2695 if (form.elements['_folder_name'])
2696 form.elements['_folder_name'].value = '';
2699 this.sort_subscription_list();
2700 this.init_subscription_list();
2701 if (selection && document.getElementById('rcmrow'+selection))
2702 this.subscription_list.select_row(selection);
2704 if (document.getElementById(id).scrollIntoView)
2705 document.getElementById(id).scrollIntoView();
2709 // replace an existing table row with a new folder line
2710 this.replace_folder_row = function(oldfolder, newfolder, display_name)
2712 var id = this.get_folder_row_id(oldfolder);
2713 var row = document.getElementById(id);
2715 // replace an existing table row (if found)
2716 this.add_folder_row(newfolder, display_name, row);
2718 // rename folder in rename-folder dropdown
2720 if ((form = this.gui_objects.editform) && (elm = form.elements['_folder_oldname']))
2722 for (var i=0;i<elm.options.length;i++)
2724 if (elm.options[i].value == oldfolder)
2726 elm.options[i].text = display_name;
2727 elm.options[i].value = newfolder;
2732 form.elements['_folder_newname'].value = '';
2737 // remove the table row of a specific mailbox from the table
2738 // (the row will not be removed, just hidden)
2739 this.remove_folder_row = function(folder)
2742 var id = this.get_folder_row_id(folder);
2743 if (id && (row = document.getElementById(id)))
2744 row.style.display = 'none';
2746 // remove folder from rename-folder list
2748 if ((form = this.gui_objects.editform) && form.elements['_folder_oldname'])
2750 for (var i=0;i<form.elements['_folder_oldname'].options.length;i++)
2752 if (form.elements['_folder_oldname'].options[i].value == folder)
2754 form.elements['_folder_oldname'].options[i] = null;
2760 if (form && form.elements['_folder_newname'])
2761 form.elements['_folder_newname'].value = '';
2765 this.subscribe_folder = function(folder)
2768 if ((form = this.gui_objects.editform) && form.elements['_unsubscribed'])
2769 this.change_subscription('_unsubscribed', '_subscribed', 'subscribe');
2771 this.http_post('subscribe', '_mboxes='+urlencode(folder));
2775 this.unsubscribe_folder = function(folder)
2778 if ((form = this.gui_objects.editform) && form.elements['_subscribed'])
2779 this.change_subscription('_subscribed', '_unsubscribed', 'unsubscribe');
2781 this.http_post('unsubscribe', '_mboxes='+urlencode(folder));
2785 this.change_subscription = function(from, to, action)
2788 if (form = this.gui_objects.editform)
2790 var a_folders = new Array();
2791 var list_from = form.elements[from];
2793 for (var i=0; list_from && i<list_from.options.length; i++)
2795 if (list_from.options[i] && list_from.options[i].selected)
2797 a_folders[a_folders.length] = list_from.options[i].value;
2798 list_from[i] = null;
2803 // yes, we have some folders selected
2804 if (a_folders.length)
2806 var list_to = form.elements[to];
2809 for (var n=0; n<a_folders.length; n++)
2811 index = list_to.options.length;
2812 list_to[index] = new Option(a_folders[n]);
2815 this.http_post(action, '_mboxes='+urlencode(a_folders.join(',')));
2821 // helper method to find a specific mailbox row ID
2822 this.get_folder_row_id = function(folder)
2824 for (var id in this.env.subscriptionrows)
2825 if (this.env.subscriptionrows[id] && this.env.subscriptionrows[id][0] == folder)
2831 // duplicate a specific table row
2832 this.clone_table_row = function(row)
2835 var new_row = document.createElement('TR');
2836 for(var n=0; n<row.childNodes.length; n++)
2838 cell = row.childNodes[n];
2839 td = document.createElement('TD');
2842 td.className = cell.className;
2844 td.setAttribute('align', cell.align);
2846 td.innerHTML = cell.innerHTML;
2847 new_row.appendChild(td);
2853 // sort subscription folder list
2854 this.sort_subscription_list = function()
2856 var index = new Array();
2857 var tbody = this.gui_objects.subscriptionlist.tBodies[0];
2858 var swapped = false;
2859 for (var i = 0; i<tbody.childNodes.length; i++)
2860 if (this.env.subscriptionrows[tbody.childNodes[i].id]!=null)
2862 for (i = 0; i<(index.length-1); i++)
2864 var one = tbody.childNodes[index[i]];
2865 var two = tbody.childNodes[index[i+1]];
2866 if (this.env.subscriptionrows[one.id][0].toLowerCase()>
2867 this.env.subscriptionrows[two.id][0].toLowerCase())
2869 var swap = one.cloneNode(true);
2870 tbody.replaceChild(swap, two);
2871 tbody.replaceChild(two, one);
2876 this.sort_subscription_list();
2880 /*********************************************************/
2881 /********* GUI functionality *********/
2882 /*********************************************************/
2885 // eable/disable buttons for page shifting
2886 this.set_page_buttons = function()
2888 this.enable_command('nextpage', (this.env.pagecount > this.env.current_page));
2889 this.enable_command('lastpage', (this.env.pagecount > this.env.current_page));
2890 this.enable_command('previouspage', (this.env.current_page > 1));
2891 this.enable_command('firstpage', (this.env.current_page > 1));
2895 // set button to a specific state
2896 this.set_button = function(command, state)
2898 var a_buttons = this.buttons[command];
2901 if(!a_buttons || !a_buttons.length)
2904 for(var n=0; n<a_buttons.length; n++)
2906 button = a_buttons[n];
2907 obj = document.getElementById(button.id);
2909 // get default/passive setting of the button
2910 if (obj && button.type=='image' && !button.status) {
2911 button.pas = obj._original_src ? obj._original_src : obj.src;
2912 // respect PNG fix on IE browsers
2913 if (obj.runtimeStyle && obj.runtimeStyle.filter && obj.runtimeStyle.filter.match(/src=['"]([^'"]+)['"]/))
2914 button.pas = RegExp.$1;
2916 else if (obj && !button.status)
2917 button.pas = String(obj.className);
2919 // set image according to button state
2920 if (obj && button.type=='image' && button[state])
2922 button.status = state;
2923 obj.src = button[state];
2925 // set class name according to button state
2926 else if (obj && typeof(button[state])!='undefined')
2928 button.status = state;
2929 obj.className = button[state];
2931 // disable/enable input buttons
2932 if (obj && button.type=='input')
2934 button.status = state;
2935 obj.disabled = !state;
2940 // display a specific alttext
2941 this.set_alttext = function(command, label)
2943 if (!this.buttons[command] || !this.buttons[command].length)
2946 var button, obj, link;
2947 for (var n=0; n<this.buttons[command].length; n++)
2949 button = this.buttons[command][n];
2950 obj = document.getElementById(button.id);
2952 if (button.type=='image' && obj)
2954 obj.setAttribute('alt', this.get_label(label));
2955 if ((link = obj.parentNode) && link.tagName == 'A')
2956 link.setAttribute('title', this.get_label(label));
2959 obj.setAttribute('title', this.get_label(label));
2963 // mouse over button
2964 this.button_over = function(command, id)
2966 var a_buttons = this.buttons[command];
2969 if(!a_buttons || !a_buttons.length)
2972 for(var n=0; n<a_buttons.length; n++)
2974 button = a_buttons[n];
2975 if(button.id==id && button.status=='act')
2977 img = document.getElementById(button.id);
2978 if (img && button.over)
2979 img.src = button.over;
2984 // mouse down on button
2985 this.button_sel = function(command, id)
2987 var a_buttons = this.buttons[command];
2990 if(!a_buttons || !a_buttons.length)
2993 for(var n=0; n<a_buttons.length; n++)
2995 button = a_buttons[n];
2996 if(button.id==id && button.status=='act')
2998 img = document.getElementById(button.id);
2999 if (img && button.sel)
3000 img.src = button.sel;
3005 // mouse out of button
3006 this.button_out = function(command, id)
3008 var a_buttons = this.buttons[command];
3011 if(!a_buttons || !a_buttons.length)
3014 for(var n=0; n<a_buttons.length; n++)
3016 button = a_buttons[n];
3017 if(button.id==id && button.status=='act')
3019 img = document.getElementById(button.id);
3020 if (img && button.act)
3021 img.src = button.act;
3027 // set/unset a specific class name
3028 this.set_classname = function(obj, classname, set)
3030 var reg = new RegExp('\s*'+classname, 'i');
3031 if (!set && obj.className.match(reg))
3032 obj.className = obj.className.replace(reg, '');
3033 else if (set && !obj.className.match(reg))
3034 obj.className += ' '+classname;
3038 // write to the document/window title
3039 this.set_pagetitle = function(title)
3041 if (title && document.title)
3042 document.title = title;
3046 // display a system message
3047 this.display_message = function(msg, type, hold)
3049 if (!this.loaded) // save message in order to display after page loaded
3051 this.pending_message = new Array(msg, type);
3055 // pass command to parent window
3056 if (this.env.framed && parent.rcmail)
3057 return parent.rcmail.display_message(msg, type, hold);
3059 if (!this.gui_objects.message)
3062 if (this.message_timer)
3063 clearTimeout(this.message_timer);
3067 cont = '<div class="'+type+'">'+cont+'</div>';
3070 this.gui_objects.message.innerHTML = cont;
3071 this.gui_objects.message.style.display = 'block';
3073 if (type!='loading')
3074 this.gui_objects.message.onmousedown = function(){ _rcube.hide_message(); return true; };
3077 this.message_timer = setTimeout(function(){ ref.hide_message(); }, this.message_time);
3081 // make a message row disapear
3082 this.hide_message = function()
3084 if (this.gui_objects.message)
3086 this.gui_objects.message.style.display = 'none';
3087 this.gui_objects.message.onmousedown = null;
3092 // mark a mailbox as selected and set environment variable
3093 this.select_folder = function(name, old)
3095 if (this.gui_objects.folderlist)
3097 var current_li, target_li;
3099 if ((current_li = this.get_folder_li(old)))
3101 this.set_classname(current_li, 'selected', false);
3102 this.set_classname(current_li, 'unfocused', false);
3105 if ((target_li = this.get_folder_li(name)))
3107 this.set_classname(target_li, 'unfocused', false);
3108 this.set_classname(target_li, 'selected', true);
3113 // helper method to find a folder list item
3114 this.get_folder_li = function(name)
3116 if (this.gui_objects.folderlist)
3118 name = String(name).replace(this.identifier_expr, '');
3119 return document.getElementById('rcmli'+name);
3126 // for reordering column array, Konqueror workaround
3127 this.set_message_coltypes = function(coltypes)
3129 this.coltypes = coltypes;
3131 // set correct list titles
3133 var thead = this.gui_objects.messagelist ? this.gui_objects.messagelist.tHead : null;
3134 for (var n=0; thead && n<this.coltypes.length; n++)
3136 col = this.coltypes[n];
3137 if ((cell = thead.rows[0].cells[n+1]) && (col=='from' || col=='to'))
3139 // if we have links for sorting, it's a bit more complicated...
3140 if (cell.firstChild && cell.firstChild.tagName=='A')
3142 cell.firstChild.innerHTML = this.get_label(this.coltypes[n]);
3143 cell.firstChild.onclick = function(){ return rcmail.command('sort', this.__col, this); };
3144 cell.firstChild.__col = col;
3147 cell.innerHTML = this.get_label(this.coltypes[n]);
3149 cell.id = 'rcmHead'+col;
3155 // create a table row in the message list
3156 this.add_message_row = function(uid, cols, flags, attachment, attop)
3158 if (!this.gui_objects.messagelist || !this.message_list)
3161 var tbody = this.gui_objects.messagelist.tBodies[0];
3162 var rowcount = tbody.rows.length;
3163 var even = rowcount%2;
3165 this.env.messages[uid] = {deleted:flags.deleted?1:0,
3166 replied:flags.replied?1:0,
3167 unread:flags.unread?1:0};
3169 var row = document.createElement('TR');
3170 row.id = 'rcmrow'+uid;
3171 row.className = 'message '+(even ? 'even' : 'odd')+(flags.unread ? ' unread' : '')+(flags.deleted ? ' deleted' : '');
3173 if (this.message_list.in_selection(uid))
3174 row.className += ' selected';
3176 var icon = flags.deleted && this.env.deletedicon ? this.env.deletedicon:
3177 (flags.unread && this.env.unreadicon ? this.env.unreadicon :
3178 (flags.replied && this.env.repliedicon ? this.env.repliedicon : this.env.messageicon));
3180 var col = document.createElement('TD');
3181 col.className = 'icon';
3182 col.innerHTML = icon ? '<img src="'+icon+'" alt="" border="0" />' : '';
3183 row.appendChild(col);
3185 // add each submitted col
3186 for (var n = 0; n < this.coltypes.length; n++)
3188 var c = this.coltypes[n];
3189 col = document.createElement('TD');
3190 col.className = String(c).toLowerCase();
3191 col.innerHTML = cols[c];
3192 row.appendChild(col);
3195 col = document.createElement('TD');
3196 col.className = 'icon';
3197 col.innerHTML = attachment && this.env.attachmenticon ? '<img src="'+this.env.attachmenticon+'" alt="" border="0" />' : '';
3198 row.appendChild(col);
3200 this.message_list.insert_row(row, attop);
3204 // replace content of row count display
3205 this.set_rowcount = function(text)
3207 if (this.gui_objects.countdisplay)
3208 this.gui_objects.countdisplay.innerHTML = text;
3210 // update page navigation buttons
3211 this.set_page_buttons();
3214 // replace content of quota display
3215 this.set_quota = function()
3217 if (this.gui_objects.quotadisplay &&
3218 this.gui_objects.quotadisplay.attributes.getNamedItem('display') &&
3219 this.gui_objects.quotadisplay.attributes.getNamedItem('id'))
3220 this.http_request('quotadisplay', '_display='+
3221 this.gui_objects.quotadisplay.attributes.getNamedItem('display').nodeValue+
3222 '&_id='+this.gui_objects.quotadisplay.attributes.getNamedItem('id').nodeValue, false);
3226 // update the mailboxlist
3227 this.set_unread_count = function(mbox, count, set_title)
3229 if (!this.gui_objects.mailboxlist)
3233 var item = this.get_folder_li(mbox);
3234 mbox = String(mbox).toLowerCase().replace(this.identifier_expr, '');
3236 if (item && item.className && item.className.indexOf('mailbox '+mbox)>=0)
3239 text_obj = item.firstChild;
3240 reg = /\s+\([0-9]+\)$/i;
3242 if (count && text_obj.innerHTML.match(reg))
3243 text_obj.innerHTML = text_obj.innerHTML.replace(reg, ' ('+count+')');
3245 text_obj.innerHTML += ' ('+count+')';
3247 text_obj.innerHTML = text_obj.innerHTML.replace(reg, '');
3249 // set the right classes
3250 this.set_classname(item, 'unread', count>0 ? true : false);
3253 // set unread count to window title
3254 reg = /^\([0-9]+\)\s+/i;
3255 if (set_title && document.title)
3257 var doc_title = String(document.title);
3260 if (count && doc_title.match(reg))
3261 new_title = doc_title.replace(reg, '('+count+') ');
3263 new_title = '('+count+') '+doc_title;
3265 new_title = doc_title.replace(reg, '');
3267 this.set_pagetitle(new_title);
3272 // add row to contacts list
3273 this.add_contact_row = function(cid, cols, select)
3275 if (!this.gui_objects.contactslist || !this.gui_objects.contactslist.tBodies[0])
3278 var tbody = this.gui_objects.contactslist.tBodies[0];
3279 var rowcount = tbody.rows.length;
3280 var even = rowcount%2;
3282 var row = document.createElement('TR');
3283 row.id = 'rcmrow'+cid;
3284 row.className = 'contact '+(even ? 'even' : 'odd');
3286 if (this.contact_list.in_selection(cid))
3287 row.className += ' selected';
3289 // add each submitted col
3292 col = document.createElement('TD');
3293 col.className = String(c).toLowerCase();
3294 col.innerHTML = cols[c];
3295 row.appendChild(col);
3298 this.contact_list.insert_row(row);
3302 this.toggle_editor = function(checkbox, textElementName)
3304 var ischecked = checkbox.checked;
3307 tinyMCE.execCommand('mceAddControl', true, textElementName);
3311 tinyMCE.execCommand('mceRemoveControl', true, textElementName);
3317 /********************************************************/
3318 /********* remote request methods *********/
3319 /********************************************************/
3321 this.redirect = function(url, lock)
3323 if (lock || lock === null)
3324 this.set_busy(true);
3326 if (this.env.framed && window.parent)
3327 parent.location.href = url;
3329 location.href = url;
3332 this.goto_url = function(action, query, lock)
3334 var querystring = query ? '&'+query : '';
3335 this.redirect(this.env.comm_path+'&_action='+action+querystring, lock);
3339 this.http_sockets = new Array();
3341 // find a non-busy socket or create a new one
3342 this.get_request_obj = function()
3344 for (var n=0; n<this.http_sockets.length; n++)
3346 if (!this.http_sockets[n].busy)
3347 return this.http_sockets[n];
3350 // create a new XMLHTTP object
3351 var i = this.http_sockets.length;
3352 this.http_sockets[i] = new rcube_http_request();
3354 return this.http_sockets[i];
3358 // send a http request to the server
3359 this.http_request = function(action, querystring, lock)
3361 var request_obj = this.get_request_obj();
3362 querystring += (querystring ? '&' : '') + '_remote=1';
3364 // add timestamp to request url to avoid cacheing problems in Safari
3366 querystring += '&_ts='+(new Date().getTime());
3371 console.log('HTTP request: '+this.env.comm_path+'&_action='+action+'&'+querystring);
3374 this.set_busy(true);
3377 request_obj.__lock = lock ? true : false;
3378 request_obj.__action = action;
3379 request_obj.onerror = function(o){ ref.http_error(o); };
3380 request_obj.oncomplete = function(o){ ref.http_response(o); };
3381 request_obj.GET(this.env.comm_path+'&_action='+action+'&'+querystring);
3385 // send a http POST request to the server
3386 this.http_post = function(action, postdata, lock)
3389 if (postdata && typeof(postdata) == 'object')
3390 postdata._remote = 1;
3392 postdata += (postdata ? '&' : '') + '_remote=1';
3395 if (request_obj = this.get_request_obj())
3397 console.log('HTTP POST: '+this.env.comm_path+'&_action='+action);
3400 this.set_busy(true);
3403 request_obj.__lock = lock ? true : false;
3404 request_obj.__action = action;
3405 request_obj.onerror = function(o){ rcm.http_error(o); };
3406 request_obj.oncomplete = function(o){ rcm.http_response(o); };
3407 request_obj.POST(this.env.comm_path+'&_action='+action, postdata);
3411 // handle HTTP response
3412 this.http_response = function(request_obj)
3414 var ctype = request_obj.get_header('Content-Type');
3416 ctype = String(ctype).toLowerCase();
3417 var ctype_array=ctype.split(";");
3418 ctype = ctype_array[0];
3421 if (request_obj.__lock)
3422 this.set_busy(false);
3424 console.log(request_obj.get_text());
3426 // if we get javascript code from server -> execute it
3427 if (request_obj.get_text() && (ctype=='text/javascript' || ctype=='application/x-javascript'))
3428 eval(request_obj.get_text());
3430 // process the response data according to the sent action
3431 switch (request_obj.__action)
3435 if (this.env.action=='show')
3436 this.command('list');
3437 else if (this.message_list)
3438 this.message_list.init();
3442 if (this.env.messagecount)
3443 this.enable_command('purge', (this.env.mailbox==this.env.trash_mailbox || this.env.mailbox==this.env.junk_mailbox));
3444 this.msglist_select(this.message_list);
3447 this.enable_command('select-all', 'select-none', 'expunge', this.env.messagecount ? true : false);
3451 request_obj.reset();
3455 // handle HTTP request errors
3456 this.http_error = function(request_obj)
3458 //alert('Error sending request: '+request_obj.url);
3460 if (request_obj.__lock)
3461 this.set_busy(false);
3463 request_obj.reset();
3464 request_obj.__lock = false;
3468 // use an image to send a keep-alive siganl to the server
3469 this.send_keep_alive = function()
3472 this.http_request('keep-alive', '_t='+d.getTime());
3476 // send periodic request to check for recent messages
3477 this.check_for_recent = function()
3481 this.send_keep_alive();
3485 this.set_busy(true, 'checkingmail');
3486 this.http_request('check-recent', '_t='+(new Date().getTime()), true);
3490 /********************************************************/
3491 /********* helper methods *********/
3492 /********************************************************/
3494 // check if we're in show mode or if we have a unique selection
3495 // and return the message uid
3496 this.get_single_uid = function()
3498 return this.env.uid ? this.env.uid : (this.message_list ? this.message_list.get_single_selection() : null);
3501 // same as above but for contacts
3502 this.get_single_cid = function()
3504 return this.env.cid ? this.env.cid : (this.contact_list ? this.contact_list.get_single_selection() : null);
3508 this.get_caret_pos = function(obj)
3510 if (typeof(obj.selectionEnd)!='undefined')
3511 return obj.selectionEnd;
3513 else if (document.selection && document.selection.createRange)
3515 var range = document.selection.createRange();
3516 if (range.parentElement()!=obj)
3519 var gm = range.duplicate();
3520 if (obj.tagName=='TEXTAREA')
3521 gm.moveToElementText(obj);
3523 gm.expand('textedit');
3525 gm.setEndPoint('EndToStart', range);
3526 var p = gm.text.length;
3528 return p<=obj.value.length ? p : -1;
3532 return obj.value.length;
3536 this.set_caret2start = function(obj)
3538 if (obj.createTextRange)
3540 var range = obj.createTextRange();
3541 range.collapse(true);
3544 else if (obj.setSelectionRange)
3545 obj.setSelectionRange(0,0);
3551 // set all fields of a form disabled
3552 this.lock_form = function(form, lock)
3554 if (!form || !form.elements)
3558 for (var n=0; n<form.elements.length; n++)
3560 type = form.elements[n];
3564 form.elements[n].disabled = lock;
3568 } // end object rcube_webmail
3573 * Class for sending HTTP requests
3576 function rcube_http_request()
3580 this.xmlhttp = null;
3583 // reset object properties
3584 this.reset = function()
3586 // set unassigned event handlers
3587 this.onloading = function(){ };
3588 this.onloaded = function(){ };
3589 this.oninteractive = function(){ };
3590 this.oncomplete = function(){ };
3591 this.onabort = function(){ };
3592 this.onerror = function(){ };
3596 this.xmlhttp = null;
3600 // create HTMLHTTP object
3601 this.build = function()
3603 if (window.XMLHttpRequest)
3604 this.xmlhttp = new XMLHttpRequest();
3605 else if (window.ActiveXObject)
3607 try { this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
3608 catch(e) { this.xmlhttp = null; }
3617 this.GET = function(url)
3631 this.xmlhttp.onreadystatechange = function(){ _ref.xmlhttp_onreadystatechange(); };
3632 this.xmlhttp.open('GET', url);
3633 this.xmlhttp.setRequestHeader('X-RoundCube-Referer', bw.get_cookie('roundcube_sessid'));
3634 this.xmlhttp.send(null);
3638 this.POST = function(url, body, contentType)
3640 // default value for contentType if not provided
3641 if (typeof(contentType) == 'undefined')
3642 contentType = 'application/x-www-form-urlencoded';
3652 var req_body = body;
3653 if (typeof(body) == 'object')
3657 req_body += (req_body ? '&' : '') + p+'='+urlencode(body[p]);
3664 this.xmlhttp.onreadystatechange = function() { ref.xmlhttp_onreadystatechange(); };
3665 this.xmlhttp.open('POST', url, true);
3666 this.xmlhttp.setRequestHeader('Content-Type', contentType);
3667 this.xmlhttp.setRequestHeader('X-RoundCube-Referer', bw.get_cookie('roundcube_sessid'));
3668 this.xmlhttp.send(req_body);
3672 // handle onreadystatechange event
3673 this.xmlhttp_onreadystatechange = function()
3675 if(this.xmlhttp.readyState == 1)
3676 this.onloading(this);
3678 else if(this.xmlhttp.readyState == 2)
3679 this.onloaded(this);
3681 else if(this.xmlhttp.readyState == 3)
3682 this.oninteractive(this);
3684 else if(this.xmlhttp.readyState == 4)
3687 if (this.xmlhttp.status == 0)
3689 else if(this.xmlhttp.status == 200)
3690 this.oncomplete(this);
3704 // getter method for HTTP headers
3705 this.get_header = function(name)
3707 return this.xmlhttp.getResponseHeader(name);
3710 this.get_text = function()
3712 return this.xmlhttp.responseText;
3715 this.get_xml = function()
3717 return this.xmlhttp.responseXML;
3722 } // end class rcube_http_request
3725 // helper function to call the init method with a delay
3726 function call_init(o)
3728 if (window[o] && window[o].init)
3729 setTimeout(o+'.init()', 200);