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 615 2007-06-13 05:57:44Z thomasb $
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 = 5000;
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 // make preview/message frame visible
169 if (this.env.action == 'preview' && this.env.framed && parent.rcmail)
171 this.enable_command('compose', 'add-contact', false);
172 parent.rcmail.show_contentframe(true);
173 parent.rcmail.mark_message('read', this.env.uid);
176 if ((this.env.action=='show' || this.env.action=='preview') && this.env.blockedobjects)
178 if (this.gui_objects.remoteobjectsmsg)
179 this.gui_objects.remoteobjectsmsg.style.display = 'block';
180 this.enable_command('load-images', true);
183 if (this.env.action=='compose')
185 this.enable_command('add-attachment', 'send-attachment', 'remove-attachment', 'send', true);
186 if (this.env.spellcheck)
188 this.env.spellcheck.spelling_state_observer = function(s){ ref.set_spellcheck_state(s); };
189 this.set_spellcheck_state('ready');
191 if (this.env.drafts_mailbox)
192 this.enable_command('savedraft', true);
195 if (this.env.messagecount)
196 this.enable_command('select-all', 'select-none', 'sort', 'expunge', true);
198 if (this.env.messagecount && (this.env.mailbox==this.env.trash_mailbox || this.env.mailbox==this.env.junk_mailbox))
199 this.enable_command('purge', true);
201 this.set_page_buttons();
206 // init message compose form
207 if (this.env.action=='compose')
208 this.init_messageform();
210 // show printing dialog
211 if (this.env.action=='print')
214 // get unread count for each mailbox
215 if (this.gui_objects.mailboxlist)
217 this.gui_objects.folderlist = this.gui_objects.mailboxlist;
218 this.http_request('getunread', '');
225 if (this.gui_objects.contactslist)
227 this.contact_list = new rcube_list_widget(this.gui_objects.contactslist, {multiselect:true, draggable:true, keyboard:true});
228 this.contact_list.addEventListener('keypress', function(o){ p.contactlist_keypress(o); });
229 this.contact_list.addEventListener('select', function(o){ p.contactlist_select(o); });
230 this.contact_list.addEventListener('dragstart', function(o){ p.drag_active = true; });
231 this.contact_list.addEventListener('dragend', function(o){ p.drag_active = false; });
232 this.contact_list.init();
235 this.contact_list.highlight_row(this.env.cid);
237 if (this.gui_objects.contactslist.parentNode)
239 this.gui_objects.contactslist.parentNode.onmousedown = function(e){ return p.click_on_list(e); };
240 document.onmouseup = function(e){ return p.doc_mouse_up(e); };
243 this.contact_list.focus();
246 this.set_page_buttons();
248 if (this.env.address_sources && !this.env.address_sources[this.env.source].readonly)
249 this.enable_command('add', true);
252 this.enable_command('show', 'edit', true);
254 if ((this.env.action=='add' || this.env.action=='edit') && this.gui_objects.editform)
255 this.enable_command('save', true);
257 this.enable_command('search', 'reset-search', 'moveto', true);
259 this.enable_command('list', true);
264 this.enable_command('preferences', 'identities', 'save', 'folders', true);
266 if (this.env.action=='identities' || this.env.action=='edit-identity' || this.env.action=='add-identity')
267 this.enable_command('edit', 'add', 'delete', true);
269 if (this.env.action=='edit-identity' || this.env.action=='add-identity')
270 this.enable_command('save', true);
272 if (this.env.action=='folders')
273 this.enable_command('subscribe', 'unsubscribe', 'create-folder', 'rename-folder', 'delete-folder', true);
275 if (this.gui_objects.identitieslist)
277 this.identity_list = new rcube_list_widget(this.gui_objects.identitieslist, {multiselect:false, draggable:false, keyboard:false});
278 this.identity_list.addEventListener('select', function(o){ p.identity_select(o); });
279 this.identity_list.init();
280 this.identity_list.focus();
283 this.identity_list.highlight_row(this.env.iid);
289 var input_user = rcube_find_object('_user');
290 var input_pass = rcube_find_object('_pass');
292 input_user.onkeypress = function(e){ return rcmail.login_user_keypress(e); };
293 if (input_user && input_user.value=='')
298 this.enable_command('login', true);
306 // enable basic commands
307 this.enable_command('logout', true);
309 // flag object as complete
313 if (this.pending_message)
314 this.display_message(this.pending_message[0], this.pending_message[1]);
316 // start keep-alive interval
317 this.start_keepalive();
320 // execute all foreign onload scripts
321 for (var i=0; i<this.onloads.length; i++)
323 if (typeof(this.onloads[i]) == 'string')
324 eval(this.onloads[i]);
325 else if (typeof(this.onloads[i]) == 'function')
331 // start interval for keep-alive/recent_check signal
332 this.start_keepalive = function()
334 if (this.env.keep_alive && !this.env.framed && this.task=='mail' && this.gui_objects.messagelist)
335 this._int = setInterval(function(){ ref.check_for_recent(); }, this.env.keep_alive * 1000);
336 else if (this.env.keep_alive && !this.env.framed && this.task!='login')
337 this._int = setInterval(function(){ ref.send_keep_alive(); }, this.env.keep_alive * 1000);
341 this.init_message_row = function(row)
344 if (uid && this.env.messages[uid])
346 row.deleted = this.env.messages[uid].deleted ? true : false;
347 row.unread = this.env.messages[uid].unread ? true : false;
348 row.replied = this.env.messages[uid].replied ? true : false;
351 // set eventhandler to message icon
352 if ((row.icon = row.obj.cells[0].childNodes[0]) && row.icon.nodeName=='IMG')
355 row.icon.id = 'msgicn_'+row.uid;
356 row.icon._row = row.obj;
357 row.icon.onmousedown = function(e) { p.command('toggle_status', this); };
362 // init message compose form: set focus and eventhandlers
363 this.init_messageform = function()
365 if (!this.gui_objects.messageform)
368 //this.messageform = this.gui_objects.messageform;
369 var input_from = rcube_find_object('_from');
370 var input_to = rcube_find_object('_to');
371 var input_cc = rcube_find_object('_cc');
372 var input_bcc = rcube_find_object('_bcc');
373 var input_replyto = rcube_find_object('_replyto');
374 var input_subject = rcube_find_object('_subject');
375 var input_message = rcube_find_object('_message');
377 // init live search events
379 this.init_address_input_events(input_to);
381 this.init_address_input_events(input_cc);
383 this.init_address_input_events(input_bcc);
385 // add signature according to selected identity
386 if (input_from && input_from.type=='select-one')
387 this.change_identity(input_from);
389 if (input_to && input_to.value=='')
391 else if (input_subject && input_subject.value=='')
392 input_subject.focus();
393 else if (input_message)
394 this.set_caret2start(input_message);
396 // get summary of all field values
397 this.compose_field_hash(true);
399 // start the auto-save timer
400 this.auto_save_start();
403 this.init_address_input_events = function(obj)
405 var handler = function(e){ return ref.ksearch_keypress(e,this); };
406 var handler2 = function(e){ return ref.ksearch_blur(e,this); };
408 if (obj.addEventListener)
410 obj.addEventListener(bw.safari ? 'keydown' : 'keypress', handler, false);
411 obj.addEventListener('blur', handler2, false);
415 obj.onkeydown = handler;
416 obj.onblur = handler2;
419 obj.setAttribute('autocomplete', 'off');
424 /*********************************************************/
425 /********* client command interface *********/
426 /*********************************************************/
429 // execute a specific command on the web client
430 this.command = function(command, props, obj)
438 // command not supported or allowed
439 if (!this.commands[command])
441 // pass command to parent window
442 if (this.env.framed && parent.rcmail && parent.rcmail.command)
443 parent.rcmail.command(command, props);
449 // check input before leaving compose step
450 if (this.task=='mail' && this.env.action=='compose' && (command=='list' || command=='mail' || command=='addressbook' || command=='settings'))
452 if (this.cmp_hash != this.compose_field_hash() && !confirm(this.get_label('notsentwarning')))
461 if (this.gui_objects.loginform)
462 this.gui_objects.loginform.submit();
466 this.goto_url('logout');
469 // commands to switch task
473 this.switch_task(command);
477 // misc list commands
479 if (this.task=='mail')
481 if (this.env.search_request<0 || (this.env.search_request && props != this.env.mailbox))
482 this.reset_qsearch();
484 this.list_mailbox(props);
486 else if (this.task=='addressbook')
488 if (this.env.search_request<0 || (this.env.search_request && props != this.env.source))
489 this.reset_qsearch();
491 this.list_contacts(props);
492 this.enable_command('add', (this.env.address_sources && !this.env.address_sources[props].readonly));
498 // get the type of sorting
499 var a_sort = props.split('_');
500 var sort_col = a_sort[0];
501 var sort_order = a_sort[1] ? a_sort[1].toUpperCase() : null;
504 // no sort order specified: toggle
505 if (sort_order==null)
507 if (this.env.sort_col==sort_col)
508 sort_order = this.env.sort_order=='ASC' ? 'DESC' : 'ASC';
510 sort_order = this.env.sort_order;
513 if (this.env.sort_col==sort_col && this.env.sort_order==sort_order)
516 // set table header class
517 if (header = document.getElementById('rcmHead'+this.env.sort_col))
518 this.set_classname(header, 'sorted'+(this.env.sort_order.toUpperCase()), false);
519 if (header = document.getElementById('rcmHead'+sort_col))
520 this.set_classname(header, 'sorted'+sort_order, true);
522 // save new sort properties
523 this.env.sort_col = sort_col;
524 this.env.sort_order = sort_order;
526 // reload message list
527 this.list_mailbox('', '', sort_col+'_'+sort_order);
531 this.list_page('next');
535 this.list_page('last');
539 this.list_page('prev');
543 this.list_page('first');
547 if (this.env.messagecount)
548 this.expunge_mailbox(this.env.mailbox);
552 case 'empty-mailbox':
553 if (this.env.messagecount)
554 this.purge_mailbox(this.env.mailbox);
558 // common commands used in multiple tasks
560 if (this.task=='mail')
562 var uid = this.get_single_uid();
563 if (uid && (!this.env.uid || uid != this.env.uid))
565 if (this.env.mailbox == this.env.drafts_mailbox)
566 this.goto_url('compose', '_draft_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
568 this.show_message(uid);
571 else if (this.task=='addressbook')
573 var cid = props ? props : this.get_single_cid();
574 if (cid && !(this.env.action=='show' && cid==this.env.cid))
575 this.load_contact(cid, 'show');
580 if (this.task=='addressbook')
581 this.load_contact(0, 'add');
582 else if (this.task=='settings')
584 this.identity_list.clear_selection();
585 this.load_identity(0, 'add-identity');
591 if (this.task=='addressbook' && (cid = this.get_single_cid()))
592 this.load_contact(cid, 'edit');
593 else if (this.task=='settings' && props)
594 this.load_identity(props, 'edit-identity');
597 case 'save-identity':
599 if (this.gui_objects.editform)
601 var input_pagesize = rcube_find_object('_pagesize');
602 var input_name = rcube_find_object('_name');
603 var input_email = rcube_find_object('_email');
606 if (input_pagesize && isNaN(input_pagesize.value))
608 alert(this.get_label('nopagesizewarning'));
609 input_pagesize.focus();
612 // contacts/identities
615 if (input_name && input_name.value == '')
617 alert(this.get_label('nonamewarning'));
621 else if (input_email && !rcube_check_email(input_email.value))
623 alert(this.get_label('noemailwarning'));
629 this.gui_objects.editform.submit();
635 if (this.task=='mail')
636 this.delete_messages();
638 else if (this.task=='addressbook')
639 this.delete_contacts();
640 // user settings task
641 else if (this.task=='settings')
642 this.delete_identity();
646 // mail task commands
649 if (this.task == 'mail')
650 this.move_messages(props);
651 else if (this.task == 'addressbook' && this.drag_active)
652 this.copy_contact(null, props);
655 case 'toggle_status':
656 if (props && !props._row)
664 uid = props._row.uid;
665 this.message_list.dont_select = true;
666 // toggle read/unread
667 if (this.message_list.rows[uid].deleted) {
669 } else if (!this.message_list.rows[uid].unread)
673 this.mark_message(flag, uid);
678 this.show_message(this.env.uid, true, this.env.action=='preview');
681 case 'load-attachment':
682 var qstring = '_mbox='+this.env.mailbox+'&_uid='+this.env.uid+'&_part='+props.part;
684 // open attachment in frame if it's of a supported mimetype
685 if (this.env.uid && props.mimetype && find_in_array(props.mimetype, this.mimetypes)>=0)
687 if (props.mimetype == 'text/html')
688 qstring += '&_safe=1';
689 this.attachment_win = window.open(this.env.comm_path+'&_action=get&'+qstring+'&_frame=1', 'rcubemailattachment');
690 if (this.attachment_win)
692 setTimeout(function(){ ref.attachment_win.focus(); }, 10);
697 this.goto_url('get', qstring+'&_download=1');
701 this.message_list.select_all(props);
705 this.message_list.clear_selection();
709 if (this.env.next_uid)
710 this.show_message(this.env.next_uid, false, this.env.action=='preview');
714 if (this.env.last_uid)
715 this.show_message(this.env.last_uid);
718 case 'previousmessage':
719 if (this.env.prev_uid)
720 this.show_message(this.env.prev_uid, false, this.env.action=='preview');
724 if (this.env.first_uid)
725 this.show_message(this.env.first_uid);
729 this.check_for_recent();
733 var url = this.env.comm_path+'&_action=compose';
735 if (this.task=='mail' && this.env.mailbox==this.env.drafts_mailbox)
738 if (uid = this.get_single_uid())
739 url += '&_draft_uid='+uid+'&_mbox='+urlencode(this.env.mailbox);
741 // modify url if we're in addressbook
742 else if (this.task=='addressbook')
744 // switch to mail compose step directly
745 if (props && props.indexOf('@') > 0)
747 url = this.get_task_url('mail', url);
748 this.redirect(url + '&_to='+urlencode(props));
752 // use contact_id passed as command parameter
753 var a_cids = new Array();
755 a_cids[a_cids.length] = props;
756 // get selected contacts
757 else if (this.contact_list)
759 var selection = this.contact_list.get_selection();
760 for (var n=0; n<selection.length; n++)
761 a_cids[a_cids.length] = selection[n];
765 this.http_request('mailto', '_cid='+urlencode(a_cids.join(','))+'&_source='+urlencode(this.env.source), true);
770 url += '&_to='+urlencode(props);
772 // don't know if this is necessary...
773 url = url.replace(/&_framed=1/, "");
779 if (this.env.spellcheck && this.env.spellcheck.spellCheck && this.spellcheck_ready)
781 this.env.spellcheck.spellCheck(this.env.spellcheck.check_link);
782 this.set_spellcheck_state('checking');
787 // Reset the auto-save timer
788 self.clearTimeout(this.save_timer);
790 if (!this.gui_objects.messageform)
793 // if saving Drafts is disabled in main.inc.php
794 // or if compose form did not change
795 if (!this.env.drafts_mailbox || this.cmp_hash == this.compose_field_hash())
798 this.set_busy(true, 'savingmessage');
799 var form = this.gui_objects.messageform;
800 form.target = "savetarget";
805 if (!this.gui_objects.messageform)
808 if (!this.check_compose_input())
811 // Reset the auto-save timer
812 self.clearTimeout(this.save_timer);
814 // all checks passed, send message
815 this.set_busy(true, 'sendingmessage');
816 var form = this.gui_objects.messageform;
817 form.target = "savetarget";
818 form._draft.value = '';
821 // clear timeout (sending could take longer)
822 clearTimeout(this.request_timer);
825 case 'add-attachment':
826 this.show_attachment_form(true);
828 case 'send-attachment':
829 // Reset the auto-save timer
830 self.clearTimeout(this.save_timer);
832 this.upload_file(props)
835 case 'remove-attachment':
836 this.remove_attachment(props);
842 if (uid = this.get_single_uid())
843 this.goto_url('compose', '_reply_uid='+uid+'&_mbox='+urlencode(this.env.mailbox)+(command=='reply-all' ? '&_all=1' : ''), true);
848 if (uid = this.get_single_uid())
849 this.goto_url('compose', '_forward_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
854 if (uid = this.get_single_uid())
856 ref.printwin = window.open(this.env.comm_path+'&_action=print&_uid='+uid+'&_mbox='+urlencode(this.env.mailbox)+(this.env.safemode ? '&_safe=1' : ''));
858 setTimeout(function(){ ref.printwin.focus(); }, 20);
864 if (uid = this.get_single_uid())
866 ref.sourcewin = window.open(this.env.comm_path+'&_action=viewsource&_uid='+this.env.uid+'&_mbox='+urlencode(this.env.mailbox));
868 setTimeout(function(){ ref.sourcewin.focus(); }, 20);
873 this.add_contact(props);
878 if (!props && this.gui_objects.qsearchbox)
879 props = this.gui_objects.qsearchbox.value;
888 var s = this.env.search_request;
889 this.reset_qsearch();
891 if (s && this.env.mailbox)
892 this.list_mailbox(this.env.mailbox);
893 else if (s && this.task == 'addressbook')
894 this.list_contacts(this.env.source);
898 // user settings commands
904 this.goto_url('identities');
907 case 'delete-identity':
908 this.delete_identity();
911 this.goto_url('folders');
915 this.subscribe_folder(props);
919 this.unsubscribe_folder(props);
922 case 'create-folder':
923 this.create_folder(props);
926 case 'rename-folder':
927 this.rename_folder(props);
930 case 'delete-folder':
931 if (confirm(this.get_label('deletefolderconfirm')))
932 this.delete_folder(props);
937 return obj ? false : true;
941 // set command enabled or disabled
942 this.enable_command = function()
944 var args = arguments;
945 if(!args.length) return -1;
948 var enable = args[args.length-1];
950 for(var n=0; n<args.length-1; n++)
953 this.commands[command] = enable;
954 this.set_button(command, (enable ? 'act' : 'pas'));
960 // lock/unlock interface
961 this.set_busy = function(a, message)
965 var msg = this.get_label(message);
969 this.display_message(msg, 'loading', true);
975 //document.body.style.cursor = a ? 'wait' : 'default';
977 if (this.gui_objects.editform)
978 this.lock_form(this.gui_objects.editform, a);
980 // clear pending timer
981 if (this.request_timer)
982 clearTimeout(this.request_timer);
984 // set timer for requests
985 if (a && this.env.request_timeout)
986 this.request_timer = setTimeout(function(){ ref.request_timed_out(); }, this.env.request_timeout * 1000);
990 // return a localized string
991 this.get_label = function(name)
993 if (this.labels[name])
994 return this.labels[name];
1000 // switch to another application task
1001 this.switch_task = function(task)
1003 if (this.task===task && task!='mail')
1006 var url = this.get_task_url(task);
1008 url += '&_mbox=INBOX';
1014 this.get_task_url = function(task, url)
1017 url = this.env.comm_path;
1019 return url.replace(/_task=[a-z]+/, '_task='+task);
1023 // called when a request timed out
1024 this.request_timed_out = function()
1026 this.set_busy(false);
1027 this.display_message('Request timed out!', 'error');
1031 /*********************************************************/
1032 /********* event handling methods *********/
1033 /*********************************************************/
1036 this.doc_mouse_up = function(e)
1038 if (this.message_list)
1039 this.message_list.blur();
1040 else if (this.contact_list)
1041 this.contact_list.blur();
1044 this.focus_folder = function(id)
1047 if (this.drag_active && this.check_droptarget(id) && (li = this.get_folder_li(id)))
1048 this.set_classname(li, 'droptarget', true);
1051 this.unfocus_folder = function(id)
1054 if (this.drag_active && (li = this.get_folder_li(id)))
1055 this.set_classname(li, 'droptarget', false);
1058 // onmouseup handler for folder list item
1059 this.folder_mouse_up = function(id)
1061 if (this.drag_active)
1063 this.unfocus_folder(id);
1064 this.command('moveto', id);
1070 this.click_on_list = function(e)
1072 if (this.message_list)
1073 this.message_list.focus();
1074 else if (this.contact_list)
1075 this.contact_list.focus();
1078 if (mbox_li = this.get_folder_li())
1079 this.set_classname(mbox_li, 'unfocused', true);
1081 rcube_event.cancel(e);
1085 this.msglist_select = function(list)
1087 if (this.preview_timer)
1088 clearTimeout(this.preview_timer);
1090 var selected = list.selection.length==1;
1091 if (this.env.mailbox == this.env.drafts_mailbox)
1093 this.enable_command('show', selected);
1094 this.enable_command('delete', 'moveto', list.selection.length>0 ? true : false);
1098 this.enable_command('show', 'reply', 'reply-all', 'forward', 'print', selected);
1099 this.enable_command('delete', 'moveto', list.selection.length>0 ? true : false);
1102 // start timer for message preview (wait for double click)
1103 if (selected && this.env.contentframe)
1104 this.preview_timer = setTimeout(function(){ ref.msglist_get_preview(); }, this.dblclick_time + 10);
1105 else if (this.env.contentframe)
1106 this.show_contentframe(false);
1110 this.msglist_dbl_click = function(list)
1112 if (this.preview_timer)
1113 clearTimeout(this.preview_timer);
1115 var uid = list.get_single_selection();
1116 if (uid && this.env.mailbox == this.env.drafts_mailbox)
1117 this.goto_url('compose', '_draft_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
1119 this.show_message(uid, false, false);
1123 this.msglist_keypress = function(list)
1125 if (list.key_pressed == list.ENTER_KEY)
1126 this.command('show');
1127 else if (list.key_pressed == list.DELETE_KEY)
1128 this.command('delete');
1132 this.msglist_get_preview = function()
1134 var uid = this.get_single_uid();
1135 if (uid && this.env.contentframe && !this.drag_active)
1136 this.show_message(uid, false, true);
1137 else if (this.env.contentframe)
1138 this.show_contentframe(false);
1142 this.check_droptarget = function(id)
1144 if (this.task == 'mail')
1145 return (id != this.env.mailbox);
1146 else if (this.task == 'addressbook')
1147 return (id != this.env.source && this.env.address_sources[id] && !this.env.address_sources[id].readonly);
1151 /*********************************************************/
1152 /********* (message) list functionality *********/
1153 /*********************************************************/
1156 // when user doble-clicks on a row
1157 this.show_message = function(id, safe, preview)
1160 var action = preview ? 'preview': 'show';
1161 var target = window;
1162 if (preview && this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1164 target = window.frames[this.env.contentframe];
1165 add_url = '&_framed=1';
1169 add_url = '&_safe=1';
1173 var url = '&_action='+action+'&_uid='+id+'&_mbox='+urlencode(this.env.mailbox)+add_url;
1174 if (action == 'preview' && String(target.location.href).indexOf(url) >= 0)
1175 this.show_contentframe(true);
1178 this.set_busy(true, 'loading');
1179 target.location.href = this.env.comm_path+url;
1185 this.show_contentframe = function(show)
1188 if (this.env.contentframe && (frm = rcube_find_object(this.env.contentframe)))
1190 if (!show && window.frames[this.env.contentframe] && frames[this.env.contentframe].location.href.indexOf(this.env.blankpage)<0)
1191 frames[this.env.contentframe].location.href = this.env.blankpage;
1193 frm.style.display = show ? 'block' : 'none';
1196 if (!show && this.busy)
1197 this.set_busy(false);
1201 // list a specific page
1202 this.list_page = function(page)
1205 page = this.env.current_page+1;
1207 page = this.env.pagecount;
1208 if (page=='prev' && this.env.current_page>1)
1209 page = this.env.current_page-1;
1210 if (page=='first' && this.env.current_page>1)
1213 if (page > 0 && page <= this.env.pagecount)
1215 this.env.current_page = page;
1217 if (this.task=='mail')
1218 this.list_mailbox(this.env.mailbox, page);
1219 else if (this.task=='addressbook')
1220 this.list_contacts(this.env.source, page);
1225 // list messages of a specific mailbox
1226 this.list_mailbox = function(mbox, page, sort)
1228 this.last_selected = 0;
1230 var target = window;
1233 mbox = this.env.mailbox;
1235 // add sort to url if set
1237 add_url += '&_sort=' + sort;
1239 // also send search request to get the right messages
1240 if (this.env.search_request)
1241 add_url += '&_search='+this.env.search_request;
1243 // set page=1 if changeing to another mailbox
1244 if (!page && mbox != this.env.mailbox)
1247 this.env.current_page = page;
1248 if (this.message_list)
1249 this.message_list.clear_selection();
1250 this.show_contentframe(false);
1253 if (mbox != this.env.mailbox || (mbox == this.env.mailbox && !page && !sort))
1254 add_url += '&_refresh=1';
1256 this.select_folder(mbox, this.env.mailbox);
1257 this.env.mailbox = mbox;
1259 // load message list remotely
1260 if (this.gui_objects.messagelist)
1262 this.list_mailbox_remote(mbox, page, add_url);
1266 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1268 target = window.frames[this.env.contentframe];
1269 add_url += '&_framed=1';
1272 // load message list to target frame/window
1275 this.set_busy(true, 'loading');
1276 target.location.href = this.env.comm_path+'&_mbox='+urlencode(mbox)+(page ? '&_page='+page : '')+add_url;
1281 // send remote request to load message list
1282 this.list_mailbox_remote = function(mbox, page, add_url)
1284 // clear message list first
1285 this.message_list.clear();
1287 // send request to server
1288 var url = '_mbox='+urlencode(mbox)+(page ? '&_page='+page : '');
1289 this.set_busy(true, 'loading');
1290 this.http_request('list', url+add_url, true);
1294 this.expunge_mailbox = function(mbox)
1299 // lock interface if it's the active mailbox
1300 if (mbox == this.env.mailbox)
1303 this.set_busy(true, 'loading');
1304 add_url = '&_reload=1';
1307 // send request to server
1308 var url = '_mbox='+urlencode(mbox);
1309 this.http_post('expunge', url+add_url, lock);
1313 this.purge_mailbox = function(mbox)
1318 if (!confirm(this.get_label('purgefolderconfirm')))
1321 // lock interface if it's the active mailbox
1322 if (mbox == this.env.mailbox)
1325 this.set_busy(true, 'loading');
1326 add_url = '&_reload=1';
1329 // send request to server
1330 var url = '_mbox='+urlencode(mbox);
1331 this.http_post('purge', url+add_url, lock);
1336 // move selected messages to the specified mailbox
1337 this.move_messages = function(mbox)
1339 // exit if current or no mailbox specified or if selection is empty
1340 if (!mbox || !this.env.uid || mbox==this.env.mailbox)
1342 if (!this.message_list || !this.message_list.get_selection().length)
1347 var add_url = '&_target_mbox='+urlencode(mbox)+'&_from='+(this.env.action ? this.env.action : '');
1349 // show wait message
1350 if (this.env.action=='show')
1353 this.set_busy(true, 'movingmessage');
1356 this.show_contentframe(false);
1358 this._with_selected_messages('moveto', lock, add_url);
1361 // delete selected messages from the current mailbox
1362 this.delete_messages = function()
1364 var selection = this.message_list ? this.message_list.get_selection() : new Array();
1366 // exit if no mailbox specified or if selection is empty
1367 if (!this.env.uid && !selection.length)
1370 // if there is a trash mailbox defined and we're not currently in it:
1371 if (this.env.trash_mailbox && String(this.env.mailbox).toLowerCase() != String(this.env.trash_mailbox).toLowerCase())
1373 // if shift was pressed delete it immediately
1374 if (this.message_list && this.message_list.shiftkey)
1376 if (confirm(this.get_label('deletemessagesconfirm')))
1377 this.permanently_remove_messages();
1380 this.move_messages(this.env.trash_mailbox);
1382 // if there is a trash mailbox defined but we *are* in it:
1383 else if (this.env.trash_mailbox && String(this.env.mailbox).toLowerCase() == String(this.env.trash_mailbox).toLowerCase())
1384 this.permanently_remove_messages();
1385 // if there isn't a defined trash mailbox and the config is set to flag for deletion
1386 else if (!this.env.trash_mailbox && this.env.flag_for_deletion)
1388 this.mark_message('delete');
1389 if(this.env.action=="show")
1390 this.command('nextmessage','',this);
1391 else if (selection.length == 1)
1392 this.message_list.select_next();
1394 // if there isn't a defined trash mailbox and the config is set NOT to flag for deletion
1395 else if (!this.env.trash_mailbox)
1396 this.permanently_remove_messages();
1400 // delete the selected messages permanently
1401 this.permanently_remove_messages = function()
1403 // exit if no mailbox specified or if selection is empty
1404 if (!this.env.uid && (!this.message_list || !this.message_list.get_selection().length))
1407 this.show_contentframe(false);
1408 this._with_selected_messages('delete', false, '&_from='+(this.env.action ? this.env.action : ''));
1411 // Send a specifc request with UIDs of all selected messages
1413 this._with_selected_messages = function(action, lock, add_url)
1415 var a_uids = new Array();
1417 a_uids[a_uids.length] = this.env.uid;
1420 var selection = this.message_list.get_selection();
1422 for (var n=0; n<selection.length; n++)
1425 a_uids[a_uids.length] = id;
1426 this.message_list.remove_row(id);
1429 this.message_list.select_next();
1432 // also send search request to get the right messages
1433 if (this.env.search_request)
1434 add_url += '&_search='+this.env.search_request;
1436 // send request to server
1437 this.http_post(action, '_uid='+a_uids.join(',')+'&_mbox='+urlencode(this.env.mailbox)+add_url, lock);
1441 // set a specific flag to one or more messages
1442 this.mark_message = function(flag, uid)
1444 var a_uids = new Array();
1445 var selection = this.message_list ? this.message_list.get_selection() : new Array();
1449 else if (this.env.uid)
1450 a_uids[0] = this.env.uid;
1451 else if (this.message_list)
1453 for (var id, n=0; n<selection.length; n++)
1456 if ((flag=='read' && this.message_list.rows[id].unread) || (flag=='unread' && !this.message_list.rows[id].unread)
1457 || (flag=='delete' && !this.message_list.rows[id].deleted) || (flag=='undelete' && this.message_list.rows[id].deleted))
1458 a_uids[a_uids.length] = id;
1470 this.toggle_read_status(flag, a_uids);
1474 this.toggle_delete_status(a_uids);
1479 // set class to read/unread
1480 this.toggle_read_status = function(flag, a_uids)
1482 // mark all message rows as read/unread
1484 var rows = this.message_list.rows;
1485 for (var i=0; i<a_uids.length; i++)
1490 rows[uid].unread = (flag=='unread' ? true : false);
1492 if (rows[uid].classname.indexOf('unread')<0 && rows[uid].unread)
1494 rows[uid].classname += ' unread';
1495 this.set_classname(rows[uid].obj, 'unread', true);
1497 if (this.env.unreadicon)
1498 icn_src = this.env.unreadicon;
1500 else if (!rows[uid].unread)
1502 rows[uid].classname = rows[uid].classname.replace(/\s*unread/, '');
1503 this.set_classname(rows[uid].obj, 'unread', false);
1505 if (rows[uid].replied && this.env.repliedicon)
1506 icn_src = this.env.repliedicon;
1507 else if (this.env.messageicon)
1508 icn_src = this.env.messageicon;
1511 if (rows[uid].icon && icn_src)
1512 rows[uid].icon.src = icn_src;
1516 this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag='+flag);
1519 // mark all message rows as deleted/undeleted
1520 this.toggle_delete_status = function(a_uids)
1522 if (this.env.read_when_deleted)
1523 this.toggle_read_status('read',a_uids);
1525 // if deleting message from "view message" don't bother with delete icon
1526 if (this.env.action == "show")
1529 var rows = this.message_list.rows;
1530 if (a_uids.length==1)
1532 if (rows[a_uids[0]] && rows[a_uids[0]].classname.indexOf('deleted') < 0)
1533 this.flag_as_deleted(a_uids);
1535 this.flag_as_undeleted(a_uids);
1540 var all_deleted = true;
1541 for (var i=0; i<a_uids.length; i++)
1545 if (rows[uid].classname.indexOf('deleted')<0)
1547 all_deleted = false;
1554 this.flag_as_undeleted(a_uids);
1556 this.flag_as_deleted(a_uids);
1562 this.flag_as_undeleted = function(a_uids)
1564 // if deleting message from "view message" don't bother with delete icon
1565 if (this.env.action == "show")
1569 var rows = this.message_list.rows;
1571 for (var i=0; i<a_uids.length; i++)
1575 rows[uid].deleted = false;
1577 if (rows[uid].classname.indexOf('deleted') > 0)
1579 rows[uid].classname = rows[uid].classname.replace(/\s*deleted/, '');
1580 this.set_classname(rows[uid].obj, 'deleted', false);
1582 if (rows[uid].unread && this.env.unreadicon)
1583 icn_src = this.env.unreadicon;
1584 else if (rows[uid].replied && this.env.repliedicon)
1585 icn_src = this.env.repliedicon;
1586 else if (this.env.messageicon)
1587 icn_src = this.env.messageicon;
1588 if (rows[uid].icon && icn_src)
1589 rows[uid].icon.src = icn_src;
1593 this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag=undelete');
1598 this.flag_as_deleted = function(a_uids)
1600 // if deleting message from "view message" don't bother with delete icon
1601 if (this.env.action == "show")
1604 var rows = this.message_list.rows;
1605 for (var i=0; i<a_uids.length; i++)
1609 rows[uid].deleted = true;
1611 if (rows[uid].classname.indexOf('deleted')<0) {
1612 rows[uid].classname += ' deleted';
1613 this.set_classname(rows[uid].obj, 'deleted', true);
1615 if (rows[uid].icon && this.env.deletedicon)
1616 rows[uid].icon.src = this.env.deletedicon;
1620 this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag=delete');
1625 /*********************************************************/
1626 /********* login form methods *********/
1627 /*********************************************************/
1629 // handler for keyboard events on the _user field
1630 this.login_user_keypress = function(e)
1632 var key = rcube_event.get_keycode(e);
1636 if ((key==13) && (elm = rcube_find_object('_pass')))
1644 /*********************************************************/
1645 /********* message compose methods *********/
1646 /*********************************************************/
1649 // checks the input fields before sending a message
1650 this.check_compose_input = function()
1652 // check input fields
1653 var input_to = rcube_find_object('_to');
1654 var input_subject = rcube_find_object('_subject');
1655 var input_message = rcube_find_object('_message');
1657 // check for empty recipient
1658 if (input_to && !rcube_check_email(input_to.value, true))
1660 alert(this.get_label('norecipientwarning'));
1665 // display localized warning for missing subject
1666 if (input_subject && input_subject.value == '')
1668 var subject = prompt(this.get_label('nosubjectwarning'), this.get_label('nosubject'));
1670 // user hit cancel, so don't send
1671 if (!subject && subject !== '')
1673 input_subject.focus();
1678 input_subject.value = subject ? subject : this.get_label('nosubject');
1682 // check for empty body
1683 if (input_message.value=='')
1685 if (!confirm(this.get_label('nobodywarning')))
1687 input_message.focus();
1696 this.set_spellcheck_state = function(s)
1698 this.spellcheck_ready = (s=='check_spelling' || s=='ready');
1699 this.enable_command('spellcheck', this.spellcheck_ready);
1703 this.set_draft_id = function(id)
1706 if (f = rcube_find_object('_draft_saveid'))
1710 this.auto_save_start = function()
1712 if (this.env.draft_autosave)
1713 this.save_timer = self.setTimeout(function(){ ref.command("savedraft"); }, this.env.draft_autosave * 1000);
1717 this.compose_field_hash = function(save)
1719 // check input fields
1720 var input_to = rcube_find_object('_to');
1721 var input_cc = rcube_find_object('_to');
1722 var input_bcc = rcube_find_object('_to');
1723 var input_subject = rcube_find_object('_subject');
1724 var input_message = rcube_find_object('_message');
1727 if (input_to && input_to.value)
1728 str += input_to.value+':';
1729 if (input_cc && input_cc.value)
1730 str += input_cc.value+':';
1731 if (input_bcc && input_bcc.value)
1732 str += input_bcc.value+':';
1733 if (input_subject && input_subject.value)
1734 str += input_subject.value+':';
1735 if (input_message && input_message.value)
1736 str += input_message.value;
1739 this.cmp_hash = str;
1745 this.change_identity = function(obj)
1747 if (!obj || !obj.options)
1750 var id = obj.options[obj.selectedIndex].value;
1751 var input_message = rcube_find_object('_message');
1752 var message = input_message ? input_message.value : '';
1753 var is_html = (rcube_find_object('_is_html').value == '1');
1756 if (!this.env.identity)
1757 this.env.identity = id
1761 // remove the 'old' signature
1762 if (this.env.identity && this.env.signatures && this.env.signatures[this.env.identity])
1764 sig = this.env.signatures[this.env.identity]['text'];
1765 if (sig.indexOf('-- ')!=0)
1768 p = message.lastIndexOf(sig);
1770 message = message.substring(0, p-1) + message.substring(p+sig.length, message.length);
1773 // add the new signature string
1774 if (this.env.signatures && this.env.signatures[id])
1776 sig = this.env.signatures[id]['text'];
1777 if (this.env.signatures[id]['is_html'])
1779 sig = this.env.signatures[id]['plain_text'];
1781 if (sig.indexOf('-- ')!=0)
1783 message += '\n'+sig;
1788 var eid = tinyMCE.getEditorId('_message');
1789 // editor is a TinyMCE_Control object
1790 var editor = tinyMCE.getInstanceById(eid);
1791 var msgDoc = editor.getDoc();
1792 var msgBody = msgDoc.body;
1794 if (this.env.signatures && this.env.signatures[id])
1796 // Append the signature as a span within the body
1797 var sigElem = msgDoc.getElementById("_rc_sig");
1800 sigElem = msgDoc.createElement("span");
1801 sigElem.setAttribute("id", "_rc_sig");
1802 msgBody.appendChild(sigElem);
1804 if (this.env.signatures[id]['is_html'])
1806 sigElem.innerHTML = this.env.signatures[id]['text'];
1810 sigElem.innerHTML = '<pre>' + this.env.signatures[id]['text'] + '</pre>';
1816 input_message.value = message;
1818 this.env.identity = id;
1823 this.show_attachment_form = function(a)
1825 if (!this.gui_objects.uploadbox)
1829 if (elm = this.gui_objects.uploadbox)
1831 if (a && (list = this.gui_objects.attachmentlist))
1833 var pos = rcube_get_object_pos(list);
1835 var top = pos.y + list.offsetHeight + 10;
1837 elm.style.top = top+'px';
1838 elm.style.left = left+'px';
1841 elm.style.visibility = a ? 'visible' : 'hidden';
1844 // clear upload form
1845 if (!a && this.gui_objects.attachmentform && this.gui_objects.attachmentform!=this.gui_objects.messageform)
1846 this.gui_objects.attachmentform.reset();
1852 // upload attachment file
1853 this.upload_file = function(form)
1859 // get file input fields
1861 for (var n=0; n<form.elements.length; n++)
1862 if (form.elements[n].type=='file' && form.elements[n].value)
1868 // create hidden iframe and post upload form
1871 var ts = new Date().getTime();
1872 var frame_name = 'rcmupload'+ts;
1874 // have to do it this way for IE
1875 // otherwise the form will be posted to a new window
1876 if(document.all && !window.opera)
1878 var html = '<iframe name="'+frame_name+'" src="program/blank.gif" style="width:0;height:0;visibility:hidden;"></iframe>';
1879 document.body.insertAdjacentHTML('BeforeEnd',html);
1881 else // for standards-compilant browsers
1883 var frame = document.createElement('IFRAME');
1884 frame.name = frame_name;
1887 frame.style.visibility = 'hidden';
1888 document.body.appendChild(frame);
1891 form.target = frame_name;
1892 form.action = this.env.comm_path+'&_action=upload';
1893 form.setAttribute('enctype', 'multipart/form-data');
1897 // set reference to the form object
1898 this.gui_objects.attachmentform = form;
1903 // add file name to attachment list
1904 // called from upload page
1905 this.add2attachment_list = function(name, content)
1907 if (!this.gui_objects.attachmentlist)
1910 var li = document.createElement('LI');
1912 li.innerHTML = content;
1913 this.gui_objects.attachmentlist.appendChild(li);
1917 this.remove_from_attachment_list = function(name)
1919 if (!this.gui_objects.attachmentlist)
1922 var list = this.gui_objects.attachmentlist.getElementsByTagName("li");
1923 for (i=0;i<list.length;i++)
1924 if (list[i].id == name)
1925 this.gui_objects.attachmentlist.removeChild(list[i]);
1928 this.remove_attachment = function(name)
1931 this.http_post('remove-attachment', '_file='+urlencode(name));
1936 // send remote request to add a new contact
1937 this.add_contact = function(value)
1940 this.http_post('addcontact', '_address='+value);
1945 // send remote request to search mail or contacts
1946 this.qsearch = function(value)
1950 if (this.message_list)
1951 this.message_list.clear();
1952 else if (this.contact_list) {
1953 this.contact_list.clear(true);
1954 this.show_contentframe(false);
1958 this.env.current_page = 1;
1959 this.set_busy(true, 'searching');
1960 this.http_request('search', '_q='+urlencode(value)+(this.env.mailbox ? '&_mbox='+this.env.mailbox : '')+(this.env.source ? '&_source='+urlencode(this.env.source) : ''), true);
1965 // reset quick-search form
1966 this.reset_qsearch = function()
1968 if (this.gui_objects.qsearchbox)
1969 this.gui_objects.qsearchbox.value = '';
1971 this.env.search_request = null;
1976 this.sent_successfully = function(msg)
1978 this.list_mailbox();
1979 this.display_message(msg, 'confirmation', true);
1983 /*********************************************************/
1984 /********* keyboard live-search methods *********/
1985 /*********************************************************/
1988 // handler for keyboard events on address-fields
1989 this.ksearch_keypress = function(e, obj)
1991 if (typeof(this.env.contacts)!='object' || !this.env.contacts.length)
1994 if (this.ksearch_timer)
1995 clearTimeout(this.ksearch_timer);
1998 var key = rcube_event.get_keycode(e);
1999 var mod = rcube_event.get_modifier(e);
2004 case 40: // key down
2005 if (!this.ksearch_pane)
2008 var dir = key==38 ? 1 : 0;
2011 highlight = document.getElementById('rcmksearchSelected');
2013 highlight = this.ksearch_pane.ul.firstChild;
2015 if (highlight && (next = dir ? highlight.previousSibling : highlight.nextSibling))
2017 highlight.removeAttribute('id');
2018 this.set_classname(highlight, 'selected', false);
2023 next.setAttribute('id', 'rcmksearchSelected');
2024 this.set_classname(next, 'selected', true);
2025 this.ksearch_selected = next._rcm_id;
2028 return rcube_event.cancel(e);
2031 if(mod == SHIFT_KEY)
2035 if (this.ksearch_selected===null || !this.ksearch_input || !this.ksearch_value)
2038 // insert selected address and hide ksearch pane
2039 this.insert_recipient(this.ksearch_selected);
2040 this.ksearch_hide();
2042 return rcube_event.cancel(e);
2045 this.ksearch_hide();
2051 this.ksearch_timer = setTimeout(function(){ ref.ksearch_get_results(); }, 200);
2052 this.ksearch_input = obj;
2058 this.insert_recipient = function(id)
2060 if (!this.env.contacts[id] || !this.ksearch_input)
2064 var inp_value = this.ksearch_input.value.toLowerCase();
2065 var cpos = this.get_caret_pos(this.ksearch_input);
2066 var p = inp_value.lastIndexOf(this.ksearch_value, cpos);
2068 // replace search string with full address
2069 var pre = this.ksearch_input.value.substring(0, p);
2070 var end = this.ksearch_input.value.substring(p+this.ksearch_value.length, this.ksearch_input.value.length);
2071 var insert = this.env.contacts[id]+', ';
2072 this.ksearch_input.value = pre + insert + end;
2074 // set caret to insert pos
2075 cpos = p+insert.length;
2076 if (this.ksearch_input.setSelectionRange)
2077 this.ksearch_input.setSelectionRange(cpos, cpos);
2082 // address search processor
2083 this.ksearch_get_results = function()
2085 var inp_value = this.ksearch_input ? this.ksearch_input.value : null;
2086 if (inp_value===null)
2089 // get string from current cursor pos to last comma
2090 var cpos = this.get_caret_pos(this.ksearch_input);
2091 var p = inp_value.lastIndexOf(',', cpos-1);
2092 var q = inp_value.substring(p+1, cpos);
2094 // trim query string
2095 q = q.replace(/(^\s+|\s+$)/g, '').toLowerCase();
2097 if (!q.length || q==this.ksearch_value)
2099 if (!q.length && this.ksearch_pane && this.ksearch_pane.visible)
2100 this.ksearch_pane.show(0);
2105 this.ksearch_value = q;
2107 // start searching the contact list
2108 var a_results = new Array();
2109 var a_result_ids = new Array();
2111 for (var i=0; i<this.env.contacts.length; i++)
2113 if (this.env.contacts[i].toLowerCase().indexOf(q)>=0)
2115 a_results[c] = this.env.contacts[i];
2116 a_result_ids[c++] = i;
2118 if (c==15) // limit search results
2123 // display search results
2124 if (c && a_results.length)
2128 // create results pane if not present
2129 if (!this.ksearch_pane)
2131 ul = document.createElement('UL');
2132 this.ksearch_pane = new rcube_layer('rcmKSearchpane', {vis:0, zindex:30000});
2133 this.ksearch_pane.elm.appendChild(ul);
2134 this.ksearch_pane.ul = ul;
2137 ul = this.ksearch_pane.ul;
2139 // remove all search results
2142 // add each result line to list
2143 for (i=0; i<a_results.length; i++)
2145 li = document.createElement('LI');
2146 li.innerHTML = a_results[i].replace(/</, '<').replace(/>/, '>');
2147 li.onmousedown = function(e){ ref.insert_recipient(this._rcm_id); ref.ksearch_pane.show(0); return rcube_event.cancel(e); };
2148 li.style.cursor = 'pointer';
2149 li._rcm_id = a_result_ids[i];
2153 // check if last selected item is still in result list
2154 if (this.ksearch_selected!==null)
2156 p = find_in_array(this.ksearch_selected, a_result_ids);
2157 if (p>=0 && ul.childNodes)
2159 ul.childNodes[p].setAttribute('id', 'rcmksearchSelected');
2160 this.set_classname(ul.childNodes[p], 'selected', true);
2163 this.ksearch_selected = null;
2166 // if no item selected, select the first one
2167 if (this.ksearch_selected===null)
2169 ul.firstChild.setAttribute('id', 'rcmksearchSelected');
2170 this.set_classname(ul.firstChild, 'selected', true);
2171 this.ksearch_selected = a_result_ids[0];
2174 // move the results pane right under the input box and make it visible
2175 var pos = rcube_get_object_pos(this.ksearch_input);
2176 this.ksearch_pane.move(pos.x, pos.y+this.ksearch_input.offsetHeight);
2177 this.ksearch_pane.show(1);
2179 // hide results pane
2181 this.ksearch_hide();
2185 this.ksearch_blur = function(e, obj)
2187 if (this.ksearch_timer)
2188 clearTimeout(this.ksearch_timer);
2190 this.ksearch_value = '';
2191 this.ksearch_input = null;
2193 this.ksearch_hide();
2197 this.ksearch_hide = function()
2199 this.ksearch_selected = null;
2201 if (this.ksearch_pane)
2202 this.ksearch_pane.show(0);
2207 /*********************************************************/
2208 /********* address book methods *********/
2209 /*********************************************************/
2212 this.contactlist_keypress = function(list)
2214 if (list.key_pressed == list.DELETE_KEY)
2215 this.command('delete');
2219 this.contactlist_select = function(list)
2221 if (this.preview_timer)
2222 clearTimeout(this.preview_timer);
2224 var id, frame, ref = this;
2225 if (id = list.get_single_selection())
2226 this.preview_timer = setTimeout(function(){ ref.load_contact(id, 'show'); }, this.dblclick_time + 10);
2227 else if (this.env.contentframe)
2228 this.show_contentframe(false);
2230 this.enable_command('edit', id?true:false);
2231 this.enable_command('compose', list.selection.length > 0);
2232 this.enable_command('delete', list.selection.length && this.env.address_sources && !this.env.address_sources[this.env.source].readonly);
2238 this.list_contacts = function(src, page)
2241 var target = window;
2244 src = this.env.source;
2246 if (page && this.current_page==page && src == this.env.source)
2249 if (src != this.env.source)
2252 this.env.current_page = page;
2255 this.select_folder(src, this.env.source);
2256 this.env.source = src;
2258 // load contacts remotely
2259 if (this.gui_objects.contactslist)
2261 this.list_contacts_remote(src, page);
2265 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2267 target = window.frames[this.env.contentframe];
2268 add_url = '&_framed=1';
2271 // also send search request to get the correct listing
2272 if (this.env.search_request)
2273 add_url += '&_search='+this.env.search_request;
2275 this.set_busy(true, 'loading');
2276 target.location.href = this.env.comm_path+(src ? '&_source='+urlencode(src) : '')+(page ? '&_page='+page : '')+add_url;
2280 // send remote request to load contacts list
2281 this.list_contacts_remote = function(src, page)
2283 // clear message list first
2284 this.contact_list.clear(true);
2285 this.show_contentframe(false);
2286 this.enable_command('delete', 'compose', false);
2288 // send request to server
2289 var url = (src ? '&_source='+urlencode(src) : '') + (page ? '&_page='+page : '');
2290 this.env.source = src;
2292 // also send search request to get the right messages
2293 if (this.env.search_request)
2294 url += '&_search='+this.env.search_request;
2296 this.set_busy(true, 'loading');
2297 this.http_request('list', url, true);
2301 // load contact record
2302 this.load_contact = function(cid, action, framed)
2305 var target = window;
2306 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2308 add_url = '&_framed=1';
2309 target = window.frames[this.env.contentframe];
2310 this.show_contentframe(true);
2315 if (action && (cid || action=='add') && !this.drag_active)
2317 this.set_busy(true);
2318 target.location.href = this.env.comm_path+'&_action='+action+'&_source='+urlencode(this.env.source)+'&_cid='+urlencode(cid) + add_url;
2323 // copy a contact to the specified target (group or directory)
2324 this.copy_contact = function(cid, to)
2327 cid = this.contact_list.get_selection().join(',');
2329 if (to != this.env.source && cid && this.env.address_sources[to] && !this.env.address_sources[to].readonly)
2330 this.http_post('copy', '_cid='+urlencode(cid)+'&_source='+urlencode(this.env.source)+'&_to='+urlencode(to));
2334 this.delete_contacts = function()
2336 // exit if no mailbox specified or if selection is empty
2337 var selection = this.contact_list.get_selection();
2338 if (!(selection.length || this.env.cid) || !confirm(this.get_label('deletecontactconfirm')))
2341 var a_cids = new Array();
2344 a_cids[a_cids.length] = this.env.cid;
2348 for (var n=0; n<selection.length; n++)
2351 a_cids[a_cids.length] = id;
2352 this.contact_list.remove_row(id);
2355 // hide content frame if we delete the currently displayed contact
2356 if (selection.length == 1)
2357 this.show_contentframe(false);
2360 // send request to server
2361 this.http_post('delete', '_cid='+urlencode(a_cids.join(','))+'&_from='+(this.env.action ? this.env.action : ''));
2366 // update a contact record in the list
2367 this.update_contact_row = function(cid, cols_arr)
2370 if (this.contact_list.rows[cid] && (row = this.contact_list.rows[cid].obj))
2372 for (var c=0; c<cols_arr.length; c++)
2374 row.cells[c].innerHTML = cols_arr[c];
2383 /*********************************************************/
2384 /********* user settings methods *********/
2385 /*********************************************************/
2387 this.identity_select = function(list)
2390 if (id = list.get_single_selection())
2391 this.load_identity(id, 'edit-identity');
2394 // load contact record
2395 this.load_identity = function(id, action)
2397 if (action=='edit-identity' && (!id || id==this.env.iid))
2401 var target = window;
2402 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2404 add_url = '&_framed=1';
2405 target = window.frames[this.env.contentframe];
2406 document.getElementById(this.env.contentframe).style.visibility = 'inherit';
2409 if (action && (id || action=='add-identity'))
2411 this.set_busy(true);
2412 target.location.href = this.env.comm_path+'&_action='+action+'&_iid='+id+add_url;
2418 this.delete_identity = function(id)
2420 // exit if no mailbox specified or if selection is empty
2421 var selection = this.identity_list.get_selection();
2422 if (!(selection.length || this.env.iid))
2426 id = this.env.iid ? this.env.iid : selection[0];
2428 // if (this.env.framed && id)
2429 this.goto_url('delete-identity', '_iid='+id, true);
2434 // tell server to create and subscribe a new mailbox
2435 this.create_folder = function(name)
2437 if (this.edit_folder)
2438 this.reset_folder_rename();
2441 if ((form = this.gui_objects.editform) && form.elements['_folder_name'])
2442 name = form.elements['_folder_name'].value;
2445 this.http_post('create-folder', '_name='+urlencode(name), true);
2446 else if (form.elements['_folder_name'])
2447 form.elements['_folder_name'].focus();
2451 // entry point for folder renaming
2452 this.rename_folder = function(props)
2454 var form, oldname, newname;
2456 // rename a specific mailbox
2458 this.edit_foldername(props);
2460 // use a dropdown and input field (old behavior)
2461 else if ((form = this.gui_objects.editform) && form.elements['_folder_oldname'] && form.elements['_folder_newname'])
2463 oldname = form.elements['_folder_oldname'].value;
2464 newname = form.elements['_folder_newname'].value;
2467 if (oldname && newname)
2468 this.http_post('rename-folder', '_folder_oldname='+urlencode(oldname)+'&_folder_newname='+urlencode(newname));
2472 // start editing the mailbox name.
2473 // this will replace the name string with an input field
2474 this.edit_foldername = function(folder)
2476 var temp, row, form;
2477 var id = this.get_folder_row_id(folder);
2479 // reset current renaming
2480 if (temp = this.edit_folder)
2482 this.reset_folder_rename();
2487 if (id && (row = document.getElementById(id)))
2489 this.name_input = document.createElement('INPUT');
2490 this.name_input.value = this.env.subscriptionrows[id][1];
2491 this.name_input.style.width = '100%';
2492 this.name_input.onkeypress = function(e){ rcmail.name_input_keypress(e); };
2494 row.cells[0].replaceChild(this.name_input, row.cells[0].firstChild);
2495 this.edit_folder = id;
2496 this.name_input.select();
2498 if (form = this.gui_objects.editform)
2499 form.onsubmit = function(){ return false; };
2504 // remove the input field and write the current mailbox name to the table cell
2505 this.reset_folder_rename = function()
2507 var cell = this.name_input ? this.name_input.parentNode : null;
2508 if (cell && this.edit_folder && this.env.subscriptionrows[this.edit_folder])
2509 cell.innerHTML = this.env.subscriptionrows[this.edit_folder][1];
2511 this.edit_folder = null;
2515 // handler for keyboard events on the input field
2516 this.name_input_keypress = function(e)
2518 var key = rcube_event.get_keycode(e);
2523 var newname = this.name_input ? this.name_input.value : null;
2524 if (this.edit_folder && newname)
2525 this.http_post('rename-folder', '_folder_oldname='+urlencode(this.env.subscriptionrows[this.edit_folder][0])+'&_folder_newname='+urlencode(newname));
2529 this.reset_folder_rename();
2533 // delete a specific mailbox with all its messages
2534 this.delete_folder = function(folder)
2536 if (this.edit_folder)
2537 this.reset_folder_rename();
2540 this.http_post('delete-folder', '_mboxes='+urlencode(folder));
2544 // add a new folder to the subscription list by cloning a folder row
2545 this.add_folder_row = function(name, display_name, replace)
2547 name = name.replace('\\',"");
2548 if (!this.gui_objects.subscriptionlist)
2551 for (var refid in this.env.subscriptionrows)
2552 if (this.env.subscriptionrows[refid]!=null)
2556 var tbody = this.gui_objects.subscriptionlist.tBodies[0];
2557 var id = replace && replace.id ? replace.id : tbody.childNodes.length+1;
2559 if (!id || !(refrow = document.getElementById(refid)))
2561 // Refresh page if we don't have a table row to clone
2562 this.goto_url('folders');
2566 // clone a table row if there are existing rows
2567 var row = this.clone_table_row(refrow);
2568 row.id = 'rcmrow'+id;
2570 tbody.replaceChild(row, replace);
2572 tbody.appendChild(row);
2575 // add to folder/row-ID map
2576 this.env.subscriptionrows[row.id] = [name, display_name];
2579 row.cells[0].innerHTML = display_name;
2580 if (row.cells[1] && row.cells[1].firstChild.tagName=='INPUT')
2582 row.cells[1].firstChild.value = name;
2583 row.cells[1].firstChild.checked = true;
2586 if (row.cells[2] && row.cells[2].firstChild.tagName=='A')
2587 row.cells[2].firstChild.onclick = new Function(this.ref+".command('rename-folder','"+name.replace('\'','\\\'')+"')");
2588 if (row.cells[3] && row.cells[3].firstChild.tagName=='A')
2589 row.cells[3].firstChild.onclick = new Function(this.ref+".command('delete-folder','"+name.replace('\'','\\\'')+"')");
2591 // add new folder to rename-folder list and clear input field
2592 if (!replace && (form = this.gui_objects.editform))
2594 if (form.elements['_folder_oldname'])
2595 form.elements['_folder_oldname'].options[form.elements['_folder_oldname'].options.length] = new Option(name,name);
2596 if (form.elements['_folder_name'])
2597 form.elements['_folder_name'].value = '';
2600 this.sort_subscription_list();
2604 // replace an existing table row with a new folder line
2605 this.replace_folder_row = function(oldfolder, newfolder, display_name)
2607 var id = this.get_folder_row_id(oldfolder);
2608 var row = document.getElementById(id);
2610 // replace an existing table row (if found)
2611 this.add_folder_row(newfolder, display_name, row);
2612 this.env.subscriptionrows[id] = null;
2614 // rename folder in rename-folder dropdown
2616 if ((form = this.gui_objects.editform) && (elm = form.elements['_folder_oldname']))
2618 for (var i=0;i<elm.options.length;i++)
2620 if (elm.options[i].value == oldfolder)
2622 elm.options[i].text = display_name;
2623 elm.options[i].value = newfolder;
2628 form.elements['_folder_newname'].value = '';
2633 // remove the table row of a specific mailbox from the table
2634 // (the row will not be removed, just hidden)
2635 this.remove_folder_row = function(folder)
2638 var id = this.get_folder_row_id(folder);
2639 if (id && (row = document.getElementById(id)))
2640 row.style.display = 'none';
2642 // remove folder from rename-folder list
2644 if ((form = this.gui_objects.editform) && form.elements['_folder_oldname'])
2646 for (var i=0;i<form.elements['_folder_oldname'].options.length;i++)
2648 if (form.elements['_folder_oldname'].options[i].value == folder)
2650 form.elements['_folder_oldname'].options[i] = null;
2656 if (form && form.elements['_folder_newname'])
2657 form.elements['_folder_newname'].value = '';
2661 this.subscribe_folder = function(folder)
2664 if ((form = this.gui_objects.editform) && form.elements['_unsubscribed'])
2665 this.change_subscription('_unsubscribed', '_subscribed', 'subscribe');
2667 this.http_post('subscribe', '_mboxes='+urlencode(folder));
2671 this.unsubscribe_folder = function(folder)
2674 if ((form = this.gui_objects.editform) && form.elements['_subscribed'])
2675 this.change_subscription('_subscribed', '_unsubscribed', 'unsubscribe');
2677 this.http_post('unsubscribe', '_mboxes='+urlencode(folder));
2681 this.change_subscription = function(from, to, action)
2684 if (form = this.gui_objects.editform)
2686 var a_folders = new Array();
2687 var list_from = form.elements[from];
2689 for (var i=0; list_from && i<list_from.options.length; i++)
2691 if (list_from.options[i] && list_from.options[i].selected)
2693 a_folders[a_folders.length] = list_from.options[i].value;
2694 list_from[i] = null;
2699 // yes, we have some folders selected
2700 if (a_folders.length)
2702 var list_to = form.elements[to];
2705 for (var n=0; n<a_folders.length; n++)
2707 index = list_to.options.length;
2708 list_to[index] = new Option(a_folders[n]);
2711 this.http_post(action, '_mboxes='+urlencode(a_folders.join(',')));
2717 // helper method to find a specific mailbox row ID
2718 this.get_folder_row_id = function(folder)
2720 for (var id in this.env.subscriptionrows)
2721 if (this.env.subscriptionrows[id] && this.env.subscriptionrows[id][0] == folder)
2727 // duplicate a specific table row
2728 this.clone_table_row = function(row)
2731 var new_row = document.createElement('TR');
2732 for(var n=0; n<row.childNodes.length; n++)
2734 cell = row.childNodes[n];
2735 td = document.createElement('TD');
2738 td.className = cell.className;
2740 td.setAttribute('align', cell.align);
2742 td.innerHTML = cell.innerHTML;
2743 new_row.appendChild(td);
2749 // sort subscription folder list
2750 this.sort_subscription_list = function()
2752 var index = new Array();
2753 var tbody = this.gui_objects.subscriptionlist.tBodies[0];
2754 var swapped = false;
2755 for (var i = 0; i<(tbody.childNodes.length-1); i++)
2756 if (this.env.subscriptionrows[tbody.childNodes[i].id]!=null)
2758 for (i = 0; i<(index.length-1); i++)
2760 if (this.env.subscriptionrows[tbody.childNodes[index[i]].id][0]>
2761 this.env.subscriptionrows[tbody.childNodes[index[i+1]].id][0])
2763 var swap = tbody.replaceChild(tbody.childNodes[index[i]], tbody.childNodes[index[i+1]]);
2764 if (typeof(tbody.childNodes[index[i]]) != 'undefined')
2765 tbody.insertBefore(swap, tbody.childNodes[index[i]])
2767 tbody.appendChild(swap);
2772 this.sort_subscription_list();
2776 /*********************************************************/
2777 /********* GUI functionality *********/
2778 /*********************************************************/
2781 // eable/disable buttons for page shifting
2782 this.set_page_buttons = function()
2784 this.enable_command('nextpage', (this.env.pagecount > this.env.current_page));
2785 this.enable_command('lastpage', (this.env.pagecount > this.env.current_page));
2786 this.enable_command('previouspage', (this.env.current_page > 1));
2787 this.enable_command('firstpage', (this.env.current_page > 1));
2791 // set button to a specific state
2792 this.set_button = function(command, state)
2794 var a_buttons = this.buttons[command];
2797 if(!a_buttons || !a_buttons.length)
2800 for(var n=0; n<a_buttons.length; n++)
2802 button = a_buttons[n];
2803 obj = document.getElementById(button.id);
2805 // get default/passive setting of the button
2806 if (obj && button.type=='image' && !button.status)
2807 button.pas = obj._original_src ? obj._original_src : obj.src;
2808 else if (obj && !button.status)
2809 button.pas = String(obj.className);
2811 // set image according to button state
2812 if (obj && button.type=='image' && button[state])
2814 button.status = state;
2815 obj.src = button[state];
2817 // set class name according to button state
2818 else if (obj && typeof(button[state])!='undefined')
2820 button.status = state;
2821 obj.className = button[state];
2823 // disable/enable input buttons
2824 if (obj && button.type=='input')
2826 button.status = state;
2827 obj.disabled = !state;
2833 // mouse over button
2834 this.button_over = function(command, id)
2836 var a_buttons = this.buttons[command];
2839 if(!a_buttons || !a_buttons.length)
2842 for(var n=0; n<a_buttons.length; n++)
2844 button = a_buttons[n];
2845 if(button.id==id && button.status=='act')
2847 img = document.getElementById(button.id);
2848 if (img && button.over)
2849 img.src = button.over;
2854 // mouse down on button
2855 this.button_sel = function(command, id)
2857 var a_buttons = this.buttons[command];
2860 if(!a_buttons || !a_buttons.length)
2863 for(var n=0; n<a_buttons.length; n++)
2865 button = a_buttons[n];
2866 if(button.id==id && button.status=='act')
2868 img = document.getElementById(button.id);
2869 if (img && button.sel)
2870 img.src = button.sel;
2875 // mouse out of button
2876 this.button_out = function(command, id)
2878 var a_buttons = this.buttons[command];
2881 if(!a_buttons || !a_buttons.length)
2884 for(var n=0; n<a_buttons.length; n++)
2886 button = a_buttons[n];
2887 if(button.id==id && button.status=='act')
2889 img = document.getElementById(button.id);
2890 if (img && button.act)
2891 img.src = button.act;
2897 // set/unset a specific class name
2898 this.set_classname = function(obj, classname, set)
2900 var reg = new RegExp('\s*'+classname, 'i');
2901 if (!set && obj.className.match(reg))
2902 obj.className = obj.className.replace(reg, '');
2903 else if (set && !obj.className.match(reg))
2904 obj.className += ' '+classname;
2908 // display a specific alttext
2909 this.alttext = function(text)
2915 // display a system message
2916 this.display_message = function(msg, type, hold)
2918 // pass command to parent window
2919 if (this.env.framed && parent.rcmail )
2920 return parent.rcmail.display_message(msg, type, hold);
2922 this.set_busy(false);
2923 if (!this.loaded) // save message in order to display after page loaded
2925 this.pending_message = new Array(msg, type);
2929 if (!this.gui_objects.message)
2932 if (this.message_timer)
2933 clearTimeout(this.message_timer);
2937 cont = '<div class="'+type+'">'+cont+'</div>';
2939 this.gui_objects.message._rcube = this;
2940 this.gui_objects.message.innerHTML = cont;
2941 this.gui_objects.message.style.display = 'block';
2943 if (type!='loading')
2944 this.gui_objects.message.onmousedown = function(){ this._rcube.hide_message(); return true; };
2947 this.message_timer = setTimeout(function(){ ref.hide_message(); }, this.message_time);
2951 // make a message row disapear
2952 this.hide_message = function()
2954 if (this.gui_objects.message)
2956 this.gui_objects.message.style.display = 'none';
2957 this.gui_objects.message.onmousedown = null;
2962 // mark a mailbox as selected and set environment variable
2963 this.select_folder = function(name, old)
2965 if (this.gui_objects.folderlist)
2967 var current_li, target_li;
2969 if ((current_li = this.get_folder_li(old)))
2971 this.set_classname(current_li, 'selected', false);
2972 this.set_classname(current_li, 'unfocused', false);
2975 if ((target_li = this.get_folder_li(name)))
2977 this.set_classname(target_li, 'unfocused', false);
2978 this.set_classname(target_li, 'selected', true);
2983 // helper method to find a folder list item
2984 this.get_folder_li = function(name)
2986 if (this.gui_objects.folderlist)
2988 name = String(name).replace(this.identifier_expr, '');
2989 return document.getElementById('rcmli'+name);
2996 // for reordering column array, Konqueror workaround
2997 this.set_message_coltypes = function(coltypes)
2999 this.coltypes = coltypes;
3001 // set correct list titles
3003 var thead = this.gui_objects.messagelist ? this.gui_objects.messagelist.tHead : null;
3004 for (var n=0; thead && n<this.coltypes.length; n++)
3006 col = this.coltypes[n];
3007 if ((cell = thead.rows[0].cells[n+1]) && (col=='from' || col=='to'))
3009 // if we have links for sorting, it's a bit more complicated...
3010 if (cell.firstChild && cell.firstChild.tagName=='A')
3012 cell.firstChild.innerHTML = this.get_label(this.coltypes[n]);
3013 cell.firstChild.onclick = function(){ return rcmail.command('sort', this.__col, this); };
3014 cell.firstChild.__col = col;
3017 cell.innerHTML = this.get_label(this.coltypes[n]);
3019 cell.id = 'rcmHead'+col;
3025 // create a table row in the message list
3026 this.add_message_row = function(uid, cols, flags, attachment, attop)
3028 if (!this.gui_objects.messagelist || !this.message_list)
3031 var tbody = this.gui_objects.messagelist.tBodies[0];
3032 var rowcount = tbody.rows.length;
3033 var even = rowcount%2;
3035 this.env.messages[uid] = {deleted:flags.deleted?1:0,
3036 replied:flags.replied?1:0,
3037 unread:flags.unread?1:0};
3039 var row = document.createElement('TR');
3040 row.id = 'rcmrow'+uid;
3041 row.className = 'message '+(even ? 'even' : 'odd')+(flags.unread ? ' unread' : '')+(flags.deleted ? ' deleted' : '');
3043 if (this.message_list.in_selection(uid))
3044 row.className += ' selected';
3046 var icon = flags.deleted && this.env.deletedicon ? this.env.deletedicon:
3047 (flags.unread && this.env.unreadicon ? this.env.unreadicon :
3048 (flags.replied && this.env.repliedicon ? this.env.repliedicon : this.env.messageicon));
3050 var col = document.createElement('TD');
3051 col.className = 'icon';
3052 col.innerHTML = icon ? '<img src="'+icon+'" alt="" border="0" />' : '';
3053 row.appendChild(col);
3055 // add each submitted col
3056 for (var n = 0; n < this.coltypes.length; n++)
3058 var c = this.coltypes[n];
3059 col = document.createElement('TD');
3060 col.className = String(c).toLowerCase();
3061 col.innerHTML = cols[c];
3062 row.appendChild(col);
3065 col = document.createElement('TD');
3066 col.className = 'icon';
3067 col.innerHTML = attachment && this.env.attachmenticon ? '<img src="'+this.env.attachmenticon+'" alt="" border="0" />' : '';
3068 row.appendChild(col);
3070 this.message_list.insert_row(row, attop);
3074 // replace content of row count display
3075 this.set_rowcount = function(text)
3077 if (this.gui_objects.countdisplay)
3078 this.gui_objects.countdisplay.innerHTML = text;
3080 // update page navigation buttons
3081 this.set_page_buttons();
3084 // replace content of quota display
3085 this.set_quota = function()
3087 if (this.gui_objects.quotadisplay &&
3088 this.gui_objects.quotadisplay.attributes.getNamedItem('display') &&
3089 this.gui_objects.quotadisplay.attributes.getNamedItem('id'))
3090 this.http_request('quotadisplay', '_display='+
3091 this.gui_objects.quotadisplay.attributes.getNamedItem('display').nodeValue+
3092 '&_id='+this.gui_objects.quotadisplay.attributes.getNamedItem('id').nodeValue, false);
3096 // update the mailboxlist
3097 this.set_unread_count = function(mbox, count, set_title)
3099 if (!this.gui_objects.mailboxlist)
3102 if (mbox==this.env.mailbox)
3106 var item = this.get_folder_li(mbox);
3107 mbox = String(mbox).toLowerCase().replace(this.identifier_expr, '');
3109 if (item && item.className && item.className.indexOf('mailbox '+mbox)>=0)
3112 text_obj = item.firstChild;
3113 reg = /\s+\([0-9]+\)$/i;
3115 if (count && text_obj.innerHTML.match(reg))
3116 text_obj.innerHTML = text_obj.innerHTML.replace(reg, ' ('+count+')');
3118 text_obj.innerHTML += ' ('+count+')';
3120 text_obj.innerHTML = text_obj.innerHTML.replace(reg, '');
3122 // set the right classes
3123 this.set_classname(item, 'unread', count>0 ? true : false);
3126 // set unread count to window title
3127 reg = /^\([0-9]+\)\s+/i;
3128 if (set_title && document.title)
3130 var doc_title = String(document.title);
3132 if (count && doc_title.match(reg))
3133 document.title = doc_title.replace(reg, '('+count+') ');
3135 document.title = '('+count+') '+doc_title;
3137 document.title = doc_title.replace(reg, '');
3142 // add row to contacts list
3143 this.add_contact_row = function(cid, cols, select)
3145 if (!this.gui_objects.contactslist || !this.gui_objects.contactslist.tBodies[0])
3148 var tbody = this.gui_objects.contactslist.tBodies[0];
3149 var rowcount = tbody.rows.length;
3150 var even = rowcount%2;
3152 var row = document.createElement('TR');
3153 row.id = 'rcmrow'+cid;
3154 row.className = 'contact '+(even ? 'even' : 'odd');
3156 if (this.contact_list.in_selection(cid))
3157 row.className += ' selected';
3159 // add each submitted col
3162 col = document.createElement('TD');
3163 col.className = String(c).toLowerCase();
3164 col.innerHTML = cols[c];
3165 row.appendChild(col);
3168 this.contact_list.insert_row(row);
3172 this.toggle_editor = function(checkbox, textElementName)
3174 var ischecked = checkbox.checked;
3177 tinyMCE.execCommand('mceAddControl', true, textElementName);
3181 tinyMCE.execCommand('mceRemoveControl', true, textElementName);
3187 /********************************************************/
3188 /********* remote request methods *********/
3189 /********************************************************/
3191 this.redirect = function(url)
3193 this.set_busy(true);
3194 if (this.env.framed && window.parent)
3195 parent.location.href = url;
3197 location.href = url;
3200 this.goto_url = function(action, query, lock)
3203 this.set_busy(true);
3205 var querystring = query ? '&'+query : '';
3206 this.redirect(this.env.comm_path+'&_action='+action+querystring);
3210 this.http_sockets = new Array();
3212 // find a non-busy socket or create a new one
3213 this.get_request_obj = function()
3215 for (var n=0; n<this.http_sockets.length; n++)
3217 if (!this.http_sockets[n].busy)
3218 return this.http_sockets[n];
3221 // create a new XMLHTTP object
3222 var i = this.http_sockets.length;
3223 this.http_sockets[i] = new rcube_http_request();
3225 return this.http_sockets[i];
3229 // send a http request to the server
3230 this.http_request = function(action, querystring, lock)
3232 var request_obj = this.get_request_obj();
3233 querystring += (querystring ? '&' : '') + '_remote=1';
3235 // add timestamp to request url to avoid cacheing problems in Safari
3237 querystring += '&_ts='+(new Date().getTime());
3242 console.log('HTTP request: '+this.env.comm_path+'&_action='+action+'&'+querystring);
3245 this.set_busy(true);
3248 request_obj.__lock = lock ? true : false;
3249 request_obj.__action = action;
3250 request_obj.onerror = function(o){ ref.http_error(o); };
3251 request_obj.oncomplete = function(o){ ref.http_response(o); };
3252 request_obj.GET(this.env.comm_path+'&_action='+action+'&'+querystring);
3256 // send a http POST request to the server
3257 this.http_post = function(action, postdata, lock)
3260 if (postdata && typeof(postdata) == 'object')
3261 postdata._remote = 1;
3263 postdata += (postdata ? '&' : '') + '_remote=1';
3266 if (request_obj = this.get_request_obj())
3268 console.log('HTTP POST: '+this.env.comm_path+'&_action='+action);
3271 this.set_busy(true);
3274 request_obj.__lock = lock ? true : false;
3275 request_obj.__action = action;
3276 request_obj.onerror = function(o){ rcm.http_error(o); };
3277 request_obj.oncomplete = function(o){ rcm.http_response(o); };
3278 request_obj.POST(this.env.comm_path+'&_action='+action, postdata);
3282 // handle HTTP response
3283 this.http_response = function(request_obj)
3285 var ctype = request_obj.get_header('Content-Type');
3287 ctype = String(ctype).toLowerCase();
3288 var ctype_array=ctype.split(";");
3289 ctype = ctype_array[0];
3292 this.set_busy(false);
3294 console.log(request_obj.get_text());
3296 // if we get javascript code from server -> execute it
3297 if (request_obj.get_text() && (ctype=='text/javascript' || ctype=='application/x-javascript'))
3298 eval(request_obj.get_text());
3300 // process the response data according to the sent action
3301 switch (request_obj.__action)
3305 if (this.env.action=='show')
3306 this.command('list');
3307 else if (this.message_list)
3308 this.message_list.init();
3312 if (this.env.messagecount)
3313 this.enable_command('purge', (this.env.mailbox==this.env.trash_mailbox || this.env.mailbox==this.env.junk_mailbox));
3316 this.enable_command('select-all', 'select-none', 'expunge', this.env.messagecount ? true : false);
3320 request_obj.reset();
3324 // handle HTTP request errors
3325 this.http_error = function(request_obj)
3327 //alert('Error sending request: '+request_obj.url);
3329 if (request_obj.__lock)
3330 this.set_busy(false);
3332 request_obj.reset();
3333 request_obj.__lock = false;
3337 // use an image to send a keep-alive siganl to the server
3338 this.send_keep_alive = function()
3341 this.http_request('keep-alive', '_t='+d.getTime());
3345 // send periodic request to check for recent messages
3346 this.check_for_recent = function()
3350 this.send_keep_alive();
3354 this.set_busy(true, 'checkingmail');
3356 this.http_request('check-recent', '_t='+d.getTime());
3360 /********************************************************/
3361 /********* helper methods *********/
3362 /********************************************************/
3364 // check if we're in show mode or if we have a unique selection
3365 // and return the message uid
3366 this.get_single_uid = function()
3368 return this.env.uid ? this.env.uid : (this.message_list ? this.message_list.get_single_selection() : null);
3371 // same as above but for contacts
3372 this.get_single_cid = function()
3374 return this.env.cid ? this.env.cid : (this.contact_list ? this.contact_list.get_single_selection() : null);
3378 this.get_caret_pos = function(obj)
3380 if (typeof(obj.selectionEnd)!='undefined')
3381 return obj.selectionEnd;
3383 else if (document.selection && document.selection.createRange)
3385 var range = document.selection.createRange();
3386 if (range.parentElement()!=obj)
3389 var gm = range.duplicate();
3390 if (obj.tagName=='TEXTAREA')
3391 gm.moveToElementText(obj);
3393 gm.expand('textedit');
3395 gm.setEndPoint('EndToStart', range);
3396 var p = gm.text.length;
3398 return p<=obj.value.length ? p : -1;
3402 return obj.value.length;
3406 this.set_caret2start = function(obj)
3408 if (obj.createTextRange)
3410 var range = obj.createTextRange();
3411 range.collapse(true);
3414 else if (obj.setSelectionRange)
3415 obj.setSelectionRange(0,0);
3421 // set all fields of a form disabled
3422 this.lock_form = function(form, lock)
3424 if (!form || !form.elements)
3428 for (var n=0; n<form.elements.length; n++)
3430 type = form.elements[n];
3434 form.elements[n].disabled = lock;
3438 } // end object rcube_webmail
3443 * Class for sending HTTP requests
3446 function rcube_http_request()
3450 this.xmlhttp = null;
3453 // reset object properties
3454 this.reset = function()
3456 // set unassigned event handlers
3457 this.onloading = function(){ };
3458 this.onloaded = function(){ };
3459 this.oninteractive = function(){ };
3460 this.oncomplete = function(){ };
3461 this.onabort = function(){ };
3462 this.onerror = function(){ };
3466 this.xmlhttp = null;
3470 // create HTMLHTTP object
3471 this.build = function()
3473 if (window.XMLHttpRequest)
3474 this.xmlhttp = new XMLHttpRequest();
3475 else if (window.ActiveXObject)
3477 try { this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
3478 catch(e) { this.xmlhttp = null; }
3487 this.GET = function(url)
3501 this.xmlhttp.onreadystatechange = function(){ ref.xmlhttp_onreadystatechange(); };
3502 this.xmlhttp.open('GET', url);
3503 this.xmlhttp.send(null);
3507 this.POST = function(url, body, contentType)
3509 // default value for contentType if not provided
3510 if (typeof(contentType) == 'undefined')
3511 contentType = 'application/x-www-form-urlencoded';
3521 var req_body = body;
3522 if (typeof(body) == 'object')
3526 req_body += (req_body ? '&' : '') + p+'='+urlencode(body[p]);
3533 this.xmlhttp.onreadystatechange = function() { ref.xmlhttp_onreadystatechange(); };
3534 this.xmlhttp.open('POST', url, true);
3535 this.xmlhttp.setRequestHeader('Content-Type', contentType);
3536 this.xmlhttp.send(req_body);
3540 // handle onreadystatechange event
3541 this.xmlhttp_onreadystatechange = function()
3543 if(this.xmlhttp.readyState == 1)
3544 this.onloading(this);
3546 else if(this.xmlhttp.readyState == 2)
3547 this.onloaded(this);
3549 else if(this.xmlhttp.readyState == 3)
3550 this.oninteractive(this);
3552 else if(this.xmlhttp.readyState == 4)
3555 if (this.xmlhttp.status == 0)
3557 else if(this.xmlhttp.status == 200)
3558 this.oncomplete(this);
3572 // getter method for HTTP headers
3573 this.get_header = function(name)
3575 return this.xmlhttp.getResponseHeader(name);
3578 this.get_text = function()
3580 return this.xmlhttp.responseText;
3583 this.get_xml = function()
3585 return this.xmlhttp.responseXML;
3590 } // end class rcube_http_request
3593 // helper function to call the init method with a delay
3594 function call_init(o)
3596 if (window[o] && window[o].init)
3597 setTimeout(o+'.init()', 200);